示例#1
0
        public void TestDisconnectClient()
        {
            Until((port, done) =>
            {
                var server = new ReverseAsyncSocket();
                server.Listen(port, socket => socket.Disconnect());

                var count  = 2;
                var client = new ReverseAsyncSocket();
                client.Read((id, command, text) => { });
                client.Disconnect(() => done(--count == 0));
                client.Connect(Host, port, () => client.Disconnect(() => done(--count == 0)));
            });
        }
示例#2
0
        public void TestServerDisconnect()
        {
            Until((port, done) =>
            {
                var server = new ReverseAsyncSocket();
                server.Listen(port, socket =>
                {
                    socket.Disconnect(done);
                    socket.Read((id, command, text) => { });
                });

                var client = new ReverseAsyncSocket();
                client.Connect("localhost", port, client.Disconnect);
            });
        }
示例#3
0
        public void TestPingPong()
        {
            Until((port, done) =>
            {
                var server = new ReverseAsyncSocket();
                server.Listen(port, socket => socket.ReadString((id, command, text) =>
                {
                    Assert.GreaterOrEqual(id, 42);
                    Assert.LessOrEqual(id, 43);
                    Assert.AreEqual("Hello", text);

                    var res = (new[] { "eirikb", "world!" })[43 - id];
                    socket.Write(id + 1, res);
                }));

                var client = new ReverseAsyncSocket();
                client.ReadString((id, command, text) =>
                {
                    Assert.GreaterOrEqual(id, 43);
                    Assert.LessOrEqual(id, 44);
                    if (id != 43)
                    {
                        return;
                    }

                    Assert.AreEqual("world!", text);
                    client.Write(id, "Hello");
                });

                client.Connect(Host, port, () =>
                {
                    client.ReadString((id, command, text) =>
                    {
                        Assert.GreaterOrEqual(id, 43);
                        Assert.LessOrEqual(id, 44);
                        if (id != 44)
                        {
                            return;
                        }

                        Assert.AreEqual("eirikb", text);
                        done();
                    });

                    client.Write(42, "Hello");
                });
            });
        }
示例#4
0
        public void TestPingClient()
        {
            Until((port, done) =>
            {
                var server = new ReverseAsyncSocket();
                server.Listen(port, socket => socket.Read((id, command, data) =>
                {
                    var text = Encoding.UTF8.GetString(data);
                    Assert.AreEqual(7, id);
                    Assert.AreEqual("Hello", text);
                    done();
                }));

                var client = new ReverseAsyncSocket();
                client.Connect(Host, port, () => client.Write(7, "Hello"));
            });
        }
示例#5
0
        public void FullTest()
        {
            Until((port, done) =>
            {
                var serverPortA = port + 1;
                var serverPortB = port + 2;

                var serverA = new AsyncSocket();
                var serverB = new AsyncSocket();
                var clientA = new AsyncSocket();

                var reverseServer = new ReverseAsyncSocket();
                var reverseClient = new ReverseAsyncSocket();

                var netro = new Netro.Netro();

                serverA.Listen(serverPortA);
                serverB.Listen(serverPortB);
                reverseServer.Listen(port);

                netro.ReverseServerToServer(reverseServer, serverA);
                netro.ReverseClientToClient(reverseClient, Host, serverPortB);

                reverseServer.Connect(s =>
                {
                    serverB.Connect(socket => socket.Read(text =>
                    {
                        Assert.AreEqual("Hello", text);
                        socket.Write("world!");
                    }));

                    clientA.Connect(Host, serverPortA, () =>
                    {
                        clientA.Write("Hello");
                        clientA.Read(text =>
                        {
                            Assert.AreEqual("world!", text);
                            done();
                        });
                    });
                });

                reverseClient.Connect(Host, port, () => { });
            });
        }
示例#6
0
        public void TestReverseServerToServer()
        {
            Until((port, done) =>
            {
                var reverseServer = new ReverseAsyncSocket();
                var server        = new AsyncSocket();

                var reverseClient = new ReverseAsyncSocket();
                var client        = new AsyncSocket();

                var netro      = new Netro.Netro();
                var serverPort = port + 1;

                reverseServer.Listen(port);
                server.Listen(serverPort);

                netro.ReverseServerToServer(reverseServer, server);

                reverseClient.ReadString((tid, command, text) =>
                {
                    Assert.AreEqual("Hello", text);
                    reverseClient.Write(tid, "world!");
                });
                client.Read(text =>
                {
                    Assert.AreEqual("world!", text);
                    done();
                });

                reverseClient.Connect(Host, port,
                                      () =>
                {
                    client.Connect(Host, serverPort, () =>
                    {
                        client.Write("Hello");
                    });
                });
            });
        }
示例#7
0
        public void ReverseClientToClient()
        {
            Until((port, done) =>
            {
                var serverPort = port + 1;

                var reverseServer = new ReverseAsyncSocket();
                var server        = new AsyncSocket();

                var reverseClient = new ReverseAsyncSocket();
                var netro         = new Netro.Netro();

                netro.ReverseClientToClient(reverseClient, Host, serverPort);

                reverseServer.Listen(port);
                server.Listen(serverPort);

                reverseClient.Connect(Host, port, () => { });

                server.Connect(socket => socket.Read(text =>
                {
                    Assert.AreEqual("Hello", text);
                    socket.Write("world!");
                }));

                reverseServer.Connect(socket =>
                {
                    socket.ReadString((id, command, text) =>
                    {
                        Assert.AreEqual("world!", text);
                        done();
                    });
                    socket.Write(42, "Hello");
                });
            });
        }