Beispiel #1
0
        /// <summary>
        ///     If the remote host closed the connection, then the socket is closed.
        ///     Otherwise, we process the received data. And if a complete message was received,
        ///     then we do some additional processing, to respond to the client.
        /// </summary>
        /// <param name="args"> </param>
        private void ProcessReceive(SocketAsyncEventArgs args)
        {
            _monitor.ProcessReceive();

            // If there was a socket error, close the connection.
            if (args.SocketError != SocketError.Success)
            {
                CloseClientSocket(args.AcceptSocket, args);
                _clientArgsPool.Push(args);
            }
            // If no data was received, close the connection.
            // This is a situation that shows when the client has finished sending data.
            else if (args.BytesTransferred == 0)
            {
                CloseClientSocket(args.AcceptSocket, args);
                _clientArgsPool.Push(args);
            }
            else
            {
                var lastFrame = _protocol.ProcessReceivedFrame(args);
                if (lastFrame)
                {
                    _protocol.PrepareFrameToSend(args, _settings.BufferSize);
                    StartSend(args);
                }
                else
                {
                    StartReceive(args);
                }
            }
        }