refactor: network implementations switched to universe objects
This commit is contained in:
@@ -15,7 +15,7 @@ public class LiteNetLibClient : LiteNetLibCommunicatorBase, INetworkCommunicator
|
||||
|
||||
public INetworkCommunicatorClient Connect(string address, int port, string? password = null)
|
||||
{
|
||||
if (!UniverseObject.IsInUniverse)
|
||||
if (!IsInUniverse)
|
||||
throw new($"{nameof(LiteNetLibClient)} must be in an universe to connect");
|
||||
|
||||
Manager.Start();
|
||||
@@ -33,14 +33,17 @@ public class LiteNetLibClient : LiteNetLibCommunicatorBase, INetworkCommunicator
|
||||
return this;
|
||||
}
|
||||
|
||||
protected override void OnEnteredUniverse(IUniverse universe)
|
||||
protected override void OnEnteringUniverse(IUniverse universe)
|
||||
{
|
||||
base.OnEnteringUniverse(universe);
|
||||
|
||||
cancellationTokenSource = new CancellationTokenSource();
|
||||
PollEvents(cancellationTokenSource.Token);
|
||||
}
|
||||
|
||||
protected override void OnExitedUniverse(IUniverse universe)
|
||||
protected override void OnExitingUniverse(IUniverse universe)
|
||||
{
|
||||
base.OnExitingUniverse(universe);
|
||||
cancellationTokenSource?.Cancel();
|
||||
}
|
||||
|
||||
|
@@ -10,7 +10,7 @@ using Syntriax.Engine.Core;
|
||||
|
||||
namespace Syntriax.Engine.Network;
|
||||
|
||||
public abstract class LiteNetLibCommunicatorBase : Behaviour, INetworkCommunicator
|
||||
public abstract class LiteNetLibCommunicatorBase : UniverseObject, INetworkCommunicator
|
||||
{
|
||||
protected readonly NetPacketProcessor netPacketProcessor = new();
|
||||
|
||||
@@ -25,8 +25,9 @@ public abstract class LiteNetLibCommunicatorBase : Behaviour, INetworkCommunicat
|
||||
return this;
|
||||
}
|
||||
|
||||
protected override void ExitedUniverse(IUniverseObject sender, IUniverse universe)
|
||||
protected override void OnExitingUniverse(IUniverse universe)
|
||||
{
|
||||
base.OnExitingUniverse(universe);
|
||||
Stop();
|
||||
}
|
||||
|
||||
|
@@ -32,7 +32,7 @@ public class LiteNetLibServer : LiteNetLibCommunicatorBase, INetworkCommunicator
|
||||
|
||||
public INetworkCommunicatorServer Start(int port, int maxConnectionCount, string? password = null)
|
||||
{
|
||||
if (!UniverseObject.IsInUniverse)
|
||||
if (!IsInUniverse)
|
||||
throw new($"{nameof(LiteNetLibServer)} must be in an universe to start");
|
||||
|
||||
Password = password ?? string.Empty;
|
||||
@@ -60,7 +60,17 @@ 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();
|
||||
|
||||
protected override void OnEnteringUniverse(IUniverse universe)
|
||||
{
|
||||
base.OnEnteringUniverse(universe);
|
||||
universe.OnPostUpdate += PollEvents;
|
||||
}
|
||||
|
||||
protected override void OnExitingUniverse(IUniverse universe)
|
||||
{
|
||||
base.OnExitingUniverse(universe);
|
||||
universe.OnPostUpdate -= PollEvents;
|
||||
}
|
||||
}
|
||||
|
@@ -2,6 +2,7 @@ using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
|
||||
using Syntriax.Engine.Core;
|
||||
|
||||
namespace Syntriax.Engine.Network;
|
||||
@@ -151,6 +152,6 @@ public class NetworkManager : UniverseObject, INetworkManager
|
||||
protected override void OnEnteringUniverse(IUniverse universe)
|
||||
{
|
||||
_networkEntityCollector.Assign(universe);
|
||||
NetworkCommunicator = BehaviourController.GetRequiredBehaviourInParent<INetworkCommunicator>();
|
||||
NetworkCommunicator = this.GetRequiredUniverseObjectInParent<INetworkCommunicator>();
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user