refactor: client is now running on a different thread
This commit is contained in:
		
							
								
								
									
										2
									
								
								Engine
									
									
									
									
									
								
							
							
								
								
								
								
								
							
						
						
									
										2
									
								
								Engine
									
									
									
									
									
								
							 Submodule Engine updated: e7bd924494...832514ba7d
									
								
							@@ -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();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /// <summary>
 | 
			
		||||
    /// Client needs to send everything as soon as possible so 
 | 
			
		||||
    /// the events are polled a separate thread running constantly 
 | 
			
		||||
    /// </summary>
 | 
			
		||||
    private async void PollEvents(CancellationToken cancellationToken) => await Task.Run(() =>
 | 
			
		||||
    {
 | 
			
		||||
        while (true)
 | 
			
		||||
        {
 | 
			
		||||
            Manager.PollEvents();
 | 
			
		||||
            Thread.Sleep(1);
 | 
			
		||||
        }
 | 
			
		||||
    }, cancellationToken);
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -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>(T packet, NetPeer peer) where T : INetworkPacket
 | 
			
		||||
    {
 | 
			
		||||
        if (!listeners.TryGetValue(typeof(T), out List<Delegate>? delegates))
 | 
			
		||||
 
 | 
			
		||||
@@ -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<T>(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();
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user