Beispiel #1
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);
        }
Beispiel #2
0
        public static void NotNullUdp(IPEndPoint ep)
        {
            var           u  = new Udp();
            Action <bool> cb = (_) => { };

            string ipstr = ep.Address.ToString();
            var    ip    = ep.Address;

            // constructor
            Assert.Throws <ArgumentNullException>(() => new Udp(null));

            // bind
            Assert.Throws <ArgumentNullException>(() => u.Bind(null));
            Assert.Throws <ArgumentNullException>(() => u.Bind(null as string, 0));
            Assert.Throws <ArgumentNullException>(() => u.Bind(null as IPAddress, 0));

            // receive
            Assert.Throws <ArgumentNullException>(() => u.Receive(null));
            Assert.Throws <ArgumentNullException>(() => u.Receive(Encoding.ASCII, null));
            Assert.Throws <ArgumentNullException>(() => u.Receive(null, (_, __) => { }));

            // send
            Assert.Throws <ArgumentNullException>(() => u.Send(null as IPEndPoint, new byte[] { }));
            Assert.Throws <ArgumentNullException>(() => u.Send(null as IPEndPoint, new byte[] { }, 0));
            Assert.Throws <ArgumentNullException>(() => u.Send(null as IPEndPoint, new byte[] { }, cb));
            Assert.Throws <ArgumentNullException>(() => u.Send(null as IPEndPoint, new byte[] { }, 0, cb));

            Assert.Throws <ArgumentNullException>(() => u.Send(ep, null as byte[]));
            Assert.Throws <ArgumentNullException>(() => u.Send(ep, null as byte[], 0));
            Assert.Throws <ArgumentNullException>(() => u.Send(ep, null as byte[], cb));
            Assert.Throws <ArgumentNullException>(() => u.Send(ep, null as byte[], 0, cb));

            Assert.Throws <ArgumentNullException>(() => u.Send(null as string, 0, new byte[] { }));
            Assert.Throws <ArgumentNullException>(() => u.Send(null as string, 0, new byte[] { }, 0));
            Assert.Throws <ArgumentNullException>(() => u.Send(null as string, 0, new byte[] { }, cb));
            Assert.Throws <ArgumentNullException>(() => u.Send(null as string, 0, new byte[] { }, 0, cb));

            Assert.Throws <ArgumentNullException>(() => u.Send(ipstr, 0, null as byte[]));
            Assert.Throws <ArgumentNullException>(() => u.Send(ipstr, 0, null as byte[], 0));
            Assert.Throws <ArgumentNullException>(() => u.Send(ipstr, 0, null as byte[], cb));
            Assert.Throws <ArgumentNullException>(() => u.Send(ipstr, 0, null as byte[], 0, cb));

            Assert.Throws <ArgumentNullException>(() => u.Send(null as IPAddress, 0, new byte[] { }));
            Assert.Throws <ArgumentNullException>(() => u.Send(null as IPAddress, 0, new byte[] { }, 0));
            Assert.Throws <ArgumentNullException>(() => u.Send(null as IPAddress, 0, new byte[] { }, cb));
            Assert.Throws <ArgumentNullException>(() => u.Send(null as IPAddress, 0, new byte[] { }, 0, cb));

            Assert.Throws <ArgumentNullException>(() => u.Send(ip, 0, null as byte[]));
            Assert.Throws <ArgumentNullException>(() => u.Send(ip, 0, null as byte[], 0));
            Assert.Throws <ArgumentNullException>(() => u.Send(ip, 0, null as byte[], cb));
            Assert.Throws <ArgumentNullException>(() => u.Send(ip, 0, null as byte[], 0, cb));

            u.Close();

            Loop.Default.RunOnce();
        }
