Added a ClientIdManager

This commit is contained in:
Albin Corén 2018-03-13 10:16:56 +01:00
parent 384dfbdab8
commit 8f3319cd14
2 changed files with 50 additions and 15 deletions

View File

@ -298,7 +298,7 @@ namespace MLAPI
//Receive stuff //Receive stuff
internal int hostId; internal int hostId;
private int clientId; private int connectionId;
private int channelId; private int channelId;
private int receivedSize; private int receivedSize;
private byte error; private byte error;
@ -324,14 +324,14 @@ namespace MLAPI
do do
{ {
processedEvents++; processedEvents++;
eventType = NetworkTransport.Receive(out hostId, out clientId, out channelId, messageBuffer, messageBuffer.Length, out receivedSize, out error); eventType = NetworkTransport.Receive(out hostId, out connectionId, out channelId, messageBuffer, messageBuffer.Length, out receivedSize, out error);
NetworkError networkError = (NetworkError)error; NetworkError networkError = (NetworkError)error;
if (networkError == NetworkError.Timeout) if (networkError == NetworkError.Timeout)
{ {
//Client timed out. //Client timed out.
if (isServer) if (isServer)
{ {
OnClientDisconnect(clientId); OnClientDisconnect(ClientIdManager.GetClientId(hostId, connectionId));
return; return;
} }
} }
@ -346,6 +346,7 @@ namespace MLAPI
case NetworkEventType.ConnectEvent: case NetworkEventType.ConnectEvent:
if (isServer) if (isServer)
{ {
int clientId = ClientIdManager.AddClientId(connectionId, hostId);
pendingClients.Add(clientId); pendingClients.Add(clientId);
StartCoroutine(ApprovalTimeout(clientId)); StartCoroutine(ApprovalTimeout(clientId));
} }
@ -366,16 +367,19 @@ namespace MLAPI
writer.Write(NetworkConfig.ConnectionData); writer.Write(NetworkConfig.ConnectionData);
} }
} }
Send(clientId, "MLAPI_CONNECTION_REQUEST", "MLAPI_INTERNAL", writeStream.GetBuffer()); Send(connectionId, "MLAPI_CONNECTION_REQUEST", "MLAPI_INTERNAL", writeStream.GetBuffer());
} }
} }
break; break;
case NetworkEventType.DataEvent: case NetworkEventType.DataEvent:
HandleIncomingData(clientId, messageBuffer, channelId); if (isServer)
HandleIncomingData(ClientIdManager.GetClientId(hostId, connectionId), messageBuffer, channelId);
else
HandleIncomingData(connectionId, messageBuffer, channelId);
break; break;
case NetworkEventType.DisconnectEvent: case NetworkEventType.DisconnectEvent:
if (isServer) if (isServer)
OnClientDisconnect(clientId); OnClientDisconnect(ClientIdManager.GetClientId(hostId, connectionId));
break; break;
} }
// Only do another iteration if: there are no more messages AND (there is no limit to max events or we have processed less than the maximum) // Only do another iteration if: there are no more messages AND (there is no limit to max events or we have processed less than the maximum)
@ -850,7 +854,9 @@ namespace MLAPI
writer.Write((ushort)data.Length); writer.Write((ushort)data.Length);
writer.Write(data); writer.Write(data);
} }
NetworkTransport.QueueMessageForSending(hostId, targetId, channelId, stream.GetBuffer(), sizeOfStream, out error); NetworkTransport.QueueMessageForSending(ClientIdManager.GetClientIdKey(targetId).hostId,
ClientIdManager.GetClientIdKey(targetId).connectionId,
channelId, stream.GetBuffer(), sizeOfStream, out error);
} }
} }
@ -898,7 +904,10 @@ namespace MLAPI
} }
if (isPassthrough) if (isPassthrough)
clientId = serverClientId; clientId = serverClientId;
NetworkTransport.QueueMessageForSending(hostId, clientId, MessageManager.channels[channelName], stream.GetBuffer(), sizeOfStream, out error); NetworkTransport.QueueMessageForSending(ClientIdManager.GetClientIdKey(clientId).hostId,
ClientIdManager.GetClientIdKey(clientId).connectionId,
MessageManager.channels[channelName], stream.GetBuffer(),
sizeOfStream, out error);
} }
} }
@ -935,7 +944,9 @@ namespace MLAPI
//Client trying to send data to host //Client trying to send data to host
clientId = serverClientId; clientId = serverClientId;
} }
NetworkTransport.QueueMessageForSending(hostId, clientId, channel, stream.GetBuffer(), sizeOfStream, out error); NetworkTransport.QueueMessageForSending(ClientIdManager.GetClientIdKey(clientId).hostId,
ClientIdManager.GetClientIdKey(clientId).connectionId,
channel, stream.GetBuffer(), sizeOfStream, out error);
} }
} }
} }
@ -974,7 +985,9 @@ namespace MLAPI
//Client trying to send data to host //Client trying to send data to host
clientId = serverClientId; clientId = serverClientId;
} }
NetworkTransport.QueueMessageForSending(hostId, clientId, channel, stream.GetBuffer(), sizeOfStream, out error); NetworkTransport.QueueMessageForSending(ClientIdManager.GetClientIdKey(clientId).hostId,
ClientIdManager.GetClientIdKey(clientId).connectionId,
channel, stream.GetBuffer(), sizeOfStream, out error);
} }
} }
} }
@ -1013,7 +1026,9 @@ namespace MLAPI
//Client trying to send data to host //Client trying to send data to host
clientId = serverClientId; clientId = serverClientId;
} }
NetworkTransport.QueueMessageForSending(hostId, clientId, channel, stream.GetBuffer(), sizeOfStream, out error); NetworkTransport.QueueMessageForSending(ClientIdManager.GetClientIdKey(clientId).hostId,
ClientIdManager.GetClientIdKey(clientId).connectionId,
channel, stream.GetBuffer(), sizeOfStream, out error);
} }
} }
@ -1055,7 +1070,9 @@ namespace MLAPI
//Client trying to send data to host //Client trying to send data to host
clientId = serverClientId; clientId = serverClientId;
} }
NetworkTransport.QueueMessageForSending(hostId, clientId, channel, stream.GetBuffer(), sizeOfStream, out error); NetworkTransport.QueueMessageForSending(ClientIdManager.GetClientIdKey(clientId).hostId,
ClientIdManager.GetClientIdKey(clientId).connectionId,
channel, stream.GetBuffer(), sizeOfStream, out error);
} }
} }
} }
@ -1068,7 +1085,9 @@ namespace MLAPI
pendingClients.Remove(clientId); pendingClients.Remove(clientId);
if (connectedClients.ContainsKey(clientId)) if (connectedClients.ContainsKey(clientId))
connectedClients.Remove(clientId); connectedClients.Remove(clientId);
NetworkTransport.Disconnect(hostId, clientId, out error); NetworkTransport.Disconnect(ClientIdManager.GetClientIdKey(clientId).hostId,
ClientIdManager.GetClientIdKey(clientId).connectionId, out error);
ClientIdManager.ReleaseClientId(clientId);
} }
private void OnClientDisconnect(int clientId) private void OnClientDisconnect(int clientId)
@ -1092,6 +1111,7 @@ namespace MLAPI
if (isServer) if (isServer)
{ {
ClientIdManager.ReleaseClientId(clientId);
using (MemoryStream stream = new MemoryStream(4)) using (MemoryStream stream = new MemoryStream(4))
{ {
using (BinaryWriter writer = new BinaryWriter(stream)) using (BinaryWriter writer = new BinaryWriter(stream))
@ -1215,7 +1235,8 @@ namespace MLAPI
{ {
if (pendingClients.Contains(clientId)) if (pendingClients.Contains(clientId))
pendingClients.Remove(clientId); pendingClients.Remove(clientId);
NetworkTransport.Disconnect(hostId, clientId, out error); NetworkTransport.Disconnect(ClientIdManager.GetClientIdKey(clientId).hostId,
ClientIdManager.GetClientIdKey(clientId).connectionId, out error);
} }
} }
} }

