コード例 #1
0
        private void SendAsyncCompleted(object sender, SocketAsyncEventArgs e)
        {
            var sent = sendArgs.BytesTransferred;

            CoreEventSource.SendChunk(name, IsAlive, sent, sendArgs.SocketError);

            // failed during send
            if (sendArgs.SocketError != SocketError.Success || sent < 1)
            {
                FinishSending(false);
                return;
            }

            var sendOffset = sendArgs.Offset + sent;
            var sendCount  = sendArgs.Count - sent;

            // OS sent less data than we asked for, so send the remaining data
            if (sendCount > 0)
            {
                PerformSend(sendOffset, sendCount);
            }
            else
            {
                Debug.Assert(sendCount == 0);

                // all data was sent
                sendBuffer.Clear();
                FinishSending(true);
            }
        }
コード例 #2
0
        private void PerformSend(int sendOffset, int sendCount)
        {
            for (;;)
            {
                // try sending all our data
                sendArgs.SetBuffer(sendOffset, sendCount);
                // send is being done asynchrously (SendAsyncCompleted will clean up)
                if (socket.SendAsync(sendArgs))
                {
                    break;
                }

                // send was done synchronously
                var sent = sendArgs.BytesTransferred;
                if (trace.IsEnabled())
                {
                    trace.SendChunk(name, IsAlive, sent, sendArgs.SocketError);
                }

                // check for fail
                if (sendArgs.SocketError != SocketError.Success || sent < 1)
                {
                    // socket error
                    FinishSending(false);
                    break;
                }

                sendOffset += sent;
                sendCount  -= sent;

                Debug.Assert(sendCount >= 0);

                // no data is remaining: quit
                // otherwise try sending a new chunk
                if (sendCount == 0)
                {
                    sendBuffer.Reset();
                    FinishSending(true);
                    break;
                }
            }
        }