feat: Working Network Code

This commit is contained in:
Syntriax 2024-07-29 23:01:27 +03:00
parent 376f18c43a
commit 7f169fc788
5 changed files with 43 additions and 45 deletions

View File

@ -67,10 +67,11 @@ public class PaddleBehaviour(Keys Up, Keys Down, float High, float Low, float Sp
public override void ReceiveData<T>(T data) public override void ReceiveData<T>(T data)
{ {
if (data is PaddleInputs paddleInputs) if (data is not PaddleInputs paddleInputs)
{ return;
System.Diagnostics.Debug.WriteLine($"Paddle Inputs Arrived: {paddleInputs.IsUpPressed}, {paddleInputs.IsDownPressed}");
} isUpPressed = paddleInputs.IsUpPressed;
isDownPressed = paddleInputs.IsDownPressed;
} }
[System.Serializable] [System.Serializable]

View File

@ -87,6 +87,9 @@ public class GamePong : Game
Window.Title = $"Pong - Client -> 127.0.0.1"; Window.Title = $"Pong - Client -> 127.0.0.1";
} }
IGameObject gameObjectNetworkManager = gameManager.InstantiateGameObject().SetGameObject("Network Manager"); ;
gameObjectNetworkManager.BehaviourController.AddBehaviour<NetworkManager>();
//////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////
IGameObject gameObjectCamera = gameManager.InstantiateGameObject().SetGameObject("Camera"); ; IGameObject gameObjectCamera = gameManager.InstantiateGameObject().SetGameObject("Camera"); ;

View File

