fix(MultiplexTransport): Use TryGetValue for Lookups (#3758)

This commit is contained in:
MrGadget 2024-01-30 04:42:44 -05:00
parent 77469f5201
commit cd358630ff

View File

@ -61,9 +61,10 @@ public ushort Port
{ {
// prevent log flood from OnGUI or similar per-frame updates // prevent log flood from OnGUI or similar per-frame updates
alreadyWarned = true; alreadyWarned = true;
Debug.LogWarning($"MultiplexTransport: Server cannot set the same listen port for all transports! Set them directly instead."); Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine($"[Multiplexer] Server cannot set the same listen port for all transports! Set them directly instead.");
Console.ResetColor();
} }
else else
{ {
// We can't set the same port for all transports because // We can't set the same port for all transports because
@ -97,10 +98,11 @@ public void RemoveFromLookup(int originalConnectionId, int transportIndex)
{ {
// remove from both // remove from both
KeyValuePair<int, int> pair = new KeyValuePair<int, int>(originalConnectionId, transportIndex); KeyValuePair<int, int> pair = new KeyValuePair<int, int>(originalConnectionId, transportIndex);
int multiplexedId = originalToMultiplexedId[pair]; if (originalToMultiplexedId.TryGetValue(pair, out int multiplexedId))
{
originalToMultiplexedId.Remove(pair); originalToMultiplexedId.Remove(pair);
multiplexedToOriginalId.Remove(multiplexedId); multiplexedToOriginalId.Remove(multiplexedId);
}
} }
public bool OriginalId(int multiplexId, out int originalConnectionId, out int transportIndex) public bool OriginalId(int multiplexId, out int originalConnectionId, out int transportIndex)
@ -121,7 +123,10 @@ public bool OriginalId(int multiplexId, out int originalConnectionId, out int tr
public int MultiplexId(int originalConnectionId, int transportIndex) public int MultiplexId(int originalConnectionId, int transportIndex)
{ {
KeyValuePair<int, int> pair = new KeyValuePair<int, int>(originalConnectionId, transportIndex); KeyValuePair<int, int> pair = new KeyValuePair<int, int>(originalConnectionId, transportIndex);
return originalToMultiplexedId[pair]; if (originalToMultiplexedId.TryGetValue(pair, out int multiplexedId))
return multiplexedId;
else
return 0;
} }
//////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////
@ -265,6 +270,19 @@ void AddServerCallbacks()
{ {
// invoke Multiplex event with multiplexed connectionId // invoke Multiplex event with multiplexed connectionId
int multiplexedId = MultiplexId(originalConnectionId, transportIndex); int multiplexedId = MultiplexId(originalConnectionId, transportIndex);
if (multiplexedId == 0)
{
if (Utils.IsHeadless())
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine($"[Multiplexer] Received data for unknown connectionId={originalConnectionId} on transport={transportIndex}");
Console.ResetColor();
}
else
Debug.LogWarning($"[Multiplexer] Received data for unknown connectionId={originalConnectionId} on transport={transportIndex}");
return;
}
OnServerDataReceived.Invoke(multiplexedId, data, channel); OnServerDataReceived.Invoke(multiplexedId, data, channel);
}; };
@ -272,6 +290,19 @@ void AddServerCallbacks()
{ {
// invoke Multiplex event with multiplexed connectionId // invoke Multiplex event with multiplexed connectionId
int multiplexedId = MultiplexId(originalConnectionId, transportIndex); int multiplexedId = MultiplexId(originalConnectionId, transportIndex);
if (multiplexedId == 0)
{
if (Utils.IsHeadless())
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine($"[Multiplexer] Received error for unknown connectionId={originalConnectionId} on transport={transportIndex}");
Console.ResetColor();
}
else
Debug.LogError($"[Multiplexer] Received error for unknown connectionId={originalConnectionId} on transport={transportIndex}");
return;
}
OnServerError.Invoke(multiplexedId, error, reason); OnServerError.Invoke(multiplexedId, error, reason);
}; };
@ -279,6 +310,19 @@ void AddServerCallbacks()
{ {
// invoke Multiplex event with multiplexed connectionId // invoke Multiplex event with multiplexed connectionId
int multiplexedId = MultiplexId(originalConnectionId, transportIndex); int multiplexedId = MultiplexId(originalConnectionId, transportIndex);
if (multiplexedId == 0)
{
if (Utils.IsHeadless())
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine($"[Multiplexer] Received disconnect for unknown connectionId={originalConnectionId} on transport={transportIndex}");
Console.ResetColor();
}
else
Debug.LogWarning($"[Multiplexer] Received disconnect for unknown connectionId={originalConnectionId} on transport={transportIndex}");
return;
}
OnServerDisconnected.Invoke(multiplexedId); OnServerDisconnected.Invoke(multiplexedId);
RemoveFromLookup(originalConnectionId, transportIndex); RemoveFromLookup(originalConnectionId, transportIndex);
}; };