示例#1
0
        private static void AsyncBeginReceive()
        {
            // this method actually kicks off the async read on the socket.
            // we aquire a reader lock here to ensure that no other thread
            // is trying to set shutdownFlag and close the socket.
            rwLock.AcquireReaderLock(-1);

            if (!shutdownFlag)
            {
                // increment the count of pending operations
                Interlocked.Increment(ref rwOperationCount);

                // allocate a packet buffer
                UDPPacketBuffer buf = new UDPPacketBuffer();

                try
                {
                    // kick off an async read
                    udpSocket.BeginReceiveFrom(
                        buf.Data,
                        0,
                        UDPPacketBuffer.BUFFER_SIZE,
                        SocketFlags.None,
                        ref buf.RemoteEndPoint,
                        new AsyncCallback(AsyncEndReceive),
                        buf);
                }
                catch (SocketException se)
                {
                    // Diagnostic.NetworkingDiagnostics.Logging.Error("AsyncBeginReceive", se);
                    // an error occurred, therefore the operation is void.  Decrement the reference count.
                    Interlocked.Decrement(ref rwOperationCount);

                }
                catch (Exception ex)
                {
                    // Diagnostic.NetworkingDiagnostics.Logging.Fatal("AsyncBeginReceive", ex);
                    // an error occurred, therefore the operation is void.  Decrement the reference count.
                    Interlocked.Decrement(ref rwOperationCount);

                }
            }
            // we're done with the socket for now, release the reader lock.
            rwLock.ReleaseReaderLock();
        }
示例#2
0
 private static void PacketWasReceived(UDPPacketBuffer buffer)
 {
     string msg = Encoding.UTF8.GetString(buffer.Data, 0, buffer.DataLength);
     if (msg.ToLower() == "q")
     {
         Close();
         Console.WriteLine("Press [ENTER] to quit");
     }
     Write(msg);
 }