Browse Source

Added server/client tick sync on handshake

James Peret 2 years ago
parent
commit
2e17ad37e5

+ 15 - 0
Runtime/Client/ClientHandshakeController.cs

@@ -2,21 +2,27 @@ using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using KairoEngine.Core;
+using Sirenix.OdinInspector;
 
 namespace KairoEngine.Multiplayer
 {
+    [HideMonoScript]
     public class ClientHandshakeController : MonoBehaviour
     {
         public ClientBehaviour client;
+        public NetworkTick networkTick;
+        private bool waitingForHandshake = false;
         
         private void Awake()
         {
             GenericEvents.StartListening($"{client.eventStreamName}_Connected", OnEvent);
+            NetMsgEvents.StartListening($"{client.eventStreamName}_HandshakeAccepted", OnHandshakeAccepted);
         }
 
         private void OnDestroy()
         {
             GenericEvents.StopListening($"{client.eventStreamName}_Connected", OnEvent);
+            NetMsgEvents.StopListening($"{client.eventStreamName}_HandshakeAccepted", OnHandshakeAccepted);
         }
 
         private void OnEvent(string text)
@@ -25,8 +31,17 @@ namespace KairoEngine.Multiplayer
             NetHandshakeMsg handshakeMsg = new NetHandshakeMsg();
             handshakeMsg.playerName = client.playerName;
             Timer.ExecuteRealTime(100, ()=> {
+                waitingForHandshake = true;
                 client.SendNetMsgToServer(((uint)NetOpCode.Handshake), handshakeMsg);
             });
         }
+
+        private void OnHandshakeAccepted(string text, int clientId, uint code, NetMsg netMsg)
+        {
+            if(!waitingForHandshake) return;
+            waitingForHandshake = false;
+            NetAcceptHandshakeMsg netAcceptHandshakeMsg = (NetAcceptHandshakeMsg)netMsg;
+            if(networkTick != null) networkTick.ChangeCurrentTick((int)netAcceptHandshakeMsg.tick);
+        }
     }
 }

+ 1 - 0
Runtime/NetOpCode.cs

@@ -5,6 +5,7 @@ namespace KairoEngine.Multiplayer
         Generic = 0, // NetMsg
         Disconnect = 1, // NetDisconnectMsg
         Handshake = 2, // NetHandshakeMsg
+        AcceptHandshake = 3, // NetAcceptHandshakeMsg
         ClientConnected = 4, // NetClientConnectedMsg
         ClientDisconnected = 5, // NetClientDisconnectedMsg
         ChatMessage = 6 //NetChatMsg

+ 1 - 1
Runtime/Server/ClientList.cs

