Skip to content

Commit

Permalink
fix: simplify checking if client is host (MirageNet#602)
Browse files Browse the repository at this point in the history
* fix: simplify checking if client is host

* Update Assets/Mirage/Runtime/NetworkClient.cs

Co-authored-by: Lymdun <lymdun@protonmail.com>

* add test

* add test fix smell

* code smell

Co-authored-by: Lymdun <lymdun@protonmail.com>
  • Loading branch information
uweeby and Lymdun committed Feb 18, 2021
1 parent 879ba01 commit dbf5784
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
11 changes: 3 additions & 8 deletions Assets/Mirage/Runtime/NetworkClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,10 @@ public class NetworkClient : MonoBehaviour, INetworkClient

public readonly NetworkTime Time = new NetworkTime();

/// <summary>
/// The host server
/// </summary>
internal NetworkServer hostServer;

/// <summary>
/// NetworkClient can connect to local server in host mode too
/// </summary>
public bool IsLocalClient => hostServer != null;
public bool IsLocalClient {get; private set; }

/// <summary>
/// Connect client to a NetworkServer instance.
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -283,7 +278,7 @@ void Cleanup()
{
logger.Log("Shutting down client.");

hostServer = null;
IsLocalClient = false;

connectState = ConnectState.Disconnected;

Expand Down
14 changes: 14 additions & 0 deletions Assets/Tests/Runtime/Host/NetworkClientTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
}
}

0 comments on commit dbf5784

Please sign in to comment.