88using MLAPI . Logging ;
99using UnityEngine . SceneManagement ;
1010using System . IO ;
11- using MLAPI . Collections ;
1211using MLAPI . Components ;
1312using MLAPI . Configuration ;
1413using MLAPI . Cryptography ;
1817using MLAPI . Transports ;
1918using MLAPI . Transports . UNET ;
2019using BitStream = MLAPI . Serialization . BitStream ;
21- using System . Runtime . CompilerServices ;
2220using System . Security . Cryptography . X509Certificates ;
23- using System . Text ;
2421
2522namespace MLAPI
2623{
@@ -160,7 +157,7 @@ internal void InvokeOnIncommingCustomMessage(uint clientId, Stream stream)
160157 /// <param name="clientIds">The clients to send to, sends to everyone if null</param>
161158 /// <param name="stream">The message stream containing the data</param>
162159 /// <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" )
164161 {
165162 if ( ! isServer )
166163 {
@@ -171,14 +168,14 @@ public void SendCustomMessage(List<uint> clientIds, Stream stream, string channe
171168 {
172169 for ( int i = 0 ; i < ConnectedClientsList . Count ; i ++ )
173170 {
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 ) ;
175172 }
176173 }
177174 else
178175 {
179176 for ( int i = 0 ; i < clientIds . Count ; i ++ )
180177 {
181- InternalMessageHandler . Send ( clientIds [ i ] , MLAPIConstants . MLAPI_CUSTOM_MESSAGE , channel , stream ) ;
178+ InternalMessageHandler . Send ( clientIds [ i ] , MLAPIConstants . MLAPI_CUSTOM_MESSAGE , channel , stream , SecuritySendFlags . None ) ;
182179 }
183180 }
184181 }
@@ -189,9 +186,9 @@ public void SendCustomMessage(List<uint> clientIds, Stream stream, string channe
189186 /// <param name="clientId">The client to send the message to</param>
190187 /// <param name="stream">The message stream containing the data</param>
191188 /// <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" )
193190 {
194- InternalMessageHandler . Send ( clientId , MLAPIConstants . MLAPI_CUSTOM_MESSAGE , channel , stream ) ;
191+ InternalMessageHandler . Send ( clientId , MLAPIConstants . MLAPI_CUSTOM_MESSAGE , channel , stream , SecuritySendFlags . None ) ;
195192 }
196193
197194 private void OnValidate ( )
@@ -696,7 +693,7 @@ private void Update()
696693 }
697694 }
698695 // 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 ) ;
700697 }
701698 }
702699 else
@@ -786,7 +783,7 @@ internal void SendConnectionRequest()
786783 writer . WriteByteArray ( NetworkConfig . ConnectionData ) ;
787784 }
788785
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 ) ;
790787 }
791788 }
792789
@@ -810,13 +807,20 @@ private IEnumerator ApprovalTimeout(uint clientId)
810807 private void HandleIncomingData ( uint clientId , byte [ ] data , int channelId , int totalSize )
811808 {
812809 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 ) )
814812 {
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 ) )
817819 {
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+ }
820824 uint headerByteSize = ( uint ) Arithmetic . VarIntSize ( messageType ) ;
821825 NetworkProfiler . StartEvent ( TickType . Receive , ( uint ) ( totalSize - headerByteSize ) , channelId , messageType ) ;
822826
@@ -836,64 +840,64 @@ private void HandleIncomingData(uint clientId, byte[] data, int channelId, int t
836840 {
837841 case MLAPIConstants . MLAPI_CONNECTION_REQUEST :
838842 if ( isServer )
839- InternalMessageHandler . HandleConnectionRequest ( clientId , stream , channelId ) ;
843+ InternalMessageHandler . HandleConnectionRequest ( clientId , messageStream , channelId ) ;
840844 break ;
841845 case MLAPIConstants . MLAPI_CONNECTION_APPROVED :
842846 if ( isClient )
843- InternalMessageHandler . HandleConnectionApproved ( clientId , stream , channelId ) ;
847+ InternalMessageHandler . HandleConnectionApproved ( clientId , messageStream , channelId ) ;
844848 break ;
845849 case MLAPIConstants . MLAPI_ADD_OBJECT :
846- if ( isClient ) InternalMessageHandler . HandleAddObject ( clientId , stream , channelId ) ;
850+ if ( isClient ) InternalMessageHandler . HandleAddObject ( clientId , messageStream , channelId ) ;
847851 break ;
848852 case MLAPIConstants . MLAPI_CLIENT_DISCONNECT :
849853 if ( isClient )
850- InternalMessageHandler . HandleClientDisconnect ( clientId , stream , channelId ) ;
854+ InternalMessageHandler . HandleClientDisconnect ( clientId , messageStream , channelId ) ;
851855 break ;
852856 case MLAPIConstants . MLAPI_DESTROY_OBJECT :
853- if ( isClient ) InternalMessageHandler . HandleDestroyObject ( clientId , stream , channelId ) ;
857+ if ( isClient ) InternalMessageHandler . HandleDestroyObject ( clientId , messageStream , channelId ) ;
854858 break ;
855859 case MLAPIConstants . MLAPI_SWITCH_SCENE :
856- if ( isClient ) InternalMessageHandler . HandleSwitchScene ( clientId , stream , channelId ) ;
860+ if ( isClient ) InternalMessageHandler . HandleSwitchScene ( clientId , messageStream , channelId ) ;
857861 break ;
858862 case MLAPIConstants . MLAPI_SPAWN_POOL_OBJECT :
859- if ( isClient ) InternalMessageHandler . HandleSpawnPoolObject ( clientId , stream , channelId ) ;
863+ if ( isClient ) InternalMessageHandler . HandleSpawnPoolObject ( clientId , messageStream , channelId ) ;
860864 break ;
861865 case MLAPIConstants . MLAPI_DESTROY_POOL_OBJECT :
862866 if ( isClient )
863- InternalMessageHandler . HandleDestroyPoolObject ( clientId , stream , channelId ) ;
867+ InternalMessageHandler . HandleDestroyPoolObject ( clientId , messageStream , channelId ) ;
864868 break ;
865869 case MLAPIConstants . MLAPI_CHANGE_OWNER :
866- if ( isClient ) InternalMessageHandler . HandleChangeOwner ( clientId , stream , channelId ) ;
870+ if ( isClient ) InternalMessageHandler . HandleChangeOwner ( clientId , messageStream , channelId ) ;
867871 break ;
868872 case MLAPIConstants . MLAPI_ADD_OBJECTS :
869- if ( isClient ) InternalMessageHandler . HandleAddObjects ( clientId , stream , channelId ) ;
873+ if ( isClient ) InternalMessageHandler . HandleAddObjects ( clientId , messageStream , channelId ) ;
870874 break ;
871875 case MLAPIConstants . MLAPI_TIME_SYNC :
872- if ( isClient ) InternalMessageHandler . HandleTimeSync ( clientId , stream , channelId ) ;
876+ if ( isClient ) InternalMessageHandler . HandleTimeSync ( clientId , messageStream , channelId ) ;
873877 break ;
874878 case MLAPIConstants . MLAPI_NETWORKED_VAR_DELTA :
875- InternalMessageHandler . HandleNetworkedVarDelta ( clientId , stream , channelId ) ;
879+ InternalMessageHandler . HandleNetworkedVarDelta ( clientId , messageStream , channelId ) ;
876880 break ;
877881 case MLAPIConstants . MLAPI_NETWORKED_VAR_UPDATE :
878- InternalMessageHandler . HandleNetworkedVarUpdate ( clientId , stream , channelId ) ;
882+ InternalMessageHandler . HandleNetworkedVarUpdate ( clientId , messageStream , channelId ) ;
879883 break ;
880884 case MLAPIConstants . MLAPI_SERVER_RPC :
881- if ( isServer ) InternalMessageHandler . HandleServerRPC ( clientId , stream , channelId ) ;
885+ if ( isServer ) InternalMessageHandler . HandleServerRPC ( clientId , messageStream , channelId ) ;
882886 break ;
883887 case MLAPIConstants . MLAPI_CLIENT_RPC :
884- if ( isClient ) InternalMessageHandler . HandleClientRPC ( clientId , stream , channelId ) ;
888+ if ( isClient ) InternalMessageHandler . HandleClientRPC ( clientId , messageStream , channelId ) ;
885889 break ;
886890 case MLAPIConstants . MLAPI_CUSTOM_MESSAGE :
887- InternalMessageHandler . HandleCustomMessage ( clientId , stream , channelId ) ;
891+ InternalMessageHandler . HandleCustomMessage ( clientId , messageStream , channelId ) ;
888892 break ;
889893 case MLAPIConstants . MLAPI_CERTIFICATE_HAIL :
890- if ( isClient ) InternalMessageHandler . HandleHailRequest ( clientId , stream , channelId ) ;
894+ if ( isClient ) InternalMessageHandler . HandleHailRequest ( clientId , messageStream , channelId ) ;
891895 break ;
892896 case MLAPIConstants . MLAPI_CERTIFICATE_HAIL_RESPONSE :
893- if ( isServer ) InternalMessageHandler . HandleHailResponse ( clientId , stream , channelId ) ;
897+ if ( isServer ) InternalMessageHandler . HandleHailResponse ( clientId , messageStream , channelId ) ;
894898 break ;
895899 case MLAPIConstants . MLAPI_GREETINGS :
896- if ( isClient ) InternalMessageHandler . HandleGreetings ( clientId , stream , channelId ) ;
900+ if ( isClient ) InternalMessageHandler . HandleGreetings ( clientId , messageStream , channelId ) ;
897901 break ;
898902 }
899903
@@ -963,7 +967,7 @@ internal void OnClientDisconnectFromServer(uint clientId)
963967 using ( PooledBitWriter writer = PooledBitWriter . Get ( stream ) )
964968 {
965969 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 ) ;
967971 }
968972 }
969973 }
@@ -979,7 +983,7 @@ private void SyncTime()
979983 writer . WriteSinglePacked ( NetworkTime ) ;
980984 int timestamp = NetworkConfig . NetworkTransport . GetNetworkTimestamp ( ) ;
981985 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 ) ;
983987 }
984988 }
985989 }
@@ -1057,7 +1061,7 @@ internal void HandleApproval(uint clientId, int prefabId, bool approved, Vector3
10571061 }
10581062 }
10591063
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 ) ;
10611065
10621066 if ( OnClientConnectedCallback != null )
10631067 OnClientConnectedCallback . Invoke ( clientId ) ;
@@ -1099,7 +1103,7 @@ internal void HandleApproval(uint clientId, int prefabId, bool approved, Vector3
10991103 {
11001104 writer . WriteUInt32Packed ( clientId ) ;
11011105 }
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 ) ;
11031107 }
11041108 }
11051109 }
0 commit comments