From 021ac0b488f80cf3436bc6849374346249ca7422 Mon Sep 17 00:00:00 2001 From: mischa Date: Thu, 16 May 2024 17:19:54 +0700 Subject: [PATCH] Telepathy: moved GetClientAddress exception handling from Transport into Telepathy itself --- .../Transports/Telepathy/Telepathy/Server.cs | 29 ++++++++++++++----- .../Telepathy/TelepathyTransport.cs | 20 +------------ 2 files changed, 23 insertions(+), 26 deletions(-) diff --git a/Assets/Mirror/Transports/Telepathy/Telepathy/Server.cs b/Assets/Mirror/Transports/Telepathy/Telepathy/Server.cs index 0b4ada7e7..08af60629 100644 --- a/Assets/Mirror/Transports/Telepathy/Telepathy/Server.cs +++ b/Assets/Mirror/Transports/Telepathy/Telepathy/Server.cs @@ -86,9 +86,9 @@ void Listen(int port) listener = TcpListener.Create(port); listener.Server.NoDelay = NoDelay; // IMPORTANT: do not set send/receive timeouts on listener. - // On linux setting the recv timeout will cause the blocking - // Accept call to timeout with EACCEPT (which mono interprets - // as EWOULDBLOCK). + // On linux setting the recv timeout will cause the blocking + // Accept call to timeout with EACCEPT (which mono interprets + // as EWOULDBLOCK). // https://stackoverflow.com/questions/1917814/eagain-error-for-accept-on-blocking-socket/1918118#1918118 // => fixes https://github.com/vis2k/Mirror/issues/2695 // @@ -318,12 +318,27 @@ public bool Send(int connectionId, ArraySegment message) // client's ip is sometimes needed by the server, e.g. for bans public string GetClientAddress(int connectionId) { - // find the connection - if (clients.TryGetValue(connectionId, out ConnectionState connection)) + try { - 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 diff --git a/Assets/Mirror/Transports/Telepathy/TelepathyTransport.cs b/Assets/Mirror/Transports/Telepathy/TelepathyTransport.cs index 7a1c88dba..f4276884e 100644 --- a/Assets/Mirror/Transports/Telepathy/TelepathyTransport.cs +++ b/Assets/Mirror/Transports/Telepathy/TelepathyTransport.cs @@ -200,25 +200,7 @@ public override void ServerSend(int connectionId, ArraySegment segment, in OnServerDataSent?.Invoke(connectionId, segment, Channels.Reliable); } public override void ServerDisconnect(int connectionId) => server?.Disconnect(connectionId); - public override string ServerGetClientAddress(int 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 string ServerGetClientAddress(int connectionId) => server?.GetClientAddress(connectionId); public override void ServerStop() { server?.Stop();