Ejemplo n.º 1
0
    static int ServerMain()
    {
        Console.Write($"started server on port {shared.ServerPort} (secure)\n");

        var time = 100.0;

        var config = new ClientServerConfig();

        config.protocolId = shared.ProtocolId;

        var privateKey = new byte[KeyBytes] {
            0x60, 0x6a, 0xbe, 0x6e, 0xc9, 0x19, 0x10, 0xea,
            0x9a, 0x65, 0x62, 0xf6, 0x6f, 0x2b, 0x30, 0xe4,
            0x43, 0x71, 0xd6, 0x2c, 0xd1, 0x99, 0x27, 0x26,
            0x6b, 0x3c, 0x60, 0xf4, 0xb7, 0x15, 0xab, 0xa1
        };

        var server = new Server(DefaultAllocator, privateKey, new Address("127.0.0.1", shared.ServerPort), config, shared.adapter, time);

        server.Start(MaxClients);

        const double deltaTime = 0.1;

        var addressString = server.Address.ToString();

        Console.Write($"server address is {addressString}\n");

        Console.CancelKeyPress += interrupt_handler;

        while (!quit)
        {
            server.SendPackets();

            server.ReceivePackets();

            time += deltaTime;

            server.AdvanceTime(time);

            if (!server.IsRunning)
            {
                break;
            }

            sleep(deltaTime);
        }

        server.Stop();

        return(0);
    }
Ejemplo n.º 2
0
    static int ServerMain()
    {
        Console.Write($"started server on port {shared.ServerPort} (insecure)\n");

        var time = 100.0;

        var config = new ClientServerConfig();

        var privateKey = new byte[KeyBytes];

        var server = new Server(DefaultAllocator, privateKey, new Address("127.0.0.1", shared.ServerPort), config, shared.adapter, time);

        server.Start(MaxClients);

        var addressString = server.Address.ToString();

        Console.Write($"server address is {addressString}\n");

        const double deltaTime = 0.01f;

        Console.CancelKeyPress += interrupt_handler;

        while (!quit)
        {
            server.SendPackets();

            server.ReceivePackets();

            time += deltaTime;

            server.AdvanceTime(time);

            if (!server.IsRunning)
            {
                break;
            }

            sleep(deltaTime);
        }

        server.Stop();

        return(0);
    }
Ejemplo n.º 3
0
    static int ClientMain(string[] args)
    {
        Console.Write("\nconnecting client (insecure)\n");

        var time = 100.0;

        var clientId = 0UL;

        random_bytes(ref clientId, 8);
        Console.Write($"client id is {clientId:x16}\n");

        var config = new ClientServerConfig();

        var client = new Client(DefaultAllocator, new Address("0.0.0.0"), config, shared.adapter, time);

        var serverAddress = new Address("127.0.0.1", shared.ServerPort);

        if (args.Length == 1)
        {
            var commandLineAddress = new Address(args[0]);
            if (commandLineAddress.IsValid)
            {
                if (commandLineAddress.Port == 0)
                {
                    commandLineAddress.Port = shared.ServerPort;
                }
                serverAddress = commandLineAddress;
            }
        }

        var privateKey = new byte[KeyBytes];

        client.InsecureConnect(privateKey, clientId, serverAddress);

        var addressString = client.Address.ToString();

        Console.Write($"client address is {addressString}\n");

        const double deltaTime = 0.01f;

        Console.CancelKeyPress += interrupt_handler;

        while (!quit)
        {
            client.SendPackets();

            client.ReceivePackets();

            if (client.IsDisconnected)
            {
                break;
            }

            time += deltaTime;

            client.AdvanceTime(time);

            if (client.ConnectionFailed)
            {
                break;
            }

            sleep(deltaTime);
        }

        client.Disconnect();

        return(0);
    }
