Skip to content

Commit 384dfbd

Browse files
committed
Added ClientIdManager
1 parent 965d644 commit 384dfbd

File tree

4 files changed

+88
-0
lines changed

4 files changed

+88
-0
lines changed

MLAPI/Data/ClientIdKey.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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+
}

MLAPI/MLAPI.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@
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>

MLAPI/MonoBehaviours/Core/NetworkingManager.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff 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>();
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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+
}

0 commit comments

Comments
 (0)