diff --git a/Engine.Core/Debug/ILogger.cs b/Engine.Core/Debug/ILogger.cs index 664ec45..54cf985 100644 --- a/Engine.Core/Debug/ILogger.cs +++ b/Engine.Core/Debug/ILogger.cs @@ -11,6 +11,7 @@ public interface ILogger enum Level { Trace, + Debug, Info, Warning, Error, diff --git a/Engine.Core/Debug/LoggerExtensions.cs b/Engine.Core/Debug/LoggerExtensions.cs index d4a40f3..966ffe9 100644 --- a/Engine.Core/Debug/LoggerExtensions.cs +++ b/Engine.Core/Debug/LoggerExtensions.cs @@ -5,13 +5,16 @@ namespace Engine.Core.Debug; public static class LoggerExtensions { - public static void Log(this ILogger logger, T caller, string message, ILogger.Level level = ILogger.Level.Info, bool force = false) + public static void Log(this ILogger logger, T caller, string message, ILogger.Level level, bool force = false) { - string body = $"{caller?.GetType().Name ?? typeof(T).Name}: {message}"; + string body = $"[{caller?.GetType().Name ?? typeof(T).Name}] {message}"; logger.Log(body, level, force); } - public static void LogWarning(this ILogger logger, T caller, string message, bool force = false) + public static void LogDebug(this ILogger logger, T caller, string message, bool force = false) + => Log(logger, caller, message, ILogger.Level.Debug, force); + + public static void LogInfo(this ILogger logger, T caller, string message, bool force = false) => Log(logger, caller, message, ILogger.Level.Info, force); public static void LogError(this ILogger logger, T caller, string message, bool force = false) diff --git a/Engine.Integration/Engine.Integration.LiteNetLib/LiteNetLibClient.cs b/Engine.Integration/Engine.Integration.LiteNetLib/LiteNetLibClient.cs index abac30e..b7458f8 100644 --- a/Engine.Integration/Engine.Integration.LiteNetLib/LiteNetLibClient.cs +++ b/Engine.Integration/Engine.Integration.LiteNetLib/LiteNetLibClient.cs @@ -8,7 +8,7 @@ using Engine.Core.Debug; namespace Engine.Systems.Network; -public class LiteNetLibClient : LiteNetLibCommunicatorBase, INetworkCommunicatorClient +public class LiteNetLibClient : LiteNetLibCommunicatorBase, INetworkCommunicatorClient, IEnterUniverse { private readonly NetDataWriter netDataWriter = new(); private readonly NetDataWriter netDataWriterEncrypted = new(); @@ -31,8 +31,8 @@ public class LiteNetLibClient : LiteNetLibCommunicatorBase, INetworkCommunicator System.Net.IPAddress[] addresses = System.Net.Dns.GetHostAddresses(address); string connectionAddress = addresses.FirstOrDefault(a => a.AddressFamily == AddressFamily.InterNetwork, addresses[0]).ToString(); - logger?.Log(this, $"Connecting to server at '{address}:{port}' with password '{password}'"); - logger?.Log(this, $"Resolved address for {address}: {connectionAddress}"); + ILogger.Shared.LogInfo(this, $"Connecting to server at '{address}:{port}' with password '{password}'"); + ILogger.Shared.LogInfo(this, $"Resolved address for {address}: {connectionAddress}"); Manager.Start(); Manager.Connect(connectionAddress, port, password); @@ -70,10 +70,8 @@ public class LiteNetLibClient : LiteNetLibCommunicatorBase, INetworkCommunicator return this; } - public override void EnterUniverse(IUniverse universe) + public void EnterUniverse(IUniverse universe) { - base.EnterUniverse(universe); - cancellationTokenSource = new CancellationTokenSource(); PollEvents(cancellationTokenSource.Token); } @@ -94,7 +92,7 @@ public class LiteNetLibClient : LiteNetLibCommunicatorBase, INetworkCommunicator { Thread.Sleep(1); try { Manager.PollEvents(); } - catch (Exception e) { logger?.LogException(this, e, force: true); } + catch (Exception e) { ILogger.Shared.LogException(this, e, force: true); } } }, cancellationToken); } diff --git a/Engine.Integration/Engine.Integration.LiteNetLib/LiteNetLibCommunicatorBase.cs b/Engine.Integration/Engine.Integration.LiteNetLib/LiteNetLibCommunicatorBase.cs index 78eb3b3..2a6f64f 100644 --- a/Engine.Integration/Engine.Integration.LiteNetLib/LiteNetLibCommunicatorBase.cs +++ b/Engine.Integration/Engine.Integration.LiteNetLib/LiteNetLibCommunicatorBase.cs @@ -9,7 +9,7 @@ using LiteNetLib.Utils; namespace Engine.Systems.Network; -public abstract class LiteNetLibCommunicatorBase : Behaviour, IEnterUniverse, IExitUniverse, INetworkCommunicator +public abstract class LiteNetLibCommunicatorBase : Behaviour, IExitUniverse, INetworkCommunicator { protected readonly NetPacketProcessor netPacketProcessor = new(); protected readonly PacketCryptor cryptor = new("At4ywW9PGoWH3g==", "NmpMFTvd3pvUbA=="); // TODO implement public key exchange @@ -19,8 +19,6 @@ public abstract class LiteNetLibCommunicatorBase : Behaviour, IEnterUniverse, IE private readonly Dictionary localPeerIdToConnectionDictionary = []; - protected ILogger? logger = null; - public IReadOnlyDictionary Connections => _connections; public EventBasedNetListener Listener { get; private set; } = null!; public NetManager Manager { get; private set; } = null!; @@ -34,16 +32,7 @@ public abstract class LiteNetLibCommunicatorBase : Behaviour, IEnterUniverse, IE return this; } - public virtual void EnterUniverse(IUniverse universe) - { - logger = universe.FindBehaviour(); - } - - public virtual void ExitUniverse(IUniverse universe) - { - logger = null; - Stop(); - } + public virtual void ExitUniverse(IUniverse universe) => Stop(); protected virtual void OnPacketArrived(T packet, NetPeer peer) where T : INetworkPacket { @@ -69,7 +58,7 @@ public abstract class LiteNetLibCommunicatorBase : Behaviour, IEnterUniverse, IE netPacketProcessor.ReadAllPackets(reader, peer); } - catch (Exception exception) { logger?.LogException(this, exception, force: true); } + catch (Exception exception) { ILogger.Shared.LogException(this, exception, force: true); } } protected abstract IConnection GetConnection(NetPeer peer); @@ -80,7 +69,7 @@ public abstract class LiteNetLibCommunicatorBase : Behaviour, IEnterUniverse, IE localPeerIdToConnectionDictionary.Add(peer.Id, connection); _connections.Add(connection.Id, connection); - logger?.Log(this, $"Connection established with ip '{peer.Address}' and id '{connection.Id}'"); + ILogger.Shared.LogInfo(this, $"Connection established with ip '{peer.Address}' and id '{connection.Id}'"); OnConnectionEstablished.Invoke(this, connection); } @@ -92,7 +81,7 @@ public abstract class LiteNetLibCommunicatorBase : Behaviour, IEnterUniverse, IE localPeerIdToConnectionDictionary.Remove(peer.Id); _connections.Remove(connection.Id); - logger?.Log(this, $"Connection abolished with ip '{peer.Address}' and id '{connection.Id}'"); + ILogger.Shared.LogInfo(this, $"Connection abolished with ip '{peer.Address}' and id '{connection.Id}'"); OnConnectionAbolished.Invoke(this, connection); } diff --git a/Engine.Integration/Engine.Integration.LiteNetLib/LiteNetLibServer.cs b/Engine.Integration/Engine.Integration.LiteNetLib/LiteNetLibServer.cs index 3b0f9cc..4be295c 100644 --- a/Engine.Integration/Engine.Integration.LiteNetLib/LiteNetLibServer.cs +++ b/Engine.Integration/Engine.Integration.LiteNetLib/LiteNetLibServer.cs @@ -28,8 +28,8 @@ public class LiteNetLibServer : LiteNetLibCommunicatorBase, INetworkCommunicator private void OnConnectionRequest(ConnectionRequest request) { - logger?.Log(this, $"Connection request from ip {request.RemoteEndPoint}"); - logger?.Log(this, $"Current connection count: {Connections.Count}"); + ILogger.Shared.LogInfo(this, $"Connection request from ip {request.RemoteEndPoint}"); + ILogger.Shared.LogInfo(this, $"Current connection count: {Connections.Count}"); if (Manager.ConnectedPeersCount < MaxConnectionCount) request.AcceptIfKey(Password); else @@ -45,8 +45,8 @@ public class LiteNetLibServer : LiteNetLibCommunicatorBase, INetworkCommunicator MaxConnectionCount = maxConnectionCount; Port = port; - 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")}"); + ILogger.Shared.LogInfo(this, $"Starting server on port '{port}' with password '{Password}' and max connection count '{maxConnectionCount}'"); + ILogger.Shared.LogInfo(this, $"Server status: {(Manager.Start(port) ? "Active" : "Failed")}"); return this; }