Esempio n. 1
0
        public override void Update(GameTime gameTime)
        {
            // send data to clients based on the difference between this snapshot and the previous one
            ServerSnapshot snapshot = ServerSnapshot.Snapshot(world);

            foreach (var playerKV in snapshot.playersData)
            {
                PlayerData d;
                if (previousSnapshot.playersData.TryGetValue(playerKV.Key, out d))
                {
                    // check if the player data changed. if yes, notify all the clients
                    if (d != playerKV.Value)
                    {
                        SendMessage(MessageContentType.PlayerUpdate, playerKV.Key, playerKV.Value);
                    }
                }
            }
            for (int i = 0; i < snapshot.entitiesData.Count; i++)
            {
                if (previousSnapshot.entitiesData[i] != snapshot.entitiesData[i])
                {
                    SendMessage(MessageContentType.EntityData, -1, snapshot.entitiesData[i]);
                }
            }
            // read if theres new data in each of the clients sockets
            foreach (Connection client in clients)
            {
                if (client.Connected)
                {
                    client.Update();
                }
            }
        }
Esempio n. 2
0
        private async Task <ServerSnapshot> GetServerSnapshotAsync(Server server)
        {
            var failRetries    = 0;
            var retry          = true;
            var serverSnapshot = new ServerSnapshot(server)
            {
                Id          = Guid.NewGuid(),
                Timestamp   = DateTime.UtcNow,
                PlayerCount = -1,
            };

            while (retry)
            {
                try
                {
                    var players = await SteamGameServer.PlayerQuery.QueryPlayersAsync(new IPEndPoint(IPAddress.Parse(server.Address), server.Port), WAIT_MAX);

                    serverSnapshot.PlayerCount     = players.Count();
                    serverSnapshot.PlayerSnapshots = players.Select(x => new PlayerSnapshot(x, serverSnapshot)).ToArray();
                    serverSnapshot.Success         = true;

                    _log.LogInformation($"Pull ({server.Name}){server.Address}:{server.Port} - SUCCEED ({serverSnapshot.Timestamp})");

                    break;
                }
                catch (Exception ex)
                {
                    _log.LogError(ex.Message);
                    _log.LogInformation($"Pull ({server.Name}){server.Address}:{server.Port}) - FAIL ({serverSnapshot.Timestamp})");

                    failRetries++;

                    if (failRetries >= FAIL_MAX)
                    {
                        break;
                    }
                }
            }

            return(serverSnapshot);
        }
Esempio n. 3
0
 public override void SnapShot()
 {
     previousSnapshot = ServerSnapshot.Snapshot(world);
 }