feat: Entity Messaging
This commit is contained in:
parent
78010c266e
commit
1c7b1cb78a
|
@ -1,5 +1,6 @@
|
|||
using System;
|
||||
using LiteNetLib;
|
||||
using LiteNetLib.Utils;
|
||||
using Syntriax.Engine.Core.Abstract;
|
||||
|
||||
namespace Pong.Network;
|
||||
|
@ -11,4 +12,8 @@ public interface INetworkCommunicator
|
|||
|
||||
void PollEvents();
|
||||
void Stop();
|
||||
|
||||
void RegisterEntityListener(IEntity entity, Action<NetPacketReader> onDataReceived);
|
||||
void UnregisterEntityListener(IEntity entity);
|
||||
NetDataWriter GetEntityWriter(IEntity entity);
|
||||
}
|
||||
|
|
|
@ -0,0 +1,73 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
using LiteNetLib;
|
||||
using LiteNetLib.Utils;
|
||||
|
||||
using Syntriax.Engine.Core;
|
||||
using Syntriax.Engine.Core.Abstract;
|
||||
|
||||
namespace Pong.Network;
|
||||
|
||||
public abstract class NetworkBase : BehaviourOverride, INetworkCommunicator
|
||||
{
|
||||
public EventBasedNetListener Listener { get; private set; } = null!;
|
||||
public NetManager Manager { get; private set; } = null!;
|
||||
|
||||
public NetworkBase()
|
||||
{
|
||||
Priority = 10;
|
||||
|
||||
Listener = new EventBasedNetListener();
|
||||
Manager = new NetManager(Listener);
|
||||
|
||||
Listener.NetworkReceiveEvent += NetworkReceiveEvent;
|
||||
}
|
||||
|
||||
public void PollEvents() => Manager.PollEvents();
|
||||
public void Stop() => Manager.Stop();
|
||||
|
||||
protected override void OnUpdate() => PollEvents();
|
||||
protected override void OnFinalize() => Stop();
|
||||
|
||||
private Dictionary<string, Action<NetPacketReader>> callbacks = new(32);
|
||||
|
||||
private void NetworkReceiveEvent(NetPeer peer, NetPacketReader reader, byte channel, DeliveryMethod deliveryMethod)
|
||||
{
|
||||
if (callbacks.TryGetValue(reader.GetString(), out var action))
|
||||
action?.Invoke(reader);
|
||||
|
||||
reader.Recycle();
|
||||
}
|
||||
|
||||
public void RegisterEntityListener(IEntity entity, Action<NetPacketReader> onDataReceived)
|
||||
{
|
||||
if (callbacks.ContainsKey(entity.Id))
|
||||
return;
|
||||
|
||||
callbacks.Add(entity.Id, onDataReceived);
|
||||
entity.OnIdChanged += OnEntityIdChanged;
|
||||
}
|
||||
|
||||
public void UnregisterEntityListener(IEntity entity)
|
||||
{
|
||||
if (!callbacks.Remove(entity.Id))
|
||||
return;
|
||||
|
||||
entity.OnIdChanged -= OnEntityIdChanged;
|
||||
}
|
||||
|
||||
public NetDataWriter GetEntityWriter(IEntity entity)
|
||||
{
|
||||
NetDataWriter netDataWriter = new();
|
||||
netDataWriter.Put(entity.Id);
|
||||
return netDataWriter;
|
||||
}
|
||||
|
||||
private void OnEntityIdChanged(IEntity entity, string previousId)
|
||||
{
|
||||
var action = callbacks[previousId];
|
||||
callbacks.Remove(previousId);
|
||||
callbacks.Add(entity.Id, action);
|
||||
}
|
||||
}
|
|
@ -1,34 +1,10 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using LiteNetLib;
|
||||
|
||||
using Syntriax.Engine.Core;
|
||||
using Syntriax.Engine.Core.Abstract;
|
||||
|
||||
namespace Pong.Network;
|
||||
|
||||
public class NetworkClient : BehaviourOverride, INetworkClient
|
||||
public class NetworkClient : NetworkBase, INetworkClient
|
||||
{
|
||||
public EventBasedNetListener Listener { get; private set; } = null!;
|
||||
public NetManager Manager { get; private set; } = null!;
|
||||
|
||||
public NetworkClient()
|
||||
{
|
||||
Priority = 10;
|
||||
|
||||
Listener = new EventBasedNetListener();
|
||||
Manager = new NetManager(Listener);
|
||||
}
|
||||
|
||||
public void Connect(string address, int port, string? password = null)
|
||||
{
|
||||
Manager.Start();
|
||||
Manager.Connect(address, port, password ?? string.Empty);
|
||||
}
|
||||
|
||||
public void PollEvents() => Manager.PollEvents();
|
||||
public void Stop() => Manager.Stop();
|
||||
|
||||
protected override void OnUpdate() => PollEvents();
|
||||
protected override void OnFinalize() => Stop();
|
||||
}
|
||||
|
|
|
@ -1,28 +1,13 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using LiteNetLib;
|
||||
|
||||
using Syntriax.Engine.Core;
|
||||
using Syntriax.Engine.Core.Abstract;
|
||||
|
||||
namespace Pong.Network;
|
||||
|
||||
public class NetworkServer : BehaviourOverride, INetworkServer
|
||||
public class NetworkServer : NetworkBase, INetworkServer
|
||||
{
|
||||
public string Password { get; private set; } = string.Empty;
|
||||
public int MaxConnectionCount { get; private set; } = 0;
|
||||
public int Port { get; private set; } = 8888;
|
||||
|
||||
public EventBasedNetListener Listener { get; private set; } = null!;
|
||||
public NetManager Manager { get; private set; } = null!;
|
||||
|
||||
public NetworkServer()
|
||||
public NetworkServer() : base()
|
||||
{
|
||||
Priority = 10;
|
||||
|
||||
Listener = new EventBasedNetListener();
|
||||
Manager = new NetManager(Listener);
|
||||
|
||||
Listener.ConnectionRequestEvent += request =>
|
||||
{
|
||||
if (Manager.ConnectedPeersCount < MaxConnectionCount)
|
||||
|
@ -40,10 +25,4 @@ public class NetworkServer : BehaviourOverride, INetworkServer
|
|||
|
||||
Manager.Start(port);
|
||||
}
|
||||
|
||||
public void PollEvents() => Manager.PollEvents();
|
||||
public void Stop() => Manager.Stop();
|
||||
|
||||
protected override void OnUpdate() => PollEvents();
|
||||
protected override void OnFinalize() => Stop();
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue