Exemplo n.º 1
0
        public void TestEstablishUDP()
        {
            var server  = new ServerPacketManagerNetworked(10045, 10046);
            var success = new AutoResetEvent(false);

            var ts = new Thread(server.Start);

            ts.Name = "ServerTestThread";
            ts.Start();
            ClientPacketManagerNetworked client = null;
            var tc = new Thread(delegate()
            {
                var conn       = NetworkingClientTest.ConnectTCP(10045, "127.0.0.1");
                conn.Receiving = true;
                client         = new ClientPacketManagerNetworked(conn);

                client.WaitForUDPConnected();
                success.Set();
            });

            tc.Name = "ClientTestThread";
            tc.Start();

            if (!success.WaitOne(5000))
            {
                throw new Exception("Test timed out!");
            }
            if (server.Clients.Count == 0 ||
                server.Clients[0].UDPEndPoint == null ||
                client == null ||
                client.IsUDPConnected == false)
            {
                throw new Exception("Test Failed, UDP connection not correctly established!");
            }
        }
Exemplo n.º 2
0
        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!");
            }
        }
Exemplo n.º 3
0
        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 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();
        }
Exemplo n.º 5
0
        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();
        }