Esempio n. 1
0
        /// <summary>
        /// Listens for packets from a client and manages that client's connection status.
        /// </summary>
        /// <param name="client"></param>
        private void Handle(TcpClientHandler client)
        {
            if (ConnectedEvent != null)
            {
                ConnectedEvent(client);
            }

            Stopwatch watch = new Stopwatch();

            watch.Start();

            while (client.Connected)
            {
                if (watch.ElapsedMilliseconds >= client.Timeout)
                {
                    if (AttemptReconnectEvent != null)
                    {
                        AttemptReconnectEvent(client);
                    }
                    client.Connected = client.Receiver.Connected;
                }

                if (client.Stream.DataAvailable)
                {
                    int          packet = client.Receiver.Available;
                    BufferStream buffer = new BufferStream(packet, 1);
                    if (client.Receiver.Connected)
                    {
                        client.Stream.Read(buffer.Memory, 0, packet);
                    }
                    if (ReceivedEvent != null)
                    {
                        ReceivedEvent(client, buffer);
                    }
                }

                watch.Reset();
            }

            if (DisconnectedEvent != null)
            {
                DisconnectedEvent(client);
            }
            ClientMap.Remove(client.Socket);
            client.Close();
        }
Esempio n. 2
0
        /// <summary>
        /// Accepts pending client connections and begins listening for packets on each accepted client.
        /// </summary>
        private void Accept()
        {
            running = true;
            if (StartedEvent != null)
            {
                StartedEvent(this);
            }

            while (Status)
            {
                if (!Listener.Pending())
                {
                    continue;
                }

                TcpClientHandler client = (ClientCreatedEvent != null) ? ClientCreatedEvent(Binder, this, ClientTimeout) : new TcpClientHandler(Binder, this, ClientTimeout);
                client.Receiver  = Listener.AcceptTcpClient();
                client.Connected = true;

                if ((ClientMap.Count <= MaxConnections && MaxConnections >= 0) || MaxConnections <= 0)
                {
                    TcpClient receiver = client.Receiver;
                    receiver.LingerState = new LingerOption(false, 0);
                    receiver.NoDelay     = true;
                    client.Stream        = receiver.GetStream();
                    ClientMap.Add(client.Socket, client);
                    ThreadPool.QueueUserWorkItem(thread => Handle(client));
                }
                else
                {
                    if (ClientOverflowEvent != null)
                    {
                        ThreadPool.QueueUserWorkItem(thread => ClientOverflowEvent(client));
                    }
                    client.Close();
                }
            }

            if (ClosedEvent != null)
            {
                ClosedEvent(this);
            }
            running = false;
            Close();
        }
        /// <summary>
        /// Listens for packets from a client and manages that client's connection status.
        /// </summary>
        /// <param name="client"></param>
        private void Handle( TcpClientHandler client )
        {
            if ( ConnectedEvent != null ) ConnectedEvent( client );

            Stopwatch watch = new Stopwatch();
            watch.Start();

            while( client.Connected ) {
                if ( watch.ElapsedMilliseconds >= client.Timeout ) {
                    if ( AttemptReconnectEvent != null ) AttemptReconnectEvent( client );
                    client.Connected = client.Receiver.Connected;
                }

                if ( client.Stream.DataAvailable ) {
                    int packet = client.Receiver.Available;
                    BufferStream buffer = new BufferStream( packet, 1 );
                    client.Stream.Read( buffer.Memory, 0, packet );
                    if ( ReceivedEvent != null ) ReceivedEvent( client, buffer );
                }

                watch.Reset();
            }

            if ( DisconnectedEvent != null ) DisconnectedEvent( client );
            client.Close();
            ClientMap.Remove(client.Socket);
        }