Skip to content

Commit 256b16c

Browse files
authored
feat: not listening can now be toggled at runtime. fixes host spawning (MirageNet#728)
* feat: not listening can now be toggled at runtime. fixes host spawning * add comment * remove comment refs to non existent method.
1 parent 24b5959 commit 256b16c

File tree

1 file changed

+11
-22
lines changed

1 file changed

+11
-22
lines changed

Assets/Mirage/Runtime/NetworkServer.cs

+11-22
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public class NetworkServer : MonoBehaviour, INetworkServer
3434

3535
/// <summary>
3636
/// <para>If you disable this, the server will not listen for incoming connections on the regular network port.</para>
37-
/// <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>
37+
/// <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>
3838
/// </summary>
3939
public bool Listening = true;
4040

@@ -200,18 +200,9 @@ public async UniTask ListenAsync()
200200

201201
try
202202
{
203-
// only start server if we want to listen
204-
if (Listening)
205-
{
206-
Transport.Started.AddListener(TransportStarted);
207-
Transport.Connected.AddListener(TransportConnected);
208-
await Transport.ListenAsync();
209-
}
210-
else
211-
{
212-
// if not listening then call started events right away
213-
NotListeningStarted();
214-
}
203+
Transport.Started.AddListener(TransportStarted);
204+
Transport.Connected.AddListener(TransportConnected);
205+
await Transport.ListenAsync();
215206
}
216207
catch (Exception ex)
217208
{
@@ -225,14 +216,6 @@ public async UniTask ListenAsync()
225216
}
226217
}
227218

228-
private void NotListeningStarted()
229-
{
230-
logger.Log("Server started but not Listening");
231-
Active = true;
232-
// (useful for loading & spawning stuff from database etc.)
233-
Started?.Invoke();
234-
}
235-
236219
private void TransportStarted()
237220
{
238221
logger.Log("Server started listening");
@@ -327,7 +310,7 @@ public void AddConnection(INetworkPlayer player)
327310
}
328311

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

377+
//Only allow host client to connect when not Listening for new connections
378+
if(!Listening && player != LocalPlayer)
379+
{
380+
return;
381+
}
382+
394383
// are more connections allowed? if not, kick
395384
// (it's easier to handle this in Mirage, so Transports can have
396385
// less code and third party transport might not do that anyway)

0 commit comments

Comments
 (0)