Пример #1
0
        private static void StartLiteNetLibServer()
        {
            var logger = System.Console.Out;
            var server = new LiteNetLibServer(logger);

            var timer = new Timer(state => server.PollEvents(), null, 1000, 10);
        }
Пример #2
0
        static void Main(string[] args)
        {
            int roomSize      = 2;
            int waitInSeconds = 5;

            var sw = new Stopwatch();

            sw.Start();
            Console.Write($"Enter room size (defaults to {roomSize} after {waitInSeconds} seconds): ");
            while (!Console.KeyAvailable && sw.Elapsed.Seconds < waitInSeconds)
            {
                Thread.Sleep(1);
            }
            sw.Stop();

            if (Console.KeyAvailable)
            {
                roomSize = Console.ReadKey().KeyChar - 48;
            }
            Console.Write(Environment.NewLine);

            var server = new LiteNetLibServer();

            var room = new Room(server, roomSize);

            room.Open(9050);

            while (!Console.KeyAvailable)
            {
                server.PollEvents();
                Thread.Sleep(1);
            }
        }
Пример #3
0
        private void Network_Tick(object sender, EventArgs e)
        {
            server.PollEvents();

            var message = GetMessage();

            var messageSize = Encoding.UTF8.GetByteCount(message);

            label6.Text = messageSize.ToString();

            if (IsReliable)
            {
                server.SendReliable(message);
            }
            else
            {
                server.SendUnreliable(message);
            }
        }
Пример #4
0
        public void StartServer()
        {
            CanStartServer = false;

            var room = new Room(_server, RoomSize);

            room.Starting += (sender, args) =>
            {
                _currentTick = 0;
                _tickDt      = 1000f / args.SimulationSpeed;

                SimulationRunning = true;

                _tickTimer.Start();

                Execute.OnUIThread(() =>
                {
                    foreach (var actorId in args.ActorIds)
                    {
                        var lineSeries = new LineSeries
                        {
                            Values = new ChartValues <TickModel>(),
                            Title  = "Actor " + actorId,
                        };

                        _tickDataPerClient.Add(actorId, lineSeries.Values);
                        SeriesCollection.Add(lineSeries);
                    }
                });
            };

            room.InputReceived += (sender, args) =>
            {
                Execute.OnUIThread(() =>
                {
                    _tickDataPerClient[args.ActorId].Add(new TickModel(DateTime.Now, _currentTick - args.Tick));


                    if (_tickDataPerClient[args.ActorId].Count > 250)
                    {
                        _tickDataPerClient[args.ActorId].RemoveAt(0);
                    }
                });
            };

            room.Open(9050);


            //A Task to poll for server-events. Will also increase the server's tick-counter as long as the simulation is running
            Task.Factory.StartNew(() =>
            {
                float accumulatedTime = 0f;

                while (true)
                {
                    _server.PollEvents();

                    if (SimulationRunning)
                    {
                        accumulatedTime += _tickTimer.Tick();

                        if (accumulatedTime >= _tickDt)
                        {
                            _currentTick++;

                            accumulatedTime -= _tickDt;
                        }
                    }

                    Thread.Sleep(1);
                }
            }, TaskCreationOptions.LongRunning);
        }