8
8
using MLAPI . Logging ;
9
9
using UnityEngine . SceneManagement ;
10
10
using System . IO ;
11
- using MLAPI . Collections ;
12
11
using MLAPI . Components ;
13
12
using MLAPI . Configuration ;
14
13
using MLAPI . Cryptography ;
18
17
using MLAPI . Transports ;
19
18
using MLAPI . Transports . UNET ;
20
19
using BitStream = MLAPI . Serialization . BitStream ;
21
- using System . Runtime . CompilerServices ;
22
20
using System . Security . Cryptography . X509Certificates ;
23
- using System . Text ;
24
21
25
22
namespace MLAPI
26
23
{
@@ -160,7 +157,7 @@ internal void InvokeOnIncommingCustomMessage(uint clientId, Stream stream)
160
157
/// <param name="clientIds">The clients to send to, sends to everyone if null</param>
161
158
/// <param name="stream">The message stream containing the data</param>
162
159
/// <param name="channel">The channel to send the data on</param>
163
- public void SendCustomMessage ( List < uint > clientIds , Stream stream , string channel = "MLAPI_DEFAULT_MESSAGE" )
160
+ public void SendCustomMessage ( List < uint > clientIds , BitStream stream , string channel = "MLAPI_DEFAULT_MESSAGE" )
164
161
{
165
162
if ( ! isServer )
166
163
{
@@ -171,14 +168,14 @@ public void SendCustomMessage(List<uint> clientIds, Stream stream, string channe
171
168
{
172
169
for ( int i = 0 ; i < ConnectedClientsList . Count ; i ++ )
173
170
{
174
- InternalMessageHandler . Send ( ConnectedClientsList [ i ] . ClientId , MLAPIConstants . MLAPI_CUSTOM_MESSAGE , channel , stream ) ;
171
+ InternalMessageHandler . Send ( ConnectedClientsList [ i ] . ClientId , MLAPIConstants . MLAPI_CUSTOM_MESSAGE , channel , stream , SecuritySendFlags . None ) ;
175
172
}
176
173
}
177
174
else
178
175
{
179
176
for ( int i = 0 ; i < clientIds . Count ; i ++ )
180
177
{
181
- InternalMessageHandler . Send ( clientIds [ i ] , MLAPIConstants . MLAPI_CUSTOM_MESSAGE , channel , stream ) ;
178
+ InternalMessageHandler . Send ( clientIds [ i ] , MLAPIConstants . MLAPI_CUSTOM_MESSAGE , channel , stream , SecuritySendFlags . None ) ;
182
179
}
183
180
}
184
181
}
@@ -189,9 +186,9 @@ public void SendCustomMessage(List<uint> clientIds, Stream stream, string channe
189
186
/// <param name="clientId">The client to send the message to</param>
190
187
/// <param name="stream">The message stream containing the data</param>
191
188
/// <param name="channel">The channel tos end the data on</param>
192
- public void SendCustomMessage ( uint clientId , Stream stream , string channel = "MLAPI_DEFAULT_MESSAGE" )
189
+ public void SendCustomMessage ( uint clientId , BitStream stream , string channel = "MLAPI_DEFAULT_MESSAGE" )
193
190
{
194
- InternalMessageHandler . Send ( clientId , MLAPIConstants . MLAPI_CUSTOM_MESSAGE , channel , stream ) ;
191
+ InternalMessageHandler . Send ( clientId , MLAPIConstants . MLAPI_CUSTOM_MESSAGE , channel , stream , SecuritySendFlags . None ) ;
195
192
}
196
193
197
194
private void OnValidate ( )
@@ -696,7 +693,7 @@ private void Update()
696
693
}
697
694
}
698
695
// Send the hail
699
- InternalMessageHandler . Send ( clientId , MLAPIConstants . MLAPI_CERTIFICATE_HAIL , "MLAPI_INTERNAL" , hailStream , true ) ;
696
+ InternalMessageHandler . Send ( clientId , MLAPIConstants . MLAPI_CERTIFICATE_HAIL , "MLAPI_INTERNAL" , hailStream , SecuritySendFlags . None , true ) ;
700
697
}
701
698
}
702
699
else
@@ -786,7 +783,7 @@ internal void SendConnectionRequest()
786
783
writer . WriteByteArray ( NetworkConfig . ConnectionData ) ;
787
784
}
788
785
789
- InternalMessageHandler . Send ( ServerClientId , MLAPIConstants . MLAPI_CONNECTION_REQUEST , "MLAPI_INTERNAL" , stream , true ) ;
786
+ InternalMessageHandler . Send ( ServerClientId , MLAPIConstants . MLAPI_CONNECTION_REQUEST , "MLAPI_INTERNAL" , stream , SecuritySendFlags . Authenticated | SecuritySendFlags . Encrypted , true ) ;
790
787
}
791
788
}
792
789
@@ -810,13 +807,20 @@ private IEnumerator ApprovalTimeout(uint clientId)
810
807
private void HandleIncomingData ( uint clientId , byte [ ] data , int channelId , int totalSize )
811
808
{
812
809
if ( LogHelper . CurrentLogLevel <= LogLevel . Developer ) LogHelper . LogInfo ( "Unwrapping Data Header" ) ;
813
- using ( BitStream stream = new BitStream ( data ) )
810
+
811
+ using ( BitStream inputStream = new BitStream ( data ) )
814
812
{
815
- stream . SetLength ( totalSize ) ;
816
- using ( PooledBitReader reader = PooledBitReader . Get ( stream ) )
813
+ inputStream . SetLength ( totalSize ) ;
814
+ //Debug.LogError(totalSize);
815
+ //string s = string.Join(" ", data.Take(totalSize).Select(b => b.ToString()).ToArray());
816
+ //Debug.LogError(s);
817
+ byte messageType ;
818
+ using ( BitStream messageStream = MessageManager . UnwrapMessage ( inputStream , clientId , out messageType ) )
817
819
{
818
- byte messageType = reader . ReadByteDirect ( ) ;
819
-
820
+ if ( messageStream == null )
821
+ {
822
+ if ( LogHelper . CurrentLogLevel <= LogLevel . Normal ) LogHelper . LogInfo ( "Message unwrap could not be completed. Was the header corrupt? Crypto error?" ) ;
823
+ }
820
824
uint headerByteSize = ( uint ) Arithmetic . VarIntSize ( messageType ) ;
821
825
NetworkProfiler . StartEvent ( TickType . Receive , ( uint ) ( totalSize - headerByteSize ) , channelId , messageType ) ;
822
826
@@ -836,64 +840,64 @@ private void HandleIncomingData(uint clientId, byte[] data, int channelId, int t
836
840
{
837
841
case MLAPIConstants . MLAPI_CONNECTION_REQUEST :
838
842
if ( isServer )
839
- InternalMessageHandler . HandleConnectionRequest ( clientId , stream , channelId ) ;
843
+ InternalMessageHandler . HandleConnectionRequest ( clientId , messageStream , channelId ) ;
840
844
break ;
841
845
case MLAPIConstants . MLAPI_CONNECTION_APPROVED :
842
846
if ( isClient )
843
- InternalMessageHandler . HandleConnectionApproved ( clientId , stream , channelId ) ;
847
+ InternalMessageHandler . HandleConnectionApproved ( clientId , messageStream , channelId ) ;
844
848
break ;
845
849
case MLAPIConstants . MLAPI_ADD_OBJECT :
846
- if ( isClient ) InternalMessageHandler . HandleAddObject ( clientId , stream , channelId ) ;
850
+ if ( isClient ) InternalMessageHandler . HandleAddObject ( clientId , messageStream , channelId ) ;
847
851
break ;
848
852
case MLAPIConstants . MLAPI_CLIENT_DISCONNECT :
849
853
if ( isClient )
850
- InternalMessageHandler . HandleClientDisconnect ( clientId , stream , channelId ) ;
854
+ InternalMessageHandler . HandleClientDisconnect ( clientId , messageStream , channelId ) ;
851
855
break ;
852
856
case MLAPIConstants . MLAPI_DESTROY_OBJECT :
853
- if ( isClient ) InternalMessageHandler . HandleDestroyObject ( clientId , stream , channelId ) ;
857
+ if ( isClient ) InternalMessageHandler . HandleDestroyObject ( clientId , messageStream , channelId ) ;
854
858
break ;
855
859
case MLAPIConstants . MLAPI_SWITCH_SCENE :
856
- if ( isClient ) InternalMessageHandler . HandleSwitchScene ( clientId , stream , channelId ) ;
860
+ if ( isClient ) InternalMessageHandler . HandleSwitchScene ( clientId , messageStream , channelId ) ;
857
861
break ;
858
862
case MLAPIConstants . MLAPI_SPAWN_POOL_OBJECT :
859
- if ( isClient ) InternalMessageHandler . HandleSpawnPoolObject ( clientId , stream , channelId ) ;
863
+ if ( isClient ) InternalMessageHandler . HandleSpawnPoolObject ( clientId , messageStream , channelId ) ;
860
864
break ;
861
865
case MLAPIConstants . MLAPI_DESTROY_POOL_OBJECT :
862
866
if ( isClient )
863
- InternalMessageHandler . HandleDestroyPoolObject ( clientId , stream , channelId ) ;
867
+ InternalMessageHandler . HandleDestroyPoolObject ( clientId , messageStream , channelId ) ;
864
868
break ;
865
869
case MLAPIConstants . MLAPI_CHANGE_OWNER :
866
- if ( isClient ) InternalMessageHandler . HandleChangeOwner ( clientId , stream , channelId ) ;
870
+ if ( isClient ) InternalMessageHandler . HandleChangeOwner ( clientId , messageStream , channelId ) ;
867
871
break ;
868
872
case MLAPIConstants . MLAPI_ADD_OBJECTS :
869
- if ( isClient ) InternalMessageHandler . HandleAddObjects ( clientId , stream , channelId ) ;
873
+ if ( isClient ) InternalMessageHandler . HandleAddObjects ( clientId , messageStream , channelId ) ;
870
874
break ;
871
875
case MLAPIConstants . MLAPI_TIME_SYNC :
872
- if ( isClient ) InternalMessageHandler . HandleTimeSync ( clientId , stream , channelId ) ;
876
+ if ( isClient ) InternalMessageHandler . HandleTimeSync ( clientId , messageStream , channelId ) ;
873
877
break ;
874
878
case MLAPIConstants . MLAPI_NETWORKED_VAR_DELTA :
875
- InternalMessageHandler . HandleNetworkedVarDelta ( clientId , stream , channelId ) ;
879
+ InternalMessageHandler . HandleNetworkedVarDelta ( clientId , messageStream , channelId ) ;
876
880
break ;
877
881
case MLAPIConstants . MLAPI_NETWORKED_VAR_UPDATE :
878
- InternalMessageHandler . HandleNetworkedVarUpdate ( clientId , stream , channelId ) ;
882
+ InternalMessageHandler . HandleNetworkedVarUpdate ( clientId , messageStream , channelId ) ;
879
883
break ;
880
884
case MLAPIConstants . MLAPI_SERVER_RPC :
881
- if ( isServer ) InternalMessageHandler . HandleServerRPC ( clientId , stream , channelId ) ;
885
+ if ( isServer ) InternalMessageHandler . HandleServerRPC ( clientId , messageStream , channelId ) ;
882
886
break ;
883
887
case MLAPIConstants . MLAPI_CLIENT_RPC :
884
- if ( isClient ) InternalMessageHandler . HandleClientRPC ( clientId , stream , channelId ) ;
888
+ if ( isClient ) InternalMessageHandler . HandleClientRPC ( clientId , messageStream , channelId ) ;
885
889
break ;
886
890
case MLAPIConstants . MLAPI_CUSTOM_MESSAGE :
887
- InternalMessageHandler . HandleCustomMessage ( clientId , stream , channelId ) ;
891
+ InternalMessageHandler . HandleCustomMessage ( clientId , messageStream , channelId ) ;
888
892
break ;
889
893
case MLAPIConstants . MLAPI_CERTIFICATE_HAIL :
890
- if ( isClient ) InternalMessageHandler . HandleHailRequest ( clientId , stream , channelId ) ;
894
+ if ( isClient ) InternalMessageHandler . HandleHailRequest ( clientId , messageStream , channelId ) ;
891
895
break ;
892
896
case MLAPIConstants . MLAPI_CERTIFICATE_HAIL_RESPONSE :
893
- if ( isServer ) InternalMessageHandler . HandleHailResponse ( clientId , stream , channelId ) ;
897
+ if ( isServer ) InternalMessageHandler . HandleHailResponse ( clientId , messageStream , channelId ) ;
894
898
break ;
895
899
case MLAPIConstants . MLAPI_GREETINGS :
896
- if ( isClient ) InternalMessageHandler . HandleGreetings ( clientId , stream , channelId ) ;
900
+ if ( isClient ) InternalMessageHandler . HandleGreetings ( clientId , messageStream , channelId ) ;
897
901
break ;
898
902
}
899
903
@@ -963,7 +967,7 @@ internal void OnClientDisconnectFromServer(uint clientId)
963
967
using ( PooledBitWriter writer = PooledBitWriter . Get ( stream ) )
964
968
{
965
969
writer . WriteUInt32Packed ( clientId ) ;
966
- InternalMessageHandler . Send ( MLAPIConstants . MLAPI_CLIENT_DISCONNECT , "MLAPI_INTERNAL" , clientId , stream ) ;
970
+ InternalMessageHandler . Send ( MLAPIConstants . MLAPI_CLIENT_DISCONNECT , "MLAPI_INTERNAL" , clientId , stream , SecuritySendFlags . None ) ;
967
971
}
968
972
}
969
973
}
@@ -979,7 +983,7 @@ private void SyncTime()
979
983
writer . WriteSinglePacked ( NetworkTime ) ;
980
984
int timestamp = NetworkConfig . NetworkTransport . GetNetworkTimestamp ( ) ;
981
985
writer . WriteInt32Packed ( timestamp ) ;
982
- InternalMessageHandler . Send ( MLAPIConstants . MLAPI_TIME_SYNC , "MLAPI_TIME_SYNC" , stream ) ;
986
+ InternalMessageHandler . Send ( MLAPIConstants . MLAPI_TIME_SYNC , "MLAPI_TIME_SYNC" , stream , SecuritySendFlags . None ) ;
983
987
}
984
988
}
985
989
}
@@ -1057,7 +1061,7 @@ internal void HandleApproval(uint clientId, int prefabId, bool approved, Vector3
1057
1061
}
1058
1062
}
1059
1063
1060
- InternalMessageHandler . Send ( clientId , MLAPIConstants . MLAPI_CONNECTION_APPROVED , "MLAPI_INTERNAL" , stream , true ) ;
1064
+ InternalMessageHandler . Send ( clientId , MLAPIConstants . MLAPI_CONNECTION_APPROVED , "MLAPI_INTERNAL" , stream , SecuritySendFlags . Encrypted | SecuritySendFlags . Authenticated , true ) ;
1061
1065
1062
1066
if ( OnClientConnectedCallback != null )
1063
1067
OnClientConnectedCallback . Invoke ( clientId ) ;
@@ -1099,7 +1103,7 @@ internal void HandleApproval(uint clientId, int prefabId, bool approved, Vector3
1099
1103
{
1100
1104
writer . WriteUInt32Packed ( clientId ) ;
1101
1105
}
1102
- InternalMessageHandler . Send ( clientPair . Key , MLAPIConstants . MLAPI_ADD_OBJECT , "MLAPI_INTERNAL" , stream ) ;
1106
+ InternalMessageHandler . Send ( clientPair . Key , MLAPIConstants . MLAPI_ADD_OBJECT , "MLAPI_INTERNAL" , stream , SecuritySendFlags . None ) ;
1103
1107
}
1104
1108
}
1105
1109
}
0 commit comments