@@ -81,8 +81,10 @@ internal set
81
81
/// Gets a list of connected clients
82
82
/// </summary>
83
83
public readonly List < NetworkedClient > ConnectedClientsList = new List < NetworkedClient > ( ) ;
84
- internal readonly HashSet < uint > connectionPendingClients = new HashSet < uint > ( ) ;
85
- internal readonly HashSet < uint > hailPendingClients = new HashSet < uint > ( ) ;
84
+ /// <summary>
85
+ /// Gets a dictionary of the clients that have been accepted by the transport but are still pending by the MLAPI.
86
+ /// </summary>
87
+ public readonly Dictionary < uint , PendingClient > PendingClients = new Dictionary < uint , PendingClient > ( ) ;
86
88
/// <summary>
87
89
/// Gets wheter or not a server is running
88
90
/// </summary>
@@ -192,12 +194,7 @@ public void SendCustomMessage(uint clientId, Stream stream, string channel = "ML
192
194
InternalMessageHandler . Send ( clientId , MLAPIConstants . MLAPI_CUSTOM_MESSAGE , channel , stream , new InternalSecuritySendOptions ( false , false ) ) ;
193
195
}
194
196
195
-
196
- #if ! DISABLE_CRYPTOGRAPHY
197
- internal readonly Dictionary < uint , EllipticDiffieHellman > pendingKeyExchanges = new Dictionary < uint , EllipticDiffieHellman > ( ) ;
198
197
internal byte [ ] clientAesKey ;
199
- internal readonly Dictionary < uint , byte [ ] > pendingClientAesKeys = new Dictionary < uint , byte [ ] > ( ) ;
200
- #endif
201
198
202
199
/// <summary>
203
200
/// An inspector bool that acts as a Trigger for regenerating RSA keys. Should not be used outside Unity editor.
@@ -282,16 +279,12 @@ private object Init(bool server)
282
279
lastEventTickTime = 0f ;
283
280
lastReceiveTickTime = 0f ;
284
281
eventOvershootCounter = 0f ;
285
- connectionPendingClients . Clear ( ) ;
286
- hailPendingClients . Clear ( ) ;
287
- pendingClientAesKeys . Clear ( ) ;
282
+ PendingClients . Clear ( ) ;
288
283
ConnectedClients . Clear ( ) ;
289
284
ConnectedClientsList . Clear ( ) ;
290
285
messageBuffer = new byte [ NetworkConfig . MessageBufferSize ] ;
291
286
encryptionBuffer = new byte [ NetworkConfig . EncryptionBufferSize ] ;
292
- #if ! DISABLE_CRYPTOGRAPHY
293
- pendingKeyExchanges . Clear ( ) ;
294
- #endif
287
+
295
288
MessageManager . channels . Clear ( ) ;
296
289
MessageManager . reverseChannels . Clear ( ) ;
297
290
SpawnManager . SpawnedObjects . Clear ( ) ;
@@ -516,27 +509,16 @@ public void StopServer()
516
509
NetworkConfig . NetworkTransport . DisconnectClient ( pair . Key ) ;
517
510
}
518
511
}
519
- foreach ( uint clientId in connectionPendingClients )
520
- {
521
- if ( ! disconnectedIds . Contains ( clientId ) )
522
- {
523
- disconnectedIds . Add ( clientId ) ;
524
- if ( clientId == NetworkConfig . NetworkTransport . ServerClientId )
525
- continue ;
526
-
527
- NetworkConfig . NetworkTransport . DisconnectClient ( clientId ) ;
528
- }
529
- }
530
-
531
- foreach ( uint clientId in hailPendingClients )
512
+
513
+ foreach ( KeyValuePair < uint , PendingClient > pair in PendingClients )
532
514
{
533
- if ( ! disconnectedIds . Contains ( clientId ) )
515
+ if ( ! disconnectedIds . Contains ( pair . Key ) )
534
516
{
535
- disconnectedIds . Add ( clientId ) ;
536
- if ( clientId == NetworkConfig . NetworkTransport . ServerClientId )
517
+ disconnectedIds . Add ( pair . Key ) ;
518
+ if ( pair . Key == NetworkConfig . NetworkTransport . ServerClientId )
537
519
continue ;
538
520
539
- NetworkConfig . NetworkTransport . DisconnectClient ( clientId ) ;
521
+ NetworkConfig . NetworkTransport . DisconnectClient ( pair . Key ) ;
540
522
}
541
523
}
542
524
@@ -710,7 +692,12 @@ private void Update()
710
692
EllipticDiffieHellman diffieHellman = new EllipticDiffieHellman ( EllipticDiffieHellman . DEFAULT_CURVE , EllipticDiffieHellman . DEFAULT_GENERATOR , EllipticDiffieHellman . DEFAULT_ORDER ) ;
711
693
byte [ ] diffieHellmanPublicPart = diffieHellman . GetPublicKey ( ) ;
712
694
hailWriter . WriteByteArray ( diffieHellmanPublicPart ) ;
713
- pendingKeyExchanges . Add ( clientId , diffieHellman ) ;
695
+ PendingClients . Add ( clientId , new PendingClient ( )
696
+ {
697
+ ClientId = clientId ,
698
+ ConnectionState = PendingClient . State . PendingHail ,
699
+ KeyExchange = diffieHellman
700
+ } ) ;
714
701
715
702
if ( NetworkConfig . SignKeyExchange )
716
703
{
@@ -736,11 +723,14 @@ private void Update()
736
723
// Send the hail
737
724
InternalMessageHandler . Send ( clientId , MLAPIConstants . MLAPI_CERTIFICATE_HAIL , "MLAPI_INTERNAL" , hailStream , new InternalSecuritySendOptions ( false , false ) , true ) ;
738
725
}
739
- hailPendingClients . Add ( clientId ) ;
740
726
}
741
727
else
742
728
{
743
- connectionPendingClients . Add ( clientId ) ;
729
+ PendingClients . Add ( clientId , new PendingClient ( )
730
+ {
731
+ ClientId = clientId ,
732
+ ConnectionState = PendingClient . State . PendingConnection
733
+ } ) ;
744
734
}
745
735
StartCoroutine ( ApprovalTimeout ( clientId ) ) ;
746
736
}
@@ -829,19 +819,12 @@ private IEnumerator ApprovalTimeout(uint clientId)
829
819
{
830
820
float timeStarted = NetworkTime ;
831
821
//We yield every frame incase a pending client disconnects and someone else gets its connection id
832
- while ( NetworkTime - timeStarted < NetworkConfig . ClientConnectionBufferTimeout && ( connectionPendingClients . Contains ( clientId ) || hailPendingClients . Contains ( clientId ) ) )
822
+ while ( NetworkTime - timeStarted < NetworkConfig . ClientConnectionBufferTimeout && PendingClients . ContainsKey ( clientId ) )
833
823
{
834
824
yield return null ;
835
825
}
836
826
837
- if ( connectionPendingClients . Contains ( clientId ) && ! ConnectedClients . ContainsKey ( clientId ) )
838
- {
839
- // Timeout
840
- if ( LogHelper . CurrentLogLevel <= LogLevel . Developer ) LogHelper . LogInfo ( "Client " + clientId + " Handshake Timed Out" ) ;
841
- DisconnectClient ( clientId ) ;
842
- }
843
-
844
- if ( hailPendingClients . Contains ( clientId ) && ! ConnectedClients . ContainsKey ( clientId ) )
827
+ if ( PendingClients . ContainsKey ( clientId ) && ! ConnectedClients . ContainsKey ( clientId ) )
845
828
{
846
829
// Timeout
847
830
if ( LogHelper . CurrentLogLevel <= LogLevel . Developer ) LogHelper . LogInfo ( "Client " + clientId + " Handshake Timed Out" ) ;
@@ -873,7 +856,7 @@ private void HandleIncomingData(uint clientId, byte[] data, int channelId, int t
873
856
using ( RijndaelManaged rijndael = new RijndaelManaged ( ) )
874
857
{
875
858
rijndael . Padding = PaddingMode . PKCS7 ;
876
- rijndael . Key = isServer ? ( ConnectedClients . ContainsKey ( clientId ) ? ConnectedClients [ clientId ] . AesKey : pendingClientAesKeys [ clientId ] ) : clientAesKey ;
859
+ rijndael . Key = isServer ? ( ConnectedClients . ContainsKey ( clientId ) ? ConnectedClients [ clientId ] . AesKey : PendingClients [ clientId ] . AesKey ) : clientAesKey ;
877
860
rijndael . IV = IVBuffer ;
878
861
using ( CryptoStream cryptoStream = new CryptoStream ( bitStream , rijndael . CreateDecryptor ( ) , CryptoStreamMode . Read ) )
879
862
{
@@ -919,8 +902,8 @@ private void HandleIncomingData(uint clientId, byte[] data, int channelId, int t
919
902
if ( LogHelper . CurrentLogLevel <= LogLevel . Developer ) LogHelper . LogInfo ( "Data Header: messageType=" + messageType ) ;
920
903
921
904
//Client tried to send a network message that was not the connection request before he was accepted.
922
- if ( isServer && ( NetworkConfig . EnableEncryption && hailPendingClients . Contains ( clientId ) && messageType != MLAPIConstants . MLAPI_CERTIFICATE_HAIL_RESPONSE ) ||
923
- ( connectionPendingClients . Contains ( clientId ) && messageType != MLAPIConstants . MLAPI_CONNECTION_REQUEST ) )
905
+ if ( isServer && ( NetworkConfig . EnableEncryption && PendingClients . ContainsKey ( clientId ) && PendingClients [ clientId ] . ConnectionState == PendingClient . State . PendingHail && messageType != MLAPIConstants . MLAPI_CERTIFICATE_HAIL_RESPONSE ) ||
906
+ ( PendingClients . ContainsKey ( clientId ) && PendingClients [ clientId ] . ConnectionState == PendingClient . State . PendingConnection && messageType != MLAPIConstants . MLAPI_CONNECTION_REQUEST ) )
924
907
{
925
908
if ( LogHelper . CurrentLogLevel <= LogLevel . Normal ) LogHelper . LogWarning ( "Message recieved from clientId " + clientId + " before it has been accepted" ) ;
926
909
return ;
@@ -1013,42 +996,25 @@ internal void DisconnectClient(uint clientId)
1013
996
if ( ! isServer )
1014
997
return ;
1015
998
1016
- if ( connectionPendingClients . Contains ( clientId ) )
1017
- connectionPendingClients . Remove ( clientId ) ;
1018
-
1019
- if ( hailPendingClients . Contains ( clientId ) )
1020
- hailPendingClients . Remove ( clientId ) ;
1021
-
1022
999
if ( ConnectedClients . ContainsKey ( clientId ) )
1023
1000
ConnectedClients . Remove ( clientId ) ;
1024
1001
1025
- if ( pendingClientAesKeys . ContainsKey ( clientId ) )
1026
- pendingClientAesKeys . Remove ( clientId ) ;
1002
+ if ( PendingClients . ContainsKey ( clientId ) )
1003
+ PendingClients . Remove ( clientId ) ;
1027
1004
1028
1005
for ( int i = ConnectedClientsList . Count - 1 ; i > - 1 ; i -- )
1029
1006
{
1030
1007
if ( ConnectedClientsList [ i ] . ClientId == clientId )
1031
1008
ConnectedClientsList . RemoveAt ( i ) ;
1032
1009
}
1033
1010
1034
- #if ! DISABLE_CRYPTOGRAPHY
1035
- if ( pendingKeyExchanges . ContainsKey ( clientId ) )
1036
- pendingKeyExchanges . Remove ( clientId ) ;
1037
- #endif
1038
-
1039
1011
NetworkConfig . NetworkTransport . DisconnectClient ( clientId ) ;
1040
1012
}
1041
1013
1042
1014
internal void OnClientDisconnectFromServer ( uint clientId )
1043
1015
{
1044
- if ( connectionPendingClients . Contains ( clientId ) )
1045
- connectionPendingClients . Remove ( clientId ) ;
1046
-
1047
- if ( hailPendingClients . Contains ( clientId ) )
1048
- hailPendingClients . Remove ( clientId ) ;
1049
-
1050
- if ( pendingClientAesKeys . ContainsKey ( clientId ) )
1051
- pendingClientAesKeys . Remove ( clientId ) ;
1016
+ if ( PendingClients . ContainsKey ( clientId ) )
1017
+ PendingClients . Remove ( clientId ) ;
1052
1018
1053
1019
if ( ConnectedClients . ContainsKey ( clientId ) )
1054
1020
{
@@ -1106,16 +1072,10 @@ internal void HandleApproval(uint clientId, int prefabId, bool approved, Vector3
1106
1072
{
1107
1073
if ( approved )
1108
1074
{
1109
- // Inform new client it got approved
1110
- if ( connectionPendingClients . Contains ( clientId ) )
1111
- connectionPendingClients . Remove ( clientId ) ;
1112
-
1113
- if ( hailPendingClients . Contains ( clientId ) )
1114
- hailPendingClients . Remove ( clientId ) ;
1115
-
1116
- byte [ ] aesKey = pendingClientAesKeys . ContainsKey ( clientId ) ? pendingClientAesKeys [ clientId ] : null ;
1117
- if ( pendingClientAesKeys . ContainsKey ( clientId ) )
1118
- pendingClientAesKeys . Remove ( clientId ) ;
1075
+ // Inform new client it got approved
1076
+ byte [ ] aesKey = PendingClients . ContainsKey ( clientId ) ? PendingClients [ clientId ] . AesKey : null ;
1077
+ if ( PendingClients . ContainsKey ( clientId ) )
1078
+ PendingClients . Remove ( clientId ) ;
1119
1079
NetworkedClient client = new NetworkedClient ( )
1120
1080
{
1121
1081
ClientId = clientId ,
@@ -1230,16 +1190,8 @@ internal void HandleApproval(uint clientId, int prefabId, bool approved, Vector3
1230
1190
}
1231
1191
else
1232
1192
{
1233
- if ( connectionPendingClients . Contains ( clientId ) )
1234
- connectionPendingClients . Remove ( clientId ) ;
1235
-
1236
- if ( hailPendingClients . Contains ( clientId ) )
1237
- hailPendingClients . Remove ( clientId ) ;
1238
-
1239
- #if ! DISABLE_CRYPTOGRAPHY
1240
- if ( pendingKeyExchanges . ContainsKey ( clientId ) )
1241
- pendingKeyExchanges . Remove ( clientId ) ;
1242
- #endif
1193
+ if ( PendingClients . ContainsKey ( clientId ) )
1194
+ PendingClients . Remove ( clientId ) ;
1243
1195
1244
1196
NetworkConfig . NetworkTransport . DisconnectClient ( clientId ) ;
1245
1197
}
0 commit comments