Exemplo n.º 1
0
        public static Tuple <int, byte[]> SendResponse(HttpListenerRequest request, HttpListenerResponse response)
        {
            if (request.Url.AbsolutePath == "/")
            {
                response.ContentType = "text/html";

                var asmPath   = Assembly.GetExecutingAssembly().Location;
                var indexPath = Path.Combine(new FileInfo(asmPath).DirectoryName, "index.htm");
                using (var reader = new StreamReader(indexPath))
                {
                    return(new Tuple <int, byte[]>(200, Encoding.UTF8.GetBytes(reader.ReadToEnd().Replace("__PROTOCOL__", isServerIpv4 ? "ipv4" : "ipv6"))));
                }
            }

            if (request.Url.AbsolutePath == "/basic")
            {
                response.ContentType = "text/html";

                var asmPath   = Assembly.GetExecutingAssembly().Location;
                var indexPath = Path.Combine(new FileInfo(asmPath).DirectoryName, "basic.htm");
                using (var reader = new StreamReader(indexPath))
                {
                    return(new Tuple <int, byte[]>(200, Encoding.UTF8.GetBytes(reader.ReadToEnd().Replace("__PROTOCOL__", isServerIpv4 ? "ipv4" : "ipv6"))));
                }
            }

            if (request.Url.AbsolutePath == "/token")
            {
                response.ContentType = "text/plain";

                var clientId = NetcodeLibrary.GetRandomUInt64();
                var token    = NetcodeLibrary.GenerateConnectTokenFromPrivateKey(
                    new[] { serverAddress + ":" + serverPort },
                    30,
                    clientId,
                    0x1122334455667788L,
                    0,
                    _privateKey);
                return(new Tuple <int, byte[]>(200, Encoding.UTF8.GetBytes(Convert.ToBase64String(token))));
            }

            if (request.Url.AbsolutePath == "/netcode-support.xpi")
            {
                response.ContentType = "application/x-xpinstall";

                var asmPath = Assembly.GetExecutingAssembly().Location;
                var xpiPath = Path.Combine(new FileInfo(asmPath).DirectoryName, "netcodeio_support_self_dist-0.1.5-fx.xpi");
                using (var reader = new FileStream(xpiPath, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    var b = new byte[reader.Length];
                    reader.Read(b, 0, b.Length);
                    return(new Tuple <int, byte[]>(200, b));
                }
            }

            return(new Tuple <int, byte[]>(404, Encoding.UTF8.GetBytes("404 not found")));
        }
Exemplo n.º 2
0
        public static string SendResponse(HttpListenerRequest request, HttpListenerResponse response)
        {
            if (request.Url.AbsolutePath == "/")
            {
                response.ContentType = "text/html";

                var asmPath   = Assembly.GetExecutingAssembly().Location;
                var indexPath = Path.Combine(new FileInfo(asmPath).DirectoryName, "index.htm");
                using (var reader = new StreamReader(indexPath))
                {
                    return(reader.ReadToEnd().Replace("__PROTOCOL__", isServerIpv4 ? "ipv4" : "ipv6"));
                }
            }

            if (request.Url.AbsolutePath == "/basic")
            {
                response.ContentType = "text/html";

                var asmPath   = Assembly.GetExecutingAssembly().Location;
                var indexPath = Path.Combine(new FileInfo(asmPath).DirectoryName, "basic.htm");
                using (var reader = new StreamReader(indexPath))
                {
                    return(reader.ReadToEnd().Replace("__PROTOCOL__", isServerIpv4 ? "ipv4" : "ipv6"));
                }
            }

            if (request.Url.AbsolutePath == "/token")
            {
                response.ContentType = "text/plain";

                var clientId = NetcodeLibrary.GetRandomUInt64();
                var token    = NetcodeLibrary.GenerateConnectTokenFromPrivateKey(
                    new[] { serverAddress + ":" + serverPort },
                    30,
                    clientId,
                    0x1122334455667788L,
                    0,
                    _privateKey);
                return(Convert.ToBase64String(token));
            }

            return("404 not found");
        }
Exemplo n.º 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);
        }
Exemplo n.º 4
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 quit = false;

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

            NetcodeLibrary.SetLogLevel(NetcodeLogLevel.Info);

            Console.WriteLine("[client]");

            var client = new Client(
                "0.0.0.0",
                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 packetData    = new byte[maxPacketSize];

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

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

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

                while (true)
                {
                    var packet = client.ReceivePacket();
                    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}!");
                            }
                        }
                    }
                }

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

                NetcodeLibrary.Sleep(deltaTime);

                time += deltaTime;
            }

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

            client.Dispose();
        }