diff --git a/Engine b/Engine index e7bd924..832514b 160000 --- a/Engine +++ b/Engine @@ -1 +1 @@ -Subproject commit e7bd9244943ba489669b60956859f14d0a5a8161 +Subproject commit 832514ba7d43484c8fec3a8e61c3b1e0eb407e6b diff --git a/Shared/Network/LiteNetLib/LiteNetLibClient.cs b/Shared/Network/LiteNetLib/LiteNetLibClient.cs index f18ddde..bbece75 100644 --- a/Shared/Network/LiteNetLib/LiteNetLibClient.cs +++ b/Shared/Network/LiteNetLib/LiteNetLibClient.cs @@ -1,11 +1,18 @@ +using System.Threading; +using System.Threading.Tasks; + using LiteNetLib.Utils; +using Syntriax.Engine.Core; + namespace Syntriax.Engine.Network; public class LiteNetLibClient : LiteNetLibCommunicatorBase, INetworkCommunicatorClient { private readonly NetDataWriter netDataWriter = new(); + private CancellationTokenSource? cancellationTokenSource = null; + public INetworkCommunicatorClient Connect(string address, int port, string? password = null) { if (!UniverseObject.IsInUniverse) @@ -25,4 +32,28 @@ public class LiteNetLibClient : LiteNetLibCommunicatorBase, INetworkCommunicator return this; } + + protected override void OnEnteredUniverse(IUniverse universe) + { + cancellationTokenSource = new CancellationTokenSource(); + PollEvents(cancellationTokenSource.Token); + } + + protected override void OnExitedUniverse(IUniverse universe) + { + cancellationTokenSource?.Cancel(); + } + + /// + /// Client needs to send everything as soon as possible so + /// the events are polled a separate thread running constantly + /// + private async void PollEvents(CancellationToken cancellationToken) => await Task.Run(() => + { + while (true) + { + Manager.PollEvents(); + Thread.Sleep(1); + } + }, cancellationToken); } diff --git a/Shared/Network/LiteNetLib/LiteNetLibCommunicatorBase.cs b/Shared/Network/LiteNetLib/LiteNetLibCommunicatorBase.cs index 01928be..b05e480 100644 --- a/Shared/Network/LiteNetLib/LiteNetLibCommunicatorBase.cs +++ b/Shared/Network/LiteNetLib/LiteNetLibCommunicatorBase.cs @@ -25,19 +25,11 @@ public abstract class LiteNetLibCommunicatorBase : Behaviour, INetworkCommunicat return this; } - protected override void OnEnteredUniverse(IUniverse universe) + protected override void ExitedUniverse(IUniverseObject sender, IUniverse universe) { - universe.OnPreUpdate += PollEvents; - } - - protected override void OnExitedUniverse(IUniverse universe) - { - universe.OnPreUpdate -= PollEvents; - Stop(); } - private void PollEvents(IUniverse sender, UniverseTime engineTime) => Manager.PollEvents(); protected virtual void OnPacketArrived(T packet, NetPeer peer) where T : INetworkPacket { if (!listeners.TryGetValue(typeof(T), out List? delegates)) diff --git a/Shared/Network/LiteNetLib/LiteNetLibServer.cs b/Shared/Network/LiteNetLib/LiteNetLibServer.cs index 13740dd..6fd3ab0 100644 --- a/Shared/Network/LiteNetLib/LiteNetLibServer.cs +++ b/Shared/Network/LiteNetLib/LiteNetLibServer.cs @@ -3,6 +3,8 @@ using System.Linq; using LiteNetLib; using LiteNetLib.Utils; +using Syntriax.Engine.Core; + namespace Syntriax.Engine.Network; public class LiteNetLibServer : LiteNetLibCommunicatorBase, INetworkCommunicatorServer @@ -42,7 +44,6 @@ public class LiteNetLibServer : LiteNetLibCommunicatorBase, INetworkCommunicator return this; } - public INetworkCommunicatorServer SendToClient(string to, T packet) where T : class, new() { bool isBroadcastToAll = to.CompareTo("*") == 0; @@ -58,4 +59,8 @@ public class LiteNetLibServer : LiteNetLibCommunicatorBase, INetworkCommunicator return this; } + + protected override void OnEnteredUniverse(IUniverse universe) => universe.OnPostUpdate += PollEvents; + protected override void OnExitedUniverse(IUniverse universe) => universe.OnPostUpdate -= PollEvents; + private void PollEvents(IUniverse sender, UniverseTime engineTime) => Manager.PollEvents(); }