Пример #1
0
        public Task <AsyncSocket> AcceptAsync()
        {
            if (m_acceptAsyncOperationState == null)
            {
                m_acceptAsyncOperationState = new AsyncOperationState(this, CompleteAcceptAsync);

                m_acceptSocketBufferSize = (m_boundAddress.Size + 16) * 2;

                m_acceptSocketBuffer = new byte[m_acceptSocketBufferSize];

                GCHandle.Alloc(m_acceptSocketBuffer, GCHandleType.Pinned);

                m_acceptSocketBufferAddress = Marshal.UnsafeAddrOfPinnedArrayElement(
                    (Array)m_acceptSocketBuffer, 0);
            }

            SocketError socketError = SocketError.Success;

            m_acceptAsyncTaskCompletionSource = new TaskCompletionSource <AsyncSocket>();
            Task <AsyncSocket> task = m_acceptAsyncTaskCompletionSource.Task;

            m_acceptAsyncOperationState.PrepareForCall();

            m_acceptedSocket = new AsyncSocket(m_addressFamily, m_socketType, m_protocolType);

            int bytesReceived;

            try
            {
                if (!
                    m_acceptEx(Handle, m_acceptedSocket.Handle, m_acceptSocketBufferAddress, 0,
                               m_acceptSocketBufferSize / 2,
                               m_acceptSocketBufferSize / 2, out bytesReceived,
                               m_acceptAsyncOperationState.OverlappdAddress))
                {
                    socketError = (SocketError)Marshal.GetLastWin32Error();
                }
            }
            catch (Exception ex)
            {
                m_acceptAsyncTaskCompletionSource = null;
                m_acceptedSocket.Dispose();
                m_acceptedSocket = null;
                throw;
            }

            if (socketError != SocketError.IOPending && socketError != SocketError.Success)
            {
                m_acceptAsyncTaskCompletionSource.SetException(new SocketException((int)socketError));
                m_acceptAsyncTaskCompletionSource = null;
                m_acceptedSocket.Dispose();
                m_acceptedSocket = null;
            }

            return(task);
        }
Пример #2
0
        public Task <int> SendAsync(byte[] buffer, int offset, int size, SocketFlags flags)
        {
            if (m_sendAsyncOperationState == null)
            {
                m_sendAsyncOperationState = new AsyncOperationState(this, CompleteSyndAsync);
            }

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

            m_sendAsyncOperationState.SetBuffer(buffer, offset, size);

            int bytesTransferred;

            SocketError sendResult;

            m_sendAsyncOperationState.PrepareForCall();

            m_sendAsyncTaskCompletionSource = new TaskCompletionSource <int>();
            Task <int> task = m_sendAsyncTaskCompletionSource.Task;

            try
            {
                sendResult = UnsafeMethods.WSASend(Handle, ref m_sendAsyncOperationState.WSABuffer, 1,
                                                   out bytesTransferred, flags, m_sendAsyncOperationState.OverlappdAddress, IntPtr.Zero);
            }
            catch (Exception ex)
            {
                m_sendAsyncTaskCompletionSource = null;
                throw;
            }

            if (sendResult != SocketError.Success)
            {
                sendResult = (SocketError)Marshal.GetLastWin32Error();
            }

            if (sendResult != SocketError.Success && sendResult != SocketError.IOPending)
            {
                m_sendAsyncTaskCompletionSource.SetException(new SocketException((int)sendResult));
                m_sendAsyncTaskCompletionSource = null;
            }

            return(task);
        }
Пример #3
0
        public Task ConnectAsync(IPEndPoint endPoint)
        {
            if (m_connectAsyncOperationState == null)
            {
                m_connectAsyncOperationState = new AsyncOperationState(this, CompleteConnectAsync);
            }

            if (m_remoteAddress != null)
            {
                m_remoteAddress.Dispose();
                m_remoteAddress = null;
            }

            m_remoteAddress = new SocketAddress(endPoint.Address, endPoint.Port);

            int bytesSend;

            SocketError socketError = SocketError.Success;

            m_connectAsyncTaskCompletionSource = new TaskCompletionSource <bool>();
            Task task = m_connectAsyncTaskCompletionSource.Task;

            m_connectAsyncOperationState.PrepareForCall();

            try
            {
                if (!m_connectEx(Handle, m_remoteAddress.Buffer, m_remoteAddress.Size, IntPtr.Zero, 0,
                                 out bytesSend,
                                 m_connectAsyncOperationState.OverlappdAddress))
                {
                    socketError = (SocketError)Marshal.GetLastWin32Error();
                }
            }
            catch (Exception ex)
            {
                m_connectAsyncTaskCompletionSource = null;
                throw;
            }

            if (socketError != SocketError.IOPending && socketError != SocketError.Success)
            {
                m_connectAsyncTaskCompletionSource.SetException(new SocketException((int)socketError));
                m_connectAsyncTaskCompletionSource = null;
            }

            return(task);
        }