diff --git a/Shared/Network/LiteNetLib/LiteNetLibClient.cs b/Shared/Network/LiteNetLib/LiteNetLibClient.cs index fd3f57d..5b1a00a 100644 --- a/Shared/Network/LiteNetLib/LiteNetLibClient.cs +++ b/Shared/Network/LiteNetLib/LiteNetLibClient.cs @@ -4,6 +4,7 @@ using System.Threading.Tasks; using LiteNetLib.Utils; using Syntriax.Engine.Core; +using Syntriax.Engine.Core.Debug; namespace Syntriax.Engine.Network; @@ -18,8 +19,11 @@ public class LiteNetLibClient : LiteNetLibCommunicatorBase, INetworkCommunicator if (!UniverseObject.IsInUniverse) throw new($"{nameof(LiteNetLibClient)} must be in an universe to connect"); + password ??= string.Empty; + + logger?.Log(this, $"Connecting to server at '{address}:{port}' with password '{password}'"); Manager.Start(); - Manager.Connect(address, port, password ?? string.Empty); + Manager.Connect(address, port, password); return this; } diff --git a/Shared/Network/LiteNetLib/LiteNetLibCommunicatorBase.cs b/Shared/Network/LiteNetLib/LiteNetLibCommunicatorBase.cs index 789a5b6..b2d1925 100644 --- a/Shared/Network/LiteNetLib/LiteNetLibCommunicatorBase.cs +++ b/Shared/Network/LiteNetLib/LiteNetLibCommunicatorBase.cs @@ -7,6 +7,7 @@ using LiteNetLib; using LiteNetLib.Utils; using Syntriax.Engine.Core; +using Syntriax.Engine.Core.Debug; namespace Syntriax.Engine.Network; @@ -17,6 +18,8 @@ public abstract class LiteNetLibCommunicatorBase : Behaviour, INetworkCommunicat private readonly Dictionary> listeners = []; private readonly Dictionary _connections = []; + protected ILogger? logger = null; + public IReadOnlyDictionary Connections => _connections; public EventBasedNetListener Listener { get; private set; } = null!; public NetManager Manager { get; private set; } = null!; @@ -30,9 +33,16 @@ public abstract class LiteNetLibCommunicatorBase : Behaviour, INetworkCommunicat return this; } + protected override void OnEnteredUniverse(IUniverse universe) + { + base.OnEnteredUniverse(universe); + logger = universe.FindBehaviour(); + } + protected override void OnExitedUniverse(IUniverse universe) { base.OnExitedUniverse(universe); + logger = null; Stop(); } @@ -47,13 +57,14 @@ public abstract class LiteNetLibCommunicatorBase : Behaviour, INetworkCommunicat private void NetworkReceiveEvent(NetPeer peer, NetPacketReader reader, byte channel, DeliveryMethod deliveryMethod) { try { netPacketProcessor.ReadAllPackets(reader, peer); } - catch { } + catch (Exception exception) { logger?.LogException(this, exception, force: true); } } private void ConnectionEstablished(NetPeer peer) { LiteNetLibConnection connection = new(peer); _connections.Add(connection.Id, connection); + logger?.Log(this, $"Connection established with ip '{peer.Address}' and id '{connection.Id}'"); OnConnectionEstablished.Invoke(this, connection); } @@ -63,6 +74,7 @@ public abstract class LiteNetLibCommunicatorBase : Behaviour, INetworkCommunicat return; _connections.Remove(connection.Id); + logger?.Log(this, $"Connection abolished with ip '{peer.Address}' and id '{connection.Id}'"); OnConnectionAbolished.Invoke(this, connection); } diff --git a/Shared/Network/LiteNetLib/LiteNetLibServer.cs b/Shared/Network/LiteNetLib/LiteNetLibServer.cs index 32b348a..b4ea3c7 100644 --- a/Shared/Network/LiteNetLib/LiteNetLibServer.cs +++ b/Shared/Network/LiteNetLib/LiteNetLibServer.cs @@ -4,6 +4,7 @@ using LiteNetLib; using LiteNetLib.Utils; using Syntriax.Engine.Core; +using Syntriax.Engine.Core.Debug; namespace Syntriax.Engine.Network; @@ -21,13 +22,17 @@ public class LiteNetLibServer : LiteNetLibCommunicatorBase, INetworkCommunicator MaxConnectionCount = maxConnectionCount; Port = port; - Listener.ConnectionRequestEvent += request => - { - if (Manager.ConnectedPeersCount < maxConnectionCount) - request.AcceptIfKey(Password); - else - request.Reject(); - }; + Listener.ConnectionRequestEvent += OnConnectionRequest; + } + + private void OnConnectionRequest(ConnectionRequest request) + { + logger?.Log(this, $"Connection request from ip {request.RemoteEndPoint}"); + logger?.Log(this, $"Current connection count: {Connections.Count}"); + if (Manager.ConnectedPeersCount < MaxConnectionCount) + request.AcceptIfKey(Password); + else + request.Reject(); } public INetworkCommunicatorServer Start(int port, int maxConnectionCount, string? password = null) @@ -39,7 +44,8 @@ public class LiteNetLibServer : LiteNetLibCommunicatorBase, INetworkCommunicator MaxConnectionCount = maxConnectionCount; Port = port; - Manager.Start(8888); + logger?.Log(this, $"Starting server on port '{port}' with password '{Password}' and max connection count '{maxConnectionCount}'"); + logger?.Log(this, $"Server status: {(Manager.Start(port) ? "Active" : "Failed")}"); return this; }