@ -42,24 +42,25 @@ public abstract class NetworkBase : BehaviourOverride, INetworkCommunicator
.Where(t => typeof(INetworkPacket).IsAssignableFrom(t) && !t.IsInterface) .Where(t => typeof(INetworkPacket).IsAssignableFrom(t) && !t.IsInterface)
.ToList(); .ToList();
// MethodInfo subscribeMethod = netPacketProcessor.GetType()
// .GetMethod(nameof(NetPacketProcessor.SubscribeReusable), [typeof(Action<,>)]);
MethodInfo[] subscribeMethods = netPacketProcessor.GetType() MethodInfo[] subscribeMethods = netPacketProcessor.GetType()
.GetMethods() .GetMethods()
.Where(m => m.Name == nameof(NetPacketProcessor.SubscribeReusable)) .Where(m => m.Name == nameof(NetPacketProcessor.SubscribeReusable))
.ToArray(); .ToArray();
MethodInfo subscribeMethod = subscribeMethods MethodInfo subscribeMethod = subscribeMethods
.FirstOrDefault(m => m.GetParameters().Length == 1 && .FirstOrDefault(m =>
m.GetParameters()[0].ParameterType.GetGenericTypeDefinition() == typeof(Action<,>)); m.GetParameters().Length == 1 &&
m.GetParameters()[0].ParameterType.GetGenericTypeDefinition() == typeof(Action<,>)
) ?? throw new Exception();
MethodInfo[] methodInfos = typeof(NetworkBase) MethodInfo[] methodInfos = typeof(NetworkBase)
.GetMethods(BindingFlags.NonPublic | BindingFlags.Instance); .GetMethods(BindingFlags.NonPublic | BindingFlags.Instance);
MethodInfo method = methodInfos
.FirstOrDefault(m => m.Name == "OnPacketArrived" && m.IsGenericMethod);
// .GetMethod(nameof(OnPacketArrived), BindingFlags.NonPublic | BindingFlags.Instance) ?? throw new Exception(); MethodInfo method = methodInfos
.FirstOrDefault(
m => m.Name == nameof(OnPacketArrived) &&
m.IsGenericMethodDefinition
) ?? throw new Exception();
foreach (var packetType in packetTypes) foreach (var packetType in packetTypes)
{ {
@ -68,39 +69,15 @@ public abstract class NetworkBase : BehaviourOverride, INetworkCommunicator
Action<object, NetPeer> handler = (packet, peer) => Action<object, NetPeer> handler = (packet, peer) =>
{ {
method = method.MakeGenericMethod(packetType); MethodInfo handlerMethod = method.MakeGenericMethod(packetType);
method.Invoke(this, [packet, peer]); handlerMethod.Invoke(this, [packet, peer]);
}; };
genericSubscribe.Invoke(netPacketProcessor, [handler]); genericSubscribe.Invoke(netPacketProcessor, [handler]);
} }
} }
// private void RegisterPackets()
// {
// IEnumerable<Type> packetTypes = Assembly.GetExecutingAssembly().GetTypes().Where(
// t => t.GetInterfaces().Contains(typeof(INetworkPacket))
// );
// MethodInfo subscribeMethod = netPacketProcessor.GetType() private void OnPacketArrived<T>(NetworkPacket<T> packet, NetPeer peer) where T : INetworkPacket
// .GetMethod(nameof(NetPacketProcessor.SubscribeReusable), [typeof(Action<,>)]) ?? throw new Exception();
// foreach (var packetType in packetTypes)
// {
// MethodInfo genericSubscribe = subscribeMethod.MakeGenericMethod(packetType, typeof(NetPeer));
// Action<object, NetPeer> handler = (packet, peer) =>
// {
// MethodInfo method = GetType()
// .GetMethod(nameof(OnPacketArrived), BindingFlags.NonPublic | BindingFlags.Instance) ?? throw new Exception();
// method = method.MakeGenericMethod(packetType.GetGenericArguments()[0]);
// method.Invoke(this, [packet, peer]);
// };
// genericSubscribe.Invoke(netPacketProcessor, [handler]);
// }
// }
private void OnPacketArrived<T>(NetworkPacket<T> packet, NetPeer peer) where T : INetworkPacket//OnPacketArrived<T>(NetworkPacket<T> packet, NetPeer peer)
{ {
// Handle packet
Console.WriteLine($"Packet of type {typeof(T)} arrived with data: {packet.Data}");
if (networkEntities.TryGetValue(packet.NetworkId, out INetworkEntity? entity)) if (networkEntities.TryGetValue(packet.NetworkId, out INetworkEntity? entity))
entity.ReceiveData(packet.Data); entity.ReceiveData(packet.Data);
@ -146,9 +123,13 @@ public abstract class NetworkBase : BehaviourOverride, INetworkCommunicator
} }
private void NetworkReceiveEvent(NetPeer peer, NetPacketReader reader, byte channel, DeliveryMethod deliveryMethod) private void NetworkReceiveEvent(NetPeer peer, NetPacketReader reader, byte channel, DeliveryMethod deliveryMethod)
{
try
{ {
netPacketProcessor.ReadAllPackets(reader, peer); netPacketProcessor.ReadAllPackets(reader, peer);
} }
catch { }
}
public void PollEvents() => Manager.PollEvents(); public void PollEvents() => Manager.PollEvents();
public void Stop() => Manager.Stop(); public void Stop() => Manager.Stop();

View File

@ -15,9 +15,6 @@ public abstract class NetworkBehaviour : BehaviourOverride, INetworkBehaviour
get => _networkId; get => _networkId;
set set
{ {
if (!IsServer)
return;
if (value == _networkId) if (value == _networkId)
return; return;

View File

@ -1,4 +1,6 @@
using System; using System;
using System.Linq;
using System.Reflection;
using LiteNetLib.Utils; using LiteNetLib.Utils;
using Syntriax.Engine.Network.Abstract; using Syntriax.Engine.Network.Abstract;
@ -47,5 +49,19 @@ public class NetworkServer : NetworkBase, INetworkCommunicatorServer
} }
private void ServerOnPacketReceived(INetworkCommunicator sender, object packet) private void ServerOnPacketReceived(INetworkCommunicator sender, object packet)
=> Send((NetworkPacket<object>)packet); {
MethodInfo[] methodInfos = GetType()
.GetMethods(BindingFlags.Public | BindingFlags.Instance);
MethodInfo method = methodInfos
.FirstOrDefault(
m => m.Name == nameof(Send) && m.IsGenericMethod
) ?? throw new Exception();
Type typeArguments = packet.GetType().GetGenericArguments()[0];
MethodInfo methodInfo = method.MakeGenericMethod(typeArguments);
methodInfo.Invoke(this, [packet]);
}
} }