Пример #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();
        }
        private void ReadFromSocket()
        {
            IPEndPoint ipEndPoint = new IPEndPoint(IPAddress.IPv6Any, 0);

            try
            {
                int      size     = 0;
                EndPoint endPoint = ipEndPoint;

                lock (Socket)
                {
                    if (Socket.Available == 0)
                    {
                        return;
                    }

                    size = Socket.ReceiveFrom(ReceiveBuffer, ref endPoint);

                    ipEndPoint = (IPEndPoint)endPoint;
                }

                UDPClient client = GetOrAddClient(ipEndPoint);

                client.Statistics.AddReceivedPacketFromLastSecond();

                ProcessReceivedBuffer(client, (uint)size);
            }
            catch (SocketException e)
            {
                if (e.SocketErrorCode == SocketError.WouldBlock)
                {
                    return;
                }
                else if (e.SocketErrorCode == SocketError.ConnectionReset)
                {
                    if (ipEndPoint.Address == IPAddress.IPv6Any)
                    {
                        return;
                    }

                    int hash = GetIPEndPointHash(ipEndPoint);

                    lock (clients)
                    {
                        UDPClient client = GetOrAddClient(ipEndPoint);

                        clients.Remove(client);
                        clientsMap.Remove(hash);

                        HandleClientDisconnection(client);
                    }

                    return;
                }

                throw e;
            }
            catch (Exception e)
            {
                throw e;
            }
        }