Skip to content

Commit 49291e2

Browse files
committed
Rewokred byte array usage to internally pass around BitWriter instance
1 parent 0db3230 commit 49291e2

File tree

10 files changed

+586
-127
lines changed

10 files changed

+586
-127
lines changed

MLAPI/MonoBehaviours/Core/NetworkedBehaviour.cs

Lines changed: 497 additions & 23 deletions
Large diffs are not rendered by default.

MLAPI/MonoBehaviours/Core/NetworkedObject.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ internal void RebuildObservers(uint? clientId = null)
185185
writer.WriteUInt(networkId);
186186
writer.WriteBool(observers.Contains(pair.Key));
187187

188-
InternalMessageHandler.Send(pair.Key, "MLAPI_SET_VISIBILITY", "MLAPI_INTERNAL", writer.Finalize(), null);
188+
InternalMessageHandler.Send(pair.Key, "MLAPI_SET_VISIBILITY", "MLAPI_INTERNAL", writer, null);
189189
}
190190
FlushToClient(pair.Key);
191191
}
@@ -362,6 +362,6 @@ internal NetworkedBehaviour GetBehaviourAtOrderIndex(ushort index)
362362
}
363363

364364
//Key: behaviourOrderId, value key: messageType, value value callback
365-
internal Dictionary<ushort, Dictionary<ushort, Action<uint, byte[]>>> targetMessageActions = new Dictionary<ushort, Dictionary<ushort, Action<uint, byte[]>>>();
365+
internal Dictionary<ushort, Dictionary<ushort, Action<uint, BitReader>>> targetMessageActions = new Dictionary<ushort, Dictionary<ushort, Action<uint, BitReader>>>();
366366
}
367367
}

MLAPI/MonoBehaviours/Core/NetworkingManager.cs

Lines changed: 39 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ private object Init(bool server)
224224
Data.Cache.messageAttributeNames = new Dictionary<ulong, string>();
225225
MessageManager.channels = new Dictionary<string, int>();
226226
MessageManager.messageTypes = new Dictionary<string, ushort>();
227-
MessageManager.messageCallbacks = new Dictionary<ushort, Dictionary<int, Action<uint, byte[]>>>();
227+
MessageManager.messageCallbacks = new Dictionary<ushort, Dictionary<int, Action<uint, BitReader>>>();
228228
MessageManager.messageHandlerCounter = new Dictionary<ushort, int>();
229229
MessageManager.releasedMessageHandlerCounters = new Dictionary<ushort, Stack<int>>();
230230
MessageManager.reverseChannels = new Dictionary<int, string>();
@@ -697,7 +697,7 @@ private void Update()
697697
if (NetworkConfig.ConnectionApproval)
698698
writer.WriteByteArray(NetworkConfig.ConnectionData);
699699

700-
InternalMessageHandler.Send(clientId, "MLAPI_CONNECTION_REQUEST", "MLAPI_INTERNAL", writer.Finalize(), null, null, null, true);
700+
InternalMessageHandler.Send(clientId, "MLAPI_CONNECTION_REQUEST", "MLAPI_INTERNAL", writer, null, null, null, true);
701701
}
702702
}
703703
break;
@@ -792,15 +792,14 @@ private void HandleIncomingData(uint clientId, byte[] data, int channelId)
792792

793793
//ushort bytesToRead = reader.ReadUShort();
794794
reader.SkipPadded();
795-
796-
byte[] incommingData = reader.ReadByteArray();
795+
byte[] readBuffer = null;
797796
if (NetworkConfig.EncryptedChannelsHashSet.Contains(MessageManager.reverseChannels[channelId]))
798797
{
799798
//Encrypted message
800799
if (isServer)
801-
incommingData = CryptographyHelper.Decrypt(incommingData, connectedClients[clientId].AesKey);
800+
readBuffer = CryptographyHelper.Decrypt(reader.ReadByteArray(), connectedClients[clientId].AesKey);
802801
else
803-
incommingData = CryptographyHelper.Decrypt(incommingData, clientAesKey);
802+
readBuffer = CryptographyHelper.Decrypt(reader.ReadByteArray(), clientAesKey);
804803
}
805804

806805
if (isServer && isPassthrough && !NetworkConfig.PassthroughMessageHashSet.Contains(messageType))
@@ -827,10 +826,16 @@ private void HandleIncomingData(uint clientId, byte[] data, int channelId)
827826
netIdTarget = targetNetworkId;
828827
netOrderId = networkOrderId;
829828
}
830-
InternalMessageHandler.PassthroughSend(passthroughTarget, clientId, messageType, channelId, incommingData, netIdTarget, netOrderId);
829+
if (readBuffer == null)
830+
readBuffer = reader.ReadByteArray();
831+
InternalMessageHandler.PassthroughSend(passthroughTarget, clientId, messageType, channelId, readBuffer, netIdTarget, netOrderId);
831832
return;
832833
}
833834

