From 2dcb0470149b04c5988c844c0db01f2b566ee7bc Mon Sep 17 00:00:00 2001 From: edmand46 Date: Sun, 5 Nov 2023 22:19:55 +0300 Subject: [PATCH] chore: added logging --- .../Sources/Handler/EntityCreateHandler.cs | 2 + .../Sources/Handler/EntityEventHandler.cs | 54 ++++++++++--------- .../Sources/Handler/EntityOwnershipHandler.cs | 8 +++ .../Sources/Handler/OwnershipRoomHandler.cs | 13 +++-- .../Sources/Handler/PlayerJoinHandler.cs | 2 +- .../Sources/Handler/PlayerLeftHandler.cs | 4 ++ .../Sources/Handler/RoomDataHandler.cs | 9 ++++ .../Sources/Handler/RoomEventHandler.cs | 14 ++--- .../Sources/Handler/SnapshotHandler.cs | 5 +- Ragon.Client/Sources/RagonPlayerCache.cs | 14 ++++- 10 files changed, 86 insertions(+), 39 deletions(-) diff --git a/Ragon.Client/Sources/Handler/EntityCreateHandler.cs b/Ragon.Client/Sources/Handler/EntityCreateHandler.cs index 375e78f..4e807aa 100644 --- a/Ragon.Client/Sources/Handler/EntityCreateHandler.cs +++ b/Ragon.Client/Sources/Handler/EntityCreateHandler.cs @@ -51,6 +51,8 @@ internal class EntityCreateHandler : IHandler if (player == null) { RagonLog.Warn($"Owner {ownerId}|{player.Name} not found in players"); + + _playerCache.Dump(); return; } diff --git a/Ragon.Client/Sources/Handler/EntityEventHandler.cs b/Ragon.Client/Sources/Handler/EntityEventHandler.cs index 6c28d0c..30d0c38 100644 --- a/Ragon.Client/Sources/Handler/EntityEventHandler.cs +++ b/Ragon.Client/Sources/Handler/EntityEventHandler.cs @@ -20,35 +20,37 @@ namespace Ragon.Client; internal class EntityEventHandler : IHandler { - private readonly RagonPlayerCache _playerCache; - private readonly RagonEntityCache _entityCache; + private readonly RagonPlayerCache _playerCache; + private readonly RagonEntityCache _entityCache; - public EntityEventHandler( - RagonPlayerCache playerCache, - RagonEntityCache entityCache - ) + public EntityEventHandler( + RagonPlayerCache playerCache, + RagonEntityCache entityCache + ) + { + _playerCache = playerCache; + _entityCache = entityCache; + } + + public void Handle(RagonBuffer reader) + { + var eventCode = reader.ReadUShort(); + var peerId = reader.ReadUShort(); + var executionMode = (RagonReplicationMode)reader.ReadByte(); + var entityId = reader.ReadUShort(); + + var player = _playerCache.GetPlayerByPeer(peerId); + if (player == null) { - _playerCache = playerCache; - _entityCache = entityCache; + RagonLog.Error($"Player with peerId:{peerId} not found as owner of event with code:{eventCode}"); + + _playerCache.Dump(); + return; } - - public void Handle(RagonBuffer reader) - { - var eventCode = reader.ReadUShort(); - var peerId = reader.ReadUShort(); - var executionMode = (RagonReplicationMode)reader.ReadByte(); - var entityId = reader.ReadUShort(); - var player = _playerCache.GetPlayerByPeer(peerId); - if (player == null) - { - RagonLog.Warn($"Player not found for event {eventCode}"); - return; - } + if (player.IsLocal && executionMode == RagonReplicationMode.LocalAndServer) + return; - if (player.IsLocal && executionMode == RagonReplicationMode.LocalAndServer) - return; - - _entityCache.OnEvent(player, entityId, eventCode, reader); - } + _entityCache.OnEvent(player, entityId, eventCode, reader); + } } \ No newline at end of file diff --git a/Ragon.Client/Sources/Handler/EntityOwnershipHandler.cs b/Ragon.Client/Sources/Handler/EntityOwnershipHandler.cs index c640a4f..6af5b0d 100644 --- a/Ragon.Client/Sources/Handler/EntityOwnershipHandler.cs +++ b/Ragon.Client/Sources/Handler/EntityOwnershipHandler.cs @@ -41,6 +41,14 @@ internal class EntityOwnershipHandler: IHandler var entities = reader.ReadUShort(); var player = _playerCache.GetPlayerByPeer(newOwnerId); + if (player == null) + { + RagonLog.Error($"Player with Id:{newOwnerId} not found in cache"); + + _playerCache.Dump(); + return; + } + for (var i = 0; i < entities; i++) { var entityId = reader.ReadUShort(); diff --git a/Ragon.Client/Sources/Handler/OwnershipRoomHandler.cs b/Ragon.Client/Sources/Handler/OwnershipRoomHandler.cs index d91741b..8167d93 100644 --- a/Ragon.Client/Sources/Handler/OwnershipRoomHandler.cs +++ b/Ragon.Client/Sources/Handler/OwnershipRoomHandler.cs @@ -19,12 +19,12 @@ using Ragon.Protocol; namespace Ragon.Client; -internal class OwnershipRoomHandler: IHandler +internal class OwnershipRoomHandler : IHandler { private readonly RagonListenerList _listenerList; private readonly RagonPlayerCache _playerCache; private readonly RagonEntityCache _entityCache; - + public OwnershipRoomHandler( RagonListenerList listenerList, RagonPlayerCache playerCache, @@ -34,11 +34,18 @@ internal class OwnershipRoomHandler: IHandler _playerCache = playerCache; _entityCache = entityCache; } - + public void Handle(RagonBuffer reader) { var newOwnerId = reader.ReadUShort(); var player = _playerCache.GetPlayerByPeer(newOwnerId); + if (player == null) + { + RagonLog.Warn($"Player with peerId:{newOwnerId} not found in cache"); + + _playerCache.Dump(); + return; + } _playerCache.OnOwnershipChanged(newOwnerId); _listenerList.OnOwnershipChanged(player); diff --git a/Ragon.Client/Sources/Handler/PlayerJoinHandler.cs b/Ragon.Client/Sources/Handler/PlayerJoinHandler.cs index e270281..d46bf94 100644 --- a/Ragon.Client/Sources/Handler/PlayerJoinHandler.cs +++ b/Ragon.Client/Sources/Handler/PlayerJoinHandler.cs @@ -45,6 +45,6 @@ internal class PlayerJoinHandler : IHandler if (player != null) _listenerList.OnPlayerJoined(player); else - RagonLog.Trace($"[Joined] {playerId}"); + RagonLog.Warn($"Player with Id:{playerId} not found in cache"); } } \ No newline at end of file diff --git a/Ragon.Client/Sources/Handler/PlayerLeftHandler.cs b/Ragon.Client/Sources/Handler/PlayerLeftHandler.cs index 4d1988f..5cbcc42 100644 --- a/Ragon.Client/Sources/Handler/PlayerLeftHandler.cs +++ b/Ragon.Client/Sources/Handler/PlayerLeftHandler.cs @@ -57,5 +57,9 @@ internal class PlayerLeftHandler : IHandler foreach (var id in toDeleteIds) _entityCache.OnDestroy(id, emptyPayload); } + else + { + RagonLog.Warn($"Player with Id:{playerId} not found in cache"); + } } } \ No newline at end of file diff --git a/Ragon.Client/Sources/Handler/RoomDataHandler.cs b/Ragon.Client/Sources/Handler/RoomDataHandler.cs index b7b78fc..21eb0c7 100644 --- a/Ragon.Client/Sources/Handler/RoomDataHandler.cs +++ b/Ragon.Client/Sources/Handler/RoomDataHandler.cs @@ -37,6 +37,15 @@ internal class RoomDataHandler: IHandler var rawData = reader.RawData; var peerId = (ushort)(rawData[1] + (rawData[2] << 8)); var player = _playerCache.GetPlayerByPeer(peerId); + + if (player == null) + { + RagonLog.Error($"Player with peerId:{peerId} not found"); + + _playerCache.Dump(); + return; + } + var headerSize = 3; var payload = new byte[rawData.Length - headerSize]; diff --git a/Ragon.Client/Sources/Handler/RoomEventHandler.cs b/Ragon.Client/Sources/Handler/RoomEventHandler.cs index e1528ed..ff3b1a6 100644 --- a/Ragon.Client/Sources/Handler/RoomEventHandler.cs +++ b/Ragon.Client/Sources/Handler/RoomEventHandler.cs @@ -18,11 +18,11 @@ using Ragon.Protocol; namespace Ragon.Client; -public class RoomEventHandler: IHandler +public class RoomEventHandler : IHandler { private readonly RagonClient _client; private readonly RagonPlayerCache _playerCache; - + public RoomEventHandler( RagonClient client, RagonPlayerCache playerCache @@ -31,23 +31,25 @@ public class RoomEventHandler: IHandler _client = client; _playerCache = playerCache; } - + public void Handle(RagonBuffer buffer) { var eventCode = buffer.ReadUShort(); var peerId = buffer.ReadUShort(); var executionMode = (RagonReplicationMode)buffer.ReadByte(); - + var player = _playerCache.GetPlayerByPeer(peerId); if (player == null) { - RagonLog.Warn($"Player not found for event {eventCode}"); + RagonLog.Error($"Player with peerId:{peerId} not found as owner of event with code:{eventCode}"); + + _playerCache.Dump(); return; } if (player.IsLocal && executionMode == RagonReplicationMode.LocalAndServer) return; - _client.Room.Event(eventCode, player, buffer); + _client.Room.Event(eventCode, player, buffer); } } \ No newline at end of file diff --git a/Ragon.Client/Sources/Handler/SnapshotHandler.cs b/Ragon.Client/Sources/Handler/SnapshotHandler.cs index 789aeca..3e04264 100644 --- a/Ragon.Client/Sources/Handler/SnapshotHandler.cs +++ b/Ragon.Client/Sources/Handler/SnapshotHandler.cs @@ -71,6 +71,8 @@ internal class SnapshotHandler : IHandler if (player == null) { RagonLog.Error($"Player not found with peerId: ${ownerPeerId}"); + + _playerCache.Dump(); return; } @@ -105,6 +107,8 @@ internal class SnapshotHandler : IHandler if (player == null) { RagonLog.Error($"Player not found with peerId: ${ownerPeerId}"); + + _playerCache.Dump(); return; } @@ -112,7 +116,6 @@ internal class SnapshotHandler : IHandler var entity = _entityCache.TryGetEntity(0, entityType, staticId, entityId, hasAuthority, out _); entity.Prepare(_client, entityId, entityType, hasAuthority, player, RagonPayload.Empty); - entity.Read(buffer); entity.Attach(); } diff --git a/Ragon.Client/Sources/RagonPlayerCache.cs b/Ragon.Client/Sources/RagonPlayerCache.cs index 516e68d..d1cdcbd 100644 --- a/Ragon.Client/Sources/RagonPlayerCache.cs +++ b/Ragon.Client/Sources/RagonPlayerCache.cs @@ -31,7 +31,7 @@ public sealed class RagonPlayerCache { if (_playersById.TryGetValue(playerId, out var player)) return player; - + return null; } @@ -39,7 +39,7 @@ public sealed class RagonPlayerCache { if (_playersByConnection.TryGetValue(peerId, out var player)) return player; - + return null; } @@ -104,4 +104,14 @@ public sealed class RagonPlayerCache _playersByConnection.Clear(); _playersById.Clear(); } + + public void Dump() + { + RagonLog.Trace("Players: "); + RagonLog.Trace("[Connection] [ID] [Name]"); + foreach (var player in _players) + { + RagonLog.Trace($"[{player.PeerId}] {player.Id} {player.Name}"); + } + } } \ No newline at end of file