From a76905f31e843de4a5e596d5d2de8af019b5bc53 Mon Sep 17 00:00:00 2001 From: Syntriax Date: Sun, 6 Jul 2025 20:08:44 +0300 Subject: [PATCH] fix: local id tracking added for issues caused by commit 08f52e9b72a09eed7db46fa9a97b7fee862b19fc --- .../Network/LiteNetLib/LiteNetLibCommunicatorBase.cs | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/Shared/Network/LiteNetLib/LiteNetLibCommunicatorBase.cs b/Shared/Network/LiteNetLib/LiteNetLibCommunicatorBase.cs index 88d1f30..03a2b39 100644 --- a/Shared/Network/LiteNetLib/LiteNetLibCommunicatorBase.cs +++ b/Shared/Network/LiteNetLib/LiteNetLibCommunicatorBase.cs @@ -18,6 +18,8 @@ public abstract class LiteNetLibCommunicatorBase : Behaviour, INetworkCommunicat private readonly Dictionary> listeners = []; private readonly Dictionary _connections = []; + private readonly Dictionary localPeerIdToConnectionDictionary = []; + protected ILogger? logger = null; public IReadOnlyDictionary Connections => _connections; @@ -51,7 +53,7 @@ public abstract class LiteNetLibCommunicatorBase : Behaviour, INetworkCommunicat if (!listeners.TryGetValue(typeof(T), out Event? @event)) return; - @event.Invoke(Connections[peer.Id.ToString()], packet); + @event.Invoke(localPeerIdToConnectionDictionary[peer.Id], packet); } private void NetworkReceiveEvent(NetPeer peer, NetPacketReader reader, byte channel, DeliveryMethod deliveryMethod) @@ -64,7 +66,10 @@ public abstract class LiteNetLibCommunicatorBase : Behaviour, INetworkCommunicat private void ConnectionEstablished(NetPeer peer) { IConnection connection = GetConnection(peer); + + localPeerIdToConnectionDictionary.Add(peer.Id, connection); _connections.Add(connection.Id, connection); + logger?.Log(this, $"Connection established with ip '{peer.Address}' and id '{connection.Id}'"); OnConnectionEstablished.Invoke(this, connection); } @@ -72,10 +77,12 @@ public abstract class LiteNetLibCommunicatorBase : Behaviour, INetworkCommunicat private void ConnectionAbolished(NetPeer peer, DisconnectInfo disconnectInfo) { - if (!_connections.TryGetValue(peer.Id.ToString(), out var connection)) + if (!localPeerIdToConnectionDictionary.TryGetValue(peer.Id, out var connection)) return; + localPeerIdToConnectionDictionary.Remove(peer.Id); _connections.Remove(connection.Id); + logger?.Log(this, $"Connection abolished with ip '{peer.Address}' and id '{connection.Id}'"); OnConnectionAbolished.Invoke(this, connection); }