Beispiel #1
0
 public static SocketAwaitable SendAsync(this Socket socket,
                                         SocketAwaitable awaitable)
 {
     awaitable.Reset();
     if (!socket.SendAsync(awaitable.m_eventArgs))
     {
         awaitable.m_wasCompleted = true;
     }
     return(awaitable);
 }
Beispiel #2
0
        public static async Task ReadAsync(this Socket s)
        {
            // Reusable SocketAsyncEventArgs and awaitable wrapper
            var args = new SocketAsyncEventArgs();

            args.SetBuffer(new byte[0x1000], 0, 0x1000);
            var awaitable = new SocketAwaitable(args);

            // Do processing, continually receiving from the socket
            while (true)
            {
                await s.ReceiveAsync(awaitable);

                int bytesRead = args.BytesTransferred;
                if (bytesRead <= 0)
                {
                    break;
                }
                Console.WriteLine(bytesRead);
            }
        }