Skip to content
This repository was archived by the owner on Sep 15, 2024. It is now read-only.

Commit cd69878

Browse files
Fix #68
1 parent 206518a commit cd69878

4 files changed

Lines changed: 17 additions & 11 deletions

File tree

Scripts/Netcode/Client/ENetClient.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ private async Task ENetThreadWorker(string ip, ushort port)
210210
Running = 0;
211211
}
212212

213-
private bool ConcurrentQueuesWorking() => NetworkManager.GodotCmds.Count != 0 || ENetCmds.Count != 0 || Outgoing.Count != 0;
213+
private bool ConcurrentQueuesWorking() => ENetCmds.Count != 0 || Outgoing.Count != 0;
214214

215215
public void Dispose()
216216
{

Scripts/Netcode/Server/ENetServer.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public abstract class ENetServer : IDisposable
1515
public ConcurrentQueue<ENetCmd> ENetCmds { get; set; }
1616
public ushort MaxPlayers { get; set; }
1717

18-
protected CancellationTokenSource CancelTokenSource { get; set; }
18+
protected CancellationTokenSource CTS { get; set; }
1919
protected bool QueueRestart { get; set; }
2020
protected long SomeoneConnected = 0;
2121
protected long Running = 0;
@@ -38,11 +38,11 @@ public async Task Start(ushort port, int maxClients)
3838
return;
3939
}
4040

41-
CancelTokenSource = new CancellationTokenSource();
41+
CTS = new CancellationTokenSource();
4242

4343
try
4444
{
45-
await Task.Run(() => ENetThreadWorker(port, maxClients), CancelTokenSource.Token);
45+
await Task.Run(() => ENetThreadWorker(port, maxClients), CTS.Token);
4646
}
4747
catch (Exception e)
4848
{
@@ -147,7 +147,7 @@ private async Task ENetThreadWorker(ushort port, int maxClients)
147147
Log($"Server listening on port {port}");
148148
Running = 1;
149149

150-
while (!CancelTokenSource.IsCancellationRequested)
150+
while (!CTS.IsCancellationRequested)
151151
{
152152
bool polled = false;
153153

@@ -259,7 +259,7 @@ private void Send(ServerPacket gamePacket, Peer peer)
259259
peer.Send(channelID, ref packet);
260260
}
261261

262-
private bool ConcurrentQueuesWorking() => NetworkManager.GodotCmds.Count != 0 || ENetCmds.Count != 0 || Outgoing.Count != 0;
262+
private bool ConcurrentQueuesWorking() => ENetCmds.Count != 0 || Outgoing.Count != 0;
263263

264264
public virtual void Dispose()
265265
{ }

Scripts/Netcode/Server/GameServer.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,19 +73,19 @@ protected override void ServerCmds()
7373
break;
7474

7575
case ENetOpcode.StopServer:
76-
if (CancelTokenSource.IsCancellationRequested)
76+
if (CTS.IsCancellationRequested)
7777
{
7878
Log("Server has been stopped already");
7979
return;
8080
}
8181

82-
CancelTokenSource.Cancel();
82+
CTS.Cancel();
8383

8484
KickAll(DisconnectOpcode.Stopping);
8585
break;
8686

8787
case ENetOpcode.RestartServer:
88-
if (CancelTokenSource.IsCancellationRequested)
88+
if (CTS.IsCancellationRequested)
8989
{
9090
Log("Server has been stopped already");
9191
return;
@@ -97,12 +97,12 @@ protected override void ServerCmds()
9797
break;
9898

9999
case ENetOpcode.ClientWantsToExitApp:
100-
CancelTokenSource.Cancel();
100+
CTS.Cancel();
101101
KickAll(DisconnectOpcode.Stopping);
102102
break;
103103

104104
case ENetOpcode.Dispose:
105-
CancelTokenSource.Dispose();
105+
CTS.Dispose();
106106
EmitClientTransforms.Stop();
107107
EmitClientTransforms.Dispose();
108108
ServerSimulation.Stop();

Scripts/Scenes/Main/GameManager.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,19 @@ private static async Task ExitCleanup()
5050
NetworkManager.GameServer.ENetCmds.Enqueue(new ENetCmd(ENetOpcode.ClientWantsToExitApp));
5151
NetworkManager.GameServer.Dispose();
5252
NetworkManager.GameServer.Stop();
53+
54+
while (NetworkManager.GameServer.IsRunning)
55+
await Task.Delay(100);
5356
}
5457

5558
if (NetworkManager.GameClient != null)
5659
if (NetworkManager.GameClient.IsRunning)
5760
{
5861
NetworkManager.GameClient.Dispose();
5962
NetworkManager.GameClient.Stop();
63+
64+
while (NetworkManager.GameClient.IsRunning)
65+
await Task.Delay(100);
6066
}
6167

6268
UtilOptions.SaveOptions();

0 commit comments

Comments
 (0)