Beispiel #1
0
        private void ProcessSend(SocketAsyncEventArgs sendEventArgs)
        {
            SendDataToken token = (SendDataToken)sendEventArgs.UserToken;

            if (sendEventArgs.SocketError == SocketError.Success)
            {
                token.SendBytesRemainingCount = token.SendBytesRemainingCount - sendEventArgs.BytesTransferred;

                if (token.SendBytesRemainingCount == 0)
                {
                    token.Reset();
                    ReturnSendSaea(sendEventArgs);
                }
                else
                {
                    token.BytesSentAlreadyCount += sendEventArgs.BytesTransferred;
                    StartSend(sendEventArgs);
                }
            }
            else
            {
                token.Reset();
                CloseClientSocket(sendEventArgs);
                ReturnSendSaea(sendEventArgs);
            }
        }
Beispiel #2
0
        public void SendData(Socket socket, byte[] data)
        {
            this.MaxSaeaSendEnforcer.Wait();
            SocketAsyncEventArgs sendEventArgs;

            this.PoolOfSendEventArgs.TryPop(out sendEventArgs);

            SendDataToken token = (SendDataToken)sendEventArgs.UserToken;

            token.DataToSend = data;
            token.SendBytesRemainingCount = data.Length;

            sendEventArgs.AcceptSocket = socket;
            StartSend(sendEventArgs);
        }
Beispiel #3
0
        private void StartSend(SocketAsyncEventArgs sendEventArgs)
        {
            SendDataToken token = (SendDataToken)sendEventArgs.UserToken;

            if (token.SendBytesRemainingCount <= this.Settings.BufferSize)
            {
                sendEventArgs.SetBuffer(sendEventArgs.Offset, token.SendBytesRemainingCount);
                Buffer.BlockCopy(token.DataToSend, token.BytesSentAlreadyCount, sendEventArgs.Buffer, sendEventArgs.Offset, token.SendBytesRemainingCount);
            }
            else
            {
                sendEventArgs.SetBuffer(sendEventArgs.Offset, this.Settings.BufferSize);
                Buffer.BlockCopy(token.DataToSend, token.BytesSentAlreadyCount, sendEventArgs.Buffer, sendEventArgs.Offset, this.Settings.BufferSize);
            }

            bool willRaiseEvent = sendEventArgs.AcceptSocket.SendAsync(sendEventArgs);

            if (!willRaiseEvent)
            {
                ProcessSend(sendEventArgs);
            }
        }