refactor: client & server connections separated for id tracking
This commit is contained in:
parent
9f8578af9b
commit
08f52e9b72
@ -15,6 +15,8 @@ public class LiteNetLibClient : LiteNetLibCommunicatorBase, INetworkCommunicator
|
||||
|
||||
private CancellationTokenSource? cancellationTokenSource = null;
|
||||
|
||||
protected override IConnection GetConnection(NetPeer peer) => new LiteNetLibServerConnection(peer);
|
||||
|
||||
public INetworkCommunicatorClient Connect(string address, int port, string? password = null)
|
||||
{
|
||||
if (!UniverseObject.IsInUniverse)
|
||||
|
@ -2,11 +2,11 @@ using LiteNetLib;
|
||||
|
||||
namespace Syntriax.Engine.Network;
|
||||
|
||||
public record class LiteNetLibConnection(NetPeer NetPeer) : IConnection
|
||||
public record class LiteNetLibClientConnection(NetPeer NetPeer) : IConnection
|
||||
{
|
||||
public string Id { get; } = NetPeer.Id.ToString();
|
||||
public float Ping => NetPeer.Ping * .001f;
|
||||
public float RoundTrip => NetPeer.RoundTripTime * .001f;
|
||||
|
||||
public override string ToString() => $"Client({Id})";
|
||||
public override string ToString() => $"Connection({Id})";
|
||||
}
|
@ -60,14 +60,16 @@ public abstract class LiteNetLibCommunicatorBase : Behaviour, INetworkCommunicat
|
||||
catch (Exception exception) { logger?.LogException(this, exception, force: true); }
|
||||
}
|
||||
|
||||
protected abstract IConnection GetConnection(NetPeer peer);
|
||||
private void ConnectionEstablished(NetPeer peer)
|
||||
{
|
||||
LiteNetLibConnection connection = new(peer);
|
||||
IConnection connection = GetConnection(peer);
|
||||
_connections.Add(connection.Id, connection);
|
||||
logger?.Log(this, $"Connection established with ip '{peer.Address}' and id '{connection.Id}'");
|
||||
OnConnectionEstablished.Invoke(this, connection);
|
||||
}
|
||||
|
||||
|
||||
private void ConnectionAbolished(NetPeer peer, DisconnectInfo disconnectInfo)
|
||||
{
|
||||
if (!_connections.TryGetValue(peer.Id.ToString(), out var connection))
|
||||
|
@ -25,6 +25,8 @@ public class LiteNetLibServer : LiteNetLibCommunicatorBase, INetworkCommunicator
|
||||
Listener.ConnectionRequestEvent += OnConnectionRequest;
|
||||
}
|
||||
|
||||
protected override IConnection GetConnection(NetPeer peer) => new LiteNetLibClientConnection(peer);
|
||||
|
||||
private void OnConnectionRequest(ConnectionRequest request)
|
||||
{
|
||||
logger?.Log(this, $"Connection request from ip {request.RemoteEndPoint}");
|
||||
|
12
Shared/Network/LiteNetLib/LiteNetLibServerConnection.cs
Normal file
12
Shared/Network/LiteNetLib/LiteNetLibServerConnection.cs
Normal file
@ -0,0 +1,12 @@
|
||||
using LiteNetLib;
|
||||
|
||||
namespace Syntriax.Engine.Network;
|
||||
|
||||
public record class LiteNetLibServerConnection(NetPeer NetPeer) : IConnection
|
||||
{
|
||||
public string Id { get; } = NetPeer.RemoteId.ToString();
|
||||
public float Ping => NetPeer.Ping * .001f;
|
||||
public float RoundTrip => NetPeer.RoundTripTime * .001f;
|
||||
|
||||
public override string ToString() => $"Connection({Id})";
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user