Пример #1
0
        /// <summary>
        ///     This method is called by I/O Completed() when an asynchronous send completes.
        ///     If all of the data has been sent, then this method calls StartReceive to start another receive op
        ///     on the socket to read any additional  data sent from the client.
        ///     If all of the data has NOT been sent, then it calls StartSend to send more data.
        /// </summary>
        /// <param name="args"> </param>
        private void ProcessSend(SocketAsyncEventArgs args)
        {
            if (args.Count > 0)
            {
                _monitor.ProcessSend();
            }

            if (args.SocketError != SocketError.Success)
            {
                CloseClientSocket(args.AcceptSocket, args);
                _clientArgsPool.Push(args);
            }
            else
            {
                var lastFrame = _protocol.IsSendCompleted(args);
                if (lastFrame)
                {
                    var closeRequired = _protocol.IsCloseRequired(args);
                    if (closeRequired)
                    {
                        CloseClientSocket(args.AcceptSocket, args);
                        _clientArgsPool.Push(args);
                    }
                    else
                    {
                        StartReceive(args);
                    }
                }
                else
                {
                    _protocol.PrepareFrameToSend(args, _settings.BufferSize);
                    StartSend(args);
                }
            }
        }