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 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 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 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 TestSyncOnlineClient() { var conn = NetworkingUtilities.ConnectTCP(10045, "5.149.17.16"); //var conn = NetworkingClientTest.ConnectTCP(10045, "127.0.0.1"); conn.Receiving = true; var client = new ClientPacketManagerNetworked(conn); var clientSyncer = new ClientSyncer(client); var physicsEngine = new PhysicsEngine(); StillDesign.PhysX.Scene serverScene = null; PhysicsDebugRendererXNA debugRenderer; PhysicsDebugRendererXNA debugRendererServer; var game = new XNAGame(); game.InitializeEvent += delegate { physicsEngine.Initialize(); serverScene = physicsEngine.CreateScene(physicsEngine.Scene.Gravity, true); debugRenderer = new PhysicsDebugRendererXNA(game, physicsEngine.Scene); game.AddXNAObject(debugRenderer); debugRendererServer = new PhysicsDebugRendererXNA(game, serverScene); game.AddXNAObject(debugRendererServer); ActorDescription actorDesc; SphereShapeDescription shape; shape = new SphereShapeDescription(1); actorDesc = new ActorDescription(shape); actorDesc.BodyDescription = new BodyDescription(10); var client1 = clientSyncer.CreateActor(physicsEngine.Scene.CreateActor(actorDesc)); client1.ID = 2; // Identify }; game.UpdateEvent += delegate { physicsEngine.Update(game); physicsEngine.UpdateScene(game.Elapsed, serverScene); clientSyncer.Update(game.Elapsed); }; client.SyncronizeRemotePacketIDs(); client.WaitForUDPConnected(); game.Run(); physicsEngine.Dispose(); }
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 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 TestClientPacketRequester() { 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.DisableUDP(); requester = manager1.CreatePacketRequester("Test1", new DataPacketFactory(), new ErrorPacketFactory(), delegate(DataPacket packet) { return(new ErrorPacket("Received: " + packet.Number.ToString() + " - " + packet.Text)); }, PacketFlags.TCP); manager1.AutoAssignPacketIDs(); ClientPacketManagerNetworked manager2 = new ClientPacketManagerNetworked(tcp2); manager2.DisableUDP(); requester = manager2.CreatePacketRequester("Test1", new DataPacketFactory(), new ErrorPacketFactory(), PacketFlags.TCP); manager2.SyncronizeRemotePacketIDs(); ErrorPacket ep; ep = requester.SendRequest(new DataPacket("Hello", 345)); Assert.AreEqual(ep.Description, "Received: 345 - Hello"); ep = requester.SendRequest(new DataPacket("Goodbye", 8)); Assert.AreEqual(ep.Description, "Received: 8 - Goodbye"); /*dataTransporter.Send( new Test.DataPacket( "Hello", 345 ) ); * * Assert.AreEqual( dp.Text, "Hello" ); * Assert.AreEqual( dp.Number, 345 );*/ }
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 void TestSyncOnline() { var server = new ServerPacketManagerNetworked(10045, 10046); var success = new AutoResetEvent(false); var serverSyncer = new ServerSyncer(server); server.Start(); var conn = NetworkingUtilities.ConnectTCP(10045, "127.0.0.1"); conn.Receiving = true; var client = new ClientPacketManagerNetworked(conn); var clientSyncer = new ClientSyncer(client); var physicsEngine = new PhysicsEngine(); StillDesign.PhysX.Scene serverScene = null; PhysicsDebugRendererXNA debugRenderer; PhysicsDebugRendererXNA debugRendererServer; var game = new XNAGame(); game.InitializeEvent += delegate { physicsEngine.Initialize(); serverScene = physicsEngine.CreateScene(physicsEngine.Scene.Gravity, true); debugRenderer = new PhysicsDebugRendererXNA(game, physicsEngine.Scene); game.AddXNAObject(debugRenderer); debugRendererServer = new PhysicsDebugRendererXNA(game, serverScene); game.AddXNAObject(debugRendererServer); ActorDescription actorDesc; SphereShapeDescription shape; shape = new SphereShapeDescription(1); actorDesc = new ActorDescription(shape); actorDesc.BodyDescription = new BodyDescription(10); var server1 = serverSyncer.CreateActor(serverScene.CreateActor(actorDesc)); ((WorldPhysxSyncActor)server1.Actor).Actor.AddForce(Vector3.UnitX * 200, ForceMode.Impulse); shape = new SphereShapeDescription(1); actorDesc = new ActorDescription(shape); actorDesc.BodyDescription = new BodyDescription(10); var client1 = clientSyncer.CreateActor(physicsEngine.Scene.CreateActor(actorDesc)); client1.ID = server1.ID; // Identify }; game.UpdateEvent += delegate { physicsEngine.Update(game); physicsEngine.UpdateScene(game.Elapsed, serverScene); serverSyncer.Update(game.Elapsed); clientSyncer.Update(game.Elapsed); }; client.SyncronizeRemotePacketIDs(); client.WaitForUDPConnected(); game.Run(); physicsEngine.Dispose(); }