Engine-Pong/Game/Network/NetworkBase.cs

141 lines
4.8 KiB
C#
Raw Permalink Normal View History

2024-07-15 17:00:04 +03:00
using System;
using System.Collections.Generic;
2024-07-18 23:07:05 +03:00
using System.Linq;
using System.Reflection;
2024-07-15 17:00:04 +03:00
using LiteNetLib;
using LiteNetLib.Utils;
using Syntriax.Engine.Core;
using Syntriax.Engine.Network.Abstract;
namespace Syntriax.Engine.Network;
public abstract class NetworkBase : BehaviourOverride, INetworkCommunicator
{
2024-07-18 23:07:05 +03:00
public event INetworkCommunicator.OnPacketReceivedDelegate? OnPacketReceived = null;
2024-07-15 17:00:04 +03:00
2024-07-18 23:07:05 +03:00
protected readonly NetPacketProcessor netPacketProcessor = new();
2024-07-15 17:00:04 +03:00
2024-07-18 23:07:05 +03:00
protected readonly Dictionary<uint, INetworkEntity> networkEntities = [];
protected BehaviourCollector<INetworkEntity> networkEntityCollector = null!;
2024-07-15 17:00:04 +03:00
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;
2024-07-18 23:07:05 +03:00
netPacketProcessor.RegisterNestedType<Pong.Behaviours.PaddleBehaviour.PaddleInputs>();
RegisterPackets();
2024-07-16 22:50:16 +03:00
}
2024-07-18 23:07:05 +03:00
public void RegisterPackets()
{
var packetTypes = Assembly.GetExecutingAssembly().GetTypes()
.Where(t => typeof(INetworkPacket).IsAssignableFrom(t) && !t.IsInterface)
.ToList();
MethodInfo[] subscribeMethods = netPacketProcessor.GetType()
.GetMethods()
.Where(m => m.Name == nameof(NetPacketProcessor.SubscribeReusable))
.ToArray();
MethodInfo subscribeMethod = subscribeMethods
2024-07-29 23:01:27 +03:00
.FirstOrDefault(m =>
m.GetParameters().Length == 1 &&
m.GetParameters()[0].ParameterType.GetGenericTypeDefinition() == typeof(Action<,>)
) ?? throw new Exception();
2024-07-18 23:07:05 +03:00
MethodInfo[] methodInfos = typeof(NetworkBase)
2024-07-29 23:01:27 +03:00
.GetMethods(BindingFlags.NonPublic | BindingFlags.Instance);
2024-07-18 23:07:05 +03:00
2024-07-29 23:01:27 +03:00
MethodInfo method = methodInfos
.FirstOrDefault(
m => m.Name == nameof(OnPacketArrived) &&
m.IsGenericMethodDefinition
) ?? throw new Exception();
2024-07-18 23:07:05 +03:00
foreach (var packetType in packetTypes)
{
var networkPacketType = typeof(NetworkPacket<>).MakeGenericType(packetType);
MethodInfo genericSubscribe = subscribeMethod.MakeGenericMethod(networkPacketType, typeof(NetPeer));
Action<object, NetPeer> handler = (packet, peer) =>
{
2024-07-29 23:01:27 +03:00
MethodInfo handlerMethod = method.MakeGenericMethod(packetType);
handlerMethod.Invoke(this, [packet, peer]);
2024-07-18 23:07:05 +03:00
};
genericSubscribe.Invoke(netPacketProcessor, [handler]);
}
}
2024-07-29 23:01:27 +03:00
private void OnPacketArrived<T>(NetworkPacket<T> packet, NetPeer peer) where T : INetworkPacket
2024-07-16 22:50:16 +03:00
{
2024-07-18 23:07:05 +03:00
if (networkEntities.TryGetValue(packet.NetworkId, out INetworkEntity? entity))
entity.ReceiveData(packet.Data);
OnPacketReceived?.Invoke(this, packet);
2024-07-15 17:00:04 +03:00
}
protected override void OnInitialize()
{
base.OnInitialize();
networkEntityCollector = new(GameObject.GameManager);
networkEntityCollector.OnCollected += OnNetworkEntityCollected;
networkEntityCollector.OnRemoved += OnNetworkEntityRemoved;
}
2024-07-16 22:50:16 +03:00
2024-07-15 17:00:04 +03:00
protected override void OnFinalize()
{
networkEntityCollector.OnCollected -= OnNetworkEntityCollected;
networkEntityCollector.OnRemoved -= OnNetworkEntityRemoved;
Stop();
}
private void OnNetworkEntityCollected(BehaviourCollector<INetworkEntity> sender, INetworkEntity behaviourCollected)
2024-07-19 21:39:50 +03:00
{
if (behaviourCollected.NetworkId != 0)
networkEntities.Add(behaviourCollected.NetworkId, behaviourCollected);
behaviourCollected.OnNetworkIdChanged += OnNetworkIdChanged;
}
private void OnNetworkIdChanged(INetworkEntity sender, uint previousId)
{
if (networkEntities.TryGetValue(previousId, out INetworkEntity? networkEntity) && sender == networkEntity)
networkEntities.Remove(previousId);
networkEntities.Add(sender.NetworkId, sender);
}
2024-07-15 17:00:04 +03:00
private void OnNetworkEntityRemoved(BehaviourCollector<INetworkEntity> sender, INetworkEntity behaviourRemoved)
2024-07-19 21:39:50 +03:00
{
networkEntities.Remove(behaviourRemoved.NetworkId);
behaviourRemoved.OnNetworkIdChanged -= OnNetworkIdChanged;
}
2024-07-15 17:00:04 +03:00
private void NetworkReceiveEvent(NetPeer peer, NetPacketReader reader, byte channel, DeliveryMethod deliveryMethod)
{
2024-07-29 23:01:27 +03:00
try
{
netPacketProcessor.ReadAllPackets(reader, peer);
}
catch { }
2024-07-15 17:00:04 +03:00
}
public void PollEvents() => Manager.PollEvents();
public void Stop() => Manager.Stop();
protected override void OnUpdate() => PollEvents();
2024-07-18 23:07:05 +03:00
public abstract void Send<T>(NetworkPacket<T> packet);
2024-07-15 17:00:04 +03:00
}