Beispiel #3
0
        public static void NotNullUdp(IPEndPoint ep)
        {
            var u = new Udp();
            Action<bool> cb = (_) => { };

            string ipstr = ep.Address.ToString();
            var ip = ep.Address;

            // constructor
            Assert.Throws<ArgumentNullException>(() => new Udp(null));

            // bind
            Assert.Throws<ArgumentNullException>(() => u.Bind(null));
            Assert.Throws<ArgumentNullException>(() => u.Bind(null as string, 0));
            Assert.Throws<ArgumentNullException>(() => u.Bind(null as IPAddress, 0));

            // receive
            Assert.Throws<ArgumentNullException>(() => u.Receive(null));
            Assert.Throws<ArgumentNullException>(() => u.Receive(Encoding.ASCII, null));
            Assert.Throws<ArgumentNullException>(() => u.Receive(null, (_, __) => { }));

            // send
            Assert.Throws<ArgumentNullException>(() => u.Send(null as IPEndPoint, new byte[] { }));
            Assert.Throws<ArgumentNullException>(() => u.Send(null as IPEndPoint, new byte[] { }, 0));
            Assert.Throws<ArgumentNullException>(() => u.Send(null as IPEndPoint, new byte[] { }, cb));
            Assert.Throws<ArgumentNullException>(() => u.Send(null as IPEndPoint, new byte[] { }, 0, cb));

            Assert.Throws<ArgumentNullException>(() => u.Send(ep, null as byte[]));
            Assert.Throws<ArgumentNullException>(() => u.Send(ep, null as byte[], 0));
            Assert.Throws<ArgumentNullException>(() => u.Send(ep, null as byte[], cb));
            Assert.Throws<ArgumentNullException>(() => u.Send(ep, null as byte[], 0, cb));

            Assert.Throws<ArgumentNullException>(() => u.Send(null as string, 0, new byte[] { }));
            Assert.Throws<ArgumentNullException>(() => u.Send(null as string, 0, new byte[] { }, 0));
            Assert.Throws<ArgumentNullException>(() => u.Send(null as string, 0, new byte[] { }, cb));
            Assert.Throws<ArgumentNullException>(() => u.Send(null as string, 0, new byte[] { }, 0, cb));

            Assert.Throws<ArgumentNullException>(() => u.Send(ipstr, 0, null as byte[]));
            Assert.Throws<ArgumentNullException>(() => u.Send(ipstr, 0, null as byte[], 0));
            Assert.Throws<ArgumentNullException>(() => u.Send(ipstr, 0, null as byte[], cb));
            Assert.Throws<ArgumentNullException>(() => u.Send(ipstr, 0, null as byte[], 0, cb));

            Assert.Throws<ArgumentNullException>(() => u.Send(null as IPAddress, 0, new byte[] { }));
            Assert.Throws<ArgumentNullException>(() => u.Send(null as IPAddress, 0, new byte[] { }, 0));
            Assert.Throws<ArgumentNullException>(() => u.Send(null as IPAddress, 0, new byte[] { }, cb));
            Assert.Throws<ArgumentNullException>(() => u.Send(null as IPAddress, 0, new byte[] { }, 0, cb));

            Assert.Throws<ArgumentNullException>(() => u.Send(ip, 0, null as byte[]));
            Assert.Throws<ArgumentNullException>(() => u.Send(ip, 0, null as byte[], 0));
            Assert.Throws<ArgumentNullException>(() => u.Send(ip, 0, null as byte[], cb));
            Assert.Throws<ArgumentNullException>(() => u.Send(ip, 0, null as byte[], 0, cb));

            u.Close();

            Loop.Default.RunOnce();
        }
