From c28568d0cbab6cee9e5b0b30cd99d87d5627488d Mon Sep 17 00:00:00 2001 From: Syntriax Date: Tue, 16 Jul 2024 22:50:16 +0300 Subject: [PATCH] feat: Test Network --- Game/GamePong.cs | 58 ++++++++++++++----- Game/Network/Abstract/INetworkCommunicator.cs | 7 --- Game/Network/Abstract/INetworkEntity.cs | 7 --- Game/Network/Abstract/NetworkPacket.cs | 20 +------ Game/Network/NetworkBase.cs | 53 +++-------------- Game/Network/NetworkBehaviour.cs | 17 ------ Game/Network/NetworkClient.cs | 2 + Game/Network/NetworkServer.cs | 2 + 8 files changed, 56 insertions(+), 110 deletions(-) diff --git a/Game/GamePong.cs b/Game/GamePong.cs index b5e4e7c..b2f121a 100644 --- a/Game/GamePong.cs +++ b/Game/GamePong.cs @@ -1,16 +1,19 @@ using System; + using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Input; -using Pong.Behaviours; using Apos.Shapes; +using Pong.Behaviours; +using Syntriax.Engine.Network; + using Syntriax.Engine.Core; using Syntriax.Engine.Core.Abstract; using Syntriax.Engine.Physics2D; -using Syntriax.Engine.Physics2D.Primitives; using Syntriax.Engine.Physics2D.Abstract; +using Syntriax.Engine.Physics2D.Primitives; namespace Pong; @@ -64,58 +67,81 @@ public class GamePong : Game //////////////////////////////////////////////////////////////////////////////////// - IGameObject gameObjectCamera = gameManager.InstantiateGameObject().SetGameObject("Camera"); ; + string[] commandLineArguments = Environment.GetCommandLineArgs(); + if (commandLineArguments.Length != 1) + { + if (commandLineArguments[1].Equals("server", StringComparison.OrdinalIgnoreCase)) + { + gameManager.InstantiateGameObject().BehaviourController.AddBehaviour().Start(8888, 2); + Window.Title = "Pong - Server"; + } + else + { + Window.Title = $"Pong - Client -> {commandLineArguments[1]}"; + gameManager.InstantiateGameObject().BehaviourController.AddBehaviour().Connect(commandLineArguments[1], 8888); + } + } + else + { + gameManager.InstantiateGameObject().BehaviourController.AddBehaviour().Connect("127.0.0.1", 8888); + Window.Title = $"Pong - Client -> 127.0.0.1"; + } + + //////////////////////////////////////////////////////////////////////////////////// + + IGameObject gameObjectCamera = gameManager.InstantiateGameObject().SetGameObject("Camera"); ; gameObjectCamera.BehaviourController.AddBehaviour(); cameraBehaviour = gameObjectCamera.BehaviourController.AddBehaviour(graphics); //////////////////////////////////////////////////////////////////////////////////// - IGameObject gameObjectPongManager = gameManager.InstantiateGameObject().SetGameObject("Pong Game Manager"); + IGameObject gameObjectPongManager = gameManager.InstantiateGameObject().SetGameObject("Pong Game Manager"); pongManager = gameObjectPongManager.BehaviourController.AddBehaviour(5); + pongManager.Id = "pongManager"; //////////////////////////////////////////////////////////////////////////////////// - IGameObject gameObjectBall = gameManager.InstantiateGameObject().SetGameObject("Ball"); + IGameObject gameObjectBall = gameManager.InstantiateGameObject().SetGameObject("Ball"); gameObjectBall.Transform.SetTransform(position: new Vector2D(0, 0f), scale: new Vector2D(10f, 10f)); gameObjectBall.BehaviourController.AddBehaviour(new Circle(Vector2D.Zero, 1f)); - gameObjectBall.BehaviourController.AddBehaviour(); + gameObjectBall.BehaviourController.AddBehaviour().Id = "ball"; gameObjectBall.BehaviourController.AddBehaviour(); //////////////////////////////////////////////////////////////////////////////////// - IGameObject gameObjectLeftPaddle = gameManager.InstantiateGameObject().SetGameObject("Left Paddle"); + IGameObject gameObjectLeftPaddle = gameManager.InstantiateGameObject().SetGameObject("Left Paddle"); gameObjectLeftPaddle.Transform.SetTransform(position: new Vector2D(-468f, 0f), scale: new Vector2D(15f, 60f)); - gameObjectLeftPaddle.BehaviourController.AddBehaviour(Keys.W, Keys.S, 228f, -228f, 400f); + gameObjectLeftPaddle.BehaviourController.AddBehaviour(Keys.W, Keys.S, 228f, -228f, 400f).Id = "leftPaddle"; gameObjectLeftPaddle.BehaviourController.AddBehaviour(Shape.Box); gameObjectLeftPaddle.BehaviourController.AddBehaviour().IsStatic = true; - IGameObject gameObjectRightPaddle = gameManager.InstantiateGameObject().SetGameObject("Right Paddle"); + IGameObject gameObjectRightPaddle = gameManager.InstantiateGameObject().SetGameObject("Right Paddle"); gameObjectRightPaddle.Transform.SetTransform(position: new Vector2D(468f, 0f), scale: new Vector2D(15f, 60f)); - gameObjectRightPaddle.BehaviourController.AddBehaviour(Keys.Up, Keys.Down, 228f, -228f, 400f); + gameObjectRightPaddle.BehaviourController.AddBehaviour(Keys.Up, Keys.Down, 228f, -228f, 400f).Id = "rightPaddle"; gameObjectRightPaddle.BehaviourController.AddBehaviour(Shape.Box); gameObjectRightPaddle.BehaviourController.AddBehaviour().IsStatic = true; //////////////////////////////////////////////////////////////////////////////////// - IGameObject gameObjectWallTop = gameManager.InstantiateGameObject().SetGameObject("Wall Top"); + IGameObject gameObjectWallTop = gameManager.InstantiateGameObject().SetGameObject("Wall Top"); gameObjectWallTop.Transform.SetTransform(position: new Vector2D(0f, 308f), scale: new Vector2D(552f, 20f)); gameObjectWallTop.BehaviourController.AddBehaviour(Shape.Box); gameObjectWallTop.BehaviourController.AddBehaviour().IsStatic = true; - IGameObject gameObjectWallBottom = gameManager.InstantiateGameObject().SetGameObject("Wall Bottom"); + IGameObject gameObjectWallBottom = gameManager.InstantiateGameObject().SetGameObject("Wall Bottom"); gameObjectWallBottom.Transform.SetTransform(position: new Vector2D(0f, -308f), scale: new Vector2D(552f, 20f)); gameObjectWallBottom.BehaviourController.AddBehaviour(Shape.Box); gameObjectWallBottom.BehaviourController.AddBehaviour().IsStatic = true; - IGameObject gameObjectWallRight = gameManager.InstantiateGameObject().SetGameObject("Wall Right"); + IGameObject gameObjectWallRight = gameManager.InstantiateGameObject().SetGameObject("Wall Right"); gameObjectWallRight.Transform.SetTransform(position: new Vector2D(532f, 0f), scale: new Vector2D(20f, 328f)); gameObjectWallRight.BehaviourController.AddBehaviour((Action)pongManager.ScoreToLeft); gameObjectWallRight.BehaviourController.AddBehaviour(Shape.Box); gameObjectWallRight.BehaviourController.AddBehaviour().IsStatic = true; - IGameObject gameObjectWallLeft = gameManager.InstantiateGameObject().SetGameObject("Wall Left"); + IGameObject gameObjectWallLeft = gameManager.InstantiateGameObject().SetGameObject("Wall Left"); gameObjectWallLeft.Transform.SetTransform(position: new Vector2D(-532f, 0f), scale: new Vector2D(20f, 328f)); gameObjectWallLeft.BehaviourController.AddBehaviour((Action)pongManager.ScoreToRight); gameObjectWallLeft.BehaviourController.AddBehaviour(Shape.Box); @@ -123,11 +149,11 @@ public class GamePong : Game //////////////////////////////////////////////////////////////////////////////////// - IGameObject gameObjectLeftScoreText = gameManager.InstantiateGameObject().SetGameObject("Score Left"); + IGameObject gameObjectLeftScoreText = gameManager.InstantiateGameObject().SetGameObject("Score Left"); gameObjectLeftScoreText.Transform.SetTransform(position: new Vector2D(-250f, 250f), scale: Vector2D.One * .25f); gameObjectLeftScoreText.BehaviourController.AddBehaviour(true, spriteFont); - IGameObject gameObjectRightScoreText = gameManager.InstantiateGameObject().SetGameObject("Score Right"); + IGameObject gameObjectRightScoreText = gameManager.InstantiateGameObject().SetGameObject("Score Right"); gameObjectRightScoreText.Transform.SetTransform(position: new Vector2D(250f, 250f), scale: Vector2D.One * .25f); gameObjectRightScoreText.BehaviourController.AddBehaviour(false, spriteFont); } diff --git a/Game/Network/Abstract/INetworkCommunicator.cs b/Game/Network/Abstract/INetworkCommunicator.cs index 33e1819..93f996b 100644 --- a/Game/Network/Abstract/INetworkCommunicator.cs +++ b/Game/Network/Abstract/INetworkCommunicator.cs @@ -1,5 +1,4 @@ using LiteNetLib; -using LiteNetLib.Utils; namespace Syntriax.Engine.Network.Abstract; @@ -10,10 +9,4 @@ public interface INetworkCommunicator void PollEvents(); void Stop(); - - void RegisterEntityPacketListener(INetworkEntity networkEntity, EntityPacketReceivedDelegate onPacketReceived) where T : INetSerializable; - void UnregisterEntityPacketListener(INetworkEntity networkEntity, EntityPacketReceivedDelegate onPacketReceived) where T : INetSerializable; - void SendEntityPacket(INetworkEntity networkEntity, T packet, params NetPeer[] netPeer) where T : INetSerializable; - - delegate void EntityPacketReceivedDelegate(INetworkEntity networkEntity, object packet, NetPeer netPeer); } diff --git a/Game/Network/Abstract/INetworkEntity.cs b/Game/Network/Abstract/INetworkEntity.cs index 45c8c81..0a1a67f 100644 --- a/Game/Network/Abstract/INetworkEntity.cs +++ b/Game/Network/Abstract/INetworkEntity.cs @@ -1,5 +1,3 @@ -using LiteNetLib.Utils; - namespace Syntriax.Engine.Network.Abstract; public interface INetworkEntity @@ -9,9 +7,4 @@ public interface INetworkEntity uint NetworkId { get; set; } delegate void OnNetworkIdChangedDelegate(INetworkEntity sender, uint previousId); - delegate void PacketReceivedDelegate(INetworkEntity entity, object packet); - - void RegisterPacketListener(PacketReceivedDelegate onPacketReceived) where T : INetSerializable; - void UnregisterPacketListener(PacketReceivedDelegate onPacketReceived) where T : INetSerializable; - void SendPacket(T packet) where T : INetSerializable; } diff --git a/Game/Network/Abstract/NetworkPacket.cs b/Game/Network/Abstract/NetworkPacket.cs index e1d9bc2..f1a81f8 100644 --- a/Game/Network/Abstract/NetworkPacket.cs +++ b/Game/Network/Abstract/NetworkPacket.cs @@ -1,21 +1,7 @@ -using LiteNetLib.Utils; - namespace Syntriax.Engine.Network.Abstract; -public class NetworkPacket() - : INetSerializable - where T : INetSerializable +public class NetworkPacket { - public uint NetworkId = 0; - public T Data = default!; - - public void Deserialize(NetDataReader reader) - { - NetworkId = reader.GetUInt(); - } - - public void Serialize(NetDataWriter writer) - { - writer.Put(NetworkId); - } + public uint NetworkId { get; set; } = 0; + public T Data { get; set; } = default!; } diff --git a/Game/Network/NetworkBase.cs b/Game/Network/NetworkBase.cs index aa9b028..54cdbff 100644 --- a/Game/Network/NetworkBase.cs +++ b/Game/Network/NetworkBase.cs @@ -1,7 +1,6 @@ using System; using System.Collections.Generic; using System.Diagnostics; - using LiteNetLib; using LiteNetLib.Utils; @@ -15,7 +14,6 @@ public abstract class NetworkBase : BehaviourOverride, INetworkCommunicator private readonly NetPacketProcessor netPacketProcessor = new(); private readonly Dictionary networkEntities = []; - private readonly Dictionary> callbacks = []; private BehaviourCollector networkEntityCollector = null!; @@ -30,6 +28,12 @@ public abstract class NetworkBase : BehaviourOverride, INetworkCommunicator Manager = new NetManager(Listener); Listener.NetworkReceiveEvent += NetworkReceiveEvent; + netPacketProcessor.SubscribeReusable, NetPeer>(OnPacketArrived); + } + + private void OnPacketArrived(NetworkPacket packet, NetPeer peer) + { + Debug.WriteLine($"Packet Arrived for {packet.NetworkId}: {packet.Data}"); } protected override void OnInitialize() @@ -40,6 +44,7 @@ public abstract class NetworkBase : BehaviourOverride, INetworkCommunicator networkEntityCollector.OnCollected += OnNetworkEntityCollected; networkEntityCollector.OnRemoved += OnNetworkEntityRemoved; } + protected override void OnFinalize() { networkEntityCollector.OnCollected -= OnNetworkEntityCollected; @@ -62,48 +67,4 @@ public abstract class NetworkBase : BehaviourOverride, INetworkCommunicator public void Stop() => Manager.Stop(); protected override void OnUpdate() => PollEvents(); - - public void RegisterEntityPacketListener(INetworkEntity networkEntity, INetworkCommunicator.EntityPacketReceivedDelegate onPacketReceived) where T : INetSerializable - { - if (!callbacks.TryGetValue(typeof(T), out var list)) - { - list = []; - callbacks.Add(typeof(T), list); - netPacketProcessor.SubscribeReusable, NetPeer>(OnPacketReceived); - } - - if (list.Contains(onPacketReceived)) - return; - - list.Add(onPacketReceived); - } - - public void UnregisterEntityPacketListener(INetworkEntity networkEntity, INetworkCommunicator.EntityPacketReceivedDelegate onPacketReceived) where T : INetSerializable - { - if (!callbacks.TryGetValue(typeof(T), out var list)) - return; - - list.Remove(onPacketReceived); - } - - public void SendEntityPacket(INetworkEntity networkEntity, T packet, params NetPeer[] netPeers) where T : INetSerializable - { - NetworkPacket networkPacket = new() { NetworkId = networkEntity.NetworkId, Data = packet }; - NetDataWriter netDataWriter = new(); - netPacketProcessor.Write(netDataWriter, networkPacket); - foreach (var netPeer in netPeers) - netPeer.Send(netDataWriter, DeliveryMethod.ReliableOrdered); - } - - private void OnPacketReceived(NetworkPacket packet, NetPeer peer) where T : INetSerializable - { - Debug.WriteLine($"Packet Received: {packet.NetworkId} - {typeof(T)}"); - - if (!callbacks.TryGetValue(typeof(T), out var list)) - return; - - INetworkEntity networkEntity = networkEntities[packet.NetworkId]; - foreach (INetworkCommunicator.EntityPacketReceivedDelegate callback in list) - callback.Invoke(networkEntity, packet, peer); - } } diff --git a/Game/Network/NetworkBehaviour.cs b/Game/Network/NetworkBehaviour.cs index 832b3de..b055245 100644 --- a/Game/Network/NetworkBehaviour.cs +++ b/Game/Network/NetworkBehaviour.cs @@ -48,21 +48,4 @@ public abstract class NetworkBehaviour : BehaviourOverride, INetworkBehaviour IsServer = true; } - - public void RegisterPacketListener(INetworkEntity.PacketReceivedDelegate onPacketReceived) where T : INetSerializable - - private void OnEntityPacketReceived(INetworkEntity networkEntity, object packet, NetPeer netPeer) - { - throw new NotImplementedException(); - } - - public void UnregisterPacketListener(INetworkEntity.PacketReceivedDelegate onPacketReceived) where T : INetSerializable - { - throw new NotImplementedException(); - } - - public void SendPacket(T packet) where T : INetSerializable - { - throw new NotImplementedException(); - } } diff --git a/Game/Network/NetworkClient.cs b/Game/Network/NetworkClient.cs index 24ef10f..32fb08b 100644 --- a/Game/Network/NetworkClient.cs +++ b/Game/Network/NetworkClient.cs @@ -1,3 +1,5 @@ +using System.Diagnostics; +using System.Threading.Tasks; using Syntriax.Engine.Network.Abstract; namespace Syntriax.Engine.Network; diff --git a/Game/Network/NetworkServer.cs b/Game/Network/NetworkServer.cs index a8e26ec..d912c14 100644 --- a/Game/Network/NetworkServer.cs +++ b/Game/Network/NetworkServer.cs @@ -1,3 +1,5 @@ +using System.Diagnostics; +using System.Threading.Tasks; using Syntriax.Engine.Network.Abstract; namespace Syntriax.Engine.Network;