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)
{
if (data is PaddleInputs paddleInputs)
{
System.Diagnostics.Debug.WriteLine($"Paddle Inputs Arrived: {paddleInputs.IsUpPressed}, {paddleInputs.IsDownPressed}");
}
if (data is not PaddleInputs paddleInputs)
return;
isUpPressed = paddleInputs.IsUpPressed;
isDownPressed = paddleInputs.IsDownPressed;
}
[System.Serializable]

View File

@ -87,6 +87,9 @@ public class GamePong : Game
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"); ;

View File

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

View File

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

View File

@ -1,4 +1,6 @@
using System;
using System.Linq;
using System.Reflection;
using LiteNetLib.Utils;
using Syntriax.Engine.Network.Abstract;
@ -47,5 +49,19 @@ public class NetworkServer : NetworkBase, INetworkCommunicatorServer
}
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]);
}
}