Beispiel #4
0
        public void RunTest(IPEndPoint ep)
        {
            int close_cb_called   = 0;
            int cl_send_cb_called = 0;
            int cl_recv_cb_called = 0;
            int sv_send_cb_called = 0;
            int sv_recv_cb_called = 0;

            Udp client = new Udp();
            Udp server = new Udp();

            server.Bind(ep);
            server.Message += (msg) => {
                var data = msg.Payload;
                var str  = Encoding.ASCII.GetString(data.Array, data.Offset, data.Count);
                Assert.Equal(str, "PING");
                sv_recv_cb_called++;
                server.Send(msg.EndPoint, Encoding.ASCII.GetBytes("PONG"), (s) => {
                    sv_send_cb_called++;
                    server.Close(() => close_cb_called++);
                });
            };
            server.Resume();

            client.Send(ep, Encoding.ASCII.GetBytes("PING"), (s) => {
                cl_send_cb_called++;
                client.Message += (msg) => {
                    var data = msg.Payload;
                    var str  = Encoding.ASCII.GetString(data.Array, data.Offset, data.Count);
                    Assert.Equal(str, "PONG");
                    cl_recv_cb_called++;
                    client.Close(() => close_cb_called++);
                };
                client.Resume();
            });


            Assert.Equal(0, close_cb_called);
            Assert.Equal(0, cl_send_cb_called);
            Assert.Equal(0, cl_recv_cb_called);
            Assert.Equal(0, sv_send_cb_called);
            Assert.Equal(0, sv_recv_cb_called);

            Loop.Default.Run();

            Assert.Equal(2, close_cb_called);
            Assert.Equal(1, cl_send_cb_called);
            Assert.Equal(1, cl_recv_cb_called);
            Assert.Equal(1, sv_send_cb_called);
            Assert.Equal(1, sv_recv_cb_called);


#if DEBUG
            Assert.Equal(1, UV.PointerCount);
#endif
        }
Beispiel #5
0
		public void RunTest(IPEndPoint ep)
		{
			int close_cb_called = 0;
			int cl_send_cb_called = 0;
			int cl_recv_cb_called = 0;
			int sv_send_cb_called = 0;
			int sv_recv_cb_called = 0;

			Udp client = new Udp();
			Udp server = new Udp();

			server.Bind(ep);
			server.Message += (msg) => {
				var data = msg.Payload;
				var str = Encoding.ASCII.GetString(data.Array, data.Offset, data.Count);
				Assert.Equal(str, "PING");
				sv_recv_cb_called++;
				server.Send(msg.EndPoint, Encoding.ASCII.GetBytes("PONG"), (s) => {
					sv_send_cb_called++;
					server.Close(() => close_cb_called++);
				});
			};
			server.Resume();

			client.Send(ep, Encoding.ASCII.GetBytes("PING"), (s) => {
				cl_send_cb_called++;
				client.Message += (msg) => {
					var data = msg.Payload;
					var str = Encoding.ASCII.GetString(data.Array, data.Offset, data.Count);
					Assert.Equal(str, "PONG");
					cl_recv_cb_called++;
					client.Close(() => close_cb_called++);
				};
				client.Resume();
			});


			Assert.Equal(0, close_cb_called);
			Assert.Equal(0, cl_send_cb_called);
			Assert.Equal(0, cl_recv_cb_called);
			Assert.Equal(0, sv_send_cb_called);
			Assert.Equal(0, sv_recv_cb_called);

			Loop.Default.Run();

			Assert.Equal(2, close_cb_called);
			Assert.Equal(1, cl_send_cb_called);
			Assert.Equal(1, cl_recv_cb_called);
			Assert.Equal(1, sv_send_cb_called);
			Assert.Equal(1, sv_recv_cb_called);


#if DEBUG
			Assert.Equal(1, UV.PointerCount);
#endif
		}
Beispiel #6
0
        public void CanSendHandles()
        {
            int count = 0;

            Loop.Default.Run(async() => {
                var handles      = new Stack <Handle>();
                string name      = "test";
                var pipelistener = new IPCPipeListener();
                pipelistener.Bind(name);
                pipelistener.Connection += () => {
                    var client = pipelistener.Accept();
                    client.Resume();
                    client.HandleData += (handle, data) => {
                        handles.Push(handle);
                        count++;
                        if (count == 3)
                        {
                            foreach (var h in handles)
                            {
                                h.Close();
                            }
                            pipelistener.Close();
                        }
                    };
                };
                pipelistener.Listen();

                var pipe = new IPCPipe();
                await pipe.ConnectAsync(name);


                var ipep        = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 7000);
                var tcplistener = new TcpListener();
                tcplistener.Bind(ipep);
                tcplistener.Connection += () => {
                    var client = tcplistener.Accept();
                    pipe.Write(client, new byte[1], (ex) => {
                        client.Close();
                        tcplistener.Close();
                    });
                };
                tcplistener.Listen();

                var tcp = new Tcp();
                await tcp.ConnectAsync(ipep);
                tcp.Write("HELLO WORLD");

                var udp = new Udp();
                udp.Bind(ipep);
                pipe.Write(udp, Encoding.Default.GetBytes("UDP"), (ex) => udp.Close());
                pipe.Write(pipe, Encoding.Default.GetBytes("pipe"), (ex) => pipe.Close());
            });

            Assert.Equal(3, count);
        }
