diff --git a/MLAPI/Data/ClientIdKey.cs b/MLAPI/Data/ClientIdKey.cs new file mode 100644 index 0000000..885a373 --- /dev/null +++ b/MLAPI/Data/ClientIdKey.cs @@ -0,0 +1,38 @@ +namespace MLAPI.Data +{ + struct ClientIdKey + { + internal readonly int hostId; + internal readonly int connectionId; + + internal ClientIdKey (int hostId, int connectionId) + { + this.hostId = hostId; + this.connectionId = connectionId; + } + + public override bool Equals (object obj) + { + if (obj == null || GetType() != obj.GetType()) + return false; + + ClientIdKey key = (ClientIdKey)obj; + return (hostId == key.hostId) && (connectionId == key.hostId); + } + + public override int GetHashCode() + { + return hostId ^ connectionId; + } + + public static bool operator ==(ClientIdKey x, ClientIdKey y) + { + return x.hostId == y.hostId && x.connectionId == y.connectionId; + } + + public static bool operator !=(ClientIdKey x, ClientIdKey y) + { + return !(x == y); + } + } +} diff --git a/MLAPI/MLAPI.csproj b/MLAPI/MLAPI.csproj index e1e29f1..3706f5e 100644 --- a/MLAPI/MLAPI.csproj +++ b/MLAPI/MLAPI.csproj @@ -74,6 +74,8 @@ + + \ No newline at end of file diff --git a/MLAPI/MonoBehaviours/Core/NetworkingManager.cs b/MLAPI/MonoBehaviours/Core/NetworkingManager.cs index 4b1e173..684d54c 100644 --- a/MLAPI/MonoBehaviours/Core/NetworkingManager.cs +++ b/MLAPI/MonoBehaviours/Core/NetworkingManager.cs @@ -94,6 +94,11 @@ namespace MLAPI NetworkSceneManager.registeredSceneNames = new HashSet(); NetworkSceneManager.sceneIndexToString = new Dictionary(); NetworkSceneManager.sceneNameToIndex = new Dictionary(); + ClientIdManager.clientIdCounter = 0; + ClientIdManager.clientIdToKey = new Dictionary(); + ClientIdManager.keyToClientId = new Dictionary(); + ClientIdManager.releasedClientIds = new Queue(); + if (NetworkConfig.HandleObjectSpawning) { NetworkedObject[] sceneObjects = FindObjectsOfType(); diff --git a/MLAPI/NetworkingManagerComponents/ClientIdManager.cs b/MLAPI/NetworkingManagerComponents/ClientIdManager.cs new file mode 100644 index 0000000..a4ce7ab --- /dev/null +++ b/MLAPI/NetworkingManagerComponents/ClientIdManager.cs @@ -0,0 +1,43 @@ +using System; +using System.Collections.Generic; +using MLAPI.Data; + +namespace MLAPI.NetworkingManagerComponents +{ + internal static class ClientIdManager + { + internal static int clientIdCounter; + // Use a queue instead of stack to (hopefully) reduce the chance of a clientId being re taken to quickly. + internal static Queue releasedClientIds; + internal static Dictionary clientIdToKey; + internal static Dictionary keyToClientId; + + internal static int GetClientId(int connectionId, int hostId) + { + int clientId; + if (releasedClientIds.Count > 0) + { + clientId = releasedClientIds.Dequeue(); + } + else + { + clientId = clientIdCounter; + clientIdCounter++; + } + clientIdToKey.Add(clientId, new ClientIdKey(hostId, connectionId)); + keyToClientId.Add(new ClientIdKey(hostId, connectionId), clientId); + return clientId; + } + + internal static void ReleaseClientId(int clientId) + { + ClientIdKey key = clientIdToKey[clientId]; + if (clientIdToKey.ContainsKey(clientId)) + clientIdToKey.Remove(clientId); + if (keyToClientId.ContainsKey(key)) + keyToClientId.Remove(key); + + releasedClientIds.Enqueue(clientId); + } + } +}