View File

@ -12,7 +12,7 @@ namespace MLAPI.NetworkingManagerComponents
internal static Dictionary<int, ClientIdKey> clientIdToKey; internal static Dictionary<int, ClientIdKey> clientIdToKey;
internal static Dictionary<ClientIdKey, int> keyToClientId; internal static Dictionary<ClientIdKey, int> keyToClientId;
internal static int GetClientId(int connectionId, int hostId) internal static int AddClientId(int connectionId, int hostId)
{ {
int clientId; int clientId;
if (releasedClientIds.Count > 0) if (releasedClientIds.Count > 0)
@ -29,6 +29,20 @@ namespace MLAPI.NetworkingManagerComponents
return clientId; return clientId;
} }
internal static int GetClientId(int hostId, int connectionId)
{
if (!keyToClientId.ContainsKey(new ClientIdKey(hostId, connectionId)))
return 0;
return keyToClientId[new ClientIdKey(hostId, connectionId)];
}
internal static ClientIdKey GetClientIdKey(int clientId)
{
if (!clientIdToKey.ContainsKey(clientId))
return new ClientIdKey(0, 0);
return clientIdToKey[clientId];
}
internal static void ReleaseClientId(int clientId) internal static void ReleaseClientId(int clientId)
{ {
ClientIdKey key = clientIdToKey[clientId]; ClientIdKey key = clientIdToKey[clientId];