From 15f27a4b6931fb5c87af0d8cdf2f5d66bd5ebf56 Mon Sep 17 00:00:00 2001 From: James Frowen Date: Sun, 11 Jun 2023 15:08:03 +0100 Subject: [PATCH] feat: adding option to rethrow exception throw message handler this can help when debugging, stack with file paths will be in unity console --- .../Runtime/Authentication/AuthenticatorSettings.cs | 2 +- Assets/Mirage/Runtime/MessageHandler.cs | 8 +++++++- Assets/Mirage/Runtime/NetworkClient.cs | 6 ++++-- Assets/Mirage/Runtime/NetworkServer.cs | 4 +++- Assets/Tests/Common/Setup/Instances/ClientInstance.cs | 1 + Assets/Tests/Common/Setup/Instances/HostInstance.cs | 1 + Assets/Tests/Common/Setup/Instances/ServerInstance.cs | 1 + 7 files changed, 18 insertions(+), 5 deletions(-) diff --git a/Assets/Mirage/Runtime/Authentication/AuthenticatorSettings.cs b/Assets/Mirage/Runtime/Authentication/AuthenticatorSettings.cs index e2394cc702..699b8ddb2a 100644 --- a/Assets/Mirage/Runtime/Authentication/AuthenticatorSettings.cs +++ b/Assets/Mirage/Runtime/Authentication/AuthenticatorSettings.cs @@ -33,7 +33,7 @@ public void Setup(NetworkServer server) // message handler used just for Auth message // this is needed because message are wrapped inside AuthMessage - _authHandler = new MessageHandler(null, true); + _authHandler = new MessageHandler(null, true, _server.RethrowException); server.Disconnected.AddListener(ServerDisconnected); diff --git a/Assets/Mirage/Runtime/MessageHandler.cs b/Assets/Mirage/Runtime/MessageHandler.cs index e58b719ec3..0af076910a 100644 --- a/Assets/Mirage/Runtime/MessageHandler.cs +++ b/Assets/Mirage/Runtime/MessageHandler.cs @@ -11,14 +11,16 @@ public class MessageHandler : IMessageReceiver private static readonly ILogger logger = LogFactory.GetLogger(); private readonly bool _disconnectOnException; + private readonly bool _rethrowException = false; private readonly IObjectLocator _objectLocator; internal readonly Dictionary _messageHandlers = new Dictionary(); - public MessageHandler(IObjectLocator objectLocator, bool disconnectOnException) + public MessageHandler(IObjectLocator objectLocator, bool disconnectOnException, bool rethrowException = false) { _disconnectOnException = disconnectOnException; _objectLocator = objectLocator; + _rethrowException = rethrowException; } public void RegisterHandler(MessageDelegateWithPlayer handler, bool allowUnauthenticated) @@ -93,6 +95,10 @@ public void HandleMessage(INetworkPlayer player, ArraySegment packet) catch (Exception e) { LogAndCheckDisconnect(player, e); + + if (_rethrowException) + // note, dont add Exception here, otherwise strack trace will be overritten + throw; } } } diff --git a/Assets/Mirage/Runtime/NetworkClient.cs b/Assets/Mirage/Runtime/NetworkClient.cs index 8c418b17fd..2d7055087c 100644 --- a/Assets/Mirage/Runtime/NetworkClient.cs +++ b/Assets/Mirage/Runtime/NetworkClient.cs @@ -44,6 +44,8 @@ public class NetworkClient : MonoBehaviour, IMessageSender public ClientObjectManager ObjectManager; public bool DisconnectOnException = true; + [Tooltip("Should the message handler rethrow the exception after logging. This should only be used when deubgging as it may stop other Mirage functions from running after messages handling")] + public bool RethrowException = false; [Tooltip("If true will set Application.runInBackground")] public bool RunInBackground = true; @@ -134,7 +136,7 @@ public void Connect(string address = null, ushort? port = null) var socket = SocketFactory.CreateClientSocket(); var maxPacketSize = SocketFactory.MaxPacketSize; - MessageHandler = new MessageHandler(World, DisconnectOnException); + MessageHandler = new MessageHandler(World, DisconnectOnException, RethrowException); var dataHandler = new DataHandler(MessageHandler); Metrics = EnablePeerMetrics ? new Metrics(MetricsSize) : null; @@ -221,7 +223,7 @@ internal void ConnectHost(NetworkServer server, IDataHandler serverDataHandler) World = server.World; // create local connection objects and connect them - MessageHandler = new MessageHandler(World, DisconnectOnException); + MessageHandler = new MessageHandler(World, DisconnectOnException, RethrowException); var dataHandler = new DataHandler(MessageHandler); (var clientConn, var serverConn) = PipePeerConnection.Create(dataHandler, serverDataHandler, OnHostDisconnected, null); diff --git a/Assets/Mirage/Runtime/NetworkServer.cs b/Assets/Mirage/Runtime/NetworkServer.cs index c6198cc270..0a9c4d6539 100644 --- a/Assets/Mirage/Runtime/NetworkServer.cs +++ b/Assets/Mirage/Runtime/NetworkServer.cs @@ -42,6 +42,8 @@ public class NetworkServer : MonoBehaviour public int MaxConnections = 4; public bool DisconnectOnException = true; + [Tooltip("Should the message handler rethrow the exception after logging. This should only be used when deubgging as it may stop other Mirage functions from running after messages handling")] + public bool RethrowException = false; [Tooltip("If true will set Application.runInBackground")] public bool RunInBackground = true; @@ -223,7 +225,7 @@ public void StartServer(NetworkClient localClient = null) SyncVarSender = new SyncVarSender(); LocalClient = localClient; - MessageHandler = new MessageHandler(World, DisconnectOnException); + MessageHandler = new MessageHandler(World, DisconnectOnException, RethrowException); MessageHandler.RegisterHandler(World.Time.OnServerPing, allowUnauthenticated: true); // create after MessageHandler, SyncVarReceiver uses it diff --git a/Assets/Tests/Common/Setup/Instances/ClientInstance.cs b/Assets/Tests/Common/Setup/Instances/ClientInstance.cs index 26a758a124..60184decc1 100644 --- a/Assets/Tests/Common/Setup/Instances/ClientInstance.cs +++ b/Assets/Tests/Common/Setup/Instances/ClientInstance.cs @@ -21,6 +21,7 @@ public ClientInstance(Config config, TestSocketFactory socketFactory, string nam { GameObject = new GameObject("client_" + nameSuffix, typeof(ClientObjectManager), typeof(NetworkClient)); Client = GameObject.GetComponent(); + Client.RethrowException = true; if (config != null) Client.PeerConfig = config; Client.SocketFactory = socketFactory; diff --git a/Assets/Tests/Common/Setup/Instances/HostInstance.cs b/Assets/Tests/Common/Setup/Instances/HostInstance.cs index da3e44c40b..e4b8b18bc8 100644 --- a/Assets/Tests/Common/Setup/Instances/HostInstance.cs +++ b/Assets/Tests/Common/Setup/Instances/HostInstance.cs @@ -16,6 +16,7 @@ public class HostInstance : ServerInstance, IClientInstance public HostInstance(Config serverConfig) : base(serverConfig) { Client = GameObject.AddComponent(); + Client.RethrowException = true; ClientObjectManager = GameObject.AddComponent(); Client.ObjectManager = ClientObjectManager; } diff --git a/Assets/Tests/Common/Setup/Instances/ServerInstance.cs b/Assets/Tests/Common/Setup/Instances/ServerInstance.cs index 9c73d0c868..1ee6aa7491 100644 --- a/Assets/Tests/Common/Setup/Instances/ServerInstance.cs +++ b/Assets/Tests/Common/Setup/Instances/ServerInstance.cs @@ -34,6 +34,7 @@ public ServerInstance(Config config) { GameObject = new GameObject("server", typeof(ServerObjectManager), typeof(NetworkServer)); Server = GameObject.GetComponent(); + Server.RethrowException = true; if (config != null) Server.PeerConfig = config; SocketFactory = GameObject.AddComponent(); Server.SocketFactory = SocketFactory;