public void Connect(string ip, int port) { AutoResetEvent ev = new AutoResetEvent(false); var conn = new TCPConnection(); conn.ConnectedToServer += delegate { ev.Set(); }; conn.Connect(ip, port); if (!ev.WaitOne(5000)) { throw new Exception("Connection timed out!"); } var cpm = new ClientPacketManagerNetworked(conn); conn.Receiving = true; Thread.Sleep(100); cpm.WaitForUDPConnected(); cpm.SyncronizeRemotePacketIDs(); var gen = new NetworkPacketFactoryCodeGenerater(TWDir.Cache.CreateChild("GodGame").CreateFile("ClientPackets" + (new Random()).Next() + ".dll").FullName); UserInputHandlerTransporter = cpm.CreatePacketTransporter("UserInputHandler", gen.GetFactory <UserInputHandlerPacket>(), PacketFlags.TCP); UserInputTransporter = cpm.CreatePacketTransporter("UserInput", gen.GetFactory <UserInputPacket>(), PacketFlags.TCP); GameStateDeltaTransporter = cpm.CreatePacketTransporter("GameStateDelta", gen.GetFactory <GameStateDeltaPacket>(), PacketFlags.TCP); gen.BuildFactoriesAssembly(); }
public void TestServerSendReceiveTCP() { var server = new ServerPacketManagerNetworked(10045, 10046); var success = new AutoResetEvent(false); var ts = new Thread(delegate() { var t = server.CreatePacketTransporter("DataPacket", new DataPacketFactory(), PacketFlags.TCP); t.EnableReceiveMode(); var t2 = server.CreatePacketTransporter("DataPacket2", new DataPacketFactory(), PacketFlags.TCP); server.Start(); IClient client; while (t.PacketAvailable == false) { Thread.Sleep(500); } var p = t.Receive(out client); Assert.AreEqual(p.Text, "Helloooo!"); var tCl = t2.GetTransporterForClient(client); tCl.Send(new DataPacket("Hi there", 8888)); }); ts.Name = "ServerTestThread"; ts.Start(); var tc = new Thread(delegate() { var conn = NetworkingClientTest.ConnectTCP(10045, "127.0.0.1"); conn.Receiving = true; var client = new ClientPacketManagerNetworked(conn); var t = client.CreatePacketTransporter("DataPacket", new DataPacketFactory(), PacketFlags.TCP); var t2 = client.CreatePacketTransporter("DataPacket2", new DataPacketFactory(), PacketFlags.TCP); client.WaitForUDPConnected(); client.SyncronizeRemotePacketIDs(); t.Send(new DataPacket("Helloooo!", 564)); var p = t2.Receive(); Assert.AreEqual(p.Text, "Hi there"); success.Set(); }); tc.Name = "ClientTestThread"; tc.Start(); if (!success.WaitOne(5000)) { throw new Exception("Test timed out!"); } }
public void TestClientPacketManagerSyncronizeSynchronous() { TCPConnection tcp1; TCPConnection tcp2; establishTCP(out tcp1, out tcp2); tcp1.Receiving = true; tcp2.Receiving = true; IClientPacketRequester <DataPacket, ErrorPacket> requester; ClientPacketManagerNetworked manager1 = new ClientPacketManagerNetworked(tcp1); manager1.CreatePacketTransporter("Test1", new DataPacketFactory(), PacketFlags.TCP); manager1.CreatePacketTransporter("Test2", new ErrorPacketFactory(), PacketFlags.TCP); manager1.DisableUDP(); manager1.AutoAssignPacketIDs(); ClientPacketManagerNetworked manager2 = new ClientPacketManagerNetworked(tcp2); manager2.CreatePacketTransporter("Test1", new ErrorPacketFactory(), PacketFlags.TCP); manager2.CreatePacketTransporter("Test2", new DataPacketFactory(), PacketFlags.TCP); manager2.DisableUDP(); manager2.SyncronizeRemotePacketIDs(); }
public void TestClientPacketManagerSyncronizeAsynchronous() { TCPConnection tcp1; TCPConnection tcp2; establishTCP(out tcp1, out tcp2); tcp1.Receiving = true; tcp2.Receiving = true; AutoResetEvent serverReadyEvent = new AutoResetEvent(false); ClientPacketManagerNetworked manager1 = null; ClientPacketManagerNetworked manager2 = null; Thread t1 = new Thread( delegate() { manager1 = new ClientPacketManagerNetworked(tcp1); manager1.DisableUDP(); IClientPacketTransporter <DataPacket> dataTransporter; manager1.CreatePacketTransporter("DataPacket", new DataPacketFactory(), PacketFlags.TCP); manager1.CreatePacketTransporter("MyOwnTestTransporter", new ErrorPacketFactory(), PacketFlags.TCP); manager1.AutoAssignPacketIDs(); serverReadyEvent.Set(); }); Thread t2 = new Thread( delegate() { serverReadyEvent.WaitOne(); manager2 = new ClientPacketManagerNetworked(tcp2); manager2.DisableUDP(); manager2.CreatePacketTransporter("DataPacket", new ErrorPacketFactory(), PacketFlags.TCP); manager2.CreatePacketTransporter("MyOwnTestTransporter", new DataPacketFactory(), PacketFlags.TCP); manager2.SyncronizeRemotePacketIDs(); }); t1.Start(); t2.Start(); t1.Join(3000); t2.Join(3000); /*t1.Join(); * t2.Join();*/ Assert.True(t1.ThreadState != ThreadState.Running && t2.ThreadState != ThreadState.Running); }
public void TestTransmitUserInputNetworked() { TCPConnection conn1, conn2; NetworkingUtilities.EstablishTwoWayTCP(out conn1, out conn2); conn1.Receiving = true; conn2.Receiving = true; var man1 = new ClientPacketManagerNetworked(conn1); var man2 = new ClientPacketManagerNetworked(conn2); man1.DisableUDP(); man2.DisableUDP(); var gen = createPacketGen(); var proxyTrans = man1.CreatePacketTransporter("Test", gen.GetFactory <UserInputHandlerPacket>(), PacketFlags.TCP); var realTrans = man2.CreatePacketTransporter("Test", gen.GetFactory <UserInputHandlerPacket>(), PacketFlags.TCP); gen.BuildFactoriesAssembly(); man2.AutoAssignPacketIDs(); man1.SyncronizeRemotePacketIDs(); testInputHandler(proxyTrans, realTrans); }
public void TestClientTransporterSinglePacket() { TCPConnection tcp1; TCPConnection tcp2; establishTCP(out tcp1, out tcp2); tcp1.Receiving = true; tcp2.Receiving = true; AutoResetEvent serverReadyEvent = new AutoResetEvent(false); Thread t1 = new Thread( delegate() { ClientPacketManagerNetworked manager = new ClientPacketManagerNetworked(tcp1); manager.DisableUDP(); IClientPacketTransporter <DataPacket> dataTransporter; dataTransporter = manager.CreatePacketTransporter("Test1", new DataPacketFactory(), PacketFlags.TCP); manager.AutoAssignPacketIDs(); serverReadyEvent.Set(); DataPacket dp = dataTransporter.Receive(); Assert.AreEqual(dp.Text, "Hello"); Assert.AreEqual(dp.Number, 345); }); Thread t2 = new Thread( delegate() { serverReadyEvent.WaitOne(); ClientPacketManagerNetworked manager = new ClientPacketManagerNetworked(tcp2); manager.DisableUDP(); IClientPacketTransporter <DataPacket> dataTransporter; dataTransporter = manager.CreatePacketTransporter("Test1", new DataPacketFactory(), PacketFlags.TCP); manager.SyncronizeRemotePacketIDs(); dataTransporter.Send(new DataPacket("Hello", 345)); }); t1.Start(); t2.Start(); t1.Join(); t2.Join(); Assert.True(t1.ThreadState != ThreadState.Running && t2.ThreadState != ThreadState.Running); }
public void TestSendAll() { var server = new ServerPacketManagerNetworked(10045, 10046); var success1 = new AutoResetEvent(false); var success2 = new AutoResetEvent(false); var ts = new Thread(delegate() { var t = server.CreatePacketTransporter("DataPacket", new DataPacketFactory(), PacketFlags.TCP); server.Start(); IClient client; while (server.Clients.Count != 2) { Thread.Sleep(500); } t.SendAll(new DataPacket("ALIVEEE!!!", 645)); }); ts.Name = "ServerTestThread"; ts.Start(); var tc1 = new Thread(delegate() { var conn = NetworkingClientTest.ConnectTCP(10045, "127.0.0.1"); var client = new ClientPacketManagerNetworked(conn); conn.Receiving = true; var t = client.CreatePacketTransporter("DataPacket", new DataPacketFactory(), PacketFlags.TCP); client.WaitForUDPConnected(); client.SyncronizeRemotePacketIDs(); var p = t.Receive(); Assert.AreEqual(p.Text, "ALIVEEE!!!"); success1.Set(); }); tc1.Name = "ClientTestThread"; tc1.Start(); var tc2 = new Thread(delegate() { var conn = NetworkingClientTest.ConnectTCP(10045, "127.0.0.1"); var client = new ClientPacketManagerNetworked(conn); conn.Receiving = true; var t = client.CreatePacketTransporter("DataPacket", new DataPacketFactory(), PacketFlags.TCP); client.WaitForUDPConnected(); client.SyncronizeRemotePacketIDs(); var p = t.Receive(); Assert.AreEqual(p.Text, "ALIVEEE!!!"); success2.Set(); }); tc2.Name = "ClientTestThread"; tc2.Start(); if (!success1.WaitOne(5000)) { throw new Exception("Test timed out!"); } if (!success2.WaitOne(5000)) { throw new Exception("Test timed out!"); } }
public ClientPacketManagerNetworked.IClientPacketTransporterNetworked CreateTransporter(ClientPacketManagerNetworked manager) { return(manager.CreatePacketTransporter(uniqueName, factory, flags)); }