예제 #1
0
        public static void OneSideClose(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;

            var server = new TcpListener();
            server.Bind(ep);
            server.Listen();
            server.IncommingStream += () => {
                var socket = server.AcceptStream();
                socket.Resume();
                socket.Read(Encoding.ASCII, (str) => {
                    sv_recv_cb_called++;
                    Assert.AreEqual("PING", str);
                    socket.Write(Encoding.ASCII, "PONG", (s) => { sv_send_cb_called++; });
                    socket.Close(() => { close_cb_called++; });
                    server.Close(() => { close_cb_called++; });
                });
            };

            Tcp client = new Tcp();
            client.Connect(ep, (_) => {
                client.Read(Encoding.ASCII, (str) => {
                    cl_recv_cb_called++;
                    Assert.AreEqual("PONG", str);
                });

                client.Complete += () => {
                    close_cb_called++;
                };
                client.Resume();
                client.Write(Encoding.ASCII, "PING", (s) => { cl_send_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(3, 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
        }
예제 #2
0
        public static void Stress(IPEndPoint ep)
        {
            for (int j = 0; j < 10; j++) {
                int times = 10;

                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;

                var server = new TcpListener();
                server.Bind(ep);
                server.IncommingStream += () => {
                    var socket = server.AcceptStream();
                    socket.Resume();
                    socket.Read(Encoding.ASCII, (str) => {
                        sv_recv_cb_called++;
                        Assert.AreEqual(Times("PING", times), str);
                        for (int i = 0; i < times; i++) {
                            socket.Write(Encoding.ASCII, "PONG", (s) => { sv_send_cb_called++; });
                        }
                        socket.Close(() => { close_cb_called++; });
                        server.Close(() => { close_cb_called++; });
                    });
                };
                server.Listen();

                var client = new Tcp();
                client.Connect(ep, (_) => {
                    client.Resume();
                    for (int i = 0; i < times; i++) {
                        client.Write(Encoding.ASCII, "PING", (s) => { cl_send_cb_called++; });
                    }
                    client.Read(Encoding.ASCII, (str) => {
                        cl_recv_cb_called++;
                        Assert.AreEqual(Times("PONG", times), str);
                        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(3, close_cb_called);
                Assert.AreEqual(times, cl_send_cb_called);
                Assert.AreEqual(1, cl_recv_cb_called);
                Assert.AreEqual(times, sv_send_cb_called);
                Assert.AreEqual(1, sv_recv_cb_called);

            #if DEBUG
                Assert.AreEqual(1, UV.PointerCount);
            #endif
            }
        }
예제 #3
0
        public static void PeerAndSockname()
        {
            Tcp client = null;
            Tcp server = null;

            bool called = false;
            var l = new TcpListener();
            l.Bind(IPAddress.Any, 8000);

            Action check = () => {
                if (client == null || server == null) {
                    return;
                }

                Assert.AreEqual(client.Sockname, server.Peername);
                Assert.AreEqual(client.Peername, server.Sockname);
                Assert.AreEqual(server.Sockname.Port, 8000);

                client.Shutdown();
                server.Shutdown();
                l.Close();

                called = true;
            };

            l.Listen();
            l.IncommingStream += () => {
                server = l.AcceptStream();
                check();
            };

            Tcp t = new Tcp();
            t.Connect("127.0.0.1", 8000, (e) => {
                client = t;
                check();
            });

            Loop.Default.Run();

            Assert.IsTrue(called);
        }