public static SocketAwaitable SendToAsync(this Socket socket, SocketAwaitable awaitable)
 {
     awaitable.Reset();
     if (!socket.SendToAsync(awaitable.m_eventArgs))
     {
         awaitable.m_wasCompleted = true;
     }
     return(awaitable);
 }
예제 #2
0
        public async void Start()
        {
            while (true)
            {
                try
                {
                    // Reusable SocketAsyncEventArgs and awaitable wrapper
                    SocketAsyncEventArgs args = new SocketAsyncEventArgs();
                    args.SetBuffer(new byte[0x1000], 0, 0x1000);
                    args.RemoteEndPoint = _listener.LocalEndPoint;
                    SocketAwaitable awaitable = new SocketAwaitable(args);

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

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

                        if (OnRequest != null)
                        {
                            Task.Factory.StartNew(() => OnRequest(args));
                        }
                        else
                        {
                            // defaults to console dump if no listener is bound
                            Task.Factory.StartNew(() => ProcessReceiveFrom(args));
                        }
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                }
                // listener restarts if an exception occurs
            }
        }
예제 #3
0
 public async void SendToAsync(SocketAsyncEventArgs args)
 {
     SocketAwaitable awaitable = new SocketAwaitable(args);
     await _listener.SendToAsync(awaitable);
 }