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"); }); }); }
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")); }); }
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"); }); }); }); }
public static void Write(this ReverseAsyncSocket socket, int id, string text) { var data = Encoding.UTF8.GetBytes(text); socket.Write(id, Command.Data, data, 0, data.Length); }