Beispiel #7
0
		void TestCanSendHandles(string pipename, IPEndPoint ipep)
		{
			int count = 0;

			Loop.Default.Run(async () => {
				var handles = new Stack<Handle>();
				var pipelistener = new IPCPipeListener();
				pipelistener.Bind(pipename);
				pipelistener.Connection += () => {
					var client = pipelistener.Accept();
					client.Resume();
					client.HandleData += (handle, data) => {
						handles.Push(handle);
						count++;
						if (count == 3) {
							foreach (var h in handles) {
								h.Close();
							}
							pipelistener.Close();
						}
					};
				};
				pipelistener.Listen();

				var pipe = new IPCPipe();
				await pipe.ConnectAsync(pipename);

				var tcplistener = new TcpListener();
				tcplistener.Bind(ipep);
				tcplistener.Connection += () => {
					var client = tcplistener.Accept();
					pipe.Write(client, new byte[1], (ex) => {
						client.Close();
						tcplistener.Close();
					});
				};
				tcplistener.Listen();

				var tcp = new Tcp();
				await tcp.ConnectAsync(ipep);
				tcp.Write("HELLO WORLD");

				var udp = new Udp();
				udp.Bind(ipep);
				pipe.Write(udp, Encoding.Default.GetBytes("UDP"), (ex) => udp.Close());
				pipe.Write(pipe, Encoding.Default.GetBytes("pipe"), (ex) => pipe.Close());
			});

			Assert.Equal(3, count);
		}
Beispiel #8
0
        public static void Run(IPEndPoint ep)
        {
            int close_cb_called   = 0;
            int cl_send_cb_called = 0;
            int cl_recv_cb_called = 0;
            int sv_send_cb_called = 0;
            int sv_recv_cb_called = 0;

            Udp client = new Udp();
            Udp server = new Udp();

            server.Bind(ep);
            server.Receive(Encoding.ASCII, (rinfo, str) => {
                Assert.AreEqual(str, "PING");
                sv_recv_cb_called++;
                server.Send(rinfo, Encoding.ASCII.GetBytes("PONG"), (s) => {
                    sv_send_cb_called++;
                    server.Close(() => { close_cb_called++; });
                });
            });

            client.Send(ep, Encoding.ASCII.GetBytes("PING"), (s) => {
                cl_send_cb_called++;
                client.Receive(Encoding.ASCII, (rinfo, str) => {
                    Assert.AreEqual(str, "PONG");
                    cl_recv_cb_called++;
                    client.Close(() => { close_cb_called++; });
                });
            });


            Assert.AreEqual(0, close_cb_called);
            Assert.AreEqual(0, cl_send_cb_called);
            Assert.AreEqual(0, cl_recv_cb_called);
            Assert.AreEqual(0, sv_send_cb_called);
            Assert.AreEqual(0, sv_recv_cb_called);

            Loop.Default.Run();

            Assert.AreEqual(2, close_cb_called);
            Assert.AreEqual(1, cl_send_cb_called);
            Assert.AreEqual(1, cl_recv_cb_called);
            Assert.AreEqual(1, sv_send_cb_called);
            Assert.AreEqual(1, sv_recv_cb_called);


#if DEBUG
            Assert.AreEqual(1, UV.PointerCount);
#endif
        }
