feat: Working Network Code
This commit is contained in:
parent
376f18c43a
commit
7f169fc788
|
@ -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]
|
||||
|
|
|
@ -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"); ;
|
||||
|
|
|
@ -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);
|
||||
|
||||
// .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);
|
||||
|
||||
|
@ -146,9 +123,13 @@ public abstract class NetworkBase : BehaviourOverride, INetworkCommunicator
|
|||
}
|
||||
|
||||
private void NetworkReceiveEvent(NetPeer peer, NetPacketReader reader, byte channel, DeliveryMethod deliveryMethod)
|
||||
{
|
||||
try
|
||||
{
|
||||
netPacketProcessor.ReadAllPackets(reader, peer);
|
||||
}
|
||||
catch { }
|
||||
}
|
||||
|
||||
public void PollEvents() => Manager.PollEvents();
|
||||
public void Stop() => Manager.Stop();
|
||||
|
|
|
@ -15,9 +15,6 @@ public abstract class NetworkBehaviour : BehaviourOverride, INetworkBehaviour
|
|||
get => _networkId;
|
||||
set
|
||||
{
|
||||
if (!IsServer)
|
||||
return;
|
||||
|
||||
if (value == _networkId)
|
||||
return;
|
||||
|
||||
|
|
|
@ -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]);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue