Skip to content

Commit 763d2c3

Browse files
authored
Add files via upload
1 parent 7916bcf commit 763d2c3

File tree

1 file changed

+66
-17
lines changed

1 file changed

+66
-17
lines changed

BubbleTransport/BubbleTransport.cs

Lines changed: 66 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@
88
using UnityEngine.iOS;
99
#endif
1010
using UnityEngine.SceneManagement;
11+
using Mirror;
1112

1213
[DisallowMultipleComponent]
13-
public class BubbleTransport : Mirror.Transport
14+
public class BubbleTransport : Transport
1415
{
1516
public static BubbleTransport instance;
16-
static bool instanceCreated = false;
1717

18-
#region DllImports
18+
#region DllImports
1919
[DllImport("__Internal")]
2020
private static extern void _InitGameCenter();
2121

@@ -74,6 +74,20 @@ public class InviteRecievedEvent : UnityEvent { }
7474
[SerializeField]
7575
private MatchFoundEvent inviteRecieved = new MatchFoundEvent();
7676

77+
static List<ArraySegment<Byte>> clientMessageBuffer = new List<ArraySegment<byte>>();
78+
struct ServerMessage
79+
{
80+
public ArraySegment<Byte> message;
81+
public int connId;
82+
83+
public ServerMessage(ArraySegment<byte> message, int connId) : this()
84+
{
85+
this.message = message;
86+
this.connId = connId;
87+
}
88+
}
89+
static List<ServerMessage> serverMessageBuffer = new List<ServerMessage>();
90+
7791

7892
bool available = true;
7993

@@ -133,7 +147,7 @@ public void FindMatch()
133147
public override bool Available()
134148
{
135149
#if UNITY_IOS
136-
return Application.platform == RuntimePlatform.IPhonePlayer && available && new Version(Device.systemVersion) >= new Version("13.0");
150+
return Application.platform == RuntimePlatform.IPhonePlayer && available && new System.Version(Device.systemVersion) >= new System.Version("13.0");
137151
#else
138152
return false;
139153
#endif
@@ -146,7 +160,8 @@ public override void ClientConnect(string address) { }
146160

147161
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
148162

149-
public override int GetMaxPacketSize(int channelId = 0) => 16384;
163+
//Sizes from: https://developer.apple.com/library/archive/documentation/NetworkingInternet/Conceptual/GameKit_Guide/Matchmaking/Matchmaking.html
164+
public override int GetMaxPacketSize(int channelId = 0) { return channelId == 0 ? 89088 : 1000; }
150165

151166
public override void Shutdown()
152167
{
@@ -161,19 +176,20 @@ public override void Shutdown()
161176
static void OnInviteRecieved()
162177
{
163178
//An invite has been recieved, we shutdown the network and then call FindMatch
164-
if (Mirror.NetworkManager.singleton.isNetworkActive)
179+
if (NetworkManager.singleton.isNetworkActive)
165180
{
166-
if (Mirror.NetworkServer.active)
167-
Mirror.NetworkManager.singleton.StopHost();
181+
if (NetworkServer.active)
182+
NetworkManager.singleton.StopHost();
168183
else
169-
Mirror.NetworkManager.singleton.StopClient();
184+
NetworkManager.singleton.StopClient();
170185
}
171186
else if(instance.InviteRecievedScene != null && instance.InviteRecievedScene != SceneManager.GetActiveScene().path)
172187
{
173188
SceneManager.sceneLoaded += OnSceneLoaded;
174189
SceneManager.LoadScene(instance.InviteRecievedScene);
175190
return;
176191
}
192+
activeTransport = instance;
177193
instance.inviteRecieved?.Invoke();
178194

179195
//Numbers do not matter, it instantiates from an invite
@@ -200,7 +216,7 @@ public override void ClientDisconnect()
200216

201217
public override void ClientSend(int channelId, ArraySegment<byte> segment)
202218
{
203-
if (!connected) return;
219+
if (!connected || segment.Count > GetMaxPacketSize(channelId)) return;
204220
if (channelId > 1) { Debug.LogError("Only channels 0 and 1 are supported"); return; }
205221
SendMessageToServer(segment.Array, segment.Offset, segment.Count, channelId);
206222
}
@@ -222,7 +238,7 @@ static void ClientDisconnectedCallback()
222238
[AOT.MonoPInvokeCallback(typeof(OnClientDidDataRecievedDelegate))]
223239
static void OnClientDidDataRecieved(IntPtr data, int offset, int count)
224240
{
225-
if (!instance.enabled || !instance.connected)
241+
if (!instance.connected)
226242
return;
227243
/*
228244
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
239255
*/
240256
byte[] _data = new byte[count];
241257
Marshal.Copy(data, _data, 0, count);
258+
259+
if (!instance.enabled)
260+
{
261+
clientMessageBuffer.Add(new ArraySegment<byte>(_data, offset, count));
262+
return;
263+
}
264+
242265
instance.OnClientDataReceived?.Invoke(new ArraySegment<byte>(_data, offset, count), 0);
243266
}
244267

@@ -267,7 +290,7 @@ public override bool ServerDisconnect(int connectionId)
267290

268291
public override void ServerSend(int connectionId, int channelId, ArraySegment<byte> segment)
269292
{
270-
if (!connected) return;
293+
if (!connected || segment.Count > GetMaxPacketSize(channelId)) return;
271294
if (channelId > 1) { Debug.LogError("Only channels 0 and 1 are supported"); return; }
272295
SendMessageToClient(connectionId, segment.Array, segment.Offset, segment.Count, channelId);
273296
}
@@ -291,7 +314,7 @@ static void ServerDisconnectedCallback(int connID)
291314
[AOT.MonoPInvokeCallback(typeof(OnServerDidDataRecievedDelegate))]
292315
static void OnServerDidDataRecieved(int connId, IntPtr data, int offset, int count)
293316
{
294-
if (!instance.enabled || !instance.connected)
317+
if (!instance.connected)
295318
return;
296319
/*
297320
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
308331
*/
309332
byte[] _data = new byte[count];
310333
Marshal.Copy(data, _data, 0, count);
334+
335+
if (!instance.enabled)
336+
{
337+
//Stores messages in a buffer to be executed after the scene change
338+
serverMessageBuffer.Add(new ServerMessage(new ArraySegment<byte>(_data, offset, count), connId));
339+
return;
340+
}
341+
311342
instance.OnServerDataReceived?.Invoke(connId, new ArraySegment<byte>(_data, offset, count), 0);
312343
}
313344

@@ -347,19 +378,37 @@ public override void ServerStop()
347378

348379
public override void ClientLateUpdate()
349380
{
381+
if (instance != this) return;
350382
if(needToDisconnectFlag)
351383
{
352384
OnClientDisconnected?.Invoke();
353385
needToDisconnectFlag = false;
354386
}
387+
388+
//This executes any messages that were not executed during a scene change
389+
for (int i = 0; i > clientMessageBuffer.Count; i++)
390+
{
391+
OnClientDataReceived?.Invoke(clientMessageBuffer[0], 0);
392+
clientMessageBuffer.RemoveAt(0);
393+
}
355394
}
356-
private void Awake()
395+
public override void ServerLateUpdate()
357396
{
358-
if (!instanceCreated)
397+
if (instance != this) return;
398+
399+
//This executes any messages that were not executed during a scene change
400+
for (int i = 0; i > serverMessageBuffer.Count; i++)
359401
{
360-
instance = this;
361-
instanceCreated = true;
402+
OnServerDataReceived?.Invoke(serverMessageBuffer[0].connId, serverMessageBuffer[0].message, 0);
403+
serverMessageBuffer.RemoveAt(0);
362404
}
405+
}
406+
private void Awake()
407+
{
408+
if (instance == null)
409+
instance = this;
410+
else
411+
Destroy(this.gameObject);
363412

364413
try
365414
{

0 commit comments

Comments
 (0)