Beispiel #9
0
        public void IpFamily(string ipString)
        {
            var endPoint = new IPEndPoint(IPAddress.Parse(ipString), Port);
            Udp udp      = this.loop.CreateUdp();

            /* don't keep the loop alive */
            udp.RemoveReference();
            udp.Bind(endPoint);

            udp.Broadcast(true);
            udp.Broadcast(true);
            udp.Broadcast(false);
            udp.Broadcast(false);

            /* values 1-255 should work */
            for (int i = 1; i <= 255; i++)
            {
                udp.Ttl(i);
            }

            var invalidTtls = new [] { -1, 0, 256 };

            foreach (int i in invalidTtls)
            {
                var error = Assert.Throws <OperationException>(() => udp.Ttl(i));
                Assert.Equal(ErrorCode.EINVAL, error.ErrorCode);
            }

            udp.MulticastLoopback(true);
            udp.MulticastLoopback(true);
            udp.MulticastLoopback(false);
            udp.MulticastLoopback(false);

            /* values 0-255 should work */
            for (int i = 0; i <= 255; i++)
            {
                udp.MulticastTtl(i);
            }

            /* anything >255 should fail */
            var exception = Assert.Throws <OperationException>(() => udp.MulticastTtl(256));

            Assert.Equal(ErrorCode.EINVAL, exception.ErrorCode);
            /* don't test ttl=-1, it's a valid value on some platforms */

            this.loop.RunDefault();
        }
        public void Run()
        {
            if (!Platform.OSSupportsIPv6)
            {
                return;
            }

            this.closeCount      = 0;
            this.serverSendCount = 0;

            this.loop = new Loop();
            var endPoint = new IPEndPoint(IPAddress.Parse("::1"), Port);

            var anyEndPoint = new IPEndPoint(IPAddress.Parse("::"), Port);
            Udp server      = this.loop.CreateUdp();

            try
            {
                server.Bind(anyEndPoint).MulticastInterface(IPAddress.IPv6Loopback);
                byte[] data = Encoding.UTF8.GetBytes("PING");
                server.QueueSend(data, endPoint, this.OnServerSendCompleted);

                this.loop.RunDefault();

                Assert.Equal(1, this.closeCount);
                Assert.Equal(1, this.serverSendCount);
            }
            catch (OperationException exception)
            {
                this.sendError = exception;
            }

            if (Platform.IsWindows)
            {
                Assert.Null(this.sendError);
            }
            else
            {
                if (this.sendError is object) // Azure DevOps(Linux) sendError is null
                {
                    Assert.IsType <OperationException>(this.sendError);
                    var error = (OperationException)this.sendError;
                    Assert.Equal(ErrorCode.EADDRNOTAVAIL, error.ErrorCode);
                }
            }
        }
Beispiel #11
0
        public static void Run(IPEndPoint ep)
        {
            int close_cb_called = 0;
            int cl_send_cb_called = 0;
            int cl_recv_cb_called = 0;
            int sv_send_cb_called = 0;
            int sv_recv_cb_called = 0;

            Udp client = new Udp();
            Udp server = new Udp();

            server.Bind(ep);
            server.Receive(Encoding.ASCII, (rinfo, str) => {
                Assert.AreEqual(str, "PING");
                sv_recv_cb_called++;
                server.Send(rinfo, Encoding.ASCII.GetBytes("PONG"), (s) => {
                    sv_send_cb_called++;
                    server.Close(() => { close_cb_called++; });
                });
            });

            client.Send(ep, Encoding.ASCII.GetBytes("PING"), (s) => {
                cl_send_cb_called++;
                client.Receive(Encoding.ASCII, (rinfo, str) => {
                    Assert.AreEqual(str, "PONG");
                    cl_recv_cb_called++;
                    client.Close(() => { close_cb_called++; });
                });
            });

            Assert.AreEqual(0, close_cb_called);
            Assert.AreEqual(0, cl_send_cb_called);
            Assert.AreEqual(0, cl_recv_cb_called);
            Assert.AreEqual(0, sv_send_cb_called);
            Assert.AreEqual(0, sv_recv_cb_called);

            Loop.Default.Run();

            Assert.AreEqual(2, close_cb_called);
            Assert.AreEqual(1, cl_send_cb_called);
            Assert.AreEqual(1, cl_recv_cb_called);
            Assert.AreEqual(1, sv_send_cb_called);
            Assert.AreEqual(1, sv_recv_cb_called);

            #if DEBUG
            Assert.AreEqual(1, UV.PointerCount);
            #endif
        }