Example #1
0
        private static void Main()
        {
            var builder = new ClientConnectionBuilder()
                          .ConnectTo(new UdpEndPoint("127.0.0.1", 45685))
                          .ConfigureChannels(builder => builder.AddChannel <ReliableOrderedChannel>(0))
                          .UseLogger(PegiLogging.Configure("Client"))
            ;

            using (var connection = builder.Build())
                using (var resetEvent = new ManualResetEventSlim(false))
                {
                    Console.CancelKeyPress += (_, e) =>
                    {
                        e.Cancel = true;
                        Log.Information("Ctrl+C");
                        resetEvent.Set();
                    };
                    Thread.Sleep(200);

                    connection.MessageReceived += (_, data) =>
                    {
                        var message = MessagePackSerializer.Deserialize <DebugMessage>(data);
                        Log.Information("[{ConnectionEndPoint}] Message: {Message}", connection.RemoteEndPoint, message);
                    };

                    Log.Information("Connecting...");
                    connection.Connect();
                    Log.Information("Connected");

                    var updateTime = 60;
                    var sendTime   = 4;
                    var time       = Timestamp.GetCurrent();
                    var i          = 0;
                    while (!resetEvent.IsSet && connection.State == ConnectionState.Connected)
                    {
                        try
                        {
                            connection.Update();
                        }
                        catch (Exception e)
                        {
                            Log.Error(e, "Update error");
                            break;
                        }

                        var now   = Timestamp.GetCurrent();
                        var delta = 1000 / sendTime;
                        if (now - time > delta)
                        {
                            time += delta;

                            for (var n = 0; n < 1; n++)
                            {
                                var message = new DebugMessage
                                {
                                    Id   = i,
                                    Text = $"Zpráva {i}",
                                };
                                var messageBytes = MessagePackSerializer.Serialize(message);

                                connection.SendMessage(messageBytes);

                                i++;
                                if (i == 100_000)
                                {
                                    break;
                                }
                            }
                        }

                        Thread.Sleep(1000 / updateTime);
                    }

                    Log.Information("Disconnecting...");
                }
            Log.Information("Disconnected");

            Thread.Sleep(1000);
        }
Example #2
0
        private static void Main()
        {
            var builder = new ConnectionListenerBuilder()
                          .ListenOn(new UdpEndPoint("127.0.0.1", 45685))
                          .ConfigureChannels(builder => builder.AddChannel <ReliableOrderedChannel>(0))
                          .UseLogger(PegiLogging.Configure("Server"))
            ;

            using (var listener = builder.Build())
                using (var resetEvent = new ManualResetEventSlim(false))
                {
                    Console.CancelKeyPress += (_, e) =>
                    {
                        e.Cancel = true;
                        Log.Information("Ctrl+C");
                        resetEvent.Set();
                    };

                    var connections = new ConcurrentDictionary <UdpEndPoint, Connection>();

                    listener.NewConnection += (_, connection) =>
                    {
                        Log.Information("[{ConnectionEndPoint}] New connection", connection.RemoteEndPoint);
                        connection.Disconnected += (_) =>
                        {
                            Log.Information("[{ConnectionEndPoint}] Disconnected", connection.RemoteEndPoint);
                            connections.TryRemove(connection.RemoteEndPoint, out _);
                        };
                        connection.MessageReceived += (_, data) =>
                        {
                            var message = MessagePackSerializer.Deserialize <DebugMessage>(data);
                            Log.Information("[{ConnectionEndPoint}] Message: {Message}", connection.RemoteEndPoint, message);
                            connection.SendMessage(data);
                        };
                        connections.TryAdd(connection.RemoteEndPoint, connection);
                    };
                    listener.Run();

                    var updateTime = 60;
                    while (!resetEvent.IsSet)
                    {
                        foreach (var connection in connections.Values)
                        {
                            try
                            {
                                connection.Update();
                            }
                            catch (Exception e)
                            {
                                Log.Error(e, "Update error");
                                connection.Dispose();
                            }
                        }
                        Thread.Sleep(1000 / updateTime);
                    }

                    Log.Information("Disconnecting...");
                }
            Log.Information("Disconnected");

            Thread.Sleep(1000);
        }