Browse Source

Refactored NetMsgController from static to MonoBehaviour

James Peret 2 years ago
parent
commit
dd60c7d5b3

+ 18 - 5
Runtime/Client/ClientBehaviour.cs

@@ -1,30 +1,43 @@
 using UnityEngine;
 using Unity.Networking.Transport;
 using KairoEngine.Core;
+using Sirenix.OdinInspector;
 
 namespace KairoEngine.Multiplayer
 {
+    [HideMonoScript]
     public class ClientBehaviour : MonoBehaviour
     {
-        
+        public NetMsgController netMsgController;
+        public string eventStreamName = "ClientEvents";
         public string address = "192.168.1.1";
         public ushort port = 9000;
         public string playerName = "Player 1";
         public bool autoConnect = true;
-        public string eventStreamName = "ClientEvents";
+        
         public bool debug = false;
 
         public NetworkDriver m_Driver;
         public NetworkConnection m_Connection;
         public bool m_Done;
 
-        void Start ()
+        private void Awake()
+        {
+            if(netMsgController == null) Debug.LogError("Client is missing NetMsgController!", this);
+        }
+
+        private void Start ()
         {
             if(autoConnect) ConnectToServer();
         }
 
         public void ConnectToServer()
         {
+            if(netMsgController == null)
+            {
+                Debug.LogError("Client cannot connect to server with missing NetMsgController!", this);
+                return;
+            }
             m_Driver = NetworkDriver.Create();
             m_Connection = default(NetworkConnection);
             var endpoint = NetworkEndPoint.Parse(address, port);
@@ -50,7 +63,7 @@ namespace KairoEngine.Multiplayer
         {
             DataStreamWriter writer = new DataStreamWriter();
             m_Driver.BeginSend(m_Connection, out writer);
-            NetMsgController.SendData(code, netMsg, ref writer, this);
+            netMsgController.SendData(code, netMsg, ref writer, this);
             m_Driver.EndSend(writer);
         }
 
@@ -88,7 +101,7 @@ namespace KairoEngine.Multiplayer
                 else if (cmd == NetworkEvent.Type.Data)
                 {
                     //if(debug) Debug.Log("Client received data from the server");
-                    NetMsgController.ReceiveData(ref stream, this);
+                    netMsgController.ReceiveData(ref stream, this);
                 }
                 else if (cmd == NetworkEvent.Type.Disconnect)
                 {

+ 14 - 7
Runtime/NetMsgController.cs

@@ -4,14 +4,21 @@ using System.Collections.Generic;
 using UnityEngine;
 using Unity.Networking.Transport;
 using KairoEngine.Core;
+using Sirenix.OdinInspector;
 
 namespace KairoEngine.Multiplayer
 {
-    public static class NetMsgController
+    [HideMonoScript]
+    public class NetMsgController : MonoBehaviour
     {
         private static List<NetMsg> netMessageTypes = new List<NetMsg>();
 
-        public static void GetNetMessageTypes()
+        private void Start()
+        {
+            GetNetMessageTypes();
+        }
+
+        private void GetNetMessageTypes()
         {
             netMessageTypes = ReflectiveEnumerator.GetEnumerableOfType<NetMsg>()
                 .Where(x => x.code != 0)
@@ -19,7 +26,7 @@ namespace KairoEngine.Multiplayer
                 .ToList();
         }
 
-        public static NetMsg FindWithCode(uint code)
+        public NetMsg FindWithCode(uint code)
         {
             for (int i = 0; i < netMessageTypes.Count; i++)
             {
@@ -28,26 +35,26 @@ namespace KairoEngine.Multiplayer
             return null;
         }
 
-        public static void ReceiveData(ref DataStreamReader stream, ServerBehaviour server, int clientId) 
+        public void ReceiveData(ref DataStreamReader stream, ServerBehaviour server, int clientId) 
         {
             uint code = stream.ReadByte();
             NetMsg msg = FindWithCode(code).ReceiveMessage(server, ref stream, clientId);
             msg.ReadMessage(server, clientId);
         }
 
-        public static void ReceiveData(ref DataStreamReader stream, ClientBehaviour client) 
+        public void ReceiveData(ref DataStreamReader stream, ClientBehaviour client) 
         {
             uint code = stream.ReadByte();
             NetMsg msg = FindWithCode(code).ReceiveMessage(client, ref stream);
             msg.ReadMessage(client);
         }
 
-        public static void SendData(uint code, NetMsg netMsg, ref DataStreamWriter writer, ServerBehaviour server, int clientId)
+        public void SendData(uint code, NetMsg netMsg, ref DataStreamWriter writer, ServerBehaviour server, int clientId)
         {
             FindWithCode(code).SendMessage(server, netMsg, ref writer, clientId);
         }
 
-        public static void SendData(uint code, NetMsg netMsg, ref DataStreamWriter writer, ClientBehaviour client)
+        public void SendData(uint code, NetMsg netMsg, ref DataStreamWriter writer, ClientBehaviour client)
         {
             FindWithCode(code).SendMessage(client, netMsg, ref writer);
         }

+ 22 - 4
Runtime/Server/ServerBehaviour.cs

@@ -3,21 +3,34 @@ using UnityEngine.Assertions;
 using Unity.Collections;
 using Unity.Networking.Transport;
 using KairoEngine.Core;
+using Sirenix.OdinInspector;
 
 namespace KairoEngine.Multiplayer
 {
+    [HideMonoScript]
     public class ServerBehaviour : MonoBehaviour
     {
+        public NetMsgController netMsgController;
+        public string eventStreamName = "ServerEvents";
         public ushort port = 9000;
         public int players = 4;
         public bool autoStart = true;
-        public string eventStreamName = "ServerEvents";
+        
         public bool debug = false;
         public NetworkDriver m_Driver;
         public NativeList<NetworkConnection> m_Connections;
         private bool locked = false;
 
-        void Start ()
+        private void Awake()
+        {
+            if(netMsgController == null)
+            {
+                Debug.LogError("Server is missing NetMsgController!", this);
+                return;
+            }
+        }
+
+        private void Start ()
         {
             if(autoStart) StartServer();
         }
@@ -25,6 +38,11 @@ namespace KairoEngine.Multiplayer
         public void StartServer()
         {
             if(locked) return;
+            if(netMsgController == null)
+            {
+                Debug.LogError("Server cannot start with missing NetMsgController!", this);
+                return;
+            }
             m_Driver = NetworkDriver.Create();
             var endpoint = NetworkEndPoint.AnyIpv4;
             endpoint.Port = port;
@@ -60,7 +78,7 @@ namespace KairoEngine.Multiplayer
         {
             DataStreamWriter writer = new DataStreamWriter();
             m_Driver.BeginSend(m_Connections[id], out writer);
-            NetMsgController.SendData(code, netMsg, ref writer, this, id);
+            netMsgController.SendData(code, netMsg, ref writer, this, id);
             m_Driver.EndSend(writer);
         }
 
@@ -114,7 +132,7 @@ namespace KairoEngine.Multiplayer
                     if (cmd == NetworkEvent.Type.Data)
                     {
                         //if(debug) Debug.Log("Server received data from client");
-                        NetMsgController.ReceiveData(ref stream, this, i);
+                        netMsgController.ReceiveData(ref stream, this, i);
                     }
                     else if (cmd == NetworkEvent.Type.Disconnect)
                     {

+ 0 - 1
Runtime/_NetMsgs/NetHandshakeMsg.cs

@@ -34,7 +34,6 @@ namespace KairoEngine.Multiplayer
 
         public override void SendMessage(ClientBehaviour client, NetMsg netMsg, ref DataStreamWriter writer ) 
         {
-            Debug.Log("Testing!");
             if(client == null) return;
             NetHandshakeMsg netHandshakeMsg = (NetHandshakeMsg)netMsg;
             string text = $"Sending handshake (playerName = {netHandshakeMsg.playerName})";