Ejemplo n.º 4
0
    static int ClientServerMain()
    {
        var time = 100.0;

        var config = new ClientServerConfig();

        var privateKey = new byte[KeyBytes];

        Console.Write($"starting server on port {shared.ServerPort}\n");

        var server = new Server(DefaultAllocator, privateKey, new Address("127.0.0.1", shared.ServerPort), config, shared.adapter, time);

        server.Start(MaxClients);

        if (!server.IsRunning)
        {
            return(1);
        }

        Console.Write("started server\n");

        var clientId = 0UL;

        random_bytes(ref clientId, 8);
        Console.Write($"client id is {clientId:x16}\n");

        var client = new Client(DefaultAllocator, new Address("0.0.0.0"), config, shared.adapter, time);

        var serverAddress = new Address("127.0.0.1", shared.ServerPort);

        client.InsecureConnect(privateKey, clientId, serverAddress);

        const double deltaTime = 0.1;

        Console.CancelKeyPress += interrupt_handler;

        while (!quit)
        {
            server.SendPackets();
            client.SendPackets();

            server.ReceivePackets();
            client.ReceivePackets();

            time += deltaTime;

            client.AdvanceTime(time);

            if (client.IsDisconnected)
            {
                break;
            }

            time += deltaTime;

            server.AdvanceTime(time);

            sleep(deltaTime);
        }

        client.Disconnect();
        server.Stop();

        return(0);
    }
Ejemplo n.º 5
0
    static int ClientMain(string[] args)
    {
        Console.Write("\nconnecting client (secure)\n");

        var clientId = 0UL;

        random_bytes(ref clientId, 8);
        Console.Write($"client id is {clientId:x16}\n");

        var matcher = new Matcher(DefaultAllocator);

        if (!matcher.Initialize())
        {
            Console.Write("error: failed to initialize matcher\n");
            return(1);
        }

        Console.Write("requesting match from https://localhost:8080\n");

        matcher.RequestMatch(shared.ProtocolId, clientId, false);

        if (matcher.MatchStatus == MatchStatus.MATCH_FAILED)
        {
            Console.Write("\nRequest match failed. Is the matcher running? Please run \"premake5 matcher\" before you connect a secure client\n");
            return(1);
        }

        var connectToken = new byte[ConnectTokenBytes];

        matcher.GetConnectToken(connectToken);
        Console.Write("received connect token from matcher\n");

        var time = 100.0;

        var config = new ClientServerConfig();

        config.protocolId = shared.ProtocolId;

        var client = new Client(DefaultAllocator, new Address("0.0.0.0"), config, shared.adapter, time);

        var serverAddress = new Address("127.0.0.1", shared.ServerPort);

        if (args.Length == 2)
        {
            var commandLineAddress = new Address(args[1]);
            if (commandLineAddress.IsValid)
            {
                if (commandLineAddress.Port == 0)
                {
                    commandLineAddress.Port = shared.ServerPort;
                }
                serverAddress = commandLineAddress;
            }
        }

        client.Connect(clientId, connectToken);

        if (client.IsDisconnected)
        {
            return(1);
        }

        var addressString = client.Address.ToString();

        Console.Write($"client address is {addressString}\n");

        const double deltaTime = 0.1;

        Console.CancelKeyPress += interrupt_handler;

        while (!quit)
        {
            client.SendPackets();

            client.ReceivePackets();

            if (client.IsDisconnected)
            {
                break;
            }

            time += deltaTime;

            client.AdvanceTime(time);

            if (client.ConnectionFailed)
            {
                break;
            }

            sleep(deltaTime);
        }

        client.Disconnect();

        return(0);
    }
        public void CouldStartClientServer()
        {
            const bool deleteOnShutdown = true;
            var        baseDir          = @"D:\tmp\aeron";
            var        serverDir        = Path.Combine(baseDir !, "server-" + Guid.NewGuid().ToString("N"));
            var        clientDir        = Path.Combine(baseDir !, "client-" + Guid.NewGuid().ToString("N"));

            var config = ClientServerConfig.SharedNetworkSleeping(serverDir);

            config.DirDeleteOnShutdown = deleteOnShutdown;

            var serverPort = 43210;
            var server     = new AeronServer(serverPort, config);

            server.MessageReceived += (identity, message) =>
            {
                Console.WriteLine("SERVER: MESSAGE RECEIVED");
                server.Send((int)identity, message);
            };

            server.ClientConnected += l =>
            {
                Console.WriteLine($"SERVER: CLIENT CONNECTED {l}");
            };

            server.ClientDisconnected += l =>
            {
                Console.WriteLine($"SERVER: CLIENT DISCONNECTED {l}");
            };

            server.Start();

            var mre          = new ManualResetEventSlim(false);
            var connectedMre = new ManualResetEventSlim(false);

            var client = AeronClient.GetClientReference(clientDir,
                                                        dir =>
            {
                var config = ClientServerConfig.SharedNetworkSleeping(dir);
                config.DirDeleteOnShutdown = deleteOnShutdown;
                return(config);
            });

            client.Start();

            var timeoutMs = 10000;

            byte[] received     = null;
            var    connectionId = client.Connect("127.0.0.1",
                                                 serverPort,
                                                 () =>
            {
                Console.WriteLine("CLIENT: CONNECTED");
                connectedMre.Set();
            },
                                                 () =>
            {
                Console.WriteLine("CLIENT: DISCONNECTED");
            },
                                                 message =>
            {
                Console.WriteLine($"CLIENT: MESSAGE RECEIVED with length: {message.Length}");
                received = message.ToArray();
                mre.Set();
            },
                                                 timeoutMs);

            connectedMre.Wait();
            var sent = new byte[] { 1, 2, 3, 4, 5 };

            client.Send(connectionId, sent);

            mre.Wait(timeoutMs);
            Assert.True(sent.SequenceEqual(received));

            client.Disconnect(connectionId);

            Thread.Sleep(1000);

            client.Dispose();

            server.Dispose();
        }
