Skip to content

Commit

Permalink
feat: not listening can now be toggled at runtime. fixes host spawning (
Browse files Browse the repository at this point in the history
MirageNet#728)

* feat: not listening can now be toggled at runtime. fixes host spawning

* add comment

* remove comment refs to non existent method.
  • Loading branch information
uweeby authored Mar 27, 2021
1 parent 24b5959 commit 256b16c
Showing 1 changed file with 11 additions and 22 deletions.
33 changes: 11 additions & 22 deletions Assets/Mirage/Runtime/NetworkServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public class NetworkServer : MonoBehaviour, INetworkServer

/// <summary>
/// <para>If you disable this, the server will not listen for incoming connections on the regular network port.</para>
/// <para>This can be used if the game is running in host mode and does not want external players to be able to connect - making it like a single-player game. Also this can be useful when using AddExternalConnection().</para>
/// <para>This can be used if the game is running in host mode and does not want external players to be able to connect - making it like a single-player game.</para>
/// </summary>
public bool Listening = true;

Expand Down Expand Up @@ -200,18 +200,9 @@ public async UniTask ListenAsync()

try
{
// only start server if we want to listen
if (Listening)
{
Transport.Started.AddListener(TransportStarted);
Transport.Connected.AddListener(TransportConnected);
await Transport.ListenAsync();
}
else
{
// if not listening then call started events right away
NotListeningStarted();
}
Transport.Started.AddListener(TransportStarted);
Transport.Connected.AddListener(TransportConnected);
await Transport.ListenAsync();
}
catch (Exception ex)
{
Expand All @@ -225,14 +216,6 @@ public async UniTask ListenAsync()
}
}

private void NotListeningStarted()
{
logger.Log("Server started but not Listening");
Active = true;
// (useful for loading & spawning stuff from database etc.)
Started?.Invoke();
}

private void TransportStarted()
{
logger.Log("Server started listening");
Expand Down Expand Up @@ -327,7 +310,7 @@ public void AddConnection(INetworkPlayer player)
}

/// <summary>
/// This removes an external connection added with AddExternalConnection().
/// This removes an external connection.
/// </summary>
/// <param name="connectionId">The id of the connection to remove.</param>
public void RemoveConnection(INetworkPlayer player)
Expand Down Expand Up @@ -391,6 +374,12 @@ async UniTaskVoid ConnectionAcceptedAsync(INetworkPlayer player)
{
if (logger.LogEnabled()) logger.Log("Server accepted client:" + player);

//Only allow host client to connect when not Listening for new connections
if(!Listening && player != LocalPlayer)
{
return;
}

// are more connections allowed? if not, kick
// (it's easier to handle this in Mirage, so Transports can have
// less code and third party transport might not do that anyway)
Expand Down

0 comments on commit 256b16c

Please sign in to comment.