From 461c11117117c8e3b0a28baea484390c3998c400 Mon Sep 17 00:00:00 2001 From: MrGadget <9826063+MrGadget1024@users.noreply.github.com> Date: Mon, 15 Apr 2024 10:57:03 -0400 Subject: [PATCH] feat(Transport): Reimplement Transport Exception Actions (#3461) * feat(Transport): Reimplement Transport Exception Actions * Fixed Script Templates * fix: Updated Network Transform Template * fix(NetworkIdentity): Separate ResetState (#3742) * fix(NetworkTransform): Separate ResetState We should not have hijacked Unity's callback for runtime resets. * Rename test too * fix(InterestManagement): Separate ResetState (#3743) * fix(InterestManagement): Separate ResetState We should not have hijacked Unity's callback for runtime resets. * Updated Template * feat(NetworkTransform): Default Sync Direction = Client To Server (#3741) * feat(NetworkTransform): Default Sync Direction = Client To Server - This is done in Reset so Network Behaviour isn't effected. - This will not change NT components already in place on objects / prefabs unless user manually chooses Reset in the inspector. * Update Assets/Mirror/Components/NetworkTransform/NetworkTransformBase.cs --------- Co-authored-by: mischa <16416509+miwarnec@users.noreply.github.com> * Prediction: detect renderers in children * Add missing replace handler override to NetworkServer (#3744) * perf(BenchmarkIdle Example): Lowered spawn amount to 10k (#3745) 50k causes a long delay, and looks like Unity has initially frozen, even on beefy machines. To prevent users from thinking this, and perhaps force closing, amount is lowered to something still crazy, but more reasonable. :) - Nothing else has been touched * fix(Room Example): Updated Reward Script - Added Headers - made `available` private with ReadOnly * feat(NetworkClient): Add ReplaceHandler with channel param (#3747) * breaking(NetworkClient): Remove NetworkConnection parameter from ReplaceHandler There is only one connection on client. Aligns with RegisterHandler that takes no NetworkConnection parameter. * feat(NetworkClient): Add ReplaceHandler with channel param * breaking(NetworkIdentity): Rename visible to visibility (#3748) * breaking(NetworkIdentity): Rename visible to visibility * Remove TODO comment * Prediction: physics copy now has the correct layer assigned to fix predicted objects falling through the ground in games which use custom physics collision matrix * Update Readme: prediction status * fix(MirrorBenchmarkIdle): Spawn Amount Correction * Prediction: disable log spam * Prediction: fix wrong null check * Prediction: catch errors * Prediction: catch errors early * chore(CI): RunUnityTests - updated unityVersions * fix(Prediction): fix MoveColliders not copying isTrigger * Prediction: make Move.* functions static for reuse later * Prediction: MoveAllColliders helper for reuse * fix(Prediction): now supports interest management by moving physics copy's components back onto self before destroying the physics copy * Prediction: now supports colliders on children * comments * fix(Prediction): when copying child Colliders, copy their relative position/rotation/scale too * fix(Prediction): child colliders now have the correct layer too * oops * Prediction: move GameObject/MonoBehaviour copying code into PredicitonUtils.cs * Prediction: MovePhysicsComponents helper function * Prediction: comments * Prediction: collider materials are now copied too * feat(SyncVar): Arrays are supported (#3750) * feat(SyncVar): Arrays are supported * Moved and revised SyncVarsCantBeArray test - Now named SyncVarsCanBeArray - thisShouldntWork renamed to thisShouldWork * Prediction: support all Joints * Prediction: add support for predicted objects which are children of parents with non-default scale * perf(Team Interest Mgmt): Complete Overhaul (#3730) * WIP * perf(Team Interest Mgmt) Use Dictionary instead of spawned This is better than iterating all spawned objects and doing TryGetComponent in Update. * TeamIntMgmt WIP * NetworkTeam SerializeField, no SyncVar * Fixed Tooltip * Removed SyncVar on forceShown * Fixed issues per review * Fixed comments and naming * Remove ReadOnly * PredictionUtils: Fixed pre-Unity 2022.3 errors * PredictionUtils: Fixed pre-Unity 2020.3 errors * chore(CI): RunUnityTests - updated unityVersion * NetworkBehaviour: Improved comments * Edgegap Hosting: updated to latest version for fixes and Websocket support * PredictedRigidbody: improve logging * Add network messages to lookup dict in network server replace handler (#3753) * Add network mesages to lookup dict in network client replace handler (#3754) * Prediction: rename DestroyCopies to DestroyGhosts for consistency * syntax * perf(Prediction): cache .transform getter because this is performance critical * perf(Prediction): optimize LateUpdate because this shows in profiler! deep profiling: 93 objects, before=0.54ms, after=0.22ms => 2.5x faster! * perf(Prediction): optimize ghosts. deep profiling: 93 objects before=0.08ms after =0.03ms! * chore: RunUnityTest - upload-artifact@v4 * chore(CI): Semantic.yml - use v4 of github actions * fix(MultiplexTransport): Better Startup Logging * perf(PredictedRigidbody): next round of optimizations * perf(Extensions): Add shim for transform.GetPositionAndRotation (#3755) Doesn't exist in unity versions 2021.2 or earlier * ThreadLog: more obvious prefix! * fix(MultiplexTransport): Fixed log output * KcpTransport: fixed ToString to match other transports * fix typos * fix(MultiplexTransport): Use TryGetValue for Lookups (#3758) * NetworkManager: fixed comment * Prediction: warn about not readable meshes * NetworkTime: SendPing method added. * fix(NetworkClient): Call NetworkTime.SendPing from OnTransportConnected (#3686) * fix: fixing time not being sent on connect client does not send ping if connecting to server within 2 seconds of starting, the new PingNow function will tell client to send the message right away instead of after lastPingTime+PingInterval * Update Assets/Mirror/Core/NetworkTime.cs * Update Assets/Mirror/Core/NetworkTime.cs * Update Assets/Mirror/Core/NetworkClient.cs * Update Assets/Mirror/Core/NetworkTime.cs --------- Co-authored-by: MrGadget <9826063+MrGadget1024@users.noreply.github.com> * SyncDictionary: Fixed typo in comment * feat: NT-UR bit flag changed detection to lower bandwidth usage. (#3721) * feat: NT-UR bit flag changed detection to lower bandwidth usage. Also major credits to our Ninja. * Tooltip updated * fix: NT-Unreliable Quaternion Compression Fix Credits to ninja of course :D * NT-U new improvements Credits to Ninja * Nothing to see here.. * Added comment to Quat Rotation Fix * Sensitivity check to improve value comparisons. Without this, X 0 and X -4.955753E-07 (0) would trigger as a change of value. Helps epsilon/floating point inaccuracies. * Moved around checks. rotationChanged not needed now for non-compressed bool, as we check individual rotation sensitivity changes. We can move this inside quat compress check. * Use Rot/All, not just RotX as a compress changed flag. * Set Just Rot. * Updated Reset to ResetState * Fixing PR 3571/3572/3572 in this new bitflag branch --------- Co-authored-by: ninjakickja <80569286+ninjakickja@users.noreply.github.com> * fix(NetworkManager): FinishStartHost-Don't force localhost (#3759) * perf(PredictedRigidbody): only record state if changed(!) * perf(PredictedRigidbody): reduce sends to 1 Hz while idle(!) * Prediction: expose physics copy components to inheriting classes * fix(PredictedRigidbody): only show 'hard correcting because too old' warning if older than entire state history limit. not when starting to record only 2-3 entries yet. * fix(PredictedRigidbody): fix host mode [FakeByte] * perf(PredictedRigidbody): OnSerialize faster * breaking: perf: change NB.syncVarDirtyBits to field (#3762) @miwarnec saw the property showing up while profiling. Very much unnecessary, so lets change that to a field. BREAKING: Changed NetworkBehaviour.syncVarDirtyBits from a "get-only" property to a field * feat(NetworkAnimator): Default Sync Direction = Client To Server - This is done in Reset so Network Behaviour isn't effected. - This will not change NA components already in place on objects / prefabs unless user manually chooses Reset in the inspector. * chore(CI): RunUnityTests - updated unityVersions * chore(CI): RunUnityTests - updated unityVersion * chore(CI): RunUnityTests - updated unityVersion * fix(SWT): Improved logging in ClientHandshake - Added hint to check Server Roles for handshake key mismatches * feat(NetworkRigidbody): Default Sync Direction = Client To Server (#3764) - This is done in Reset so Network Behaviour isn't effected. - This will not change components already in place on objects / prefabs unless user manually chooses Reset in the inspector. * PredictedRigidbody: add OnBeforeApplyState callback * doc(ReadMe): Fixed sponsor link * CanvasNetworkManagerHUD: fixed indent * perf(Prediction): ghosts-on-demand(!) * Prediction: sync angularVelocity too * Prediction: better motion smoothing time * Prediction: only set velocity if not kinematic * Prediction: sync delta rotations too [FakeByte] * fix(PredictedRigidbody): reduce-send-rate considers angular velocity now too! * fix(Prediction): ConfigurableJoints' range of motion is now moved correctly * fix: Safer version for FindAnyObjectByType (#3765) Unity docs are a mess on when FindAnyObjectByType was implemented: - 2021.3 says it is - 2022.1 says it's not - 2022.2 says it is * chore(CI): RunUnityTests - updated unityVersions * fix(Prediction): disable setting scale for joints since it causes issues with parenting * feat: LagCompensator as convenience component that wraps all the Lag Compensation logic * Update README.md * LagCompensator: rename collider field to be more obvious in Inspector * LagCompensator: preview notice * refactor: NetworkServer DestroyObject(DestroyMode) logic moved to Unspawn() + Destroy to remove the DestroyMode parameter * fix: NetworkServer.Destroy never destroys scene objects anymore since unlike prefabs, once they are gone we can never instantiate them again (#3771) * fix: NetworkServer.Spawn now activates inactive GameObjects (for example, in case they were previously Unspawned) * chore(CI): RunUnityTests - updated unityVersion * Upgrade to Unity 2021.3.35 LTS * MIRROR_88_OR_NEWER * feat: EncryptionTransport (#3768) * initial working transport * code cleanup & transport wrap tests * better connection tests * Handle bouncycastle exceptions * clean up usings * Mirror icon :) * list to allow for removing entries during loop * Profiler sampling * Unity 2019 compat * code style * pubkey validation * use builtin aes engine selector this is overly optimistic, as the hardware accelerated engine is only available on .net core 3 or higher * Older unity version fix * fix: EncryptionTransport uses KDF to ensure fixed size key (#3773) * reset _time * fix: EncryptionTransport use KDF to ensure fixed size key * Expose pub key through transport * Old Unity compat * breaking: Asset Store Release BREAKING CHANGE: Asset Store Release * Removed empty folder meta * chore: EncryptionTransport help url (#3774) * fix: NetworkServer.Destroy now warns instead of throwing for null objects again * fix: NetworkServer.OnCommandMessage NullReferenceException due to log message accessing a guaranteed-to-be-null value * feat(Transport): IsEncrypted, EncryptionCipher virtuals * feat: EncryptionTransport overrides IsEncrypted & Cipher (#3776) * feat: SWT overrides Transport.IsEncrypted & EncryptionCipher (#3777) TLS isn't technically correct, but there's no good way to get that info * SimpleWebTransport: moved overrides down * fix: #2503 Weaver Custom Read Write functions for Serialization not working over multiple assembly definitions * chore(CI): RunUnityTests - updated unityVersion * fix: EdgegapKcp StopHost() null reference exception (#3779) see https://github.com/MirrorNetworking/Mirror/issues/3708 * fix(ILPostProcessorAssemblyResolver): Ignore Bee.BeeDriver - There is now BeeDriver2, so using StartsWith * PredictedRigidbody: make .predictedRigidbody accessible to other components! * Examples: Prediction Benchmark with readme & current results * readme changed from .txt to .md for formatting * Prediction: syntax * chore(CI): RunUnityTests - updated unityVersions * PredictedRigidbody: OnDeserialize parsing grouped together (syntax change) * PredictedRigidbody: syntax * perf: PredictedRigidbody doesn't log hard correction when ahead anymore * perf: Prediction.Sample() removed SortedList foreach iteration allocations(!) * perf: PredictedRigidbody doesn't log "Separating Physics" anymore * PredictedSyncData struct first step: same reads & writes * perf: PredictedRigidbody syncs a blittable struct once instead of doing 5 separate reads/writes * perf: PredictedRigidbody caches IsMoving sqr computations * inlining * perf: PredictedRigidbody FixedUpdate caches sqr computations * perf: PredictedRigidbody RecordState only calls NetworkTime.time getter once * perf: PredictedRigidbody state properties inlined * perf: PredictedRigidbody RecordState only grabs count once * prediction benchmark updated * perf: PredictedRigidbody UpdateGhosting: only every few frames * perf: PredictedRigidbody caches physics transform to use GetPositionAndRotation * ProjectSettings resaved with current Unity version * PredictedRigidbody: update hard follow commented code * perf: PredictedRigidbody smoothFollowThreshold squared caching * perf: PredictedRigidbody MoveTowardsCustom * perf: PredictedRigidbody MoveTowardsCustom: only calculate distance factor once * Prediction: RingBuffer perf comments * feat: Edgegap Lobby Transport & Demo (#3780) * feat: Edgegap Lobby Transport & Example * Code Cleanup * Rename data -> models * Remove debug logs * Log errors as OnClient/ServerError dont always log * Only show joinable lobbies * naming * Transport help url * old unity compat * Unity 2020 compat as well * Readme * lobby name is required + correct placeholder * Update lobby ui prefab too * Lobby creation ui tweaks * lobby url tooltip tweak * Remove test lobby url * chore: old unity compat for PredictedRigidbody (#3781) * fix(EdgegapLobbyKcpTransport): Fixed HelpURL * EncryptionTransportInspector: Fixed button label * fix: Edgegap Lobby Create UI Tweaks (#3782) * chore: Edgegap Lobby Create UI Tweaks * 3 loading dots * help urls --------- Co-authored-by: mischa <16416509+miwarnec@users.noreply.github.com> Co-authored-by: mischa Co-authored-by: Justin Nolan Co-authored-by: JesusLuvsYooh <57072365+JesusLuvsYooh@users.noreply.github.com> Co-authored-by: Robin Rolf Co-authored-by: James Frowen Co-authored-by: ninjakickja <80569286+ninjakickja@users.noreply.github.com> Co-authored-by: mischa <16416509+vis2k@users.noreply.github.com> --- Assets/Mirror/Core/NetworkClient.cs | 12 ++++++++++ Assets/Mirror/Core/NetworkManager.cs | 9 ++++++++ Assets/Mirror/Core/NetworkServer.cs | 14 ++++++++++++ Assets/Mirror/Core/Transport.cs | 7 ++++++ .../NetworkClient/NetworkClientTests.cs | 1 + .../Editor/NetworkServer/NetworkServerTest.cs | 1 + .../Transports/MiddlewareTransportTest.cs | 20 +++++++++++++++++ .../Transports/Latency/LatencySimulation.cs | 11 ++++++---- .../Middleware/MiddlewareTransport.cs | 2 ++ .../Multiplex/MultiplexTransport.cs | 9 ++++++++ ...__Network Manager-NewNetworkManager.cs.txt | 14 ++++++++++++ ...ctions-NewNetworkManagerWithActions.cs.txt | 22 +++++++++++++++++++ 12 files changed, 118 insertions(+), 4 deletions(-) diff --git a/Assets/Mirror/Core/NetworkClient.cs b/Assets/Mirror/Core/NetworkClient.cs index 097dd3e68..b8de1fa8d 100644 --- a/Assets/Mirror/Core/NetworkClient.cs +++ b/Assets/Mirror/Core/NetworkClient.cs @@ -91,6 +91,7 @@ public static partial class NetworkClient public static Action OnConnectedEvent; public static Action OnDisconnectedEvent; public static Action OnErrorEvent; + public static Action OnTransportExceptionEvent; /// Registered spawnable prefabs by assetId. public static readonly Dictionary prefabs = @@ -150,6 +151,7 @@ static void AddTransportHandlers() Transport.active.OnClientDataReceived += OnTransportData; Transport.active.OnClientDisconnected += OnTransportDisconnected; Transport.active.OnClientError += OnTransportError; + Transport.active.OnClientTransportException += OnTransportException; } static void RemoveTransportHandlers() @@ -159,6 +161,7 @@ static void RemoveTransportHandlers() Transport.active.OnClientDataReceived -= OnTransportData; Transport.active.OnClientDisconnected -= OnTransportDisconnected; Transport.active.OnClientError -= OnTransportError; + Transport.active.OnClientTransportException -= OnTransportException; } // connect ///////////////////////////////////////////////////////////// @@ -459,6 +462,14 @@ static void OnTransportError(TransportError error, string reason) OnErrorEvent?.Invoke(error, reason); } + static void OnTransportException(Exception exception) + { + // transport errors will happen. logging a warning is enough. + // make sure the user does not panic. + Debug.LogWarning($"Client Transport Exception: {exception}. This is fine."); + OnTransportExceptionEvent?.Invoke(exception); + } + // send //////////////////////////////////////////////////////////////// /// Send a NetworkMessage to the server over the given channel. public static void Send(T message, int channelId = Channels.Reliable) @@ -1807,6 +1818,7 @@ public static void Shutdown() OnConnectedEvent = null; OnDisconnectedEvent = null; OnErrorEvent = null; + OnTransportExceptionEvent = null; } // GUI ///////////////////////////////////////////////////////////////// diff --git a/Assets/Mirror/Core/NetworkManager.cs b/Assets/Mirror/Core/NetworkManager.cs index 86e0f84bd..b3ceaacd6 100644 --- a/Assets/Mirror/Core/NetworkManager.cs +++ b/Assets/Mirror/Core/NetworkManager.cs @@ -773,6 +773,7 @@ void RegisterServerMessages() NetworkServer.OnConnectedEvent = OnServerConnectInternal; NetworkServer.OnDisconnectedEvent = OnServerDisconnect; NetworkServer.OnErrorEvent = OnServerError; + NetworkServer.OnTransportExceptionEvent = OnServerTransportException; NetworkServer.RegisterHandler(OnServerAddPlayerInternal); // Network Server initially registers its own handler for this, so we replace it here. @@ -784,6 +785,8 @@ void RegisterClientMessages() NetworkClient.OnConnectedEvent = OnClientConnectInternal; NetworkClient.OnDisconnectedEvent = OnClientDisconnectInternal; NetworkClient.OnErrorEvent = OnClientError; + NetworkClient.OnTransportExceptionEvent = OnClientTransportException; + // Don't require authentication because server may send NotReadyMessage from ServerChangeScene NetworkClient.RegisterHandler(OnClientNotReadyMessageInternal, false); NetworkClient.RegisterHandler(OnClientSceneInternal, false); @@ -1399,6 +1402,9 @@ public virtual void OnServerAddPlayer(NetworkConnectionToClient conn) /// Called on server when transport raises an exception. NetworkConnection may be null. public virtual void OnServerError(NetworkConnectionToClient conn, TransportError error, string reason) { } + /// Called on server when transport raises an exception. NetworkConnection may be null. + public virtual void OnServerTransportException(NetworkConnectionToClient conn, Exception exception) { } + /// Called from ServerChangeScene immediately before SceneManager.LoadSceneAsync is executed public virtual void OnServerChangeScene(string newSceneName) { } @@ -1447,6 +1453,9 @@ public virtual void OnConnectionQualityChanged(ConnectionQuality previous, Conne /// Called on client when transport raises an exception. public virtual void OnClientError(TransportError error, string reason) { } + /// Called on client when transport raises an exception. + public virtual void OnClientTransportException(Exception exception) { } + /// Called on clients when a servers tells the client it is no longer ready, e.g. when switching scenes. public virtual void OnClientNotReady() { } diff --git a/Assets/Mirror/Core/NetworkServer.cs b/Assets/Mirror/Core/NetworkServer.cs index 7b5026583..eafa74a41 100644 --- a/Assets/Mirror/Core/NetworkServer.cs +++ b/Assets/Mirror/Core/NetworkServer.cs @@ -89,6 +89,7 @@ public static partial class NetworkServer public static Action OnConnectedEvent; public static Action OnDisconnectedEvent; public static Action OnErrorEvent; + public static Action OnTransportExceptionEvent; // keep track of actual achieved tick rate. // might become lower under heavy load. @@ -184,6 +185,7 @@ static void AddTransportHandlers() Transport.active.OnServerDataReceived += OnTransportData; Transport.active.OnServerDisconnected += OnTransportDisconnected; Transport.active.OnServerError += OnTransportError; + Transport.active.OnServerTransportException += OnTransportException; } /// Shuts down the server and disconnects all clients @@ -243,6 +245,7 @@ public static void Shutdown() OnConnectedEvent = null; OnDisconnectedEvent = null; OnErrorEvent = null; + OnTransportExceptionEvent = null; if (aoi != null) aoi.ResetState(); } @@ -850,6 +853,17 @@ static void OnTransportError(int connectionId, TransportError error, string reas OnErrorEvent?.Invoke(conn, error, reason); } + // transport errors are forwarded to high level + static void OnTransportException(int connectionId, Exception exception) + { + // transport errors will happen. logging a warning is enough. + // make sure the user does not panic. + Debug.LogWarning($"Server Transport Exception for connId={connectionId}: {exception}"); + // try get connection. passes null otherwise. + connections.TryGetValue(connectionId, out NetworkConnectionToClient conn); + OnTransportExceptionEvent?.Invoke(conn, exception); + } + /// Destroys all of the connection's owned objects on the server. // This is used when a client disconnects, to remove the players for // that client. This also destroys non-player objects that have client diff --git a/Assets/Mirror/Core/Transport.cs b/Assets/Mirror/Core/Transport.cs index 7f488ebf3..a20d84f78 100644 --- a/Assets/Mirror/Core/Transport.cs +++ b/Assets/Mirror/Core/Transport.cs @@ -59,6 +59,9 @@ public abstract class Transport : MonoBehaviour /// Called by Transport when the client encountered an error. public Action OnClientError; + /// Called by Transport when the client encountered an error. + public Action OnClientTransportException; + /// Called by Transport when the client disconnected from the server. public Action OnClientDisconnected; @@ -80,6 +83,10 @@ public abstract class Transport : MonoBehaviour /// If a Disconnect will also be raised, raise the Error first. public Action OnServerError; + /// Called by Transport when a server's connection encountered a problem. + /// If a Disconnect will also be raised, raise the Error first. + public Action OnServerTransportException; + /// Called by Transport when a client disconnected from the server. public Action OnServerDisconnected; diff --git a/Assets/Mirror/Tests/Editor/NetworkClient/NetworkClientTests.cs b/Assets/Mirror/Tests/Editor/NetworkClient/NetworkClientTests.cs index 5275e768d..524457207 100644 --- a/Assets/Mirror/Tests/Editor/NetworkClient/NetworkClientTests.cs +++ b/Assets/Mirror/Tests/Editor/NetworkClient/NetworkClientTests.cs @@ -146,6 +146,7 @@ public void ShutdownCleanup() Assert.That(NetworkClient.OnConnectedEvent, Is.Null); Assert.That(NetworkClient.OnDisconnectedEvent, Is.Null); Assert.That(NetworkClient.OnErrorEvent, Is.Null); + Assert.That(NetworkClient.OnTransportExceptionEvent, Is.Null); } // test to prevent a bug where host mode scene transitions would diff --git a/Assets/Mirror/Tests/Editor/NetworkServer/NetworkServerTest.cs b/Assets/Mirror/Tests/Editor/NetworkServer/NetworkServerTest.cs index 6031dafc5..ff2c56e18 100644 --- a/Assets/Mirror/Tests/Editor/NetworkServer/NetworkServerTest.cs +++ b/Assets/Mirror/Tests/Editor/NetworkServer/NetworkServerTest.cs @@ -1162,6 +1162,7 @@ public void ShutdownCleanup() Assert.That(NetworkServer.OnConnectedEvent, Is.Null); Assert.That(NetworkServer.OnDisconnectedEvent, Is.Null); Assert.That(NetworkServer.OnErrorEvent, Is.Null); + Assert.That(NetworkServer.OnTransportExceptionEvent, Is.Null); } [Test] diff --git a/Assets/Mirror/Tests/Editor/Transports/MiddlewareTransportTest.cs b/Assets/Mirror/Tests/Editor/Transports/MiddlewareTransportTest.cs index d4905dcbf..32af6cbf7 100644 --- a/Assets/Mirror/Tests/Editor/Transports/MiddlewareTransportTest.cs +++ b/Assets/Mirror/Tests/Editor/Transports/MiddlewareTransportTest.cs @@ -271,6 +271,26 @@ public void TestClientErrorCallback() Assert.That(called, Is.EqualTo(2)); } + [Test] + public void TestClientExceptionCallback() + { + int called = 0; + middleware.OnClientTransportException = (exception) => + { + called++; + // Assert that exception is System.Exception + Assert.That(exception, Is.TypeOf()); + }; + // connect to give callback to inner + middleware.ClientConnect("localhost"); + + inner.OnClientTransportException.Invoke(new Exception()); + Assert.That(called, Is.EqualTo(1)); + + inner.OnClientTransportException.Invoke(new Exception()); + Assert.That(called, Is.EqualTo(2)); + } + [Test] [TestCase(0)] [TestCase(1)] diff --git a/Assets/Mirror/Transports/Latency/LatencySimulation.cs b/Assets/Mirror/Transports/Latency/LatencySimulation.cs index c286326a7..0277d7960 100644 --- a/Assets/Mirror/Transports/Latency/LatencySimulation.cs +++ b/Assets/Mirror/Transports/Latency/LatencySimulation.cs @@ -161,18 +161,20 @@ void SimulateSend( public override void ClientConnect(string address) { - wrap.OnClientConnected = OnClientConnected; + wrap.OnClientConnected = OnClientConnected; wrap.OnClientDataReceived = OnClientDataReceived; - wrap.OnClientError = OnClientError; + wrap.OnClientError = OnClientError; + wrap.OnClientTransportException = OnClientTransportException; wrap.OnClientDisconnected = OnClientDisconnected; wrap.ClientConnect(address); } public override void ClientConnect(Uri uri) { - wrap.OnClientConnected = OnClientConnected; + wrap.OnClientConnected = OnClientConnected; wrap.OnClientDataReceived = OnClientDataReceived; - wrap.OnClientError = OnClientError; + wrap.OnClientError = OnClientError; + wrap.OnClientTransportException = OnClientTransportException; wrap.OnClientDisconnected = OnClientDisconnected; wrap.ClientConnect(uri); } @@ -211,6 +213,7 @@ public override void ServerStart() wrap.OnServerConnected = OnServerConnected; wrap.OnServerDataReceived = OnServerDataReceived; wrap.OnServerError = OnServerError; + wrap.OnServerTransportException = OnServerTransportException; wrap.OnServerDisconnected = OnServerDisconnected; wrap.ServerStart(); } diff --git a/Assets/Mirror/Transports/Middleware/MiddlewareTransport.cs b/Assets/Mirror/Transports/Middleware/MiddlewareTransport.cs index 7dd934a52..fc7c9cd00 100644 --- a/Assets/Mirror/Transports/Middleware/MiddlewareTransport.cs +++ b/Assets/Mirror/Transports/Middleware/MiddlewareTransport.cs @@ -26,6 +26,7 @@ public override void ClientConnect(string address) inner.OnClientDataReceived = OnClientDataReceived; inner.OnClientDisconnected = OnClientDisconnected; inner.OnClientError = OnClientError; + inner.OnClientTransportException = OnClientTransportException; inner.ClientConnect(address); } @@ -45,6 +46,7 @@ public override void ServerStart() inner.OnServerDataReceived = OnServerDataReceived; inner.OnServerDisconnected = OnServerDisconnected; inner.OnServerError = OnServerError; + inner.OnServerTransportException = OnServerTransportException; inner.ServerStart(); } diff --git a/Assets/Mirror/Transports/Multiplex/MultiplexTransport.cs b/Assets/Mirror/Transports/Multiplex/MultiplexTransport.cs index 3ef97d755..d567115f8 100644 --- a/Assets/Mirror/Transports/Multiplex/MultiplexTransport.cs +++ b/Assets/Mirror/Transports/Multiplex/MultiplexTransport.cs @@ -197,6 +197,7 @@ public override void ClientConnect(string address) transport.OnClientConnected = OnClientConnected; transport.OnClientDataReceived = OnClientDataReceived; transport.OnClientError = OnClientError; + transport.OnClientTransportException = OnClientTransportException; transport.OnClientDisconnected = OnClientDisconnected; transport.ClientConnect(address); return; @@ -217,6 +218,7 @@ public override void ClientConnect(Uri uri) transport.OnClientConnected = OnClientConnected; transport.OnClientDataReceived = OnClientDataReceived; transport.OnClientError = OnClientError; + transport.OnClientTransportException = OnClientTransportException; transport.OnClientDisconnected = OnClientDisconnected; transport.ClientConnect(uri); return; @@ -306,6 +308,13 @@ void AddServerCallbacks() OnServerError.Invoke(multiplexedId, error, reason); }; + transport.OnServerTransportException = (originalConnectionId, exception) => + { + // invoke Multiplex event with multiplexed connectionId + int multiplexedId = MultiplexId(originalConnectionId, transportIndex); + OnServerTransportException.Invoke(multiplexedId, exception); + }; + transport.OnServerDisconnected = originalConnectionId => { // invoke Multiplex event with multiplexed connectionId diff --git a/Assets/ScriptTemplates/50-Mirror__Network Manager-NewNetworkManager.cs.txt b/Assets/ScriptTemplates/50-Mirror__Network Manager-NewNetworkManager.cs.txt index 36f3c999d..15b0611df 100644 --- a/Assets/ScriptTemplates/50-Mirror__Network Manager-NewNetworkManager.cs.txt +++ b/Assets/ScriptTemplates/50-Mirror__Network Manager-NewNetworkManager.cs.txt @@ -171,6 +171,14 @@ public class #SCRIPTNAME# : NetworkManager /// String message of the error. public override void OnServerError(NetworkConnectionToClient conn, TransportError transportError, string message) { } + /// + /// Called on server when transport raises an exception. + /// NetworkConnection may be null. + /// + /// Connection of the client...may be null + /// Exception thrown from the Transport. + public override void OnServerTransportException(NetworkConnectionToClient conn, Exception exception) { } + #endregion #region Client System Callbacks @@ -203,6 +211,12 @@ public class #SCRIPTNAME# : NetworkManager /// String message of the error. public override void OnClientError(TransportError transportError, string message) { } + /// + /// Called on client when transport raises an exception. + /// + /// Exception thrown from the Transport. + public override void OnClientTransportException(Exception exception) { } + #endregion #region Start & Stop Callbacks diff --git a/Assets/ScriptTemplates/51-Mirror__Network Manager With Actions-NewNetworkManagerWithActions.cs.txt b/Assets/ScriptTemplates/51-Mirror__Network Manager With Actions-NewNetworkManagerWithActions.cs.txt index c250f6831..887e76f97 100644 --- a/Assets/ScriptTemplates/51-Mirror__Network Manager With Actions-NewNetworkManagerWithActions.cs.txt +++ b/Assets/ScriptTemplates/51-Mirror__Network Manager With Actions-NewNetworkManagerWithActions.cs.txt @@ -28,11 +28,13 @@ public class #SCRIPTNAME# : NetworkManager public event Action OnServerAddPlayerAction; public event Action OnServerDisconnectAction; public event Action OnServerErrorAction; + public event Action OnServerTransportExceptionAction; public event Action OnClientConnectAction; public event Action OnClientDisconnectAction; public event Action OnClientNotReadyAction; public event Action OnClientErrorAction; + public event Action OnClientTransportExceptionAction; public event Action OnStartServerAction; public event Action OnStopServerAction; @@ -239,6 +241,17 @@ public class #SCRIPTNAME# : NetworkManager OnServerErrorAction?.Invoke(conn, transportError, message); } + /// + /// Called on server when transport raises an exception. + /// NetworkConnection may be null. + /// + /// Connection of the client...may be null + /// Exception thrown from the Transport. + public override void OnServerTransportException(NetworkConnectionToClient conn, Exception exception) + { + OnServerTransportExceptionAction?.Invoke(conn, exception); + } + #endregion #region Client System Callbacks @@ -281,6 +294,15 @@ public class #SCRIPTNAME# : NetworkManager OnClientErrorAction?.Invoke(transportError, message); } + /// + /// Called on client when transport raises an exception. + /// + /// Exception thrown from the Transport. + public override void OnClientTransportException(Exception exception) + { + OnClientTransportExceptionAction?.Invoke(exception); + } + #endregion #region Start & Stop Callbacks