Example #1
0
        /// <summary>
        /// Works the specified data.
        /// </summary>
        /// <param name="data">The specified data.</param>
        protected void Worker(object data)
        {
            ConnectionManagerThreadArgs args = (ConnectionManagerThreadArgs)data;

            while (!args.Stop)
            {
                try
                {
                    EnhancedTcpClient client = new EnhancedTcpClient(this.tcpListener.AcceptTcpClient());

                    if (client.Connected)
                    {
                        TcpConnection connection = new TcpConnection(client);
                        connection.MessageReceived += this.FireMessageReceived;
                        connection.RawDataReceived += this.FireRawDataReceived;
                        connection.ConnectionLost  += this.Connection_Disconnected;
                        connection.TimedOut        += this.Connection_Disconnected;
                        connection.TimeoutLimit     = this.TimeoutLimit;
                        this.Connections.Add(connection);
                        this.FireClientAccepted(this, new ClientAcceptedEventArgs(connection));
                        connection.StartListening();
                    }
                }
                catch (SocketException)
                {
                    // Exception occurs on closing the connection manger.
                }

                Thread.Sleep(args.PollDelay);
            }
        }
Example #2
0
 /// <summary>
 /// Initializes a new instance of the <see cref="TcpConnectionManager"/> class.
 /// </summary>
 /// <param name="tcpListener">The TCP listener.</param>
 /// <param name="timeoutLimit">The timeout limit.</param>
 public TcpConnectionManager(TcpListener tcpListener, int timeoutLimit)
 {
     this.tcpListener                 = tcpListener;
     this.connectionManagerThread     = new Thread(this.Worker);
     this.connectionManagerThreadArgs = new ConnectionManagerThreadArgs();
     this.Connections                 = new List <IConnection>();
     this.TimeoutLimit                = timeoutLimit;
 }