From 006d5d3bc886382e69e8b0d91c194e09c277a1c4 Mon Sep 17 00:00:00 2001 From: vis2k Date: Sun, 24 Mar 2019 20:02:14 +0100 Subject: [PATCH 01/27] NetworkClient.handlers made static --- Assets/Mirror/Runtime/NetworkClient.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Assets/Mirror/Runtime/NetworkClient.cs b/Assets/Mirror/Runtime/NetworkClient.cs index b9d680713..e7956fe91 100644 --- a/Assets/Mirror/Runtime/NetworkClient.cs +++ b/Assets/Mirror/Runtime/NetworkClient.cs @@ -12,7 +12,7 @@ public class NetworkClient [Obsolete("Use NetworkClient.singleton instead. There is always exactly one client.")] public static List allClients => new List{singleton}; - public readonly Dictionary handlers = new Dictionary(); + public static readonly Dictionary handlers = new Dictionary(); public NetworkConnection connection { get; protected set; } From f351a8a6d45d64a44a4eba32c689f77b5541152b Mon Sep 17 00:00:00 2001 From: vis2k Date: Sun, 24 Mar 2019 20:03:45 +0100 Subject: [PATCH 02/27] NetworkClient.connectState made static --- Assets/Mirror/Runtime/NetworkClient.cs | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/Assets/Mirror/Runtime/NetworkClient.cs b/Assets/Mirror/Runtime/NetworkClient.cs index e7956fe91..002daaf00 100644 --- a/Assets/Mirror/Runtime/NetworkClient.cs +++ b/Assets/Mirror/Runtime/NetworkClient.cs @@ -4,6 +4,14 @@ namespace Mirror { + public enum ConnectState + { + None, + Connecting, + Connected, + Disconnected + } + public class NetworkClient { // the client (can be a regular NetworkClient or a LocalClient) @@ -16,14 +24,7 @@ public class NetworkClient public NetworkConnection connection { get; protected set; } - protected enum ConnectState - { - None, - Connecting, - Connected, - Disconnected - } - protected ConnectState connectState = ConnectState.None; + internal static ConnectState connectState = ConnectState.None; public string serverIp { get; private set; } = ""; From 882a7d5ba8e75493b81f2952c0d05232985706f3 Mon Sep 17 00:00:00 2001 From: vis2k Date: Sun, 24 Mar 2019 20:10:15 +0100 Subject: [PATCH 03/27] NetworkClient.connection made static --- Assets/Mirror/Components/NetworkLobbyManager.cs | 2 +- Assets/Mirror/Runtime/NetworkClient.cs | 2 +- Assets/Mirror/Runtime/NetworkManager.cs | 2 +- Assets/Mirror/Runtime/NetworkManagerHUD.cs | 6 +++--- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/Assets/Mirror/Components/NetworkLobbyManager.cs b/Assets/Mirror/Components/NetworkLobbyManager.cs index 666e621ef..469cc9b3e 100644 --- a/Assets/Mirror/Components/NetworkLobbyManager.cs +++ b/Assets/Mirror/Components/NetworkLobbyManager.cs @@ -375,7 +375,7 @@ public override void OnClientChangeScene(string newSceneName) if (SceneManager.GetActiveScene().name == LobbyScene && newSceneName == GameplayScene && dontDestroyOnLoad && IsClientConnected() && client != null) { - GameObject lobbyPlayer = client?.connection?.playerController?.gameObject; + GameObject lobbyPlayer = NetworkClient.connection?.playerController?.gameObject; if (lobbyPlayer != null) { lobbyPlayer.transform.SetParent(null); diff --git a/Assets/Mirror/Runtime/NetworkClient.cs b/Assets/Mirror/Runtime/NetworkClient.cs index 002daaf00..9514a3e49 100644 --- a/Assets/Mirror/Runtime/NetworkClient.cs +++ b/Assets/Mirror/Runtime/NetworkClient.cs @@ -22,7 +22,7 @@ public class NetworkClient public static readonly Dictionary handlers = new Dictionary(); - public NetworkConnection connection { get; protected set; } + public static NetworkConnection connection { get; protected set; } internal static ConnectState connectState = ConnectState.None; diff --git a/Assets/Mirror/Runtime/NetworkManager.cs b/Assets/Mirror/Runtime/NetworkManager.cs index f38d51249..58af0f80c 100644 --- a/Assets/Mirror/Runtime/NetworkManager.cs +++ b/Assets/Mirror/Runtime/NetworkManager.cs @@ -511,7 +511,7 @@ void FinishLoadScene() if (IsClientConnected() && client != null) { RegisterClientMessages(client); - OnClientSceneChanged(client.connection); + OnClientSceneChanged(NetworkClient.connection); } } diff --git a/Assets/Mirror/Runtime/NetworkManagerHUD.cs b/Assets/Mirror/Runtime/NetworkManagerHUD.cs index 4c546608b..eac015e5f 100644 --- a/Assets/Mirror/Runtime/NetworkManagerHUD.cs +++ b/Assets/Mirror/Runtime/NetworkManagerHUD.cs @@ -26,8 +26,8 @@ void OnGUI() if (!showGUI) return; - bool noConnection = (manager.client == null || manager.client.connection == null || - manager.client.connection.connectionId == -1); + bool noConnection = (manager.client == null || NetworkClient.connection == null || + NetworkClient.connection.connectionId == -1); GUILayout.BeginArea(new Rect(10 + offsetX, 40 + offsetY, 215, 9999)); if (!manager.IsClientConnected() && !NetworkServer.active) @@ -91,7 +91,7 @@ void OnGUI() { if (GUILayout.Button("Client Ready")) { - ClientScene.Ready(manager.client.connection); + ClientScene.Ready(NetworkClient.connection); if (ClientScene.localPlayer == null) { From 604c205025241885f37fc9a2d5b0bd0ae11cc304 Mon Sep 17 00:00:00 2001 From: vis2k Date: Sun, 24 Mar 2019 20:10:54 +0100 Subject: [PATCH 04/27] NetworkClient.serverIp made static --- Assets/Mirror/Runtime/NetworkClient.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Assets/Mirror/Runtime/NetworkClient.cs b/Assets/Mirror/Runtime/NetworkClient.cs index 9514a3e49..c8175002c 100644 --- a/Assets/Mirror/Runtime/NetworkClient.cs +++ b/Assets/Mirror/Runtime/NetworkClient.cs @@ -26,7 +26,7 @@ public class NetworkClient internal static ConnectState connectState = ConnectState.None; - public string serverIp { get; private set; } = ""; + public static string serverIp { get; private set; } = ""; // active is true while a client is connecting/connected // (= while the network is active) From 180d3f8cf43be9c71154e4661f4c5838e0e3ded2 Mon Sep 17 00:00:00 2001 From: vis2k Date: Sun, 24 Mar 2019 20:13:49 +0100 Subject: [PATCH 05/27] NetworkClient.isConnected made static --- Assets/Mirror/Components/NetworkLobbyManager.cs | 2 +- Assets/Mirror/Runtime/NetworkClient.cs | 2 +- Assets/Mirror/Runtime/NetworkManager.cs | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Assets/Mirror/Components/NetworkLobbyManager.cs b/Assets/Mirror/Components/NetworkLobbyManager.cs index 469cc9b3e..8aaa23338 100644 --- a/Assets/Mirror/Components/NetworkLobbyManager.cs +++ b/Assets/Mirror/Components/NetworkLobbyManager.cs @@ -392,7 +392,7 @@ public override void OnClientSceneChanged(NetworkConnection conn) { if (SceneManager.GetActiveScene().name == LobbyScene) { - if (client.isConnected) + if (NetworkClient.isConnected) CallOnClientEnterLobby(); } else diff --git a/Assets/Mirror/Runtime/NetworkClient.cs b/Assets/Mirror/Runtime/NetworkClient.cs index c8175002c..35dfb4357 100644 --- a/Assets/Mirror/Runtime/NetworkClient.cs +++ b/Assets/Mirror/Runtime/NetworkClient.cs @@ -32,7 +32,7 @@ public class NetworkClient // (= while the network is active) public static bool active { get; protected set; } - public bool isConnected => connectState == ConnectState.Connected; + public static bool isConnected => connectState == ConnectState.Connected; public NetworkClient() { diff --git a/Assets/Mirror/Runtime/NetworkManager.cs b/Assets/Mirror/Runtime/NetworkManager.cs index 58af0f80c..c47615d9e 100644 --- a/Assets/Mirror/Runtime/NetworkManager.cs +++ b/Assets/Mirror/Runtime/NetworkManager.cs @@ -546,7 +546,7 @@ public static void UnRegisterStartPosition(Transform start) public bool IsClientConnected() { - return client != null && client.isConnected; + return client != null && NetworkClient.isConnected; } // this is the only way to clear the singleton, so another instance can be created. From 558e1d875465d536d9675db68c48d1eefc0f2442 Mon Sep 17 00:00:00 2001 From: vis2k Date: Sun, 24 Mar 2019 20:14:39 +0100 Subject: [PATCH 06/27] NetworkClient.SetHandlers made static --- Assets/Mirror/Runtime/NetworkClient.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Assets/Mirror/Runtime/NetworkClient.cs b/Assets/Mirror/Runtime/NetworkClient.cs index 35dfb4357..e8905c777 100644 --- a/Assets/Mirror/Runtime/NetworkClient.cs +++ b/Assets/Mirror/Runtime/NetworkClient.cs @@ -46,7 +46,7 @@ public NetworkClient() singleton = this; } - internal void SetHandlers(NetworkConnection conn) + internal static void SetHandlers(NetworkConnection conn) { conn.SetHandlers(handlers); } From 8c125d2fd3c4a3910d904a4720d5d0acd56db16b Mon Sep 17 00:00:00 2001 From: vis2k Date: Sun, 24 Mar 2019 20:16:52 +0100 Subject: [PATCH 07/27] NetworkClient.Send made static --- Assets/Mirror/Runtime/NetworkClient.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Assets/Mirror/Runtime/NetworkClient.cs b/Assets/Mirror/Runtime/NetworkClient.cs index e8905c777..428692561 100644 --- a/Assets/Mirror/Runtime/NetworkClient.cs +++ b/Assets/Mirror/Runtime/NetworkClient.cs @@ -158,7 +158,7 @@ public bool Send(short msgType, MessageBase msg) return false; } - public bool Send(T message) where T : IMessageBase + public static bool Send(T message) where T : IMessageBase { if (connection != null) { From cd56c6a6f7c5d218f469be9c5c047861313c60e7 Mon Sep 17 00:00:00 2001 From: vis2k Date: Sun, 24 Mar 2019 20:17:06 +0100 Subject: [PATCH 08/27] NetworkTime.UpdateClient doesn't require NetworkClient anymore --- Assets/Mirror/Runtime/NetworkClient.cs | 4 ++-- Assets/Mirror/Runtime/NetworkTime.cs | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Assets/Mirror/Runtime/NetworkClient.cs b/Assets/Mirror/Runtime/NetworkClient.cs index 428692561..82f5421a2 100644 --- a/Assets/Mirror/Runtime/NetworkClient.cs +++ b/Assets/Mirror/Runtime/NetworkClient.cs @@ -111,7 +111,7 @@ void OnConnected() // the handler may want to send messages to the client // thus we should set the connected state before calling the handler connectState = ConnectState.Connected; - NetworkTime.UpdateClient(this); + NetworkTime.UpdateClient(); connection.InvokeHandler(new ConnectMessage()); } else Debug.LogError("Skipped Connect message handling because m_Connection is null."); @@ -178,7 +178,7 @@ internal virtual void Update() // only update things while connected if (active && connectState == ConnectState.Connected) { - NetworkTime.UpdateClient(this); + NetworkTime.UpdateClient(); } } diff --git a/Assets/Mirror/Runtime/NetworkTime.cs b/Assets/Mirror/Runtime/NetworkTime.cs index 3f04e45fb..4bd35bd11 100644 --- a/Assets/Mirror/Runtime/NetworkTime.cs +++ b/Assets/Mirror/Runtime/NetworkTime.cs @@ -50,12 +50,12 @@ internal static NetworkPingMessage GetPing() return new NetworkPingMessage(LocalTime()); } - internal static void UpdateClient(NetworkClient networkClient) + internal static void UpdateClient() { if (Time.time - lastPingTime >= PingFrequency) { NetworkPingMessage pingMessage = GetPing(); - networkClient.Send(pingMessage); + NetworkClient.Send(pingMessage); lastPingTime = Time.time; } } From 4abc58b136f1ca7a608184c2d36e6d2c36c51169 Mon Sep 17 00:00:00 2001 From: vis2k Date: Sun, 24 Mar 2019 20:19:02 +0100 Subject: [PATCH 09/27] NetworkClient.Send(msgId, msg) made static --- Assets/Mirror/Runtime/NetworkClient.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Assets/Mirror/Runtime/NetworkClient.cs b/Assets/Mirror/Runtime/NetworkClient.cs index 82f5421a2..19be8d1b6 100644 --- a/Assets/Mirror/Runtime/NetworkClient.cs +++ b/Assets/Mirror/Runtime/NetworkClient.cs @@ -143,7 +143,7 @@ void RemoveTransportHandlers() } [Obsolete("Use SendMessage instead with no message id instead")] - public bool Send(short msgType, MessageBase msg) + public static bool Send(short msgType, MessageBase msg) { if (connection != null) { From ba00554e0c00353a8472420334f13b14dde75456 Mon Sep 17 00:00:00 2001 From: vis2k Date: Sun, 24 Mar 2019 20:19:27 +0100 Subject: [PATCH 10/27] NetworkClient.GetRTT made static --- Assets/Mirror/Runtime/NetworkClient.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Assets/Mirror/Runtime/NetworkClient.cs b/Assets/Mirror/Runtime/NetworkClient.cs index 19be8d1b6..6a3966f83 100644 --- a/Assets/Mirror/Runtime/NetworkClient.cs +++ b/Assets/Mirror/Runtime/NetworkClient.cs @@ -229,7 +229,7 @@ void GenerateError(byte error) */ [Obsolete("Use NetworkTime.rtt instead")] - public float GetRTT() + public static float GetRTT() { return (float)NetworkTime.rtt; } From 712aecb4525b2bb8aacbf9f3ee1e788f991a6e62 Mon Sep 17 00:00:00 2001 From: vis2k Date: Sun, 24 Mar 2019 20:24:30 +0100 Subject: [PATCH 11/27] NetworkClient.REgisterHandle made static --- Assets/Mirror/Runtime/NetworkClient.cs | 6 +++--- Assets/Mirror/Runtime/NetworkManager.cs | 10 +++++----- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/Assets/Mirror/Runtime/NetworkClient.cs b/Assets/Mirror/Runtime/NetworkClient.cs index 6a3966f83..28120548a 100644 --- a/Assets/Mirror/Runtime/NetworkClient.cs +++ b/Assets/Mirror/Runtime/NetworkClient.cs @@ -269,7 +269,7 @@ internal void RegisterSystemHandlers(bool localClient) } [Obsolete("Use RegisterHandler instead")] - public void RegisterHandler(int msgType, NetworkMessageDelegate handler) + public static void RegisterHandler(int msgType, NetworkMessageDelegate handler) { if (handlers.ContainsKey(msgType)) { @@ -279,12 +279,12 @@ public void RegisterHandler(int msgType, NetworkMessageDelegate handler) } [Obsolete("Use RegisterHandler instead")] - public void RegisterHandler(MsgType msgType, NetworkMessageDelegate handler) + public static void RegisterHandler(MsgType msgType, NetworkMessageDelegate handler) { RegisterHandler((int)msgType, handler); } - public void RegisterHandler(Action handler) where T : IMessageBase, new() + public static void RegisterHandler(Action handler) where T : IMessageBase, new() { int msgType = MessagePacker.GetId(); if (handlers.ContainsKey(msgType)) diff --git a/Assets/Mirror/Runtime/NetworkManager.cs b/Assets/Mirror/Runtime/NetworkManager.cs index c47615d9e..1a1ee3080 100644 --- a/Assets/Mirror/Runtime/NetworkManager.cs +++ b/Assets/Mirror/Runtime/NetworkManager.cs @@ -300,11 +300,11 @@ public bool StartServer() internal void RegisterClientMessages(NetworkClient client) { - client.RegisterHandler(OnClientConnectInternal); - client.RegisterHandler(OnClientDisconnectInternal); - client.RegisterHandler(OnClientNotReadyMessageInternal); - client.RegisterHandler(OnClientErrorInternal); - client.RegisterHandler(OnClientSceneInternal); + NetworkClient.RegisterHandler(OnClientConnectInternal); + NetworkClient.RegisterHandler(OnClientDisconnectInternal); + NetworkClient.RegisterHandler(OnClientNotReadyMessageInternal); + NetworkClient.RegisterHandler(OnClientErrorInternal); + NetworkClient.RegisterHandler(OnClientSceneInternal); if (playerPrefab != null) { From 9eac9838727608e48ab0a7b00b346f27d20cf11d Mon Sep 17 00:00:00 2001 From: vis2k Date: Sun, 24 Mar 2019 20:24:39 +0100 Subject: [PATCH 12/27] . NetworkManager.RegisterClientMessages doesn't require NetworkClient parameter anymore --- Assets/Mirror/Runtime/NetworkManager.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Assets/Mirror/Runtime/NetworkManager.cs b/Assets/Mirror/Runtime/NetworkManager.cs index 1a1ee3080..74ffe8e82 100644 --- a/Assets/Mirror/Runtime/NetworkManager.cs +++ b/Assets/Mirror/Runtime/NetworkManager.cs @@ -298,7 +298,7 @@ public bool StartServer() return true; } - internal void RegisterClientMessages(NetworkClient client) + internal void RegisterClientMessages() { NetworkClient.RegisterHandler(OnClientConnectInternal); NetworkClient.RegisterHandler(OnClientDisconnectInternal); @@ -331,7 +331,7 @@ public NetworkClient StartClient() client = new NetworkClient(); - RegisterClientMessages(client); + RegisterClientMessages(); if (string.IsNullOrEmpty(networkAddress)) { @@ -364,7 +364,7 @@ NetworkClient ConnectLocalClient() if (LogFilter.Debug) Debug.Log("NetworkManager StartHost"); networkAddress = "localhost"; client = ClientScene.ConnectLocalServer(); - RegisterClientMessages(client); + RegisterClientMessages(); return client; } @@ -510,7 +510,7 @@ void FinishLoadScene() if (IsClientConnected() && client != null) { - RegisterClientMessages(client); + RegisterClientMessages(); OnClientSceneChanged(NetworkClient.connection); } } From bc6c2542f6f991a119a251a198da8300f3c81047 Mon Sep 17 00:00:00 2001 From: vis2k Date: Sun, 24 Mar 2019 20:26:23 +0100 Subject: [PATCH 13/27] NetworkClient.UnregisterHandler made static --- Assets/Mirror/Runtime/NetworkClient.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Assets/Mirror/Runtime/NetworkClient.cs b/Assets/Mirror/Runtime/NetworkClient.cs index 28120548a..669ae44ed 100644 --- a/Assets/Mirror/Runtime/NetworkClient.cs +++ b/Assets/Mirror/Runtime/NetworkClient.cs @@ -298,18 +298,18 @@ public static void RegisterHandler(MsgType msgType, NetworkMessageDelegate handl } [Obsolete("Use UnregisterHandler instead")] - public void UnregisterHandler(int msgType) + public static void UnregisterHandler(int msgType) { handlers.Remove(msgType); } [Obsolete("Use UnregisterHandler instead")] - public void UnregisterHandler(MsgType msgType) + public static void UnregisterHandler(MsgType msgType) { UnregisterHandler((int)msgType); } - public void UnregisterHandler() where T : IMessageBase + public static void UnregisterHandler() where T : IMessageBase { // use int to minimize collisions int msgType = MessagePacker.GetId(); From 493bf7fa5f798d45f7e3abaf7c38334cf050c0b3 Mon Sep 17 00:00:00 2001 From: vis2k Date: Sun, 24 Mar 2019 20:27:05 +0100 Subject: [PATCH 14/27] NetworkClient.RegisterSystemHandlers made static --- Assets/Mirror/Runtime/NetworkClient.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Assets/Mirror/Runtime/NetworkClient.cs b/Assets/Mirror/Runtime/NetworkClient.cs index 669ae44ed..5efad3005 100644 --- a/Assets/Mirror/Runtime/NetworkClient.cs +++ b/Assets/Mirror/Runtime/NetworkClient.cs @@ -234,7 +234,7 @@ public static float GetRTT() return (float)NetworkTime.rtt; } - internal void RegisterSystemHandlers(bool localClient) + internal static void RegisterSystemHandlers(bool localClient) { // local client / regular client react to some messages differently. // but we still need to add handlers for all of them to avoid From 42bceb25d9c69efc5044999315abb35109f1a9df Mon Sep 17 00:00:00 2001 From: vis2k Date: Sun, 24 Mar 2019 20:54:45 +0100 Subject: [PATCH 15/27] NetworkClient.OnConnected made static --- Assets/Mirror/Runtime/NetworkClient.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Assets/Mirror/Runtime/NetworkClient.cs b/Assets/Mirror/Runtime/NetworkClient.cs index 5efad3005..d3436a103 100644 --- a/Assets/Mirror/Runtime/NetworkClient.cs +++ b/Assets/Mirror/Runtime/NetworkClient.cs @@ -101,7 +101,7 @@ protected void OnDataReceived(byte[] data) else Debug.LogError("Skipped Data message handling because m_Connection is null."); } - void OnConnected() + static void OnConnected() { if (connection != null) { From 60083ff72d3134c56400cb54257b890b1e124833 Mon Sep 17 00:00:00 2001 From: vis2k Date: Sun, 24 Mar 2019 20:55:16 +0100 Subject: [PATCH 16/27] NetworkClient.OnDataReceived made static --- Assets/Mirror/Runtime/NetworkClient.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Assets/Mirror/Runtime/NetworkClient.cs b/Assets/Mirror/Runtime/NetworkClient.cs index d3436a103..28a2cc5b3 100644 --- a/Assets/Mirror/Runtime/NetworkClient.cs +++ b/Assets/Mirror/Runtime/NetworkClient.cs @@ -92,7 +92,7 @@ void OnDisconnected() connection?.InvokeHandler(new DisconnectMessage()); } - protected void OnDataReceived(byte[] data) + internal static void OnDataReceived(byte[] data) { if (connection != null) { From 461ba0b036f0ec4ea4023772ff5181e2cf24b490 Mon Sep 17 00:00:00 2001 From: vis2k Date: Sun, 24 Mar 2019 20:55:41 +0100 Subject: [PATCH 17/27] NetworkClient.OnDisconnected made static --- Assets/Mirror/Runtime/NetworkClient.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Assets/Mirror/Runtime/NetworkClient.cs b/Assets/Mirror/Runtime/NetworkClient.cs index 28a2cc5b3..5929fd483 100644 --- a/Assets/Mirror/Runtime/NetworkClient.cs +++ b/Assets/Mirror/Runtime/NetworkClient.cs @@ -83,7 +83,7 @@ void OnError(Exception exception) Debug.LogException(exception); } - void OnDisconnected() + static void OnDisconnected() { connectState = ConnectState.Disconnected; From 32ed62f82b1b1c07a0fce9efc398b5b0f4c128e4 Mon Sep 17 00:00:00 2001 From: vis2k Date: Sun, 24 Mar 2019 20:56:14 +0100 Subject: [PATCH 18/27] NetworkClient.OnError made static --- Assets/Mirror/Runtime/NetworkClient.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Assets/Mirror/Runtime/NetworkClient.cs b/Assets/Mirror/Runtime/NetworkClient.cs index 5929fd483..e699ddf98 100644 --- a/Assets/Mirror/Runtime/NetworkClient.cs +++ b/Assets/Mirror/Runtime/NetworkClient.cs @@ -78,7 +78,7 @@ void InitializeTransportHandlers() Transport.activeTransport.OnClientError.AddListener(OnError); } - void OnError(Exception exception) + static void OnError(Exception exception) { Debug.LogException(exception); } From 080bf2eaf0303880f0dd435f30c5e8ae1f582f1c Mon Sep 17 00:00:00 2001 From: vis2k Date: Sun, 24 Mar 2019 20:57:07 +0100 Subject: [PATCH 19/27] NetworkClient.InitializeTransportHandlers made static --- Assets/Mirror/Runtime/NetworkClient.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Assets/Mirror/Runtime/NetworkClient.cs b/Assets/Mirror/Runtime/NetworkClient.cs index e699ddf98..ff893c495 100644 --- a/Assets/Mirror/Runtime/NetworkClient.cs +++ b/Assets/Mirror/Runtime/NetworkClient.cs @@ -70,7 +70,7 @@ public void Connect(string ip) connection.SetHandlers(handlers); } - void InitializeTransportHandlers() + static void InitializeTransportHandlers() { Transport.activeTransport.OnClientConnected.AddListener(OnConnected); Transport.activeTransport.OnClientDataReceived.AddListener(OnDataReceived); From d1d006e6d78f19d1f1f1a4af7b0c69ffb6deafae Mon Sep 17 00:00:00 2001 From: vis2k Date: Mon, 25 Mar 2019 13:23:06 +0100 Subject: [PATCH 20/27] NetworkClient.Connect made static --- Assets/Mirror/Runtime/NetworkClient.cs | 2 +- Assets/Mirror/Runtime/NetworkManager.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Assets/Mirror/Runtime/NetworkClient.cs b/Assets/Mirror/Runtime/NetworkClient.cs index ff893c495..a8e30e240 100644 --- a/Assets/Mirror/Runtime/NetworkClient.cs +++ b/Assets/Mirror/Runtime/NetworkClient.cs @@ -51,7 +51,7 @@ internal static void SetHandlers(NetworkConnection conn) conn.SetHandlers(handlers); } - public void Connect(string ip) + public static void Connect(string ip) { if (LogFilter.Debug) Debug.Log("Client Connect: " + ip); diff --git a/Assets/Mirror/Runtime/NetworkManager.cs b/Assets/Mirror/Runtime/NetworkManager.cs index 74ffe8e82..6ee652ef1 100644 --- a/Assets/Mirror/Runtime/NetworkManager.cs +++ b/Assets/Mirror/Runtime/NetworkManager.cs @@ -340,7 +340,7 @@ public NetworkClient StartClient() } if (LogFilter.Debug) Debug.Log("NetworkManager StartClient address:" + networkAddress); - client.Connect(networkAddress); + NetworkClient.Connect(networkAddress); OnStartClient(client); s_Address = networkAddress; From 4dbaff8ac1cf6eabe11acc3b340434718f71e16b Mon Sep 17 00:00:00 2001 From: vis2k Date: Mon, 25 Mar 2019 13:23:54 +0100 Subject: [PATCH 21/27] NetworkManager.StartClient/StartHost don't return NetworkClient anymore --- Assets/Mirror/Runtime/NetworkManager.cs | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/Assets/Mirror/Runtime/NetworkManager.cs b/Assets/Mirror/Runtime/NetworkManager.cs index 6ee652ef1..53b4e08de 100644 --- a/Assets/Mirror/Runtime/NetworkManager.cs +++ b/Assets/Mirror/Runtime/NetworkManager.cs @@ -320,7 +320,7 @@ internal void RegisterClientMessages() } } - public NetworkClient StartClient() + public void StartClient() { InitializeSingleton(); @@ -336,7 +336,7 @@ public NetworkClient StartClient() if (string.IsNullOrEmpty(networkAddress)) { Debug.LogError("Must set the Network Address field in the manager"); - return null; + return; } if (LogFilter.Debug) Debug.Log("NetworkManager StartClient address:" + networkAddress); @@ -344,19 +344,16 @@ public NetworkClient StartClient() OnStartClient(client); s_Address = networkAddress; - return client; } - public virtual NetworkClient StartHost() + public virtual void StartHost() { OnStartHost(); if (StartServer()) { NetworkClient localClient = ConnectLocalClient(); OnStartClient(localClient); - return localClient; } - return null; } NetworkClient ConnectLocalClient() From 31836c16d1be33ef5b45e501461e35f3b182186a Mon Sep 17 00:00:00 2001 From: vis2k Date: Mon, 25 Mar 2019 13:30:12 +0100 Subject: [PATCH 22/27] NetworkClient.RemoveTransportHandlers made static --- Assets/Mirror/Runtime/NetworkClient.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Assets/Mirror/Runtime/NetworkClient.cs b/Assets/Mirror/Runtime/NetworkClient.cs index a8e30e240..c12fdc1bc 100644 --- a/Assets/Mirror/Runtime/NetworkClient.cs +++ b/Assets/Mirror/Runtime/NetworkClient.cs @@ -133,7 +133,7 @@ public virtual void Disconnect() active = false; } - void RemoveTransportHandlers() + static void RemoveTransportHandlers() { // so that we don't register them more than once Transport.activeTransport.OnClientConnected.RemoveListener(OnConnected); From 7bc320d0988faaf67a7fc0bd121f0f531532a230 Mon Sep 17 00:00:00 2001 From: vis2k Date: Mon, 25 Mar 2019 16:05:25 +0100 Subject: [PATCH 23/27] LocalClient class moved into NetworkClient --- Assets/Mirror/Runtime/ClientScene.cs | 8 -- Assets/Mirror/Runtime/LocalClient.cs | 68 -------------- Assets/Mirror/Runtime/LocalClient.cs.meta | 11 --- Assets/Mirror/Runtime/LocalConnections.cs | 8 +- Assets/Mirror/Runtime/NetworkClient.cs | 103 ++++++++++++++++++---- Assets/Mirror/Runtime/NetworkManager.cs | 9 +- Assets/Mirror/Runtime/NetworkServer.cs | 2 +- 7 files changed, 92 insertions(+), 117 deletions(-) delete mode 100644 Assets/Mirror/Runtime/LocalClient.cs delete mode 100644 Assets/Mirror/Runtime/LocalClient.cs.meta diff --git a/Assets/Mirror/Runtime/ClientScene.cs b/Assets/Mirror/Runtime/ClientScene.cs index 508abab9c..cf8f17a6d 100644 --- a/Assets/Mirror/Runtime/ClientScene.cs +++ b/Assets/Mirror/Runtime/ClientScene.cs @@ -138,14 +138,6 @@ public static bool Ready(NetworkConnection conn) return false; } - public static NetworkClient ConnectLocalServer() - { - LocalClient newClient = new LocalClient(); - NetworkServer.ActivateLocalClientScene(); - newClient.InternalConnectLocalServer(); - return newClient; - } - internal static void HandleClientDisconnect(NetworkConnection conn) { if (readyConnection == conn && ready) diff --git a/Assets/Mirror/Runtime/LocalClient.cs b/Assets/Mirror/Runtime/LocalClient.cs deleted file mode 100644 index a03ebd412..000000000 --- a/Assets/Mirror/Runtime/LocalClient.cs +++ /dev/null @@ -1,68 +0,0 @@ -using System.Collections.Generic; -using UnityEngine; - -namespace Mirror -{ - sealed class LocalClient : NetworkClient - { - // local client in host mode might call Cmds/Rpcs during Update, but we - // want to apply them in LateUpdate like all other Transport messages - // to avoid race conditions. keep packets in Queue until LateUpdate. - internal Queue packetQueue = new Queue(); - - internal void InternalConnectLocalServer() - { - // create local connection to server - connection = new ULocalConnectionToServer(); - SetHandlers(connection); - - // create server connection to local client - ULocalConnectionToClient connectionToClient = new ULocalConnectionToClient(this); - NetworkServer.SetLocalConnection(connectionToClient); - - connectState = ConnectState.Connected; - - active = true; - RegisterSystemHandlers(true); - - packetQueue.Enqueue(MessagePacker.Pack(new ConnectMessage())); - } - - public override void Disconnect() - { - connectState = ConnectState.Disconnected; - ClientScene.HandleClientDisconnect(connection); - if (isConnected) - { - packetQueue.Enqueue(MessagePacker.Pack(new DisconnectMessage())); - } - NetworkServer.RemoveLocalConnection(); - } - - internal override void Update() - { - // process internal messages so they are applied at the correct time - while (packetQueue.Count > 0) - { - byte[] packet = packetQueue.Dequeue(); - OnDataReceived(packet); - } - } - - // Called by the server to set the LocalClient's LocalPlayer object during NetworkServer.AddPlayer() - internal void AddLocalPlayer(NetworkIdentity localPlayer) - { - if (LogFilter.Debug) Debug.Log("Local client AddLocalPlayer " + localPlayer.gameObject.name + " conn=" + connection.connectionId); - connection.isReady = true; - connection.SetPlayerController(localPlayer); - if (localPlayer != null) - { - localPlayer.isClient = true; - NetworkIdentity.spawned[localPlayer.netId] = localPlayer; - localPlayer.connectionToServer = connection; - } - // there is no SystemOwnerMessage for local client. add to ClientScene here instead - ClientScene.InternalAddPlayer(localPlayer); - } - } -} diff --git a/Assets/Mirror/Runtime/LocalClient.cs.meta b/Assets/Mirror/Runtime/LocalClient.cs.meta deleted file mode 100644 index 6c073e640..000000000 --- a/Assets/Mirror/Runtime/LocalClient.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 5c4d04450e91c438385de7300abef1b6 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Mirror/Runtime/LocalConnections.cs b/Assets/Mirror/Runtime/LocalConnections.cs index de2c5e5f5..31f59d617 100644 --- a/Assets/Mirror/Runtime/LocalConnections.cs +++ b/Assets/Mirror/Runtime/LocalConnections.cs @@ -6,19 +6,15 @@ namespace Mirror // sending messages on this connection causes the client's handler function to be invoked directly class ULocalConnectionToClient : NetworkConnection { - public LocalClient localClient; - - public ULocalConnectionToClient(LocalClient localClient) : base ("localClient") + public ULocalConnectionToClient() : base ("localClient") { - this.localClient = localClient; - // local player always has connectionId == 0 connectionId = 0; } internal override bool SendBytes(byte[] bytes, int channelId = Channels.DefaultReliable) { - localClient.packetQueue.Enqueue(bytes); + NetworkClient.localClientPacketQueue.Enqueue(bytes); return true; } } diff --git a/Assets/Mirror/Runtime/NetworkClient.cs b/Assets/Mirror/Runtime/NetworkClient.cs index c12fdc1bc..09a947624 100644 --- a/Assets/Mirror/Runtime/NetworkClient.cs +++ b/Assets/Mirror/Runtime/NetworkClient.cs @@ -34,6 +34,14 @@ public class NetworkClient public static bool isConnected => connectState == ConnectState.Connected; + // NetworkClient can connect to local server in host mode too + public static bool isLocalClient => connection != null && connection is ULocalConnectionToServer; + + // local client in host mode might call Cmds/Rpcs during Update, but we + // want to apply them in LateUpdate like all other Transport messages + // to avoid race conditions. keep packets in Queue until LateUpdate. + internal static Queue localClientPacketQueue = new Queue(); + public NetworkClient() { if (LogFilter.Debug) Debug.Log("Client created version " + Version.Current); @@ -51,6 +59,7 @@ internal static void SetHandlers(NetworkConnection conn) conn.SetHandlers(handlers); } + // connect remote public static void Connect(string ip) { if (LogFilter.Debug) Debug.Log("Client Connect: " + ip); @@ -70,6 +79,41 @@ public static void Connect(string ip) connection.SetHandlers(handlers); } + // connect host mode + internal static void ConnectLocalServer() + { + // create local connection to server + connection = new ULocalConnectionToServer(); + SetHandlers(connection); + + // create server connection to local client + ULocalConnectionToClient connectionToClient = new ULocalConnectionToClient(); + NetworkServer.SetLocalConnection(connectionToClient); + + connectState = ConnectState.Connected; + + active = true; + RegisterSystemHandlers(true); + + localClientPacketQueue.Enqueue(MessagePacker.Pack(new ConnectMessage())); + } + + // Called by the server to set the LocalClient's LocalPlayer object during NetworkServer.AddPlayer() + internal static void AddLocalPlayer(NetworkIdentity localPlayer) + { + if (LogFilter.Debug) Debug.Log("Local client AddLocalPlayer " + localPlayer.gameObject.name + " conn=" + connection.connectionId); + connection.isReady = true; + connection.SetPlayerController(localPlayer); + if (localPlayer != null) + { + localPlayer.isClient = true; + NetworkIdentity.spawned[localPlayer.netId] = localPlayer; + localPlayer.connectionToServer = connection; + } + // there is no SystemOwnerMessage for local client. add to ClientScene here instead + ClientScene.InternalAddPlayer(localPlayer); + } + static void InitializeTransportHandlers() { Transport.activeTransport.OnClientConnected.AddListener(OnConnected); @@ -117,20 +161,33 @@ static void OnConnected() else Debug.LogError("Skipped Connect message handling because m_Connection is null."); } - public virtual void Disconnect() + public static void Disconnect() { connectState = ConnectState.Disconnected; ClientScene.HandleClientDisconnect(connection); - if (connection != null) - { - connection.Disconnect(); - connection.Dispose(); - connection = null; - RemoveTransportHandlers(); - } - // the client's network is not active anymore. - active = false; + // local or remote connection? + if (isLocalClient) + { + if (isConnected) + { + localClientPacketQueue.Enqueue(MessagePacker.Pack(new DisconnectMessage())); + } + NetworkServer.RemoveLocalConnection(); + } + else + { + if (connection != null) + { + connection.Disconnect(); + connection.Dispose(); + connection = null; + RemoveTransportHandlers(); + } + + // the client's network is not active anymore. + active = false; + } } static void RemoveTransportHandlers() @@ -173,12 +230,25 @@ public static bool Send(T message) where T : IMessageBase return false; } - internal virtual void Update() + internal static void Update() { - // only update things while connected - if (active && connectState == ConnectState.Connected) + // local or remote connection? + if (isLocalClient) { - NetworkTime.UpdateClient(); + // process internal messages so they are applied at the correct time + while (localClientPacketQueue.Count > 0) + { + byte[] packet = localClientPacketQueue.Dequeue(); + OnDataReceived(packet); + } + } + else + { + // only update things while connected + if (active && connectState == ConnectState.Connected) + { + NetworkTime.UpdateClient(); + } } } @@ -316,11 +386,6 @@ public static void UnregisterHandler() where T : IMessageBase handlers.Remove(msgType); } - internal static void UpdateClient() - { - singleton?.Update(); - } - public static void Shutdown() { if (LogFilter.Debug) Debug.Log("Shutting down client."); diff --git a/Assets/Mirror/Runtime/NetworkManager.cs b/Assets/Mirror/Runtime/NetworkManager.cs index 53b4e08de..f75f87bfc 100644 --- a/Assets/Mirror/Runtime/NetworkManager.cs +++ b/Assets/Mirror/Runtime/NetworkManager.cs @@ -187,7 +187,7 @@ public virtual void LateUpdate() // -> we don't only call while Client/Server.Connected, because then we would stop if disconnected and the // NetworkClient wouldn't receive the last Disconnect event, result in all kinds of issues NetworkServer.Update(); - NetworkClient.UpdateClient(); + NetworkClient.Update(); UpdateScene(); } @@ -360,7 +360,8 @@ NetworkClient ConnectLocalClient() { if (LogFilter.Debug) Debug.Log("NetworkManager StartHost"); networkAddress = "localhost"; - client = ClientScene.ConnectLocalServer(); + NetworkServer.ActivateLocalClientScene(); + NetworkClient.ConnectLocalServer(); RegisterClientMessages(); return client; } @@ -398,8 +399,8 @@ public void StopClient() isNetworkActive = false; if (client != null) { - // only shutdown this client, not ALL clients. - client.Disconnect(); + // shutdown client + NetworkClient.Disconnect(); NetworkClient.Shutdown(); client = null; } diff --git a/Assets/Mirror/Runtime/NetworkServer.cs b/Assets/Mirror/Runtime/NetworkServer.cs index 575a032b7..efd6dc509 100644 --- a/Assets/Mirror/Runtime/NetworkServer.cs +++ b/Assets/Mirror/Runtime/NetworkServer.cs @@ -721,7 +721,7 @@ static bool SetupLocalPlayerForConnection(NetworkConnection conn, NetworkIdentit SendSpawnMessage(identity, null); // Set up local player instance on the client instance and update local object map - localConnection.localClient.AddLocalPlayer(identity); + NetworkClient.AddLocalPlayer(identity); identity.SetClientOwner(conn); // Trigger OnAuthority From 912572d6feb4ebd655575cba63f28c1455175067 Mon Sep 17 00:00:00 2001 From: vis2k Date: Mon, 25 Mar 2019 16:33:47 +0100 Subject: [PATCH 24/27] NetworkClient.ConnectLocalServer: use the same order as Connect --- Assets/Mirror/Runtime/NetworkClient.cs | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/Assets/Mirror/Runtime/NetworkClient.cs b/Assets/Mirror/Runtime/NetworkClient.cs index 09a947624..993488ff0 100644 --- a/Assets/Mirror/Runtime/NetworkClient.cs +++ b/Assets/Mirror/Runtime/NetworkClient.cs @@ -82,6 +82,12 @@ public static void Connect(string ip) // connect host mode internal static void ConnectLocalServer() { + if (LogFilter.Debug) Debug.Log("Client Connect Local Server"); + active = true; + RegisterSystemHandlers(true); + + connectState = ConnectState.Connected; + // create local connection to server connection = new ULocalConnectionToServer(); SetHandlers(connection); @@ -90,11 +96,6 @@ internal static void ConnectLocalServer() ULocalConnectionToClient connectionToClient = new ULocalConnectionToClient(); NetworkServer.SetLocalConnection(connectionToClient); - connectState = ConnectState.Connected; - - active = true; - RegisterSystemHandlers(true); - localClientPacketQueue.Enqueue(MessagePacker.Pack(new ConnectMessage())); } From 9bc1dc1e8546525e86db94b76a35f0a6d30eb6d5 Mon Sep 17 00:00:00 2001 From: vis2k Date: Mon, 25 Mar 2019 16:35:01 +0100 Subject: [PATCH 25/27] NetworkClient class made static --- Assets/Mirror/Components/NetworkAnimator.cs | 2 +- .../Mirror/Components/NetworkLobbyManager.cs | 10 ++-- Assets/Mirror/Runtime/NetworkClient.cs | 27 ++------- Assets/Mirror/Runtime/NetworkManager.cs | 57 +++++++------------ Assets/Mirror/Runtime/NetworkManagerHUD.cs | 5 +- 5 files changed, 31 insertions(+), 70 deletions(-) diff --git a/Assets/Mirror/Components/NetworkAnimator.cs b/Assets/Mirror/Components/NetworkAnimator.cs index 77b101890..a223bdee2 100644 --- a/Assets/Mirror/Components/NetworkAnimator.cs +++ b/Assets/Mirror/Components/NetworkAnimator.cs @@ -314,7 +314,7 @@ public void SetTrigger(int hash) { if (hasAuthority && localPlayerAuthority) { - if (NetworkClient.singleton != null && ClientScene.readyConnection != null) + if (ClientScene.readyConnection != null) { CmdOnAnimationTriggerServerMessage(hash); } diff --git a/Assets/Mirror/Components/NetworkLobbyManager.cs b/Assets/Mirror/Components/NetworkLobbyManager.cs index 8aaa23338..82cbea84a 100644 --- a/Assets/Mirror/Components/NetworkLobbyManager.cs +++ b/Assets/Mirror/Components/NetworkLobbyManager.cs @@ -328,7 +328,7 @@ public override void OnStopHost() #region client handlers - public override void OnStartClient(NetworkClient lobbyClient) + public override void OnStartClient() { if (lobbyPlayerPrefab == null || lobbyPlayerPrefab.gameObject == null) Debug.LogError("NetworkLobbyManager no LobbyPlayer prefab is registered. Please add a LobbyPlayer prefab."); @@ -340,7 +340,7 @@ public override void OnStartClient(NetworkClient lobbyClient) else ClientScene.RegisterPrefab(playerPrefab); - OnLobbyStartClient(lobbyClient); + OnLobbyStartClient(); } public override void OnClientConnect(NetworkConnection conn) @@ -373,7 +373,7 @@ public override void OnClientChangeScene(string newSceneName) { if (LogFilter.Debug) Debug.LogFormat("OnClientChangeScene from {0} to {1}", SceneManager.GetActiveScene().name, newSceneName); - if (SceneManager.GetActiveScene().name == LobbyScene && newSceneName == GameplayScene && dontDestroyOnLoad && IsClientConnected() && client != null) + if (SceneManager.GetActiveScene().name == LobbyScene && newSceneName == GameplayScene && dontDestroyOnLoad && IsClientConnected()) { GameObject lobbyPlayer = NetworkClient.connection?.playerController?.gameObject; if (lobbyPlayer != null) @@ -385,7 +385,7 @@ public override void OnClientChangeScene(string newSceneName) Debug.LogWarningFormat("OnClientChangeScene: lobbyPlayer is null"); } else - if (LogFilter.Debug) Debug.LogFormat("OnClientChangeScene {0} {1} {2}", dontDestroyOnLoad, IsClientConnected(), client != null); + if (LogFilter.Debug) Debug.LogFormat("OnClientChangeScene {0} {1}", dontDestroyOnLoad, IsClientConnected()); } public override void OnClientSceneChanged(NetworkConnection conn) @@ -452,7 +452,7 @@ public virtual void OnLobbyClientConnect(NetworkConnection conn) {} public virtual void OnLobbyClientDisconnect(NetworkConnection conn) {} - public virtual void OnLobbyStartClient(NetworkClient lobbyClient) {} + public virtual void OnLobbyStartClient() {} public virtual void OnLobbyStopClient() {} diff --git a/Assets/Mirror/Runtime/NetworkClient.cs b/Assets/Mirror/Runtime/NetworkClient.cs index 993488ff0..c30e1d716 100644 --- a/Assets/Mirror/Runtime/NetworkClient.cs +++ b/Assets/Mirror/Runtime/NetworkClient.cs @@ -12,25 +12,19 @@ public enum ConnectState Disconnected } - public class NetworkClient + public static class NetworkClient { - // the client (can be a regular NetworkClient or a LocalClient) - public static NetworkClient singleton; - - [Obsolete("Use NetworkClient.singleton instead. There is always exactly one client.")] - public static List allClients => new List{singleton}; - public static readonly Dictionary handlers = new Dictionary(); - public static NetworkConnection connection { get; protected set; } + public static NetworkConnection connection { get; internal set; } internal static ConnectState connectState = ConnectState.None; - public static string serverIp { get; private set; } = ""; + public static string serverIp { get; internal set; } = ""; // active is true while a client is connecting/connected // (= while the network is active) - public static bool active { get; protected set; } + public static bool active { get; internal set; } public static bool isConnected => connectState == ConnectState.Connected; @@ -42,18 +36,6 @@ public class NetworkClient // to avoid race conditions. keep packets in Queue until LateUpdate. internal static Queue localClientPacketQueue = new Queue(); - public NetworkClient() - { - if (LogFilter.Debug) Debug.Log("Client created version " + Version.Current); - - if (singleton != null) - { - Debug.LogError("NetworkClient: can only create one!"); - return; - } - singleton = this; - } - internal static void SetHandlers(NetworkConnection conn) { conn.SetHandlers(handlers); @@ -390,7 +372,6 @@ public static void UnregisterHandler() where T : IMessageBase public static void Shutdown() { if (LogFilter.Debug) Debug.Log("Shutting down client."); - singleton = null; active = false; } diff --git a/Assets/Mirror/Runtime/NetworkManager.cs b/Assets/Mirror/Runtime/NetworkManager.cs index f75f87bfc..4980286ac 100644 --- a/Assets/Mirror/Runtime/NetworkManager.cs +++ b/Assets/Mirror/Runtime/NetworkManager.cs @@ -61,7 +61,6 @@ public class NetworkManager : MonoBehaviour public static string networkSceneName = ""; [NonSerialized] public bool isNetworkActive; - public NetworkClient client; static int s_StartPositionIndex; public static NetworkManager singleton; @@ -329,8 +328,6 @@ public void StartClient() isNetworkActive = true; - client = new NetworkClient(); - RegisterClientMessages(); if (string.IsNullOrEmpty(networkAddress)) @@ -342,7 +339,7 @@ public void StartClient() NetworkClient.Connect(networkAddress); - OnStartClient(client); + OnStartClient(); s_Address = networkAddress; } @@ -351,19 +348,18 @@ public virtual void StartHost() OnStartHost(); if (StartServer()) { - NetworkClient localClient = ConnectLocalClient(); - OnStartClient(localClient); + ConnectLocalClient(); + OnStartClient(); } } - NetworkClient ConnectLocalClient() + void ConnectLocalClient() { if (LogFilter.Debug) Debug.Log("NetworkManager StartHost"); networkAddress = "localhost"; NetworkServer.ActivateLocalClientScene(); NetworkClient.ConnectLocalServer(); RegisterClientMessages(); - return client; } public void StopHost() @@ -397,13 +393,10 @@ public void StopClient() if (LogFilter.Debug) Debug.Log("NetworkManager StopClient"); isNetworkActive = false; - if (client != null) - { - // shutdown client - NetworkClient.Disconnect(); - NetworkClient.Shutdown(); - client = null; - } + + // shutdown client + NetworkClient.Disconnect(); + NetworkClient.Shutdown(); ClientScene.DestroyAllClientObjects(); if (!string.IsNullOrEmpty(offlineScene)) @@ -465,11 +458,8 @@ internal void ClientChangeScene(string newSceneName, bool forceReload) // vis2k: pause message handling while loading scene. otherwise we will process messages and then lose all // the state as soon as the load is finishing, causing all kinds of bugs because of missing state. // (client may be null after StopClient etc.) - if (client != null) - { - if (LogFilter.Debug) Debug.Log("ClientChangeScene: pausing handlers while scene is loading to avoid data loss after scene was loaded."); - Transport.activeTransport.enabled = false; - } + if (LogFilter.Debug) Debug.Log("ClientChangeScene: pausing handlers while scene is loading to avoid data loss after scene was loaded."); + Transport.activeTransport.enabled = false; // Let client prepare for scene change OnClientChangeScene(newSceneName); @@ -482,22 +472,15 @@ void FinishLoadScene() { // NOTE: this cannot use NetworkClient.allClients[0] - that client may be for a completely different purpose. - if (client != null) - { - // process queued messages that we received while loading the scene - if (LogFilter.Debug) Debug.Log("FinishLoadScene: resuming handlers after scene was loading."); - Transport.activeTransport.enabled = true; + // process queued messages that we received while loading the scene + if (LogFilter.Debug) Debug.Log("FinishLoadScene: resuming handlers after scene was loading."); + Transport.activeTransport.enabled = true; - if (s_ClientReadyConnection != null) - { - clientLoadedScene = true; - OnClientConnect(s_ClientReadyConnection); - s_ClientReadyConnection = null; - } - } - else + if (s_ClientReadyConnection != null) { - if (LogFilter.Debug) Debug.Log("FinishLoadScene client is null"); + clientLoadedScene = true; + OnClientConnect(s_ClientReadyConnection); + s_ClientReadyConnection = null; } if (NetworkServer.active) @@ -506,7 +489,7 @@ void FinishLoadScene() OnServerSceneChanged(networkSceneName); } - if (IsClientConnected() && client != null) + if (IsClientConnected()) { RegisterClientMessages(); OnClientSceneChanged(NetworkClient.connection); @@ -544,7 +527,7 @@ public static void UnRegisterStartPosition(Transform start) public bool IsClientConnected() { - return client != null && NetworkClient.isConnected; + return NetworkClient.isConnected; } // this is the only way to clear the singleton, so another instance can be created. @@ -808,7 +791,7 @@ public virtual void OnClientSceneChanged(NetworkConnection conn) public virtual void OnStartHost() {} public virtual void OnStartServer() {} - public virtual void OnStartClient(NetworkClient client) {} + public virtual void OnStartClient() {} public virtual void OnStopServer() {} public virtual void OnStopClient() {} public virtual void OnStopHost() {} diff --git a/Assets/Mirror/Runtime/NetworkManagerHUD.cs b/Assets/Mirror/Runtime/NetworkManagerHUD.cs index eac015e5f..b1a3610e1 100644 --- a/Assets/Mirror/Runtime/NetworkManagerHUD.cs +++ b/Assets/Mirror/Runtime/NetworkManagerHUD.cs @@ -26,13 +26,10 @@ void OnGUI() if (!showGUI) return; - bool noConnection = (manager.client == null || NetworkClient.connection == null || - NetworkClient.connection.connectionId == -1); - GUILayout.BeginArea(new Rect(10 + offsetX, 40 + offsetY, 215, 9999)); if (!manager.IsClientConnected() && !NetworkServer.active) { - if (noConnection) + if (!NetworkClient.active) { // LAN Host if (Application.platform != RuntimePlatform.WebGLPlayer) From 1879f5ecdc031b701d808497226927c45e5fb32d Mon Sep 17 00:00:00 2001 From: vis2k Date: Mon, 25 Mar 2019 16:59:17 +0100 Subject: [PATCH 26/27] Add [Obsolete] for compatibility --- Assets/Mirror/Runtime/NetworkClient.cs | 9 ++++++++- Assets/Mirror/Runtime/NetworkManager.cs | 4 ++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/Assets/Mirror/Runtime/NetworkClient.cs b/Assets/Mirror/Runtime/NetworkClient.cs index c30e1d716..e75747f15 100644 --- a/Assets/Mirror/Runtime/NetworkClient.cs +++ b/Assets/Mirror/Runtime/NetworkClient.cs @@ -12,8 +12,15 @@ public enum ConnectState Disconnected } - public static class NetworkClient + // TODO make fully static after removing obsoleted singleton! + public class NetworkClient { + [Obsolete("Use NetworkClient directly. Singleton isn't needed anymore, all functions are static now. For example: NetworkClient.Send(message) instead of NetworkClient.singleton.Send(message).")] + public static NetworkClient singleton = new NetworkClient(); + + [Obsolete("Use NetworkClient.singleton instead. There is always exactly one client.")] + public static List allClients => new List{singleton}; + public static readonly Dictionary handlers = new Dictionary(); public static NetworkConnection connection { get; internal set; } diff --git a/Assets/Mirror/Runtime/NetworkManager.cs b/Assets/Mirror/Runtime/NetworkManager.cs index 4980286ac..d7a5c0384 100644 --- a/Assets/Mirror/Runtime/NetworkManager.cs +++ b/Assets/Mirror/Runtime/NetworkManager.cs @@ -61,6 +61,8 @@ public class NetworkManager : MonoBehaviour public static string networkSceneName = ""; [NonSerialized] public bool isNetworkActive; + [Obsolete("Use NetworkClient directly, it will be made static soon. For example, use NetworkClient.Send(message) instead of NetworkManager.client.Send(message)")] + public NetworkClient client => NetworkClient.singleton; static int s_StartPositionIndex; public static NetworkManager singleton; @@ -791,6 +793,8 @@ public virtual void OnClientSceneChanged(NetworkConnection conn) public virtual void OnStartHost() {} public virtual void OnStartServer() {} + [Obsolete("Use OnStartClient() instead of OnStartClient(NetworkClient client). All NetworkClient functions are static now, so you can use NetworkClient.Send(message) instead of client.Send(message) directly now.")] + public virtual void OnStartClient(NetworkClient client) {} public virtual void OnStartClient() {} public virtual void OnStopServer() {} public virtual void OnStopClient() {} From 4d74f555b115700ee46c3b424fe97be89a528175 Mon Sep 17 00:00:00 2001 From: Paul Pacheco Date: Tue, 26 Mar 2019 12:10:36 +0100 Subject: [PATCH 27/27] Update Assets/Mirror/Runtime/NetworkClient.cs Co-Authored-By: vis2k --- Assets/Mirror/Runtime/NetworkClient.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Assets/Mirror/Runtime/NetworkClient.cs b/Assets/Mirror/Runtime/NetworkClient.cs index e75747f15..36f9dbfc3 100644 --- a/Assets/Mirror/Runtime/NetworkClient.cs +++ b/Assets/Mirror/Runtime/NetworkClient.cs @@ -36,7 +36,7 @@ public class NetworkClient public static bool isConnected => connectState == ConnectState.Connected; // NetworkClient can connect to local server in host mode too - public static bool isLocalClient => connection != null && connection is ULocalConnectionToServer; + public static bool isLocalClient => connection is ULocalConnectionToServer; // local client in host mode might call Cmds/Rpcs during Update, but we // want to apply them in LateUpdate like all other Transport messages