コード例 #1
0
    static void profile_initialize()
    {
        Console.Write("initializing\n");

        netcode.init();

        netcode.random_bytes(private_key, netcode.KEY_BYTES);

        int i;

        for (i = 0; i < netcode.MAX_PACKET_SIZE; ++i)
        {
            packet_data[i] = (byte)i;
        }

        netcode.default_server_config(out var server_config);
        server_config.protocol_id = PROTOCOL_ID;
        BufferEx.Copy(server_config.private_key, private_key, netcode.KEY_BYTES);

        for (i = 0; i < MAX_SERVERS; ++i)
        {
            var server_address = $"127.0.0.1:{SERVER_BASE_PORT + i}";
            server[i] = netcode.server_create(server_address, server_config, 0.0);
        }

        netcode.default_client_config(out var client_config);

        for (i = 0; i < MAX_CLIENTS; ++i)
        {
            client[i] = netcode.client_create("0.0.0.0", client_config, 0.0);
        }
    }
コード例 #2
0
        public void BufferEx_BulkCopy()
        {
            var bytes = new byte[256];
            var rgbs  = Enumerable.Range(0, 16).Select(i => new RGB {
                R = i, B = i * 10, G = i * 100
            }).ToArray();

            BufferEx.Copy(rgbs, 0, bytes, 0, rgbs.Length);
            var rgbs2 = new RGB[rgbs.Length];

            BufferEx.Copy(bytes, 0, rgbs2, 0, rgbs.Length);

            for (int i = 0; i < 16; i++)
            {
                Assert.AreEqual(rgbs[i], rgbs2[i]);
            }
        }
コード例 #3
0
    static int Main(string[] args)
    {
        if (netcode.init() != netcode.OK)
        {
            Console.Write("error: failed to initialize netcode.io\n");
            return(1);
        }

        netcode.log_level(netcode.LOG_LEVEL_INFO);

        var          time       = 0.0;
        const double delta_time = 1.0 / 60.0;

        Console.Write("[client/server]\n");

        netcode.default_client_config(out var client_config);
        var client = netcode.client_create("::", client_config, time);

        if (client == null)
        {
            Console.Write("error: failed to create client\n");
            return(1);
        }

        netcode.default_server_config(out var server_config);
        server_config.protocol_id = PROTOCOL_ID;
        BufferEx.Copy(server_config.private_key, private_key, netcode.KEY_BYTES);

        var server_address = "[::1]:40000";

        var server = netcode.server_create(server_address, server_config, time);

        if (server == null)
        {
            Console.Write("error: failed to create server\n");
            return(1);
        }

        netcode.server_start(server, 1);

        var connect_token = new byte[netcode.CONNECT_TOKEN_BYTES];

        var client_id = 0UL;

        netcode.random_bytes(ref client_id, 8);
        Console.Write($"client id is {client_id,16}\n");

        var user_data = new byte [netcode.USER_DATA_BYTES];

        netcode.random_bytes(user_data, netcode.USER_DATA_BYTES);

        if (netcode.generate_connect_token(1, new[] { server_address }, new[] { server_address }, CONNECT_TOKEN_EXPIRY, CONNECT_TOKEN_TIMEOUT, client_id, PROTOCOL_ID, private_key, user_data, connect_token) != netcode.OK)
        {
            Console.Write("error: failed to generate connect token\n");
            return(1);
        }

        netcode.client_connect(client, connect_token);

        Console.CancelKeyPress += interrupt_handler;

        var server_num_packets_received = 0;
        var client_num_packets_received = 0;

        var packet_data = new byte[netcode.MAX_PACKET_SIZE];
        int i;

        for (i = 0; i < netcode.MAX_PACKET_SIZE; ++i)
        {
            packet_data[i] = (byte)i;
        }

        while (!quit)
        {
            netcode.client_update(client, time);

            netcode.server_update(server, time);

            if (netcode.client_state(client) == netcode.CLIENT_STATE_CONNECTED)
            {
                netcode.client_send_packet(client, packet_data, netcode.MAX_PACKET_SIZE);
            }

            if (netcode.server_client_connected(server, 0))
            {
                netcode.server_send_packet(server, 0, packet_data, netcode.MAX_PACKET_SIZE);
            }

            while (true)
            {
                var packet = netcode.client_receive_packet(client, out var packet_bytes, out var packet_sequence);
                if (packet == null)
                {
                    break;
                }
                Debug.Assert(packet_bytes == netcode.MAX_PACKET_SIZE);
                Debug.Assert(BufferEx.Equal(packet, packet_data, netcode.MAX_PACKET_SIZE));
                client_num_packets_received++;
                netcode.client_free_packet(client, ref packet);
            }

            while (true)
            {
                var packet = netcode.server_receive_packet(server, 0, out var packet_bytes, out var packet_sequence);
                if (packet == null)
                {
                    break;
                }
                Debug.Assert(packet_bytes == netcode.MAX_PACKET_SIZE);
                Debug.Assert(BufferEx.Equal(packet, packet_data, netcode.MAX_PACKET_SIZE));
                server_num_packets_received++;
                netcode.server_free_packet(server, ref packet);
            }

            if (client_num_packets_received >= 10 && server_num_packets_received >= 10)
            {
                if (netcode.server_client_connected(server, 0))
                {
                    Console.Write("client and server successfully exchanged packets\n");

                    netcode.server_disconnect_client(server, 0);
                }
            }

            if (netcode.client_state(client) <= netcode.CLIENT_STATE_DISCONNECTED)
            {
                break;
            }

            netcode.sleep(delta_time);

            time += delta_time;
        }

        if (quit)
        {
            Console.Write("\nshutting down\n");
        }

        netcode.server_destroy(ref server);

        netcode.client_destroy(ref client);

        netcode.term();

        return(0);
    }
