Skip to content

Commit

Permalink
refactor: renaming listen to start and merging it with start host (#795)
Browse files Browse the repository at this point in the history
Listen required domain knowledge to understand what it does, so Start is a better name.

Adding optional parameter for starting in host mode. This makes the logic flow simpler and will also avoid naming collisions with unity's Start callback.

BREAKING CHANGE:
- ListenAsync renamed to StartAsync
- StartHost removed, use StartAsync with localClient parameter instead
- OnStartHost is now always called after Started
  • Loading branch information
James-Frowen committed Apr 30, 2021
1 parent f885e80 commit 3d4e091
Show file tree
Hide file tree
Showing 11 changed files with 29 additions and 52 deletions.
2 changes: 1 addition & 1 deletion Assets/Mirage/Components/HeadlessAutoStart.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ void Start()
// some transports might not be ready until Start.
if (Server && SystemInfo.graphicsDeviceType == GraphicsDeviceType.Null && startOnHeadless)
{
Server.ListenAsync().Forget();
Server.StartAsync().Forget();
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions Assets/Mirage/Runtime/NetworkManagerHud.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,14 @@ internal void OfflineSetActive()
public void StartHostButtonHandler()
{
SetLabel("Host Mode");
NetworkManager.Server.StartHost(NetworkManager.Client).Forget();
NetworkManager.Server.StartAsync(NetworkManager.Client).Forget();
OnlineSetActive();
}

public void StartServerOnlyButtonHandler()
{
SetLabel("Server Mode");
NetworkManager.Server.ListenAsync().Forget();
NetworkManager.Server.StartAsync().Forget();
OnlineSetActive();
}

Expand Down
50 changes: 19 additions & 31 deletions Assets/Mirage/Runtime/NetworkServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -189,14 +189,23 @@ void Initialize()
}

/// <summary>
/// Start the server, setting the maximum number of connections.
/// Start the server
/// <para>If <paramref name="localClient"/> is given then will start in host mode</para>
/// </summary>
/// <param name="maxConns">Maximum number of allowed connections</param>
/// <param name="localClient">if not null then start the server and client in hostmode</param>
/// <returns></returns>
public async UniTask ListenAsync()
public async UniTask StartAsync(NetworkClient localClient = null)
{
LocalClient = localClient;

Initialize();

if (LocalClient != null)
{
localClient.ConnectHost(this);
logger.Log("NetworkServer StartHost");
}

try
{
Transport.Started.AddListener(TransportStarted);
Expand All @@ -221,6 +230,13 @@ private void TransportStarted()
Active = true;
// (useful for loading & spawning stuff from database etc.)
_started?.Invoke();

if (LocalClient != null)
{
// we should call onStartHost after transport is ready to be used
// this allows server methods like NetworkServer.Spawn to be called in there
_onStartHost?.Invoke();
}
}

private void TransportConnected(IConnection connection)
Expand All @@ -229,34 +245,6 @@ private void TransportConnected(IConnection connection)
ConnectionAcceptedAsync(networkConnectionToClient).Forget();
}

/// <summary>
/// This starts a network "host" - a server and client in the same application.
/// <para>The client returned from StartHost() is a special "local" client that communicates to the in-process server using a message queue instead of the real network. But in almost all other cases, it can be treated as a normal client.</para>
/// </summary>
public UniTask StartHost(NetworkClient client)
{
if (!client)
throw new InvalidOperationException("NetworkClient not assigned. Unable to StartHost()");

// need to set local client before listen as that is when world is created
LocalClient = client;

// start listening to network connections
UniTask task = ListenAsync();

Active = true;

client.ConnectHost(this);

// call OnStartHost AFTER SetupServer. this way we can use
// NetworkServer.Spawn etc. in there too. just like OnStartServer
// is called after the server is actually properly started.
_onStartHost?.Invoke();

logger.Log("NetworkServer StartHost");
return task;
}

/// <summary>
/// cleanup resources so that we can start again
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion Assets/Mirage/Samples~/Chat/Scripts/ServerWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public void StartClient()

public void StartHost()
{
NetworkManager.Server.StartHost(NetworkManager.Client).Forget();
NetworkManager.Server.StartAsync(NetworkManager.Client).Forget();
}

public void SetServerIp(string serverIp)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class BenchmarkPerformance
// load host
benchmarker = Object.FindObjectOfType<NetworkManager>();
benchmarker.Server.StartHost(benchmarker.Client).Forget();
benchmarker.Server.StartAsync(benchmarker.Client).Forget();
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class BenchmarkPerformanceLight
// load host
benchmarker = Object.FindObjectOfType<NetworkManager>();
benchmarker.Server.StartHost(benchmarker.Client).Forget();
benchmarker.Server.StartAsync(benchmarker.Client).Forget();
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ void ParseForServerMode()

server.Started.AddListener(OnServerStarted);
server.Authenticated.AddListener(conn => serverObjectManager.SetClientReady(conn));
_ = server.ListenAsync();
_ = server.StartAsync();
Console.WriteLine("Starting Server Only Mode");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public class MultipleClients
// wait 1 frame before Starting server to give time for Unity to call "Start"
await UniTask.Yield();
Server.ListenAsync().Forget();
Server.StartAsync().Forget();
await started.Task;
Expand Down
2 changes: 1 addition & 1 deletion Assets/Tests/Runtime/ClientServer/ClientServerSetup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public class ClientServerSetup<T> where T : NetworkBehaviour
// start the server
var started = new UniTaskCompletionSource();
server.Started.AddListener(() => started.TrySetResult());
server.ListenAsync().Forget();
server.StartAsync().Forget();
await started.Task;
Expand Down
2 changes: 1 addition & 1 deletion Assets/Tests/Runtime/Host/HostSetup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ void Started()

server.Started.AddListener(Started);
// now start the host
manager.Server.StartHost(client).Forget();
manager.Server.StartAsync(client).Forget();

await completionSource.Task;
}
Expand Down
11 changes: 0 additions & 11 deletions Assets/Tests/Runtime/Host/NetworkManagerTest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System;
using System.Collections;
using Cysharp.Threading.Tasks;
using NUnit.Framework;
Expand Down Expand Up @@ -35,16 +34,6 @@ public void IsNetworkActiveTest()
await AsyncUtil.WaitUntilWithTimeout(() => !client.Active);
});

[Test]
public void StartHostException()
{
manager.Client = null;
Assert.Throws<InvalidOperationException>(() =>
{
manager.Server.StartHost(manager.Client).GetAwaiter().GetResult();
});
}

[Test]
public void NetworkManagerModeHostTest()
{
Expand Down

0 comments on commit 3d4e091

Please sign in to comment.