chore: added litenetlib logs

This commit is contained in:
Syntriax 2025-06-18 18:29:43 +03:00
parent 0047111244
commit 43c4eb6e4c
3 changed files with 32 additions and 10 deletions

View File

@ -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;
}

View File

@ -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<Type, Event<IConnection, object>> listeners = [];
private readonly Dictionary<string, IConnection> _connections = [];
protected ILogger? logger = null;
public IReadOnlyDictionary<string, IConnection> 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<ILogger>();
}
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);
}

View File

@ -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 =>
Listener.ConnectionRequestEvent += OnConnectionRequest;
}
private void OnConnectionRequest(ConnectionRequest request)
{
if (Manager.ConnectedPeersCount < maxConnectionCount)
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;
}