feat: Entity Messaging

This commit is contained in:
Syntriax 2024-02-02 12:42:59 +03:00
parent 78010c266e
commit 1c7b1cb78a
4 changed files with 81 additions and 48 deletions

View File

@ -1,5 +1,6 @@
using System; using System;
using LiteNetLib; using LiteNetLib;
using LiteNetLib.Utils;
using Syntriax.Engine.Core.Abstract; using Syntriax.Engine.Core.Abstract;
namespace Pong.Network; namespace Pong.Network;
@ -11,4 +12,8 @@ public interface INetworkCommunicator
void PollEvents(); void PollEvents();
void Stop(); void Stop();
void RegisterEntityListener(IEntity entity, Action<NetPacketReader> onDataReceived);
void UnregisterEntityListener(IEntity entity);
NetDataWriter GetEntityWriter(IEntity entity);
} }

View File

@ -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);
}
}

View File

@ -1,34 +1,10 @@
using System;
using System.Collections.Generic;
using LiteNetLib;
using Syntriax.Engine.Core;
using Syntriax.Engine.Core.Abstract;
namespace Pong.Network; 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) public void Connect(string address, int port, string? password = null)
{ {
Manager.Start(); Manager.Start();
Manager.Connect(address, port, password ?? string.Empty); 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();
} }

View File

@ -1,28 +1,13 @@
using System;
using System.Collections.Generic;
using LiteNetLib;
using Syntriax.Engine.Core;
using Syntriax.Engine.Core.Abstract;
namespace Pong.Network; namespace Pong.Network;
public class NetworkServer : BehaviourOverride, INetworkServer public class NetworkServer : NetworkBase, INetworkServer
{ {
public string Password { get; private set; } = string.Empty; public string Password { get; private set; } = string.Empty;
public int MaxConnectionCount { get; private set; } = 0; public int MaxConnectionCount { get; private set; } = 0;
public int Port { get; private set; } = 8888; public int Port { get; private set; } = 8888;
public EventBasedNetListener Listener { get; private set; } = null!; public NetworkServer() : base()
public NetManager Manager { get; private set; } = null!;
public NetworkServer()
{ {
Priority = 10;
Listener = new EventBasedNetListener();
Manager = new NetManager(Listener);
Listener.ConnectionRequestEvent += request => Listener.ConnectionRequestEvent += request =>
{ {
if (Manager.ConnectedPeersCount < MaxConnectionCount) if (Manager.ConnectedPeersCount < MaxConnectionCount)
@ -40,10 +25,4 @@ public class NetworkServer : BehaviourOverride, INetworkServer
Manager.Start(port); Manager.Start(port);
} }
public void PollEvents() => Manager.PollEvents();
public void Stop() => Manager.Stop();
protected override void OnUpdate() => PollEvents();
protected override void OnFinalize() => Stop();
} }