Added ClientIdManager
This commit is contained in:
parent
965d644fb7
commit
384dfbdab8
38
MLAPI/Data/ClientIdKey.cs
Normal file
38
MLAPI/Data/ClientIdKey.cs
Normal file
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
@ -74,6 +74,8 @@
|
||||
<Compile Include="NetworkingManagerComponents\NetworkSceneManager.cs" />
|
||||
<Compile Include="NetworkingManagerComponents\SpawnManager.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<Compile Include="NetworkingManagerComponents\ClientIdManager.cs" />
|
||||
<Compile Include="Data\ClientIdKey.cs" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||
</Project>
|
@ -94,6 +94,11 @@ namespace MLAPI
|
||||
NetworkSceneManager.registeredSceneNames = new HashSet<string>();
|
||||
NetworkSceneManager.sceneIndexToString = new Dictionary<uint, string>();
|
||||
NetworkSceneManager.sceneNameToIndex = new Dictionary<string, uint>();
|
||||
ClientIdManager.clientIdCounter = 0;
|
||||
ClientIdManager.clientIdToKey = new Dictionary<int, ClientIdKey>();
|
||||
ClientIdManager.keyToClientId = new Dictionary<ClientIdKey, int>();
|
||||
ClientIdManager.releasedClientIds = new Queue<int>();
|
||||
|
||||
if (NetworkConfig.HandleObjectSpawning)
|
||||
{
|
||||
NetworkedObject[] sceneObjects = FindObjectsOfType<NetworkedObject>();
|
||||
|
43
MLAPI/NetworkingManagerComponents/ClientIdManager.cs
Normal file
43
MLAPI/NetworkingManagerComponents/ClientIdManager.cs
Normal file
@ -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<int> releasedClientIds;
|
||||
internal static Dictionary<int, ClientIdKey> clientIdToKey;
|
||||
internal static Dictionary<ClientIdKey, int> 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);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user