Ejemplo n.º 7
0
    static int SoakMain()
    {
        var config = new ClientServerConfig();

        config.maxPacketSize         = MaxPacketSize;
        config.clientMemory          = 10 * 1024 * 1024;
        config.serverGlobalMemory    = 10 * 1024 * 1024;
        config.serverPerClientMemory = 10 * 1024 * 1024;
        config.numChannels           = 2;
        config.channel[UNRELIABLE_UNORDERED_CHANNEL].type          = ChannelType.CHANNEL_TYPE_UNRELIABLE_UNORDERED;
        config.channel[UNRELIABLE_UNORDERED_CHANNEL].maxBlockSize  = MaxSnapshotSize;
        config.channel[RELIABLE_ORDERED_CHANNEL].type              = ChannelType.CHANNEL_TYPE_RELIABLE_ORDERED;
        config.channel[RELIABLE_ORDERED_CHANNEL].maxBlockSize      = MaxBlockSize;
        config.channel[RELIABLE_ORDERED_CHANNEL].blockFragmentSize = 1024;

        var privateKey = new byte[KeyBytes];

        var time = 0.0;

        var serverAddress = new Address("127.0.0.1", shared.ServerPort);

        var server = new Server(DefaultAllocator, privateKey, serverAddress, config, shared.adapter, time);

        server.Start(1);

        var clientId = 0UL;

        random_bytes(ref clientId, 8);

        var client = new Client(DefaultAllocator, new Address("0.0.0.0"), config, shared.adapter, time);

        client.InsecureConnect(privateKey, clientId, serverAddress);

        var numMessagesSentToServer       = 0UL;
        var numMessagesSentToClient       = 0UL;
        var numMessagesReceivedFromClient = 0UL;
        var numMessagesReceivedFromServer = 0UL;

        Console.CancelKeyPress += interrupt_handler;

        var clientConnected = false;
        var serverConnected = false;

        while (!quit)
        {
            client.SendPackets();
            server.SendPackets();

            client.ReceivePackets();
            server.ReceivePackets();

            if (client.ConnectionFailed)
            {
                Console.Write("error: client connect failed!\n");
                break;
            }

            time += 0.1f;

            if (client.IsConnected)
            {
                clientConnected = true;

                {
                    var messagesToSend = random_int(0, 64);

                    for (var i = 0; i < messagesToSend; ++i)
                    {
                        if (!client.CanSendMessage(RELIABLE_ORDERED_CHANNEL))
                        {
                            break;
                        }

                        if ((BufferEx.Rand() % 25) != 0)
                        {
                            var message = (TestMessage)client.CreateMessage((int)TestMessageType.TEST_MESSAGE);
                            if (message != null)
                            {
                                message.sequence = (ushort)numMessagesSentToServer;
                                client.SendMessage(RELIABLE_ORDERED_CHANNEL, message);
                                numMessagesSentToServer++;
                            }
                        }
                        else
                        {
                            var blockMessage = (TestBlockMessage)client.CreateMessage((int)TestMessageType.TEST_BLOCK_MESSAGE);
                            if (blockMessage != null)
                            {
                                blockMessage.sequence = (ushort)numMessagesSentToServer;
                                var blockSize = 1 + ((int)numMessagesSentToServer * 33) % MaxBlockSize;
                                var blockData = client.AllocateBlock(blockSize);
                                if (blockData != null)
                                {
                                    for (var j = 0; j < blockSize; ++j)
                                    {
                                        blockData[j] = (byte)((int)numMessagesSentToServer + j);
                                    }
                                    client.AttachBlockToMessage(blockMessage, blockData, blockSize);
                                    client.SendMessage(RELIABLE_ORDERED_CHANNEL, blockMessage);
                                    numMessagesSentToServer++;
                                }
                                else
                                {
                                    client.ReleaseMessage(ref blockMessage);
                                }
                            }
                        }
                    }
                }

                var clientIndex = client.ClientIndex;

                if (server.IsClientConnected(clientIndex))
                {
                    serverConnected = true;

                    var messagesToSend = random_int(0, 64);

                    for (var i = 0; i < messagesToSend; ++i)
                    {
                        if (!server.CanSendMessage(clientIndex, RELIABLE_ORDERED_CHANNEL))
                        {
                            break;
                        }

                        if ((BufferEx.Rand() % 25) != 0)
                        {
                            var message = (TestMessage)server.CreateMessage(clientIndex, (int)TestMessageType.TEST_MESSAGE);
                            if (message != null)
                            {
                                message.sequence = (ushort)numMessagesSentToClient;
                                server.SendMessage(clientIndex, RELIABLE_ORDERED_CHANNEL, message);
                                numMessagesSentToClient++;
                            }
                        }
                        else
                        {
                            var blockMessage = (TestBlockMessage)server.CreateMessage(clientIndex, (int)TestMessageType.TEST_BLOCK_MESSAGE);
                            if (blockMessage != null)
                            {
                                blockMessage.sequence = (ushort)numMessagesSentToClient;
                                var blockSize = 1 + ((int)numMessagesSentToClient * 33) % MaxBlockSize;
                                var blockData = server.AllocateBlock(clientIndex, blockSize);
                                if (blockData != null)
                                {
                                    for (var j = 0; j < blockSize; ++j)
                                    {
                                        blockData[j] = (byte)((int)numMessagesSentToClient + j);
                                    }
                                    server.AttachBlockToMessage(clientIndex, blockMessage, blockData, blockSize);
                                    server.SendMessage(clientIndex, RELIABLE_ORDERED_CHANNEL, blockMessage);
                                    numMessagesSentToClient++;
                                }
                                else
                                {
                                    server.ReleaseMessage(clientIndex, ref blockMessage);
                                }
                            }
                        }
                    }

                    while (true)
                    {
                        var message = server.ReceiveMessage(clientIndex, RELIABLE_ORDERED_CHANNEL);
                        if (message == null)
                        {
                            break;
                        }

                        assert(message.Id == (ushort)numMessagesReceivedFromClient);

                        switch (message.Type)
                        {
                        case (int)TestMessageType.TEST_MESSAGE:
                        {
                            var testMessage = (TestMessage)message;
                            assert(testMessage.sequence == (ushort)numMessagesReceivedFromClient);
                            Console.Write($"server received message {testMessage.sequence}\n");
                            server.ReleaseMessage(clientIndex, ref message);
                            numMessagesReceivedFromClient++;
                        }
                        break;

                        case (int)TestMessageType.TEST_BLOCK_MESSAGE:
                        {
                            var blockMessage = (TestBlockMessage)message;
                            assert(blockMessage.sequence == (ushort)numMessagesReceivedFromClient);
                            var blockSize         = blockMessage.BlockSize;
                            var expectedBlockSize = 1 + ((int)numMessagesReceivedFromClient * 33) % MaxBlockSize;
                            if (blockSize != expectedBlockSize)
                            {
                                Console.Write($"error: block size mismatch. expected {expectedBlockSize}, got {blockSize}\n");
                                return(1);
                            }
                            var blockData = blockMessage.BlockData;
                            assert(blockData != null);
                            for (var i = 0; i < blockSize; ++i)
                            {
                                if (blockData[i] != (byte)((int)numMessagesReceivedFromClient + i))
                                {
                                    Console.Write($"error: block data mismatch. expected {(byte)((int)numMessagesReceivedFromClient + i)}, but blockData[{i}] = {blockData[i]}\n");
                                    return(1);
                                }
                            }
                            Console.Write($"server received message {(ushort)numMessagesReceivedFromClient}\n");
                            server.ReleaseMessage(clientIndex, ref message);
                            numMessagesReceivedFromClient++;
                        }
                        break;
                        }
                    }
                }

                while (true)
                {
                    var message = client.ReceiveMessage(RELIABLE_ORDERED_CHANNEL);

                    if (message == null)
                    {
                        break;
                    }

                    assert(message.Id == (ushort)numMessagesReceivedFromServer);

                    switch (message.Type)
                    {
                    case (int)TestMessageType.TEST_MESSAGE:
                    {
                        var testMessage = (TestMessage)message;
                        assert(testMessage.sequence == (ushort)numMessagesReceivedFromServer);
                        Console.Write($"client received message {testMessage.sequence}\n");
                        client.ReleaseMessage(ref message);
                        numMessagesReceivedFromServer++;
                    }
                    break;

                    case (int)TestMessageType.TEST_BLOCK_MESSAGE:
                    {
                        var blockMessage = (TestBlockMessage)message;
                        assert(blockMessage.sequence == (ushort)numMessagesReceivedFromServer);
                        var blockSize         = blockMessage.BlockSize;
                        var expectedBlockSize = 1 + ((int)numMessagesReceivedFromServer * 33) % MaxBlockSize;
                        if (blockSize != expectedBlockSize)
                        {
                            Console.Write($"error: block size mismatch. expected {expectedBlockSize}, got {blockSize}\n");
                            return(1);
                        }
                        var blockData = blockMessage.BlockData;
                        assert(blockData != null);
                        for (var i = 0; i < blockSize; ++i)
                        {
                            if (blockData[i] != (byte)((int)numMessagesReceivedFromServer + i))
                            {
                                Console.Write($"error: block data mismatch. expected {(byte)((int)numMessagesReceivedFromServer + i)}, but blockData[{i}] = {blockData[i]}\n");
                                return(1);
                            }
                        }
                        Console.Write($"client received message {(ushort)numMessagesReceivedFromServer}\n");
                        client.ReleaseMessage(ref message);
                        numMessagesReceivedFromServer++;
                    }
                    break;
                    }
                }

                if (clientConnected && !client.IsConnected)
                {
                    break;
                }

                if (serverConnected && server.NumConnectedClients == 0)
                {
                    break;
                }
            }

            client.AdvanceTime(time);
            server.AdvanceTime(time);
        }

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

        client.Disconnect();

        server.Stop();

        return(0);
    }