@@ -224,7 +224,7 @@ private object Init(bool server)
224
224
Data . Cache . messageAttributeNames = new Dictionary < ulong , string > ( ) ;
225
225
MessageManager . channels = new Dictionary < string , int > ( ) ;
226
226
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 > > > ( ) ;
228
228
MessageManager . messageHandlerCounter = new Dictionary < ushort , int > ( ) ;
229
229
MessageManager . releasedMessageHandlerCounters = new Dictionary < ushort , Stack < int > > ( ) ;
230
230
MessageManager . reverseChannels = new Dictionary < int , string > ( ) ;
@@ -697,7 +697,7 @@ private void Update()
697
697
if ( NetworkConfig . ConnectionApproval )
698
698
writer . WriteByteArray ( NetworkConfig . ConnectionData ) ;
699
699
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 ) ;
701
701
}
702
702
}
703
703
break ;
@@ -792,15 +792,14 @@ private void HandleIncomingData(uint clientId, byte[] data, int channelId)
792
792
793
793
//ushort bytesToRead = reader.ReadUShort();
794
794
reader . SkipPadded ( ) ;
795
-
796
- byte [ ] incommingData = reader . ReadByteArray ( ) ;
795
+ byte [ ] readBuffer = null ;
797
796
if ( NetworkConfig . EncryptedChannelsHashSet . Contains ( MessageManager . reverseChannels [ channelId ] ) )
798
797
{
799
798
//Encrypted message
800
799
if ( isServer )
801
- incommingData = CryptographyHelper . Decrypt ( incommingData , connectedClients [ clientId ] . AesKey ) ;
800
+ readBuffer = CryptographyHelper . Decrypt ( reader . ReadByteArray ( ) , connectedClients [ clientId ] . AesKey ) ;
802
801
else
803
- incommingData = CryptographyHelper . Decrypt ( incommingData , clientAesKey ) ;
802
+ readBuffer = CryptographyHelper . Decrypt ( reader . ReadByteArray ( ) , clientAesKey ) ;
804
803
}
805
804
806
805
if ( isServer && isPassthrough && ! NetworkConfig . PassthroughMessageHashSet . Contains ( messageType ) )
@@ -827,10 +826,16 @@ private void HandleIncomingData(uint clientId, byte[] data, int channelId)
827
826
netIdTarget = targetNetworkId ;
828
827
netOrderId = networkOrderId ;
829
828
}
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 ) ;
831
832
return ;
832
833
}
833
834
835
+ BitReader messageReader = reader ;
836
+ if ( readBuffer != null )
837
+ messageReader = new BitReader ( reader . ReadByteArray ( ) ) ;
838
+
834
839
if ( messageType >= 32 )
835
840
{
836
841
#region CUSTOM MESSAGE
@@ -852,16 +857,19 @@ private void HandleIncomingData(uint clientId, byte[] data, int channelId)
852
857
Debug . LogWarning ( "MLAPI: No target found with the given messageType" ) ;
853
858
return ;
854
859
}
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
+ }
856
864
}
857
865
else
858
866
{
859
- foreach ( KeyValuePair < int , Action < uint , byte [ ] > > pair in MessageManager . messageCallbacks [ messageType ] )
867
+ foreach ( KeyValuePair < int , Action < uint , BitReader > > pair in MessageManager . messageCallbacks [ messageType ] )
860
868
{
861
869
if ( isPassthrough )
862
- pair . Value ( passthroughOrigin , incommingData ) ;
870
+ pair . Value ( passthroughOrigin , messageReader ) ;
863
871
else
864
- pair . Value ( clientId , incommingData ) ;
872
+ pair . Value ( clientId , messageReader ) ;
865
873
}
866
874
}
867
875
#endregion
@@ -874,73 +882,73 @@ private void HandleIncomingData(uint clientId, byte[] data, int channelId)
874
882
{
875
883
case 0 : //Client to server > sends connection buffer
876
884
if ( isServer )
877
- InternalMessageHandler . HandleConnectionRequest ( clientId , incommingData , channelId ) ;
885
+ InternalMessageHandler . HandleConnectionRequest ( clientId , messageReader , channelId ) ;
878
886
break ;
879
887
case 1 : //Server informs client it has been approved:
880
888
if ( isClient )
881
- InternalMessageHandler . HandleConnectionApproved ( clientId , incommingData , channelId ) ;
889
+ InternalMessageHandler . HandleConnectionApproved ( clientId , messageReader , channelId ) ;
882
890
break ;
883
891
case 2 :
884
892
//Server informs client another client connected
885
893
//MLAPI_ADD_OBJECT
886
894
if ( isClient )
887
- InternalMessageHandler . HandleAddObject ( clientId , incommingData , channelId ) ;
895
+ InternalMessageHandler . HandleAddObject ( clientId , messageReader , channelId ) ;
888
896
break ;
889
897
case 3 :
890
898
//Server informs client another client disconnected
891
899
//MLAPI_CLIENT_DISCONNECT
892
900
if ( isClient )
893
- InternalMessageHandler . HandleClientDisconnect ( clientId , incommingData , channelId ) ;
901
+ InternalMessageHandler . HandleClientDisconnect ( clientId , messageReader , channelId ) ;
894
902
break ;
895
903
case 4 :
896
904
//Server infroms clients to destroy an object
897
905
if ( isClient )
898
- InternalMessageHandler . HandleDestroyObject ( clientId , incommingData , channelId ) ;
906
+ InternalMessageHandler . HandleDestroyObject ( clientId , messageReader , channelId ) ;
899
907
break ;
900
908
case 5 :
901
909
//Scene switch
902
910
if ( isClient )
903
- InternalMessageHandler . HandleSwitchScene ( clientId , incommingData , channelId ) ;
911
+ InternalMessageHandler . HandleSwitchScene ( clientId , messageReader , channelId ) ;
904
912
break ;
905
913
case 6 : //Spawn pool object
906
914
if ( isClient )
907
- InternalMessageHandler . HandleSpawnPoolObject ( clientId , incommingData , channelId ) ;
915
+ InternalMessageHandler . HandleSpawnPoolObject ( clientId , messageReader , channelId ) ;
908
916
break ;
909
917
case 7 : //Destroy pool object
910
918
if ( isClient )
911
- InternalMessageHandler . HandleDestroyPoolObject ( clientId , incommingData , channelId ) ;
919
+ InternalMessageHandler . HandleDestroyPoolObject ( clientId , messageReader , channelId ) ;
912
920
break ;
913
921
case 8 : //Change owner
914
922
if ( isClient )
915
- InternalMessageHandler . HandleChangeOwner ( clientId , incommingData , channelId ) ;
923
+ InternalMessageHandler . HandleChangeOwner ( clientId , messageReader , channelId ) ;
916
924
break ;
917
925
case 9 : //Syncvar
918
926
if ( isClient )
919
- InternalMessageHandler . HandleSyncVarUpdate ( clientId , incommingData , channelId ) ;
927
+ InternalMessageHandler . HandleSyncVarUpdate ( clientId , messageReader , channelId ) ;
920
928
break ;
921
929
case 10 :
922
930
if ( isClient ) //MLAPI_ADD_OBJECTS (plural)
923
- InternalMessageHandler . HandleAddObjects ( clientId , incommingData , channelId ) ;
931
+ InternalMessageHandler . HandleAddObjects ( clientId , messageReader , channelId ) ;
924
932
break ;
925
933
case 11 :
926
934
if ( isClient )
927
- InternalMessageHandler . HandleTimeSync ( clientId , incommingData , channelId ) ;
935
+ InternalMessageHandler . HandleTimeSync ( clientId , messageReader , channelId ) ;
928
936
break ;
929
937
case 12 :
930
938
if ( isServer )
931
- InternalMessageHandler . HandleCommand ( clientId , incommingData , channelId ) ;
939
+ InternalMessageHandler . HandleCommand ( clientId , messageReader , channelId ) ;
932
940
break ;
933
941
case 13 :
934
942
if ( isClient )
935
- InternalMessageHandler . HandleRpc ( clientId , incommingData , channelId ) ;
943
+ InternalMessageHandler . HandleRpc ( clientId , messageReader , channelId ) ;
936
944
break ;
937
945
case 14 :
938
946
if ( isClient )
939
- InternalMessageHandler . HandleTargetRpc ( clientId , incommingData , channelId ) ;
947
+ InternalMessageHandler . HandleTargetRpc ( clientId , messageReader , channelId ) ;
940
948
break ;
941
949
case 15 :
942
950
if ( isClient )
943
- InternalMessageHandler . HandleSetVisibility ( clientId , incommingData , channelId ) ;
951
+ InternalMessageHandler . HandleSetVisibility ( clientId , messageReader , channelId ) ;
944
952
break ;
945
953
}
946
954
#endregion
@@ -994,7 +1002,7 @@ internal void OnClientDisconnect(uint clientId)
994
1002
using ( BitWriter writer = new BitWriter ( ) )
995
1003
{
996
1004
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 ) ;
998
1006
}
999
1007
}
1000
1008
}
@@ -1006,10 +1014,7 @@ private void SyncTime()
1006
1014
writer . WriteFloat ( NetworkTime ) ;
1007
1015
int timestamp = NetworkConfig . NetworkTransport . GetNetworkTimestamp ( ) ;
1008
1016
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 ) ;
1013
1018
}
1014
1019
}
1015
1020
@@ -1112,7 +1117,7 @@ internal void HandleApproval(uint clientId, bool approved, Vector3 position, Qua
1112
1117
}
1113
1118
}
1114
1119
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 ) ;
1116
1121
1117
1122
if ( OnClientConnectedCallback != null )
1118
1123
OnClientConnectedCallback . Invoke ( clientId ) ;
@@ -1146,7 +1151,7 @@ internal void HandleApproval(uint clientId, bool approved, Vector3 position, Qua
1146
1151
writer . WriteUInt ( clientId ) ;
1147
1152
}
1148
1153
1149
- InternalMessageHandler . Send ( "MLAPI_ADD_OBJECT" , "MLAPI_INTERNAL" , writer . Finalize ( ) , clientId , null ) ;
1154
+ InternalMessageHandler . Send ( "MLAPI_ADD_OBJECT" , "MLAPI_INTERNAL" , writer , clientId , null ) ;
1150
1155
}
1151
1156
}
1152
1157
else
0 commit comments