예제 #1
0
        public override IAsyncResult BeginSend(byte[] buffer, int offset, int size, SocketFlags socketFlags, AsyncCallback callback, Object state)
        {
            var client = _client;

            Debug.WriteLine("Client ({0}): BeginSend", InternalRemoteEndPoint);

            CheckDisposed();

            if (_shutdown.HasValue && (_shutdown == SocketShutdown.Send || _shutdown == SocketShutdown.Both))
            {
                throw new SocketException(SocketError.Shutdown);
            }

            if (client.ClientStatus != SocketStatus.SOCKET_STATUS_CONNECTED)
            {
                throw new SocketException(SocketError.NotConnected);
            }

            if (buffer == null)
            {
                throw new ArgumentNullException("buffer");
            }

            if (offset < 0 || offset > buffer.Length)
            {
                throw new ArgumentOutOfRangeException("offset");
            }

            if (size < 0 || size > buffer.Length - offset)
            {
                throw new ArgumentOutOfRangeException("size");
            }

            var ssir = new SocketClientSendAsyncResult {
                AsyncState = state
            };

            if (size == 0)
            {
                ssir.CompletedSynchronously = true;
                ((CEvent)ssir.AsyncWaitHandle).Set();
                ssir.IsCompleted = true;
                ssir.errorCode   = SocketErrorCodes.SOCKET_OK;
                if (callback != null)
                {
                    DoAsyncCallback(callback, ssir);
                }
            }
            else
            {
                var result = client.SendDataAsync(buffer, offset, size, AsyncSendComplete, new AsyncSendState(ssir, callback));

                if (result != SocketErrorCodes.SOCKET_OK && result != SocketErrorCodes.SOCKET_OPERATION_PENDING)
                {
                    throw new SocketException(result.ToError());
                }
            }

            return(ssir);
        }
예제 #2
0
 public AsyncSendState(SocketClientSendAsyncResult asyncResult, AsyncCallback asyncCallback)
 {
     AsyncResult   = asyncResult;
     AsyncCallback = asyncCallback;
 }