예제 #1
0
        private static void NetcodeServer()
        {
            NetcodeLibrary.SetLogLevel(NetcodeLogLevel.Info);

            double time      = 0f;
            double deltaTime = 1.0 / 60.0;

            var server = new Server(
                serverAddress + ":" + serverPort,
                0x1122334455667788L,
                _privateKey,
                0);

            server.Start(NetcodeLibrary.GetMaxClients());

            lastPacketMessage = new byte[NetcodeLibrary.GetMaxClients()][];

            while (running)
            {
                server.Update(time);

                for (var clientIndex = 0; clientIndex < NetcodeLibrary.GetMaxClients(); clientIndex++)
                {
                    if (server.ClientConnected(clientIndex) && lastPacketMessage[clientIndex] != null)
                    {
                        server.SendPacket(clientIndex, lastPacketMessage[clientIndex]);
                        lastPacketMessage[clientIndex] = null;
                    }
                }

                for (var clientIndex = 0; clientIndex < NetcodeLibrary.GetMaxClients(); clientIndex++)
                {
                    while (true)
                    {
                        var packet = server.ReceivePacket(clientIndex);
                        if (packet == null)
                        {
                            break;
                        }

                        lastPacketMessage[clientIndex] = packet;
                    }
                }

                NetcodeLibrary.Sleep(deltaTime);

                time += deltaTime;
            }

            server.Dispose();
        }
예제 #2
0
        public static void Main(string[] args)
        {
            const UInt64 testProtocolId = 0x1122334455667788L;

            double time      = 0f;
            double deltaTime = 1.0 / 60.0;

            bool quit = false;

            Console.CancelKeyPress += (sender, a) =>
            {
                quit = true;
            };

            NetcodeLibrary.SetLogLevel(NetcodeLogLevel.Info);
            var server = new Server(
                "127.0.0.1:40000",
                testProtocolId,
                privateKey,
                time);

            var maxPacketSize = NetcodeLibrary.GetMaxPacketSize();
            var packetData    = new byte[maxPacketSize];

            for (var i = 0; i < maxPacketSize; i++)
            {
                packetData[i] = (byte)i;
            }

            server.Start(NetcodeLibrary.GetMaxClients());

            while (!quit)
            {
                server.Update(time);

                if (server.ClientConnected(0))
                {
                    server.SendPacket(0, packetData);
                }

                for (var clientIndex = 0; clientIndex < NetcodeLibrary.GetMaxClients(); ++clientIndex)
                {
                    while (true)
                    {
                        var packet = server.ReceivePacket(clientIndex);
                        if (packet == null)
                        {
                            break;
                        }

                        if (packet.Length != packetData.Length)
                        {
                            Console.Error.WriteLine("Packet doesn't have expected length!");
                        }
                        else
                        {
                            for (var i = 0; i < maxPacketSize; i++)
                            {
                                if (packet[i] != packetData[i])
                                {
                                    Console.Error.WriteLine($"Packet differs at index {i}!");
                                }
                            }
                        }
                    }
                }

                NetcodeLibrary.Sleep(deltaTime);

                time += deltaTime;
            }

            if (quit)
            {
                Console.WriteLine("Shutting down");
            }

            server.Dispose();
        }
예제 #3
0
        public static void Main(string[] args)
        {
            const int    testConnectTokenExpiry = 30;
            const UInt64 testProtocolId         = 0x1122334455667788L;

            double time      = 0f;
            double deltaTime = 1.0 / 60.0;

            bool     validatedServer = false;
            bool     validatedClient = false;
            DateTime startTime       = DateTime.UtcNow;

            NetcodeLibrary.SetLogLevel(NetcodeLogLevel.Info);

            var server = new Server(
                "127.0.0.1:40000",
                testProtocolId,
                privateKey,
                time);

            server.Start(NetcodeLibrary.GetMaxClients());

            var client = new Client(
                "127.0.0.1",
                time);

            var clientId = NetcodeLibrary.GetRandomUInt64();

            Console.WriteLine($"client id is {clientId:X}");

            var serverAddress = "127.0.0.1:40000";

            byte[] connectToken = NetcodeLibrary.GenerateConnectTokenFromPrivateKey(
                new[] { serverAddress },
                testConnectTokenExpiry,
                clientId,
                testProtocolId,
                0,
                privateKey);

            client.Connect(connectToken);

            var maxPacketSize    = NetcodeLibrary.GetMaxPacketSize();
            var serverPacketData = new byte[maxPacketSize];
            var clientPacketData = new byte[maxPacketSize];

            for (var i = 0; i < maxPacketSize; i++)
            {
                serverPacketData[i] = (byte)i;
                clientPacketData[i] = (byte)i;
            }

            while ((!validatedServer || !validatedClient) && (DateTime.UtcNow - startTime).TotalSeconds < 30)
            {
                server.Update(time);

                if (server.ClientConnected(0))
                {
                    server.SendPacket(0, serverPacketData);
                }

                for (var clientIndex = 0; clientIndex < NetcodeLibrary.GetMaxClients(); ++clientIndex)
                {
                    while (true)
                    {
                        var packet = server.ReceivePacket(clientIndex);
                        if (packet == null)
                        {
                            break;
                        }

                        var matches = true;

                        if (packet.Length != clientPacketData.Length)
                        {
                            Console.Error.WriteLine("Packet doesn't have expected length!");
                            matches = false;
                        }
                        else
                        {
                            for (var i = 0; i < maxPacketSize; i++)
                            {
                                if (packet[i] != clientPacketData[i])
                                {
                                    Console.Error.WriteLine($"Packet differs at index {i}!");
                                    matches = false;
                                }
                            }
                        }

                        if (matches)
                        {
                            validatedServer = true;
                        }
                    }
                }

                client.Update(time);

                if (client.State == ClientState.Connected)
                {
                    client.SendPacket(clientPacketData);
                }

                while (true)
                {
                    var packet = client.ReceivePacket();
                    if (packet == null)
                    {
                        break;
                    }

                    var matches = true;

                    if (packet.Length != serverPacketData.Length)
                    {
                        Console.Error.WriteLine("Packet doesn't have expected length!");
                        matches = false;
                    }
                    else
                    {
                        for (var i = 0; i < maxPacketSize; i++)
                        {
                            if (packet[i] != serverPacketData[i])
                            {
                                Console.Error.WriteLine($"Packet differs at index {i}!");
                                matches = false;
                            }
                        }
                    }

                    if (matches)
                    {
                        validatedClient = true;
                    }
                }

                if (client.State <= ClientState.Disconnected)
                {
                    break;
                }

                NetcodeLibrary.Sleep(deltaTime);

                time += deltaTime;
            }

            if ((DateTime.UtcNow - startTime).TotalSeconds > 30)
            {
                Console.Error.WriteLine("Timed out after 30 seconds.");
                Environment.Exit(1);
            }

            if (!validatedClient || !validatedServer)
            {
                Console.Error.WriteLine("Not in a valid state when exiting loop.");
                Environment.Exit(1);
            }

            client.Dispose();
            server.Dispose();

            Console.WriteLine("Test passed.");
            Environment.Exit(0);
        }