示例#1
0
        static void OnReceive(Udp udp, IDatagramReadCompletion completion)
        {
            if (completion.Error != null)
            {
                completion.Dispose();
                Console.WriteLine($"{nameof(EchoServer)} receive error {completion.Error}");
                udp.ReceiveStop();
                udp.CloseHandle(OnClose);
                return;
            }

            ReadableBuffer data    = completion.Data;
            string         message = data.ReadString(Encoding.UTF8);

            data.Dispose();

            IPEndPoint remoteEndPoint = completion.RemoteEndPoint;

            if (string.IsNullOrEmpty(message) ||
                remoteEndPoint == null)
            {
                return;
            }

            WritableBuffer buffer = WritableBuffer.From(Encoding.UTF8.GetBytes(message));

            udp.QueueSend(buffer, remoteEndPoint, OnSendCompleted);
        }
示例#2
0
        static void OnReceive(Udp udp, IDatagramReadCompletion completion)
        {
            if (completion.Error != null)
            {
                Console.WriteLine($"Echo server receive error {completion.Error}");
                udp.CloseHandle(OnClosed);
                return;
            }

            IPEndPoint     remoteEndPoint = completion.RemoteEndPoint;
            ReadableBuffer data           = completion.Data;
            string         message        = data.Count > 0 ? data.ReadString(data.Count, Encoding.UTF8) : null;

            if (string.IsNullOrEmpty(message))
            {
                return;
            }
            Console.WriteLine($"Echo server received : {message} from {remoteEndPoint}");

            Console.WriteLine($"Echo server sending echo back to {remoteEndPoint}.");
            byte[]         array  = Encoding.UTF8.GetBytes($"ECHO [{message}]");
            WritableBuffer buffer = WritableBuffer.From(array);

            udp.QueueSend(buffer, remoteEndPoint, OnSendCompleted);
        }
示例#3
0
        void OnReceive(Udp udp, IDatagramReadCompletion completion)
        {
            if (completion.Error != null ||
                completion.RemoteEndPoint == null)
            {
                return;
            }

            ReadableBuffer buffer = completion.Data;

            if (buffer.Count == 0)
            {
                return;
            }

            string message = buffer.ReadString(buffer.Count, Encoding.UTF8);

            if (message == "PING" ||
                message == "PANG")
            {
                this.serverReceiveCount++;
            }

            if (this.serverReceiveCount == 2)
            {
                udp.CloseHandle(this.OnClose);
            }
        }
示例#4
0
        void OnReceive(Udp udp, IDatagramReadCompletion completion)
        {
            var error = completion.Error as OperationException;

            if (error != null &&
                error.ErrorCode == ErrorCode.ECANCELED)    // UV_ECANCELED
            {
                return;
            }

            ReadableBuffer data = completion.Data;

            if (data.Count == 0)
            {
                return;
            }

            string message = data.ReadString(data.Count, Encoding.UTF8);

            if (message != ExpectedMessage)
            {
                Console.WriteLine(
                    $"Udp pummel {this.numberOfSenders}v{this.numberOfReceivers} failed, wrong message '{message}' received.");
            }

            this.receiveCount++;
        }
示例#5
0
 void OnReceive(Udp udp, IDatagramReadCompletion completion)
 {
     if (completion.Error == null &&
         completion.Data.Count > 0)
     {
         this.receiveCount++;
     }
 }
示例#6
0
        void OnClientReceive(Udp udp, IDatagramReadCompletion completion)
        {
            ReadableBuffer buffer  = completion.Data;
            string         message = buffer.ReadString(Encoding.UTF8);

            if (message == "PONG")
            {
                this.clientReceiveCount++;
            }

            udp.CloseHandle(this.OnClose);
        }
示例#7
0
        static void OnReceive(Udp udp, IDatagramReadCompletion completion)
        {
            if (completion.Error != null)
            {
                Console.WriteLine($"Echo client receive error {completion.Error}");
            }

            IPEndPoint     remoteEndPoint = completion.RemoteEndPoint;
            ReadableBuffer data           = completion.Data;
            string         message        = data.ReadString(Encoding.UTF8);

            Console.WriteLine($"Echo client received : {message} from {remoteEndPoint}");
            udp.CloseHandle(OnClosed);
        }
示例#8
0
        void OnClientReceive(Udp udp, IDatagramReadCompletion completion)
        {
            this.receiveError = completion.Error;
            ReadableBuffer buffer  = completion.Data;
            string         message = buffer.ReadString(Encoding.UTF8);

            if (message == "PING")
            {
                this.clientReceiveCount++;
            }

            /* we are done with the client handle, we can close it */
            udp.CloseHandle(this.OnClose);
        }
示例#9
0
        void OnServerReceive(Udp udp, IDatagramReadCompletion completion)
        {
            ReadableBuffer buffer  = completion.Data;
            string         message = buffer.ReadString(Encoding.UTF8);

            if (message == "PING")
            {
                this.serverReceiveCount++;
            }

            udp.ReceiveStop();
            byte[] data = Encoding.UTF8.GetBytes("PONG");
            udp.QueueSend(data, completion.RemoteEndPoint, this.OnServerSendCompleted);
        }
示例#10
0
        void OnServerReceive(Udp udp, IDatagramReadCompletion completion)
        {
            this.receiveError = completion.Error;

            ReadableBuffer data    = completion.Data;
            string         message = data.ReadString(Encoding.UTF8);

            if (message == "EXIT")
            {
                this.serverReceiveCount++;
            }

            udp.CloseHandle(this.OnClose);
            this.client?.CloseHandle(this.OnClose);

            this.server.ReceiveStop();
            this.server.CloseHandle(this.OnClose);
        }
示例#11
0
        void OnReceive(Udp udp, IDatagramReadCompletion completion)
        {
            if (completion.Error is OperationException error &&
                error.ErrorCode == ErrorCode.ECANCELED)    // UV_ECANCELED
            {
                return;
            }

            ReadableBuffer data    = completion.Data;
            string         message = data.ReadString(Encoding.UTF8);

            if (!string.IsNullOrEmpty(message) &&
                message != ExpectedMessage)
            {
                Console.WriteLine($"Udp pummel {this.numberOfSenders}v{this.numberOfReceivers} failed, wrong message '{message}' received.");
            }

            this.receiveCount++;
        }
示例#12
0
        void OnReceive(Udp udp, IDatagramReadCompletion completion)
        {
            if (completion.Error == null)
            {
                ReadableBuffer data = completion.Data;
                if (data.Count == 0)
                {
                    return;
                }

                IPEndPoint localEndPoint = udp.GetLocalEndPoint();
                if (Equals(localEndPoint.Address, IPAddress.Any) &&
                    localEndPoint.Port == Port)
                {
                    this.connectionCount++;
                }
            }

            udp.CloseHandle(this.OnClose);
        }
示例#13
0
 void OnReceive(Udp udp, IDatagramReadCompletion completion)
 {
     this.receiveError = completion.Error;
     this.clientReceiveCount++;
 }
示例#14
0
文件: RefTests.cs 项目: heng83/NetUV
 void OnReceive(Udp udp, IDatagramReadCompletion datagramReadCompletion) => this.callbackCount++;