internal void Push(IEasyServerSocket item)
 {
     if (item == null)
     {
         throw new ArgumentNullException("Items added to a SocketAsyncEventArgsPool cannot be null");
     }
     lock (_pool)
     {
         --_count;
         _pool.Push(item);
     }
 }
示例#2
0
        private void ProcessAccept(SocketAsyncEventArgs args)
        {
            Socket socket = args.AcceptSocket;

            try
            {
                Task.Run(() =>
                {
                    IEasyServerSocket easySocket = _socketPool.Pop();
                    if (easySocket != null)
                    {
                        easySocket.SocketId                 = KeyGenerator.GetServerSocketId();
                        easySocket.Socket                   = socket;
                        easySocket.Socket.NoDelay           = SocketConfiguration.NoDelay;
                        easySocket.Socket.ReceiveBufferSize = SocketConfiguration.ReceiveBufferSize;
                        easySocket.Socket.SendBufferSize    = SocketConfiguration.SendBufferSize;

                        Console.WriteLine("Client connection accepted. There are {0} clients connected to the server", _socketPool.GetCount());

                        _connectAction(easySocket);
                    }
                    else
                    {
                        Console.WriteLine("There are no more available sockets to allocate.");
                    }
                });
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                _exceptionAction?.Invoke(ex);
            }

            // Accept the next connection request.
            this.StartAccept(args);
        }