Example #1
0
        // Transfers AcceptArgs to another SocketAsyncEventArgs object from the pool, and begins to receive data.
        private void ProcessAccept(SocketAsyncEventArgs e)
        {
            if (e.SocketError != SocketError.Success)
            {
                Logger.WriteStr("Error while processing accept.");
                StartAccept(null);      //start new Accept socket
                return;
            }

            //TODO: deleteme
            //StartAccept(null);

            Logger.WriteStr("Client connection accepted. Processing Accept from client " + e.AcceptSocket.RemoteEndPoint.ToString());

            // Get the socket for the accepted client connection and put it into the ReadEventArg object user token
            SocketAsyncEventArgs readEventArgs = argsReadWritePool.Pop();

            if (readEventArgs != null)
            {
                ((HostAsyncUserToken)readEventArgs.UserToken).Socket = e.AcceptSocket;
                readEventArgs.AcceptSocket = e.AcceptSocket;
                e.AcceptSocket             = null;              // We'll reuse Accept SocketAsyncEventArgs object

                // As soon as the client is connected, post a receive to the connection, to get client's identification info
                StartReceive(readEventArgs);
            }
            else
            {
                Logger.WriteStr("There is no more SocketAsyncEventArgs available in r/w pool");
            }

            // Accept the next connection request. We'll reuse Accept SocketAsyncEventArgs object.
            StartAccept(e);
        }
Example #2
0
        // Transfers AcceptArgs to another SocketAsyncEventArgs object from the pool, and begins to receive data.
        private void ProcessAccept(SocketAsyncEventArgs args)
        {
            if (args.SocketError != SocketError.Success)
            {
                Logger.WriteStr("Error while processing accept.");
                StartAccept(null, args.UserToken as Action <HostCustomEventArgs>);      //start new Accept socket
                return;
            }

            Logger.WriteStr("Client connection accepted. Processing Accept from client " + args.AcceptSocket.RemoteEndPoint);

            // Get Arg from the Pool, for recieving
            SocketAsyncEventArgs readEventArgs = _argsReadWritePool.Pop();

            if (readEventArgs != null)
            {
                // create user's token
                var token = new HostAsyncUserToken();
                readEventArgs.UserToken = token;
                token.Callback          = args.UserToken as Action <HostCustomEventArgs>;

                // store this object in token for fast reusing
                token.readEventArgs = readEventArgs;

                // Get the socket for the accepted client connection and put it into the readEventArg object user token
                token.Socket = args.AcceptSocket;
                readEventArgs.AcceptSocket = args.AcceptSocket;

                // Get another Arg from the Pool, for sending.
                SocketAsyncEventArgs writeEventArgs = _argsReadWritePool.Pop();

                if (writeEventArgs != null)
                {
                    // point it's UserToken to the same token
                    writeEventArgs.UserToken    = token;
                    writeEventArgs.AcceptSocket = args.AcceptSocket;

                    // Link writeEventArgs to readEventArgs in the token
                    token.writeEventArgs = writeEventArgs;

                    // Initialize agent's data/info holder
                    token.Agent = new Agent();

                    // Start timer that sends keep-alive messages
                    token.KeepAliveTimer          = new KeepAliveTimer(token);
                    token.KeepAliveTimer.Elapsed += SendKeepAlive;

                    // As soon as the client is connected, post a receive to the connection, to get client's identification info
                    WaitForReceiveMessage(readEventArgs);
                }
                else
                {
                    Logger.WriteStr("WARNING: There is no more SocketAsyncEventArgs available in write pool! Cannot continue. Close connection.");
                    CloseConnection(args);
                }
            }
            else
            {
                Logger.WriteStr("WARNING: There is no more SocketAsyncEventArgs available in read pool! Cannot continue. Close connection.");
                // increase maxNumberConnectedClients simaphore back because it has been decreased in StartAccept
                _maxNumberConnectedClients.Release();
            }

            // Accept the next connection request. We'll reuse Accept SocketAsyncEventArgs object.
            args.AcceptSocket = null;
            StartAccept(args, args.UserToken as Action <HostCustomEventArgs>);
        }