Example #1
0
    public AsyncOperationState(AsyncSocket socketExtended, Action<uint, uint> callback)
    {
      m_socketExtended = socketExtended;
      m_callback = callback;

      m_nativeOverlapped = new Overlapped().UnsafePack(Complete, null);
            
      WSABuffer = new WSABuffer();
    }
Example #2
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);
        }
Example #3
0
        private void CompleteAcceptAsync(uint errorCode, uint bytesTransfered)
        {
            var taskCompletionSource = m_acceptAsyncTaskCompletionSource;
            var acceptedSocket       = m_acceptedSocket;

            m_acceptedSocket = null;
            m_acceptAsyncTaskCompletionSource = null;

            SocketError error = (SocketError)errorCode;

            if (error != SocketError.Success)
            {
                taskCompletionSource.SetException(new SocketException((int)error));
                acceptedSocket.Dispose();
            }
            else
            {
                taskCompletionSource.SetResult(acceptedSocket);
            }
        }
Example #4
0
 protected override void Dispose(bool disposing)
 {
     base.Dispose(disposing);
     m_socket = null;
 }
Example #5
0
 internal AsyncSocketStream(AsyncSocket socket)
 {
     m_socket = socket;
 }
Example #6
0
 protected override void Dispose(bool disposing)
 {
   base.Dispose(disposing);
   m_socket = null;
 }
Example #7
0
 internal AsyncSocketStream(AsyncSocket socket)
 {
   m_socket = socket;
 }
Example #8
0
    private void CompleteAcceptAsync(uint errorCode, uint bytesTransfered)
    {
      var taskCompletionSource = m_acceptAsyncTaskCompletionSource;
      var acceptedSocket = m_acceptedSocket;

      m_acceptedSocket = null;
      m_acceptAsyncTaskCompletionSource = null;

      SocketError error = (SocketError)errorCode;

      if (error != SocketError.Success)
      {
        taskCompletionSource.SetException(new SocketException((int)error));
        acceptedSocket.Dispose();
      }
      else
      {
        taskCompletionSource.SetResult(acceptedSocket);
      }
    }
Example #9
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;
    }