コード例 #4
0
    static int Main(string[] args)
    {
        if (netcode.init() != netcode.OK)
        {
            Console.Write("error: failed to initialize netcode.io\n");
            return(1);
        }

        netcode.log_level(netcode.LOG_LEVEL_INFO);

        var          time       = 0.0;
        const double delta_time = 1.0 / 60.0;

        Console.Write("[server]\n");

        const ulong TEST_PROTOCOL_ID = 0x1122334455667788;

        var server_address = "127.0.0.1:40000";

        if (args.Length == 2)
        {
            server_address = args[1];
        }

        netcode.default_server_config(out var server_config);
        server_config.protocol_id = TEST_PROTOCOL_ID;
        BufferEx.Copy(server_config.private_key, private_key, netcode.KEY_BYTES);

        var server = netcode.server_create(server_address, server_config, time);

        if (server == null)
        {
            Console.Write("error: failed to create server\n");
            return(1);
        }

        netcode.server_start(server, netcode.MAX_CLIENTS);

        Console.CancelKeyPress += interrupt_handler;

        var packet_data = new byte[netcode.MAX_PACKET_SIZE];
        int i;

        for (i = 0; i < netcode.MAX_PACKET_SIZE; ++i)
        {
            packet_data[i] = (byte)i;
        }

        while (!quit)
        {
            netcode.server_update(server, time);

            if (netcode.server_client_connected(server, 0))
            {
                netcode.server_send_packet(server, 0, packet_data, netcode.MAX_PACKET_SIZE);
            }

            int client_index;
            for (client_index = 0; client_index < netcode.MAX_CLIENTS; ++client_index)
            {
                while (true)
                {
                    var packet = netcode.server_receive_packet(server, client_index, out var packet_bytes, out var packet_sequence);
                    if (packet == null)
                    {
                        break;
                    }
                    Debug.Assert(packet_bytes == netcode.MAX_PACKET_SIZE);
                    Debug.Assert(BufferEx.Equal(packet, packet_data, netcode.MAX_PACKET_SIZE));
                    netcode.server_free_packet(server, ref packet);
                }
            }

            netcode.sleep(delta_time);

            time += delta_time;
        }

        if (quit)
        {
            Console.Write("\nshutting down\n");
        }

        netcode.server_destroy(ref server);

        netcode.term();

        return(0);
    }
