예제 #1
0
        public async Task <int> ReceiveAsync(ArraySegment <byte> buffer)
        {
            var awaitable = SocketAwaitablePool.Take();

            try
            {
                awaitable.EventArgs.SetBuffer(buffer.Array, buffer.Offset, buffer.Count);
                await _socket.ReceiveAsync(awaitable);

                return(awaitable.EventArgs.BytesTransferred);
            }
            finally
            {
                SocketAwaitablePool.Add(awaitable);
            }
        }
예제 #2
0
        public async Task <int> ReceiveAsync(byte[] array, int offset, int count)
        {
            var awaitableSocket = AwaitableSocketPool.Take();

            awaitableSocket.EventArgs.SetBuffer(array, offset, count);

            try
            {
                await _socket.ReceiveAsync(awaitableSocket);

                int bytesRead = awaitableSocket.EventArgs.BytesTransferred;
                return(bytesRead);
            }
            finally
            {
                AwaitableSocketPool.Add(awaitableSocket);
            }
        }
예제 #3
0
 private static void ConnectCompleted(object sender, SocketAsyncEventArgs socketAsyncEventArgs)
 {
     try
     {
         var callback = (ConnectionConnectCallback)socketAsyncEventArgs.UserToken;
         var success  = socketAsyncEventArgs.SocketError == SocketError.Success;
         callback(success);
     }
     finally
     {
         ConnectSaeaPool.Add(socketAsyncEventArgs);
     }
 }
예제 #4
0
 private void IOCompleted(object sender, SocketAsyncEventArgs saea)
 {
     if (Listener != null)
     {
         Listen();
     }
     try
     {
         if (saea.SocketError == SocketError.Success)
         {
             Notify(saea);
         }
     }
     finally
     {
         saea.Completed -= IOCompleted;
         ConnectSaeaPool.Add(saea);
     }
 }
예제 #5
0
        private static void SendRecvCompleted(object sender, SocketAsyncEventArgs socketAsyncEventArgs)
        {
            try
            {
                var callback = (ConnectionIoCallback)socketAsyncEventArgs.UserToken;

                if (socketAsyncEventArgs.SocketError != SocketError.Success ||
                    socketAsyncEventArgs.BytesTransferred == 0)
                {
                    callback(0, false);
                    return;
                }

                callback(socketAsyncEventArgs.BytesTransferred, true);
            }
            finally
            {
                SendRecvSaeaPool.Add(socketAsyncEventArgs);
            }
        }