From f761a864c14c103487cb59aa28f719e1f73f11fe Mon Sep 17 00:00:00 2001 From: vis2k Date: Wed, 27 Jan 2021 14:41:26 +0800 Subject: [PATCH] NetworkServer/NetworkClient: add/remove from transport events instead of overwriting them. This allows addons to hook into Transport OnConnect/OnData/etc. --- Assets/Mirror/Runtime/NetworkClient.cs | 18 ++++++++++++++---- Assets/Mirror/Runtime/NetworkServer.cs | 17 +++++++++++++---- Assets/Mirror/Runtime/Transport/Transport.cs | 18 +++++++++--------- 3 files changed, 36 insertions(+), 17 deletions(-) diff --git a/Assets/Mirror/Runtime/NetworkClient.cs b/Assets/Mirror/Runtime/NetworkClient.cs index 9a3e66c27..d8c55760d 100644 --- a/Assets/Mirror/Runtime/NetworkClient.cs +++ b/Assets/Mirror/Runtime/NetworkClient.cs @@ -147,10 +147,18 @@ public static void DisconnectLocalServer() static void AddTransportHandlers() { - Transport.activeTransport.OnClientConnected = OnConnected; - Transport.activeTransport.OnClientDataReceived = OnDataReceived; - Transport.activeTransport.OnClientDisconnected = OnDisconnected; - Transport.activeTransport.OnClientError = OnError; + Transport.activeTransport.OnClientConnected += OnConnected; + Transport.activeTransport.OnClientDataReceived += OnDataReceived; + Transport.activeTransport.OnClientDisconnected += OnDisconnected; + Transport.activeTransport.OnClientError += OnError; + } + + static void RemoveTransportHandlers() + { + Transport.activeTransport.OnClientConnected -= OnConnected; + Transport.activeTransport.OnClientDataReceived -= OnDataReceived; + Transport.activeTransport.OnClientDisconnected -= OnDisconnected; + Transport.activeTransport.OnClientError -= OnError; } static void OnError(Exception exception) @@ -218,6 +226,8 @@ public static void Disconnect() connection = null; } } + + RemoveTransportHandlers(); } /// diff --git a/Assets/Mirror/Runtime/NetworkServer.cs b/Assets/Mirror/Runtime/NetworkServer.cs index 734cc0f68..c04e558cb 100644 --- a/Assets/Mirror/Runtime/NetworkServer.cs +++ b/Assets/Mirror/Runtime/NetworkServer.cs @@ -98,6 +98,7 @@ public static void Shutdown() CleanupNetworkIdentities(); NetworkIdentity.ResetNextNetworkId(); + RemoveTransportHandlers(); } static void CleanupNetworkIdentities() @@ -489,10 +490,18 @@ static void CheckForInactiveConnections() static void AddTransportHandlers() { - Transport.activeTransport.OnServerConnected = OnConnected; - Transport.activeTransport.OnServerDataReceived = OnDataReceived; - Transport.activeTransport.OnServerDisconnected = OnDisconnected; - Transport.activeTransport.OnServerError = OnError; + Transport.activeTransport.OnServerConnected += OnConnected; + Transport.activeTransport.OnServerDataReceived += OnDataReceived; + Transport.activeTransport.OnServerDisconnected += OnDisconnected; + Transport.activeTransport.OnServerError += OnError; + } + + static void RemoveTransportHandlers() + { + Transport.activeTransport.OnServerConnected -= OnConnected; + Transport.activeTransport.OnServerDataReceived -= OnDataReceived; + Transport.activeTransport.OnServerDisconnected -= OnDisconnected; + Transport.activeTransport.OnServerError -= OnError; } static void OnConnected(int connectionId) diff --git a/Assets/Mirror/Runtime/Transport/Transport.cs b/Assets/Mirror/Runtime/Transport/Transport.cs index bc3818819..81101088b 100644 --- a/Assets/Mirror/Runtime/Transport/Transport.cs +++ b/Assets/Mirror/Runtime/Transport/Transport.cs @@ -8,7 +8,7 @@ namespace Mirror /// /// ///

- /// Transport Rules + /// Transport Rules ///

/// /// @@ -58,25 +58,25 @@ public abstract class Transport : MonoBehaviour /// Notify subscribers when when this client establish a successful connection to the server /// callback() /// - public Action OnClientConnected = () => Debug.LogWarning("OnClientConnected called with no handler"); + public Action OnClientConnected; /// /// Notify subscribers when this client receive data from the server /// callback(ArraySegment<byte> data, int channel) /// - public Action, int> OnClientDataReceived = (data, channel) => Debug.LogWarning("OnClientDataReceived called with no handler"); + public Action, int> OnClientDataReceived; /// /// Notify subscribers when this client encounters an error communicating with the server /// callback(Exception e) /// - public Action OnClientError = (error) => Debug.LogWarning("OnClientError called with no handler"); + public Action OnClientError; /// /// Notify subscribers when this client disconnects from the server /// callback() /// - public Action OnClientDisconnected = () => Debug.LogWarning("OnClientDisconnected called with no handler"); + public Action OnClientDisconnected; /// /// Determines if we are currently connected to the server @@ -131,25 +131,25 @@ public virtual void ClientConnect(Uri uri) /// Notify subscribers when a client connects to this server /// callback(int connId) /// - public Action OnServerConnected = (connId) => Debug.LogWarning("OnServerConnected called with no handler"); + public Action OnServerConnected; /// /// Notify subscribers when this server receives data from the client /// callback(int connId, ArraySegment<byte> data, int channel) /// - public Action, int> OnServerDataReceived = (connId, data, channel) => Debug.LogWarning("OnServerDataReceived called with no handler"); + public Action, int> OnServerDataReceived; /// /// Notify subscribers when this server has some problem communicating with the client /// callback(int connId, Exception e) /// - public Action OnServerError = (connId, error) => Debug.LogWarning("OnServerError called with no handler"); + public Action OnServerError; /// /// Notify subscribers when a client disconnects from this server /// callback(int connId) /// - public Action OnServerDisconnected = (connId) => Debug.LogWarning("OnServerDisconnected called with no handler"); + public Action OnServerDisconnected; /// /// Determines if the server is up and running