コード例 #5
0
    static void soak_iteration(double time)
    {
        int i;

        netcode.default_server_config(out var server_config);
        server_config.protocol_id = PROTOCOL_ID;
        BufferEx.Copy(server_config.private_key, private_key, netcode.KEY_BYTES);

        for (i = 0; i < MAX_SERVERS; ++i)
        {
            if (server[i] == null && random_int(0, 10) == 0)
            {
                var server_address = $"127.0.0.1:{SERVER_BASE_PORT + i }";
                server[i] = netcode.server_create(server_address, server_config, time);

                Console.Write($"created server {server[i]}\n");
            }

            if (server[i] != null && netcode.server_num_connected_clients(server[i]) == netcode.server_max_clients(server[i]) && random_int(0, 10000) == 0)
            {
                Console.Write($"destroy server {server[i]}\n");
                netcode.server_destroy(ref server[i]);
                server[i] = null;
            }
        }

        for (i = 0; i < MAX_CLIENTS; ++i)
        {
            if (client[i] == null && random_int(0, 10) == 0)
            {
                netcode.default_client_config(out var client_config);
                client[i] = netcode.client_create("0.0.0.0", client_config, time);
                Console.Write($"created client {client[i]}\n");
            }

            if (client[i] != null && random_int(0, 1000) == 0)
            {
                Console.Write($"destroy client {client[i]}\n");
                netcode.client_destroy(ref client[i]);
                client[i] = null;
            }
        }

        for (i = 0; i < MAX_SERVERS; ++i)
        {
            if (server[i] != null)
            {
                if (random_int(0, 10) == 0 && !netcode.server_running(server[i]))
                {
                    netcode.server_start(server[i], random_int(1, netcode.MAX_CLIENTS));
                }

                if (random_int(0, 1000) == 0 && netcode.server_num_connected_clients(server[i]) == netcode.server_max_clients(server[i]) && netcode.server_running(server[i]))
                {
                    netcode.server_stop(server[i]);
                }

                if (netcode.server_running(server[i]))
                {
                    var max_clients = netcode.server_max_clients(server[i]);
                    int client_index;
                    for (client_index = 0; client_index < max_clients; ++client_index)
                    {
                        if (netcode.server_client_connected(server[i], client_index))
                        {
                            netcode.server_send_packet(server[i], 0, packet_data, random_int(1, netcode.MAX_PACKET_SIZE));
                        }
                    }

                    for (client_index = 0; client_index < max_clients; ++client_index)
                    {
                        if (netcode.server_client_connected(server[i], client_index))
                        {
                            while (true)
                            {
                                var packet = netcode.server_receive_packet(server[i], client_index, out var packet_bytes, out var packet_sequence);
                                if (packet == null)
                                {
                                    break;
                                }
                                Debug.Assert(BufferEx.Equal(packet, packet_data, (int)packet_bytes));
                                netcode.server_free_packet(server[i], ref packet);
                            }
                        }
                    }
                }

                netcode.server_update(server[i], time);
            }
        }

        for (i = 0; i < MAX_CLIENTS; ++i)
        {
            if (client[i] != null)
            {
                if (random_int(0, 10) == 0 && netcode.client_state(client[i]) <= netcode.CLIENT_STATE_DISCONNECTED)
                {
                    var client_id = 0UL;
                    netcode.random_bytes(ref client_id, 8);

                    var user_data = new byte[netcode.USER_DATA_BYTES];
                    netcode.random_bytes(user_data, netcode.USER_DATA_BYTES);

                    var connect_token = new byte[netcode.CONNECT_TOKEN_BYTES];

                    var num_server_addresses = 0;
                    var server_address       = new string[netcode.MAX_SERVERS_PER_CONNECT];
                    int j;
                    for (j = 0; j < MAX_SERVERS; ++j)
                    {
                        if (num_server_addresses == netcode.MAX_SERVERS_PER_CONNECT)
                        {
                            break;
                        }

                        if (server[j] != null && netcode.server_running(server[j]))
                        {
                            server_address[num_server_addresses] = $"127.0.0.1:{SERVER_BASE_PORT + j}";
                            num_server_addresses++;
                        }
                    }

                    if (num_server_addresses > 0 && netcode.generate_connect_token(num_server_addresses, server_address, server_address, CONNECT_TOKEN_EXPIRY, CONNECT_TOKEN_TIMEOUT, client_id, PROTOCOL_ID, private_key, user_data, connect_token) == netcode.OK)
                    {
                        netcode.client_connect(client[i], connect_token);
                    }

                    for (j = 0; j < num_server_addresses; ++j)
                    {
                        server_address[j] = null;
                    }
                }

                if (random_int(0, 100) == 0 && netcode.client_state(client[i]) == netcode.CLIENT_STATE_CONNECTED)
                {
                    netcode.client_disconnect(client[i]);
                }

                if (netcode.client_state(client[i]) == netcode.CLIENT_STATE_CONNECTED)
                {
                    netcode.client_send_packet(client[i], packet_data, random_int(1, netcode.MAX_PACKET_SIZE));

                    while (true)
                    {
                        var packet = netcode.client_receive_packet(client[i], out var packet_bytes, out var packet_sequence);
                        if (packet == null)
                        {
                            break;
                        }
                        Debug.Assert(BufferEx.Equal(packet, packet_data, packet_bytes));
                        netcode.client_free_packet(client[i], ref packet);
                    }
                }

                netcode.client_update(client[i], time);
            }
        }
    }