Telepathy: moved GetClientAddress exception handling from Transport into Telepathy itself

This commit is contained in:
mischa 2024-05-16 17:19:54 +07:00 committed by MrGadget
parent 14ed0696ec
commit 021ac0b488
2 changed files with 23 additions and 26 deletions

View File

@ -318,12 +318,27 @@ public bool Send(int connectionId, ArraySegment<byte> message)
// client's ip is sometimes needed by the server, e.g. for bans // client's ip is sometimes needed by the server, e.g. for bans
public string GetClientAddress(int connectionId) public string GetClientAddress(int connectionId)
{ {
// find the connection try
if (clients.TryGetValue(connectionId, out ConnectionState connection))
{ {
return ((IPEndPoint)connection.client.Client.RemoteEndPoint).Address.ToString(); // find the connection
if (clients.TryGetValue(connectionId, out ConnectionState connection))
{
return ((IPEndPoint)connection.client.Client.RemoteEndPoint).Address.ToString();
}
return "";
}
catch (SocketException)
{
// using server.listener.LocalEndpoint causes an Exception
// in UWP + Unity 2019:
// Exception thrown at 0x00007FF9755DA388 in UWF.exe:
// Microsoft C++ exception: Il2CppExceptionWrapper at memory
// location 0x000000E15A0FCDD0. SocketException: An address
// incompatible with the requested protocol was used at
// System.Net.Sockets.Socket.get_LocalEndPoint ()
// so let's at least catch it and recover
return "unknown";
} }
return "";
} }
// disconnect (kick) a client // disconnect (kick) a client

View File

@ -200,25 +200,7 @@ public override void ServerSend(int connectionId, ArraySegment<byte> segment, in
OnServerDataSent?.Invoke(connectionId, segment, Channels.Reliable); OnServerDataSent?.Invoke(connectionId, segment, Channels.Reliable);
} }
public override void ServerDisconnect(int connectionId) => server?.Disconnect(connectionId); public override void ServerDisconnect(int connectionId) => server?.Disconnect(connectionId);
public override string ServerGetClientAddress(int connectionId) public override string ServerGetClientAddress(int connectionId) => server?.GetClientAddress(connectionId);
{
try
{
return server?.GetClientAddress(connectionId);
}
catch (SocketException)
{
// using server.listener.LocalEndpoint causes an Exception
// in UWP + Unity 2019:
// Exception thrown at 0x00007FF9755DA388 in UWF.exe:
// Microsoft C++ exception: Il2CppExceptionWrapper at memory
// location 0x000000E15A0FCDD0. SocketException: An address
// incompatible with the requested protocol was used at
// System.Net.Sockets.Socket.get_LocalEndPoint ()
// so let's at least catch it and recover
return "unknown";
}
}
public override void ServerStop() public override void ServerStop()
{ {
server?.Stop(); server?.Stop();