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

Commit 3825d26

Browse files
Improve stability of tasks a bit
1 parent 3d6d142 commit 3825d26

4 files changed

Lines changed: 30 additions & 43 deletions

File tree

Scripts/Msc/UIPopupCreateLobby.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,7 @@ private async void _on_Create_pressed()
104104
NetworkManager.StartServer(port, ValidatedMaxPlayerCount);
105105
NetworkManager.StartClient(localIp, port);
106106

107-
await NetworkManager.WaitForClientToConnect(3000, async () =>
108-
{
107+
await SceneGameServersScript.ClientConnect(async () => {
109108
await NetworkManager.WaitForHostToConnectToServer();
110109
await NetworkManager.GameClient.Send(ClientPacketOpcode.Lobby, new CPacketLobby
111110
{

Scripts/Scenes/Game Servers/SceneGameServers.cs

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,26 @@ namespace GodotModules
99
{
1010
public class SceneGameServers : AScene
1111
{
12-
public Dictionary<string, LobbyListing> LobbyListings { get; set; }
12+
public UIPopupCreateLobby ServerCreationPopup { get; set; }
1313
public UILobbyListing SelectedLobbyInstance { get; set; }
1414
public bool GettingServers { get; set; }
1515

1616
[Export] public readonly NodePath NodePathServerList;
1717
[Export] public readonly NodePath NodePathServerCreationPopup;
1818

19+
private Dictionary<string, LobbyListing> LobbyListings { get; set; }
1920
private VBoxContainer ServerList { get; set; }
20-
public UIPopupCreateLobby ServerCreationPopup { get; set; }
21+
private CancellationTokenSource CTSClientConnecting { get; set; }
22+
private CancellationTokenSource CTSPingServers { get; set; }
2123

2224
public override async void _Ready()
2325
{
26+
CTSClientConnecting = new CancellationTokenSource();
2427
GettingServers = true; // because we await GetServers() at bottom
2528
UIGameServersNavBtns.BtnRefresh.Disabled = true;
26-
2729
ServerList = GetNode<VBoxContainer>(NodePathServerList);
2830
ServerCreationPopup = GetNode<UIPopupCreateLobby>(NodePathServerCreationPopup);
31+
2932
LobbyListings = new();
3033

3134
if (GameClient.Disconnected)
@@ -67,7 +70,7 @@ public override async void _Input(InputEvent @event)
6770
await SceneManager.EscapeToScene("Menu", () =>
6871
{
6972
WebClient.Client.CancelPendingRequests();
70-
NetworkManager.CancelClientConnectingTokenSource();
73+
NetworkManager.GameClient.CancelTask();
7174
});
7275
}
7376

@@ -82,8 +85,7 @@ public async Task JoinServer(LobbyListing info, bool directConnect)
8285
GD.Print("Connecting to lobby...");
8386
NetworkManager.StartClient(info.Ip, info.Port);
8487

85-
await NetworkManager.WaitForClientToConnect(3000, async () =>
86-
{
88+
await ClientConnect(async () => {
8789
await NetworkManager.GameClient.Send(ClientPacketOpcode.Lobby, new CPacketLobby
8890
{
8991
LobbyOpcode = LobbyOpcode.LobbyJoin,
@@ -93,6 +95,8 @@ await NetworkManager.WaitForClientToConnect(3000, async () =>
9395
});
9496
}
9597

98+
public async Task ClientConnect(Action action) => await NetworkManager.WaitForClientToConnect(3000, CTSClientConnecting, action);
99+
96100
public void AddServer(LobbyListing info)
97101
{
98102
var lobby = Prefabs.LobbyListing.Instance<UILobbyListing>();
@@ -106,8 +110,6 @@ public void ClearServers()
106110
child.QueueFree();
107111
}
108112

109-
public CancellationTokenSource PingServersCTS;
110-
111113
public async Task ListServers()
112114
{
113115
GettingServers = true;
@@ -132,8 +134,8 @@ public async Task ListServers()
132134

133135
res.Content.ForEach(async server =>
134136
{
135-
PingServersCTS = new CancellationTokenSource();
136-
PingServersCTS.CancelAfter(1000);
137+
CTSPingServers = new CancellationTokenSource();
138+
CTSPingServers.CancelAfter(1000);
137139

138140
var dummyClient = new ENetClient();
139141
dummyClient.Start("127.0.0.1", 7777);
@@ -143,23 +145,23 @@ public async Task ListServers()
143145
try
144146
{
145147
while (!dummyClient.IsConnected)
146-
await Task.Delay(100, PingServersCTS.Token);
148+
await Task.Delay(100, CTSPingServers.Token);
147149

148150
await dummyClient.Send(Netcode.ClientPacketOpcode.Ping);
149151

150152
while (!dummyClient.WasPingReceived)
151-
await Task.Delay(1, PingServersCTS.Token);
153+
await Task.Delay(1, CTSPingServers.Token);
152154

153155
dummyClient.WasPingReceived = false;
154156
}
155157
catch (TaskCanceledException) { }
156-
}, PingServersCTS.Token);
158+
}, CTSPingServers.Token);
157159

158160
tasks.Add(task);
159161

160162
await task;
161163

162-
if (!PingServersCTS.IsCancellationRequested)
164+
if (!CTSPingServers.IsCancellationRequested)
163165
{
164166
LobbyListings[server.Ip] = server;
165167
server.Ping = dummyClient.PingMs;
@@ -197,11 +199,14 @@ private void _on_Control_resized()
197199

198200
public override void Cleanup()
199201
{
200-
if (PingServersCTS != null)
202+
if (CTSPingServers != null)
201203
{
202-
PingServersCTS.Cancel();
203-
PingServersCTS.Dispose();
204+
CTSPingServers.Cancel();
205+
CTSPingServers.Dispose();
204206
}
207+
208+
CTSClientConnecting.Cancel();
209+
CTSClientConnecting.Dispose();
205210
}
206211
}
207212
}

Scripts/Scenes/Main/GameManager.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,6 @@ private static async Task ExitCleanup()
6161

6262
UtilOptions.SaveOptions();
6363
NetworkManager.WebClient.Dispose();
64-
65-
//if (SceneGameServers.PingServersCTS != null)
66-
//SceneGameServers.PingServersCTS.Dispose();
67-
if (NetworkManager.ClientConnectingTokenSource != null)
68-
NetworkManager.ClientConnectingTokenSource.Dispose();
6964
}
7065
catch (Exception e)
7166
{

Scripts/Scenes/Main/NetworkManager.cs

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ public class NetworkManager : Node
1616
public static GameClient GameClient { get; set; }
1717
public static WebClient WebClient { get; set; }
1818
public static NetworkManager Instance { get; set; }
19-
public static CancellationTokenSource ClientConnectingTokenSource { get; set; }
2019

2120
public static DisconnectOpcode DisconnectOpcode { get; set; }
2221
public static uint PeerId { get; set; } // this clients peer id (grabbed from server at some point)
@@ -86,7 +85,7 @@ public override async void _Process(float delta)
8685
Console.ForegroundColor = ConsoleColor.Red;
8786
GD.PrintErr(exception);
8887
Console.ResetColor();
89-
88+
9089
ErrorNotifier.IncrementErrorCount();
9190
UIDebugger.AddMessage(errorText);
9291
break;
@@ -129,33 +128,22 @@ public static async Task WaitForHostToConnectToServer()
129128
await Task.Delay(200);
130129
}
131130

132-
public static async Task WaitForClientToConnect(int timeoutMs, Action onClientConnected)
131+
public static async Task WaitForClientToConnect(int timeoutMs, CancellationTokenSource cts, Action onClientConnected)
133132
{
134-
ClientConnectingTokenSource = new CancellationTokenSource();
135-
ClientConnectingTokenSource.CancelAfter(timeoutMs);
133+
cts.CancelAfter(timeoutMs);
136134
await Task.Run(async () =>
137135
{
138136
while (!NetworkManager.GameClient.IsConnected)
139137
{
140-
if (ClientConnectingTokenSource.IsCancellationRequested)
138+
if (cts.IsCancellationRequested)
141139
break;
142140

143141
await Task.Delay(100);
144142
}
145-
}, ClientConnectingTokenSource.Token).ContinueWith((task) =>
146-
{
147-
if (!ClientConnectingTokenSource.IsCancellationRequested)
148-
onClientConnected();
149-
});
150-
}
151-
152-
public static void CancelClientConnectingTokenSource()
153-
{
154-
if (ClientConnectingTokenSource == null)
155-
return;
143+
}, cts.Token);
156144

157-
ClientConnectingTokenSource.Cancel();
158-
GameClient.CancelTask();
145+
if (!cts.IsCancellationRequested)
146+
onClientConnected();
159147
}
160148
}
161149

0 commit comments

Comments
 (0)