835+
BitReader messageReader = reader;
836+
if (readBuffer != null)
837+
messageReader = new BitReader(reader.ReadByteArray());
838+
834839
if (messageType >= 32)
835840
{
836841
#region CUSTOM MESSAGE
@@ -852,16 +857,19 @@ private void HandleIncomingData(uint clientId, byte[] data, int channelId)
852857
Debug.LogWarning("MLAPI: No target found with the given messageType");
853858
return;
854859
}
855-
SpawnManager.spawnedObjects[targetNetworkId].targetMessageActions[networkOrderId][messageType].Invoke(clientId, incommingData);
860+
if (SpawnManager.spawnedObjects[targetNetworkId].targetMessageActions[networkOrderId].ContainsKey(messageType))
861+
{
862+
SpawnManager.spawnedObjects[targetNetworkId].targetMessageActions[networkOrderId][messageType].Invoke(clientId, messageReader);
863+
}
856864
}
857865
else
858866
{
859-
foreach (KeyValuePair<int, Action<uint, byte[]>> pair in MessageManager.messageCallbacks[messageType])
867+
foreach (KeyValuePair<int, Action<uint, BitReader>> pair in MessageManager.messageCallbacks[messageType])
860868
{
861869
if (isPassthrough)
862-
pair.Value(passthroughOrigin, incommingData);
870+
pair.Value(passthroughOrigin, messageReader);
863871
else
864-
pair.Value(clientId, incommingData);
872+
pair.Value(clientId, messageReader);
865873
}
866874
}
867875
#endregion
@@ -874,73 +882,73 @@ private void HandleIncomingData(uint clientId, byte[] data, int channelId)
874882
{
875883
case 0: //Client to server > sends connection buffer
876884
if (isServer)
877-
InternalMessageHandler.HandleConnectionRequest(clientId, incommingData, channelId);
885+
InternalMessageHandler.HandleConnectionRequest(clientId, messageReader, channelId);
878886
break;
879887
case 1: //Server informs client it has been approved:
880888
if (isClient)
881-
InternalMessageHandler.HandleConnectionApproved(clientId, incommingData, channelId);
889+
InternalMessageHandler.HandleConnectionApproved(clientId, messageReader, channelId);
882890
break;
883891
case 2:
884892
//Server informs client another client connected
885893
//MLAPI_ADD_OBJECT
886894
if (isClient)
887-
InternalMessageHandler.HandleAddObject(clientId, incommingData, channelId);
895+
InternalMessageHandler.HandleAddObject(clientId, messageReader, channelId);
888896
break;
889897
case 3:
890898
//Server informs client another client disconnected
891899
//MLAPI_CLIENT_DISCONNECT
892900
if (isClient)
893-
InternalMessageHandler.HandleClientDisconnect(clientId, incommingData, channelId);
901+
InternalMessageHandler.HandleClientDisconnect(clientId, messageReader, channelId);
894902
break;
895903
case 4:
896904
//Server infroms clients to destroy an object
897905
if (isClient)
898-
InternalMessageHandler.HandleDestroyObject(clientId, incommingData, channelId);
906+
InternalMessageHandler.HandleDestroyObject(clientId, messageReader, channelId);
899907
break;
900908
case 5:
901909
//Scene switch
902910
if (isClient)
903-
InternalMessageHandler.HandleSwitchScene(clientId, incommingData, channelId);
911+
InternalMessageHandler.HandleSwitchScene(clientId, messageReader, channelId);
904912
break;
905913
case 6: //Spawn pool object
906914
if (isClient)
907-
InternalMessageHandler.HandleSpawnPoolObject(clientId, incommingData, channelId);
915+
InternalMessageHandler.HandleSpawnPoolObject(clientId, messageReader, channelId);
908916
break;
909917
case 7: //Destroy pool object
910918
if (isClient)
911-
InternalMessageHandler.HandleDestroyPoolObject(clientId, incommingData, channelId);
919+
InternalMessageHandler.HandleDestroyPoolObject(clientId, messageReader, channelId);
912920
break;
913921
case 8: //Change owner
914922
if (isClient)
915-
InternalMessageHandler.HandleChangeOwner(clientId, incommingData, channelId);
923+
InternalMessageHandler.HandleChangeOwner(clientId, messageReader, channelId);
916924
break;
917925
case 9: //Syncvar
918926
if (isClient)
919-
InternalMessageHandler.HandleSyncVarUpdate(clientId, incommingData, channelId);
927+
InternalMessageHandler.HandleSyncVarUpdate(clientId, messageReader, channelId);
920928
break;
921929
case 10:
922930
if (isClient) //MLAPI_ADD_OBJECTS (plural)
923-
InternalMessageHandler.HandleAddObjects(clientId, incommingData, channelId);
931+
InternalMessageHandler.HandleAddObjects(clientId, messageReader, channelId);
924932
break;
925933
case 11:
926934
if (isClient)
927-
InternalMessageHandler.HandleTimeSync(clientId, incommingData, channelId);
935+
InternalMessageHandler.HandleTimeSync(clientId, messageReader, channelId);
928936
break;
929937
case 12:
930938
if (isServer)
931-
InternalMessageHandler.HandleCommand(clientId, incommingData, channelId);
939+
InternalMessageHandler.HandleCommand(clientId, messageReader, channelId);
932940
break;
933941
case 13:
934942
if (isClient)
935-
InternalMessageHandler.HandleRpc(clientId, incommingData, channelId);
943+
InternalMessageHandler.HandleRpc(clientId, messageReader, channelId);
936944
break;
937945
case 14:
938946
if (isClient)
939-
InternalMessageHandler.HandleTargetRpc(clientId, incommingData, channelId);
947+
InternalMessageHandler.HandleTargetRpc(clientId, messageReader, channelId);
940948
break;
941949
case 15:
942950
if (isClient)
943-
InternalMessageHandler.HandleSetVisibility(clientId, incommingData, channelId);
951+
InternalMessageHandler.HandleSetVisibility(clientId, messageReader, channelId);
944952
break;
945953
}
946954
#endregion
@@ -994,7 +1002,7 @@ internal void OnClientDisconnect(uint clientId)
9941002
using (BitWriter writer = new BitWriter())
9951003
{
9961004
writer.WriteUInt(clientId);
997-
InternalMessageHandler.Send("MLAPI_CLIENT_DISCONNECT", "MLAPI_INTERNAL", writer.Finalize(), clientId, null);
1005+
InternalMessageHandler.Send("MLAPI_CLIENT_DISCONNECT", "MLAPI_INTERNAL", writer, clientId, null);
9981006
}
9991007
}
10001008
}
@@ -1006,10 +1014,7 @@ private void SyncTime()
10061014
writer.WriteFloat(NetworkTime);
10071015
int timestamp = NetworkConfig.NetworkTransport.GetNetworkTimestamp();
10081016
writer.WriteInt(timestamp);
1009-
1010-
byte[] buffer = writer.Finalize();
1011-
foreach (KeyValuePair<uint, NetworkedClient> pair in connectedClients)
1012-
InternalMessageHandler.Send("MLAPI_TIME_SYNC", "MLAPI_TIME_SYNC", buffer, null);
1017+
InternalMessageHandler.Send("MLAPI_TIME_SYNC", "MLAPI_TIME_SYNC", writer, null);
10131018
}
10141019
}
10151020

