Пример #1
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);
        }
Пример #2
0
        public void Bind(bool reuseAddress)
        {
            var endPoint = new IPEndPoint(IPAddress.Loopback, Port);

            Udp udp1 = this.loop.CreateUdp();

            udp1.Bind(endPoint, reuseAddress);

            Udp udp2 = this.loop.CreateUdp();

            if (reuseAddress)
            {
                udp2.Bind(endPoint, true);
            }
            else
            {
                var error = Assert.Throws <OperationException>(() => udp2.Bind(endPoint));
                Assert.Equal(ErrorCode.EADDRINUSE, error.ErrorCode);
            }

            udp1.CloseHandle(this.OnClose);
            udp2.CloseHandle(this.OnClose);

            this.loop.RunDefault();
            Assert.Equal(2, this.closeCount);
        }
Пример #3
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);
        }
Пример #4
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);
            }
        }
Пример #5
0
 static void OnSendCompleted(Udp udp, Exception exception)
 {
     if (exception != null)
     {
         udp.CloseHandle(OnClose);
         Console.WriteLine($"{nameof(EchoServer)} send error {exception}");
     }
 }
Пример #6
0
 static void OnSendCompleted(Udp udp, Exception exception)
 {
     if (exception != null)
     {
         Console.WriteLine($"Echo server send error {exception}");
         udp.CloseHandle(OnClosed);
     }
 }
Пример #7
0
        void OnSendCompleted(Udp udp, Exception exception)
        {
            if (exception == null)
            {
                this.connectedCount++;
            }

            udp.CloseHandle(this.OnClose);
        }
Пример #8
0
        void OnSendCompleted(Udp udp, Exception exception)
        {
            if (exception == null)
            {
                this.clientSendCount++;
            }

            if (this.clientSendCount == 2)
            {
                udp.CloseHandle(this.OnClose);
            }
        }
Пример #9
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);
        }
Пример #10
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);
        }
Пример #11
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);
        }
Пример #12
0
        void OnServerSendCompleted(Udp udp, Exception exception)
        {
            if (exception == null)
            {
                this.serverSendCount++;
            }
            else
            {
                var error = exception as OperationException;
                if (error != null &&
                    error.ErrorCode == ErrorCode.ENETUNREACH)
                {
                    this.serverSendCount++;
                }
            }

            udp.CloseHandle(this.OnClose);
        }
Пример #13
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);
        }
Пример #14
0
        void OnSendCompleted(Udp udp, Exception exception)
        {
            if (exception == null)
            {
                this.sendErrorValid = true;
            }
            else
            {
                var error = exception as OperationException;
                if (error != null)
                {
                    this.sendErrorValid = error.ErrorCode == ErrorCode.ENETUNREACH;
                }
            }

            this.sendCount++;
            udp.CloseHandle(this.OnClose);
        }
Пример #15
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);
        }
Пример #16
0
        public void NoBind()
        {
            Udp udp = this.loop.CreateUdp();

            var error = Assert.Throws <OperationException>(() => udp.MulticastTtl(32));

            Assert.Equal(ErrorCode.EBADF, error.ErrorCode);

            error = Assert.Throws <OperationException>(() => udp.Broadcast(true));
            Assert.Equal(ErrorCode.EBADF, error.ErrorCode);

            error = Assert.Throws <OperationException>(() => udp.Ttl(1));
            Assert.Equal(ErrorCode.EBADF, error.ErrorCode);

            error = Assert.Throws <OperationException>(() => udp.MulticastInterface(IPAddress.Any));
            Assert.Equal(ErrorCode.EBADF, error.ErrorCode);

            udp.CloseHandle(OnClose);

            this.loop.RunDefault();
        }
Пример #17
0
        void OnSendCompleted(Udp udp, Exception exception)
        {
            if (exception != null)
            {
                var error = exception as OperationException;
                if (error != null &&
                    error.ErrorCode == ErrorCode.ECANCELED)
                {
                    return;
                }

                Console.WriteLine(
                    $"Udp pummel {this.numberOfSenders}v{this.numberOfReceivers} failed, {exception}");
            }

            if (this.exiting)
            {
                return;
            }

            if (this.timeout > 0 ||
                this.packetCounter > 0)
            {
                IPEndPoint endPoint = this.senders[udp];
                udp.QueueSend(this.content, endPoint, this.OnSendCompleted);
                this.sendCount++;
            }

            if (this.timeout != 0)
            {
                return;
            }

            this.packetCounter--;
            if (this.packetCounter == 0)
            {
                udp.CloseHandle(this.OnClose);
            }
        }
Пример #18
0
 void OnServerSendCompleted(Udp udp, Exception exception)
 {
     this.sendError = exception;
     this.serverSendCount++;
     udp.CloseHandle(this.OnClose);
 }