From 763d2c38bf73baa669209857c60e31bb6a9da571 Mon Sep 17 00:00:00 2001 From: Matthew Collier <51029884+Squaresweets@users.noreply.github.com> Date: Thu, 17 Jun 2021 18:45:01 +0100 Subject: [PATCH] Add files via upload --- BubbleTransport/BubbleTransport.cs | 83 ++++++++++++++++++++++++------ 1 file changed, 66 insertions(+), 17 deletions(-) diff --git a/BubbleTransport/BubbleTransport.cs b/BubbleTransport/BubbleTransport.cs index a46aae4..ce5aa3a 100644 --- a/BubbleTransport/BubbleTransport.cs +++ b/BubbleTransport/BubbleTransport.cs @@ -8,14 +8,14 @@ using UnityEngine.iOS; #endif using UnityEngine.SceneManagement; +using Mirror; [DisallowMultipleComponent] -public class BubbleTransport : Mirror.Transport +public class BubbleTransport : Transport { public static BubbleTransport instance; - static bool instanceCreated = false; -#region DllImports + #region DllImports [DllImport("__Internal")] private static extern void _InitGameCenter(); @@ -74,6 +74,20 @@ public class InviteRecievedEvent : UnityEvent { } [SerializeField] private MatchFoundEvent inviteRecieved = new MatchFoundEvent(); + static List> clientMessageBuffer = new List>(); + struct ServerMessage + { + public ArraySegment message; + public int connId; + + public ServerMessage(ArraySegment message, int connId) : this() + { + this.message = message; + this.connId = connId; + } + } + static List serverMessageBuffer = new List(); + bool available = true; @@ -133,7 +147,7 @@ public void FindMatch() public override bool Available() { #if UNITY_IOS - return Application.platform == RuntimePlatform.IPhonePlayer && available && new Version(Device.systemVersion) >= new Version("13.0"); + return Application.platform == RuntimePlatform.IPhonePlayer && available && new System.Version(Device.systemVersion) >= new System.Version("13.0"); #else return false; #endif @@ -146,7 +160,8 @@ public override void ClientConnect(string address) { } //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - public override int GetMaxPacketSize(int channelId = 0) => 16384; + //Sizes from: https://developer.apple.com/library/archive/documentation/NetworkingInternet/Conceptual/GameKit_Guide/Matchmaking/Matchmaking.html + public override int GetMaxPacketSize(int channelId = 0) { return channelId == 0 ? 89088 : 1000; } public override void Shutdown() { @@ -161,12 +176,12 @@ public override void Shutdown() static void OnInviteRecieved() { //An invite has been recieved, we shutdown the network and then call FindMatch - if (Mirror.NetworkManager.singleton.isNetworkActive) + if (NetworkManager.singleton.isNetworkActive) { - if (Mirror.NetworkServer.active) - Mirror.NetworkManager.singleton.StopHost(); + if (NetworkServer.active) + NetworkManager.singleton.StopHost(); else - Mirror.NetworkManager.singleton.StopClient(); + NetworkManager.singleton.StopClient(); } else if(instance.InviteRecievedScene != null && instance.InviteRecievedScene != SceneManager.GetActiveScene().path) { @@ -174,6 +189,7 @@ static void OnInviteRecieved() SceneManager.LoadScene(instance.InviteRecievedScene); return; } + activeTransport = instance; instance.inviteRecieved?.Invoke(); //Numbers do not matter, it instantiates from an invite @@ -200,7 +216,7 @@ public override void ClientDisconnect() public override void ClientSend(int channelId, ArraySegment segment) { - if (!connected) return; + if (!connected || segment.Count > GetMaxPacketSize(channelId)) return; if (channelId > 1) { Debug.LogError("Only channels 0 and 1 are supported"); return; } SendMessageToServer(segment.Array, segment.Offset, segment.Count, channelId); } @@ -222,7 +238,7 @@ static void ClientDisconnectedCallback() [AOT.MonoPInvokeCallback(typeof(OnClientDidDataRecievedDelegate))] static void OnClientDidDataRecieved(IntPtr data, int offset, int count) { - if (!instance.enabled || !instance.connected) + if (!instance.connected) return; /* We get a pointer back from the plugin containing the location of the array of bytes @@ -239,6 +255,13 @@ Objective C code splits this up into offset and count and sends it here */ byte[] _data = new byte[count]; Marshal.Copy(data, _data, 0, count); + + if (!instance.enabled) + { + clientMessageBuffer.Add(new ArraySegment(_data, offset, count)); + return; + } + instance.OnClientDataReceived?.Invoke(new ArraySegment(_data, offset, count), 0); } @@ -267,7 +290,7 @@ public override bool ServerDisconnect(int connectionId) public override void ServerSend(int connectionId, int channelId, ArraySegment segment) { - if (!connected) return; + if (!connected || segment.Count > GetMaxPacketSize(channelId)) return; if (channelId > 1) { Debug.LogError("Only channels 0 and 1 are supported"); return; } SendMessageToClient(connectionId, segment.Array, segment.Offset, segment.Count, channelId); } @@ -291,7 +314,7 @@ static void ServerDisconnectedCallback(int connID) [AOT.MonoPInvokeCallback(typeof(OnServerDidDataRecievedDelegate))] static void OnServerDidDataRecieved(int connId, IntPtr data, int offset, int count) { - if (!instance.enabled || !instance.connected) + if (!instance.connected) return; /* We get a pointer back from the plugin containing the location of the array of bytes @@ -308,6 +331,14 @@ Objective C code splits this up into offset and count and sends it here */ byte[] _data = new byte[count]; Marshal.Copy(data, _data, 0, count); + + if (!instance.enabled) + { + //Stores messages in a buffer to be executed after the scene change + serverMessageBuffer.Add(new ServerMessage(new ArraySegment(_data, offset, count), connId)); + return; + } + instance.OnServerDataReceived?.Invoke(connId, new ArraySegment(_data, offset, count), 0); } @@ -347,19 +378,37 @@ public override void ServerStop() public override void ClientLateUpdate() { + if (instance != this) return; if(needToDisconnectFlag) { OnClientDisconnected?.Invoke(); needToDisconnectFlag = false; } + + //This executes any messages that were not executed during a scene change + for (int i = 0; i > clientMessageBuffer.Count; i++) + { + OnClientDataReceived?.Invoke(clientMessageBuffer[0], 0); + clientMessageBuffer.RemoveAt(0); + } } - private void Awake() + public override void ServerLateUpdate() { - if (!instanceCreated) + if (instance != this) return; + + //This executes any messages that were not executed during a scene change + for (int i = 0; i > serverMessageBuffer.Count; i++) { - instance = this; - instanceCreated = true; + OnServerDataReceived?.Invoke(serverMessageBuffer[0].connId, serverMessageBuffer[0].message, 0); + serverMessageBuffer.RemoveAt(0); } + } + private void Awake() + { + if (instance == null) + instance = this; + else + Destroy(this.gameObject); try {