From dbf5784884012bd08a46d823452f8f9d95009721 Mon Sep 17 00:00:00 2001 From: uwee Date: Thu, 18 Feb 2021 10:42:12 -0500 Subject: [PATCH] fix: simplify checking if client is host (#602) * fix: simplify checking if client is host * Update Assets/Mirage/Runtime/NetworkClient.cs Co-authored-by: Lymdun * add test * add test fix smell * code smell Co-authored-by: Lymdun --- Assets/Mirage/Runtime/NetworkClient.cs | 11 +++-------- Assets/Tests/Runtime/Host/NetworkClientTest.cs | 14 ++++++++++++++ 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/Assets/Mirage/Runtime/NetworkClient.cs b/Assets/Mirage/Runtime/NetworkClient.cs index f7744d7b105..e381f77dbe0 100644 --- a/Assets/Mirage/Runtime/NetworkClient.cs +++ b/Assets/Mirage/Runtime/NetworkClient.cs @@ -72,15 +72,10 @@ public class NetworkClient : MonoBehaviour, INetworkClient public readonly NetworkTime Time = new NetworkTime(); - /// - /// The host server - /// - internal NetworkServer hostServer; - /// /// NetworkClient can connect to local server in host mode too /// - public bool IsLocalClient => hostServer != null; + public bool IsLocalClient {get; private set; } /// /// Connect client to a NetworkServer instance. @@ -165,7 +160,7 @@ internal void ConnectHost(NetworkServer server) (IConnection c1, IConnection c2) = PipeConnection.CreatePipe(); server.SetLocalConnection(this, c2); - hostServer = server; + IsLocalClient = true; Connection = GetNewConnection(c1); RegisterHostHandlers(); @@ -283,7 +278,7 @@ void Cleanup() { logger.Log("Shutting down client."); - hostServer = null; + IsLocalClient = false; connectState = ConnectState.Disconnected; diff --git a/Assets/Tests/Runtime/Host/NetworkClientTest.cs b/Assets/Tests/Runtime/Host/NetworkClientTest.cs index 29816e79a46..159ac87a5ed 100644 --- a/Assets/Tests/Runtime/Host/NetworkClientTest.cs +++ b/Assets/Tests/Runtime/Host/NetworkClientTest.cs @@ -47,5 +47,19 @@ public void ConnectionClearHandlersTest() Assert.That(clientConn.messageHandlers.Count == 0); } + + [Test] + public void IsLocalClientHostTest() + { + Assert.That(client.IsLocalClient, Is.True); + } + + [UnityTest] + public IEnumerator IsLocalClientShutdownTest() => UniTask.ToCoroutine(async () => + { + client.Disconnect(); + + await AsyncUtil.WaitUntilWithTimeout(() => !client.IsLocalClient); + }); } }