Skip to content

Commit

Permalink
feat: adding option to rethrow exception throw message handler
Browse files Browse the repository at this point in the history
this can help when debugging, stack with file paths will be in unity console
  • Loading branch information
James-Frowen committed Jun 11, 2023
1 parent 773910c commit 15f27a4
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
8 changes: 7 additions & 1 deletion Assets/Mirage/Runtime/MessageHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,16 @@ public class MessageHandler : IMessageReceiver
private static readonly ILogger logger = LogFactory.GetLogger<MessageHandler>();

private readonly bool _disconnectOnException;
private readonly bool _rethrowException = false;
private readonly IObjectLocator _objectLocator;

internal readonly Dictionary<int, Handler> _messageHandlers = new Dictionary<int, Handler>();

public MessageHandler(IObjectLocator objectLocator, bool disconnectOnException)
public MessageHandler(IObjectLocator objectLocator, bool disconnectOnException, bool rethrowException = false)
{
_disconnectOnException = disconnectOnException;
_objectLocator = objectLocator;
_rethrowException = rethrowException;
}

public void RegisterHandler<T>(MessageDelegateWithPlayer<T> handler, bool allowUnauthenticated)
Expand Down Expand Up @@ -93,6 +95,10 @@ public void HandleMessage(INetworkPlayer player, ArraySegment<byte> packet)
catch (Exception e)
{
LogAndCheckDisconnect(player, e);

if (_rethrowException)
// note, dont add Exception here, otherwise strack trace will be overritten
throw;
}
}
}
Expand Down
6 changes: 4 additions & 2 deletions Assets/Mirage/Runtime/NetworkClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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);

Expand Down
4 changes: 3 additions & 1 deletion Assets/Mirage/Runtime/NetworkServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<NetworkPingMessage>(World.Time.OnServerPing, allowUnauthenticated: true);

// create after MessageHandler, SyncVarReceiver uses it
Expand Down
1 change: 1 addition & 0 deletions Assets/Tests/Common/Setup/Instances/ClientInstance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public ClientInstance(Config config, TestSocketFactory socketFactory, string nam
{
GameObject = new GameObject("client_" + nameSuffix, typeof(ClientObjectManager), typeof(NetworkClient));
Client = GameObject.GetComponent<NetworkClient>();
Client.RethrowException = true;
if (config != null) Client.PeerConfig = config;
Client.SocketFactory = socketFactory;

Expand Down
1 change: 1 addition & 0 deletions Assets/Tests/Common/Setup/Instances/HostInstance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public class HostInstance : ServerInstance, IClientInstance
public HostInstance(Config serverConfig) : base(serverConfig)
{
Client = GameObject.AddComponent<NetworkClient>();
Client.RethrowException = true;
ClientObjectManager = GameObject.AddComponent<ClientObjectManager>();
Client.ObjectManager = ClientObjectManager;
}
Expand Down
1 change: 1 addition & 0 deletions Assets/Tests/Common/Setup/Instances/ServerInstance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public ServerInstance(Config config)
{
GameObject = new GameObject("server", typeof(ServerObjectManager), typeof(NetworkServer));
Server = GameObject.GetComponent<NetworkServer>();
Server.RethrowException = true;
if (config != null) Server.PeerConfig = config;
SocketFactory = GameObject.AddComponent<TestSocketFactory>();
Server.SocketFactory = SocketFactory;
Expand Down

0 comments on commit 15f27a4

Please sign in to comment.