Пример #1
0
        /// <summary>
        /// Callback for client wait
        /// </summary>
        protected void ClientConnected(IAsyncResult ar)
        {
            try
            {
                TcpListener listener = (TcpListener)ar.AsyncState;
                if (this._isRunning)
                {
                    System.Net.Sockets.TcpClient client = listener.EndAcceptTcpClient(ar);
                    // Create a manager for client
                    this.__clientCounter++;
                    TcpClientManager cm = new TcpClientManager(this.__clientCounter, client, this._serverIP);
                    cm.InvokeServer += new TcpClientManager.InvokeServerHandler(cm_InvokeServer);
                    if (this.OnDataReceived != null)
                    {
                        cm.OnClientDataReceived += new TcpClientManager.CommunicationStatusEventHandler(this.OnDataReceived);
                    }
                    if (this.OnAcknowledged != null)
                    {
                        cm.OnClientAcknowledged += new TcpClientManager.CommunicationStatusEventHandler(this.OnAcknowledged);
                    }

                    // Add to the list of clients
                    this._clientManager.Add(this.__clientCounter, cm);
                    this._clientNames.Add(this.__clientCounter, this._clientManager.Count.ToString());
                    this.DispatchNameTable();
                    if (this.OnClientConnected != null)
                    {
                        this.OnClientConnected(this._tcpListener, new NetEventArgs(client.Client.RemoteEndPoint, "Client Connected: " + client.Client.RemoteEndPoint, new NetPacket(this._clientManager.Count, 0, null)));
                    }
                }
            }
            catch (Exception e)
            {
                if (this.OnError != null)
                {
                    this.OnError(this._tcpListener, new NetEventArgs(e));
                }
            }
            finally     // Restart listening
            {
                if (this._isRunning)
                {
                    this._tcpListener.BeginAcceptTcpClient(new AsyncCallback(ClientConnected), this._tcpListener);
                }
            }
        }
Пример #2
0
        /// <summary>
        /// Callback for client wait
        /// </summary>        
        protected void ClientConnected(IAsyncResult ar)
        {
            try
            {
                TcpListener listener = (TcpListener)ar.AsyncState;
                if (this._isRunning)
                {
                    System.Net.Sockets.TcpClient client = listener.EndAcceptTcpClient(ar);
                    // Create a manager for client
                    this.__clientCounter++;
                    TcpClientManager cm = new TcpClientManager(this.__clientCounter, client, this._serverIP);
                    cm.InvokeServer += new TcpClientManager.InvokeServerHandler(cm_InvokeServer);
                    if (this.OnDataReceived != null)
                        cm.OnClientDataReceived += new TcpClientManager.CommunicationStatusEventHandler(this.OnDataReceived);
                    if (this.OnAcknowledged != null)
                        cm.OnClientAcknowledged += new TcpClientManager.CommunicationStatusEventHandler(this.OnAcknowledged);

                    // Add to the list of clients
                    this._clientManager.Add(this.__clientCounter, cm);
                    this._clientNames.Add(this.__clientCounter, this._clientManager.Count.ToString());
                    this.DispatchNameTable();
                    if (this.OnClientConnected != null)
                        this.OnClientConnected(this._tcpListener, new NetEventArgs(client.Client.RemoteEndPoint, "Client Connected: " + client.Client.RemoteEndPoint, new NetPacket(this._clientManager.Count,0,null)));
                }
            }
            catch (Exception e)
            {
                if (this.OnError != null)
                    this.OnError(this._tcpListener, new NetEventArgs(e));
            }
            finally     // Restart listening
            {
                if(this._isRunning)
                    this._tcpListener.BeginAcceptTcpClient(new AsyncCallback(ClientConnected), this._tcpListener);
            }
        }