@@ -1112,7 +1117,7 @@ internal void HandleApproval(uint clientId, bool approved, Vector3 position, Qua
11121117
}
11131118
}
11141119

1115-
InternalMessageHandler.Send(clientId, "MLAPI_CONNECTION_APPROVED", "MLAPI_INTERNAL", writer.Finalize(), null, null, null, true);
1120+
InternalMessageHandler.Send(clientId, "MLAPI_CONNECTION_APPROVED", "MLAPI_INTERNAL", writer, null, null, null, true);
11161121

11171122
if (OnClientConnectedCallback != null)
11181123
OnClientConnectedCallback.Invoke(clientId);
@@ -1146,7 +1151,7 @@ internal void HandleApproval(uint clientId, bool approved, Vector3 position, Qua
11461151
writer.WriteUInt(clientId);
11471152
}
11481153

1149-
InternalMessageHandler.Send("MLAPI_ADD_OBJECT", "MLAPI_INTERNAL", writer.Finalize(), clientId, null);
1154+
InternalMessageHandler.Send("MLAPI_ADD_OBJECT", "MLAPI_INTERNAL", writer, clientId, null);
11501155
}
11511156
}
11521157
else

MLAPI/NetworkingManagerComponents/Binary/BitWriter.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public Partial(byte value, byte count)
2323
this.count = count;
2424
}
2525
}
26+
2627
private static readonly Queue<List<object>> listPool = new Queue<List<object>>();
2728

2829
private static readonly float[] holder_f = new float[1];
@@ -120,6 +121,13 @@ private void Push<T>(T b)
120121
public void WriteIntArray(int[] i, bool known = false) => PushArray(i, known);
121122
public void WriteLongArray(long[] l, bool known = false) => PushArray(l, known);
122123
public void WriteBits(byte value, int bits) => Push(new Partial(ReadNBits(value, 0, bits % 8), (byte)(bits%8))); // Suggestion: store (bits % 8) result?
124+
public void WriteWriter(BitWriter writer)
125+
{
126+
for (int i = 0; i < writer.collect.Count; i++)
127+
{
128+
Push(writer.collect[i]);
129+
}
130+
}
123131

124132
private void PushArray<T>(T[] t, bool knownSize = false)
125133
{

0 commit comments

Comments
 (0)