@@ -26,7 +26,7 @@ namespace KairoEngine.Multiplayer
 
         private void OnServerAcceptingHandshakeEvent(string text, int clientId, uint code, NetMsg netMsg)
         {
-            NetHandshakeMsg handshake = (NetHandshakeMsg)netMsg;
+            NetAcceptHandshakeMsg handshake = (NetAcceptHandshakeMsg)netMsg;
             bool registered = false;
             for (int i = 0; i < clients.Count; i++)
             {

+ 8 - 3
Runtime/Server/ServerHandshakeController.cs

@@ -8,7 +8,9 @@ namespace KairoEngine.Multiplayer
     public class ServerHandshakeController : MonoBehaviour
     {
         public ServerBehaviour server;
+        public NetworkTick networkTick;
         public string eventStreamName = "ServerEvents";
+
         private void OnEnable()
         {
             NetMsgEvents.StartListening($"{eventStreamName}_HandshakeReceived", OnEvent);
@@ -23,14 +25,17 @@ namespace KairoEngine.Multiplayer
         {
             if(server == null) return;
             // Accept the client handshake
-            NetHandshakeMsg handshakeMsg = (NetHandshakeMsg)netMsg; 
+            NetHandshakeMsg netHandshakeMsg = (NetHandshakeMsg)netMsg;
+            NetAcceptHandshakeMsg netAcceptHandshakeMsg = new NetAcceptHandshakeMsg();
+            if(netHandshakeMsg != null) netAcceptHandshakeMsg.playerName = netHandshakeMsg.playerName;
+            if(networkTick != null) netAcceptHandshakeMsg.tick = (uint)networkTick.tick;
             Timer.ExecuteRealTime(100, ()=> {
-                server.SendNetMsgToClient(clientId, (uint)NetOpCode.Handshake, handshakeMsg);
+                server.SendNetMsgToClient(clientId, (uint)NetOpCode.AcceptHandshake, netAcceptHandshakeMsg);
             });
             // Alert all clients of a new connection
             Timer.ExecuteRealTime(200, ()=> {
                 NetClientConnectedMsg msg = new NetClientConnectedMsg();
-                msg.playerName = handshakeMsg.playerName;
+                msg.playerName = netAcceptHandshakeMsg.playerName;
                 server.SendNetMsgToAllClients((uint)NetOpCode.ClientConnected, msg);
             });
         }

+ 52 - 0
Runtime/_NetMsgs/NetAcceptHandshakeMsg.cs

@@ -0,0 +1,52 @@
+using System.Collections;
+using System.Collections.Generic;
+using UnityEngine;
+using Unity.Networking.Transport;
+
+namespace KairoEngine.Multiplayer
+{
+    public class NetAcceptHandshakeMsg : NetMsg
+    {
+        public override uint code { get { return (uint)NetOpCode.AcceptHandshake; }}
+        public string playerName = "";
+        public uint tick = 0;
+        public NetAcceptHandshakeMsg() {}
+
+        public override void Serialize(ref DataStreamWriter writer)
+        {
+            writer.WriteByte((byte)code);
+            writer.WriteFixedString64(playerName);
+            writer.WriteUInt(tick);
+        }
+
+        public override void Deserialize(ref DataStreamReader reader)
+        {
+            playerName = reader.ReadFixedString64().ToString();
+            tick = reader.ReadUInt();
+        }
+
+        public override void SendMessage(ServerBehaviour server, NetMsg netMsg, ref DataStreamWriter writer,  int clientId) 
+        {
+            if(server == null) return;
+            NetAcceptHandshakeMsg netAcceptHandshakeMsg = (NetAcceptHandshakeMsg)netMsg;
+            string text = $"Accepting handshake from client (ID {clientId}, playerName = {netAcceptHandshakeMsg.playerName}, tick = {netAcceptHandshakeMsg.tick})";
+            if(server.debug) Debug.Log(text);
+            NetMsgEvents.Trigger($"{server.eventStreamName}_AcceptingHandshake", text, clientId, code, netAcceptHandshakeMsg);
+            netAcceptHandshakeMsg.Serialize(ref writer);
+        }
+
+        public override NetMsg ReceiveMessage(ClientBehaviour client, ref DataStreamReader stream) 
+        {
+            NetAcceptHandshakeMsg netAcceptHandshakeMsg = new NetAcceptHandshakeMsg();
+            netAcceptHandshakeMsg.Deserialize(ref stream);
+            return netAcceptHandshakeMsg;
+        }
+
+        public override void ReadMessage(ClientBehaviour client) 
+        {
+            string text = $"Server accepted handshake (playerName = {playerName}, tick = {tick})";
+            if(client.debug) Debug.Log(text);
+            NetMsgEvents.Trigger($"{client.eventStreamName}_HandshakeAccepted", text, -1, code, this);
+        }
+    }
+}

+ 11 - 0
Runtime/_NetMsgs/NetAcceptHandshakeMsg.cs.meta

@@ -0,0 +1,11 @@
+fileFormatVersion: 2
+guid: 6b68b32da66d55349a0a8b38e3e65c18
+MonoImporter:
+  externalObjects: {}
+  serializedVersion: 2
+  defaultReferences: []
+  executionOrder: 0
+  icon: {instanceID: 0}
+  userData: 
+  assetBundleName: 
+  assetBundleVariant: 

+ 15 - 15
Runtime/_NetMsgs/NetHandshakeMsg.cs

@@ -22,15 +22,15 @@ namespace KairoEngine.Multiplayer
             playerName = reader.ReadFixedString64().ToString();
         }
 
-        public override void SendMessage(ServerBehaviour server, NetMsg netMsg, ref DataStreamWriter writer,  int clientId) 
-        {
-            if(server == null) return;
-            NetHandshakeMsg netHandshakeMsg = (NetHandshakeMsg)netMsg;
-            string text = $"Accepting handshake from client (ID {clientId}, playerName = {netHandshakeMsg.playerName})";
-            if(server.debug) Debug.Log(text);
-            NetMsgEvents.Trigger($"{server.eventStreamName}_AcceptingHandshake", text, clientId, code, netHandshakeMsg);
-            netHandshakeMsg.Serialize(ref writer);
-        }
+        // public override void SendMessage(ServerBehaviour server, NetMsg netMsg, ref DataStreamWriter writer,  int clientId) 
+        // {
+        //     if(server == null) return;
+        //     NetHandshakeMsg netHandshakeMsg = (NetHandshakeMsg)netMsg;
+        //     string text = $"Accepting handshake from client (ID {clientId}, playerName = {netHandshakeMsg.playerName})";
+        //     if(server.debug) Debug.Log(text);
+        //     NetMsgEvents.Trigger($"{server.eventStreamName}_AcceptingHandshake", text, clientId, code, netHandshakeMsg);
+        //     netHandshakeMsg.Serialize(ref writer);
+        // }
 
         public override void SendMessage(ClientBehaviour client, NetMsg netMsg, ref DataStreamWriter writer ) 
         {
@@ -49,12 +49,12 @@ namespace KairoEngine.Multiplayer
             return netHandshakeMsg;
         }
 
-        public override NetMsg ReceiveMessage(ClientBehaviour client, ref DataStreamReader stream) 
-        {
-            NetHandshakeMsg netHandshakeMsg = new NetHandshakeMsg();
-            netHandshakeMsg.Deserialize(ref stream);
-            return netHandshakeMsg;
-        }
+        // public override NetMsg ReceiveMessage(ClientBehaviour client, ref DataStreamReader stream) 
+        // {
+        //     NetHandshakeMsg netHandshakeMsg = new NetHandshakeMsg();
+        //     netHandshakeMsg.Deserialize(ref stream);
+        //     return netHandshakeMsg;
+        // }
 
         public override void ReadMessage(ServerBehaviour server, int clientId = -1) 
         {