コード例 #1
0
        public TCPServerSocket(uint MaxConnection = 32) : base(Protocols.TCP)
        {
            clients = new TCPClientList();

            this.MaxConnection = MaxConnection;
        }
コード例 #2
0
        protected override void ReadFromClients()
        {
            lock (clients)
            {
                TCPClientList disconnectedClients = new TCPClientList();

                for (int i = 0; i < clients.Count; ++i)
                {
                    TCPClient client = clients[i];

                    try
                    {
                        int receiveSize   = 0;
                        int availableSize = client.Socket.Available;

                        lock (client.Socket)
                        {
                            if (availableSize == 0)
                            {
                                if (!client.IsReady)
                                {
                                    disconnectedClients.Add(client);

                                    HandleClientDisconnection(client);
                                }

                                continue;
                            }

                            receiveSize = client.Socket.Receive(ReceiveBuffer, (int)ReceiveBufferIndex, availableSize, SocketFlags.None);
                        }

                        client.Statistics.AddReceivedPacketFromLastSecond();

                        ProcessReceivedBuffer(client, (uint)receiveSize);
                    }
                    catch (SocketException e)
                    {
                        if (e.SocketErrorCode == SocketError.WouldBlock)
                        {
                            continue;
                        }
                        else if (e.SocketErrorCode == SocketError.ConnectionReset)
                        {
                            disconnectedClients.Add(client);

                            HandleClientDisconnection(client);

                            continue;
                        }

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

                for (int i = 0; i < disconnectedClients.Count; ++i)
                {
                    clients.Remove(disconnectedClients[i]);
                }
            }
        }