File tree Expand file tree Collapse file tree 4 files changed +88
-0
lines changed
NetworkingManagerComponents Expand file tree Collapse file tree 4 files changed +88
-0
lines changed Original file line number Diff line number Diff line change 1+ namespace MLAPI . Data
2+ {
3+ struct ClientIdKey
4+ {
5+ internal readonly int hostId ;
6+ internal readonly int connectionId ;
7+
8+ internal ClientIdKey ( int hostId , int connectionId )
9+ {
10+ this . hostId = hostId ;
11+ this . connectionId = connectionId ;
12+ }
13+
14+ public override bool Equals ( object obj )
15+ {
16+ if ( obj == null || GetType ( ) != obj . GetType ( ) )
17+ return false ;
18+
19+ ClientIdKey key = ( ClientIdKey ) obj ;
20+ return ( hostId == key . hostId ) && ( connectionId == key . hostId ) ;
21+ }
22+
23+ public override int GetHashCode ( )
24+ {
25+ return hostId ^ connectionId ;
26+ }
27+
28+ public static bool operator == ( ClientIdKey x , ClientIdKey y )
29+ {
30+ return x . hostId == y . hostId && x . connectionId == y . connectionId ;
31+ }
32+
33+ public static bool operator != ( ClientIdKey x , ClientIdKey y )
34+ {
35+ return ! ( x == y ) ;
36+ }
37+ }
38+ }
Original file line number Diff line number Diff line change 7474 <Compile Include =" NetworkingManagerComponents\NetworkSceneManager.cs" />
7575 <Compile Include =" NetworkingManagerComponents\SpawnManager.cs" />
7676 <Compile Include =" Properties\AssemblyInfo.cs" />
77+ <Compile Include =" NetworkingManagerComponents\ClientIdManager.cs" />
78+ <Compile Include =" Data\ClientIdKey.cs" />
7779 </ItemGroup >
7880 <Import Project =" $(MSBuildToolsPath)\Microsoft.CSharp.targets" />
7981</Project >
Original file line number Diff line number Diff line change @@ -94,6 +94,11 @@ private ConnectionConfig Init(NetworkingConfiguration netConfig)
9494 NetworkSceneManager . registeredSceneNames = new HashSet < string > ( ) ;
9595 NetworkSceneManager . sceneIndexToString = new Dictionary < uint , string > ( ) ;
9696 NetworkSceneManager . sceneNameToIndex = new Dictionary < string , uint > ( ) ;
97+ ClientIdManager . clientIdCounter = 0 ;
98+ ClientIdManager . clientIdToKey = new Dictionary < int , ClientIdKey > ( ) ;
99+ ClientIdManager . keyToClientId = new Dictionary < ClientIdKey , int > ( ) ;
100+ ClientIdManager . releasedClientIds = new Queue < int > ( ) ;
101+
97102 if ( NetworkConfig . HandleObjectSpawning )
98103 {
99104 NetworkedObject [ ] sceneObjects = FindObjectsOfType < NetworkedObject > ( ) ;
Original file line number Diff line number Diff line change 1+ using System ;
2+ using System . Collections . Generic ;
3+ using MLAPI . Data ;
4+
5+ namespace MLAPI . NetworkingManagerComponents
6+ {
7+ internal static class ClientIdManager
8+ {
9+ internal static int clientIdCounter ;
10+ // Use a queue instead of stack to (hopefully) reduce the chance of a clientId being re taken to quickly.
11+ internal static Queue < int > releasedClientIds ;
12+ internal static Dictionary < int , ClientIdKey > clientIdToKey ;
13+ internal static Dictionary < ClientIdKey , int > keyToClientId ;
14+
15+ internal static int GetClientId ( int connectionId , int hostId )
16+ {
17+ int clientId ;
18+ if ( releasedClientIds . Count > 0 )
19+ {
20+ clientId = releasedClientIds . Dequeue ( ) ;
21+ }
22+ else
23+ {
24+ clientId = clientIdCounter ;
25+ clientIdCounter ++ ;
26+ }
27+ clientIdToKey . Add ( clientId , new ClientIdKey ( hostId , connectionId ) ) ;
28+ keyToClientId . Add ( new ClientIdKey ( hostId , connectionId ) , clientId ) ;
29+ return clientId ;
30+ }
31+
32+ internal static void ReleaseClientId ( int clientId )
33+ {
34+ ClientIdKey key = clientIdToKey [ clientId ] ;
35+ if ( clientIdToKey . ContainsKey ( clientId ) )
36+ clientIdToKey . Remove ( clientId ) ;
37+ if ( keyToClientId . ContainsKey ( key ) )
38+ keyToClientId . Remove ( key ) ;
39+
40+ releasedClientIds . Enqueue ( clientId ) ;
41+ }
42+ }
43+ }
You can’t perform that action at this time.
0 commit comments