Skip to content

Commit

Permalink
Increased the framerate by adding a message buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
Squaresweets authored Jun 20, 2021
1 parent 4a66e89 commit ee9b96c
Showing 1 changed file with 9 additions and 21 deletions.
30 changes: 9 additions & 21 deletions BubbleTransport/BubbleTransport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public class BubbleTransport : Transport
#endregion

public int MinPlayers = 2;
public int MaxReceivesPerTick = 1000;
[Tooltip("Incase you are in a scene such as a menu when an invite is recieved, set this to the scene where matches normally start")]
[Mirror.Scene]
public string InviteRecievedScene;
Expand Down Expand Up @@ -257,13 +258,7 @@ 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<byte>(_data, offset, count));
return;
}

instance.OnClientDataReceived?.Invoke(new ArraySegment<byte>(_data, offset, count), 0);
clientMessageBuffer.Add(new ArraySegment<byte>(_data, offset, count));
}

delegate void OnClientStartDelegate();
Expand Down Expand Up @@ -334,14 +329,7 @@ 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<byte>(_data, offset, count), connId));
return;
}

instance.OnServerDataReceived?.Invoke(connId, new ArraySegment<byte>(_data, offset, count), 0);
serverMessageBuffer.Add(new ServerMessage(new ArraySegment<byte>(_data, offset, count), connId));
}

/// <summary>
Expand Down Expand Up @@ -381,26 +369,26 @@ public override void ServerStop()

public override void ClientLateUpdate()
{
if (instance != this) return;
if (instance != this || !enabled) 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++)
//This executes any messages that were recieved in the last frame
for (int i = 0; i < clientMessageBuffer.Count && i < MaxReceivesPerTick; i++)
{
OnClientDataReceived?.Invoke(clientMessageBuffer[0], 0);
clientMessageBuffer.RemoveAt(0);
}
}
public override void ServerLateUpdate()
{
if (instance != this) return;
if (instance != this || !enabled) return;

//This executes any messages that were not executed during a scene change
for (int i = 0; i < serverMessageBuffer.Count; i++)
//This executes any messages that were recieved in the last frame
for (int i = 0; i < serverMessageBuffer.Count && i < MaxReceivesPerTick; i++)
{
OnServerDataReceived?.Invoke(serverMessageBuffer[0].connId, serverMessageBuffer[0].message, 0);
serverMessageBuffer.RemoveAt(0);
Expand Down

0 comments on commit ee9b96c

Please sign in to comment.