@@ -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}
0 commit comments