Exemplo n.º 1
0
        public void OnSendHeaderCallback(IAsyncResult result)
        {
            WriteAttachment attachment = (WriteAttachment)result.AsyncState;

            attachment.NumberOfBytesWrittenSoFar += (ulong)ClientSocket.EndSend(result);

            if (attachment.NumberOfBytesWrittenSoFar < (ulong)attachment.WriteHeaderBuffer.LongLength)
            {
                // We have not finished sending the header, keep sending it
                ClientSocket.BeginSend(attachment.WriteHeaderBuffer,
                                       (int)attachment.NumberOfBytesWrittenSoFar,
                                       (int)((ulong)attachment.WriteHeaderBuffer.LongLength -
                                             attachment.NumberOfBytesWrittenSoFar),
                                       SocketFlags.None, OnSendHeaderCallback, attachment);
            }
            else
            {
                // The header was sent completely
                attachment.IsHeaderSent = true;
                attachment.NumberOfBytesWrittenSoFar = 0;
                // We have sent the header, begin sending the payload now
                ClientSocket.BeginSend(attachment.WritePayloadBuffer,
                                       0,
                                       (int)attachment.ExpectedWritePayloadLength,
                                       SocketFlags.None, OnSendPayloadCallback, attachment);
            }
        }
Exemplo n.º 2
0
        private void OnSendPayloadCallback(IAsyncResult asyncResult)
        {
            WriteAttachment attachment = (WriteAttachment)asyncResult.AsyncState;

            attachment.NumberOfBytesWrittenSoFar += (ulong)ClientSocket.EndSend(asyncResult);

            if (attachment.NumberOfBytesWrittenSoFar < attachment.ExpectedWritePayloadLength)
            {
                // Write buffer has not been sent completely
                // send the remaining
                ClientSocket.BeginSend(attachment.WritePayloadBuffer,
                                       (int)attachment.NumberOfBytesWrittenSoFar,
                                       (int)(attachment.ExpectedWritePayloadLength - attachment.NumberOfBytesWrittenSoFar),
                                       SocketFlags.None, OnSendHeaderCallback, attachment);
            }
            else
            {
                OnPacketSent();
            }
        }