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

Commit ae8fd5a

Browse files
only do stuff with master server if alive
1 parent 38219ec commit ae8fd5a

4 files changed

Lines changed: 32 additions & 10 deletions

File tree

Scripts/Msc/UIPopupCreateLobby.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,8 @@ private async void _on_Create_pressed()
9696
};
9797

9898
SceneGameServersScript.AddServer(info);
99-
SceneGameServersScript.PostServer(info);
99+
if (WebClient.ConnectionAlive)
100+
SceneGameServersScript.PostServer(info);
100101
SceneLobby.CurrentLobby = info;
101102

102103
Hide();

Scripts/Netcode/Server/ENetServer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ protected virtual void ServerCmds()
119119
private async Task ENetThreadWorker(ushort port, int maxClients)
120120
{
121121
Thread.CurrentThread.Name = "Server";
122-
if (SceneLobby.CurrentLobby.Public)
122+
if (SceneLobby.CurrentLobby.Public && WebClient.ConnectionAlive)
123123
WebClient.TimerPingMasterServer.Start();
124124

125125
MaxPlayers = (ushort)maxClients;

Scripts/Netcode/Web/WebClient.cs

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ namespace GodotModules.Netcode
1212
public class WebClient : IDisposable
1313
{
1414
public static HttpClient Client { get; set; }
15+
public static bool ConnectionAlive { get; set; }
1516
public static Task<WebServerResponse<LobbyListing[]>> TaskGetServers { get; set; }
1617
public static string ExternalIp { get; set; }
1718
private static int FailedPingAttempts { get; set; }
@@ -31,6 +32,16 @@ public WebClient()
3132
TimerPingMasterServer = new(WebClient.WEB_PING_INTERVAL);
3233
TimerPingMasterServer.AutoReset = true;
3334
TimerPingMasterServer.Elapsed += new(OnTimerPingMasterServerEvent);
35+
36+
try
37+
{
38+
ExternalIp = new System.Net.WebClient().DownloadString("http://icanhazip.com").Replace("\\r\\n", "").Replace("\\n", "").Trim();
39+
}
40+
catch (Exception e)
41+
{
42+
ExternalIp = "";
43+
System.Console.WriteLine($"Failed to get external IP {e.Message}\n{e.StackTrace}");
44+
}
3445
}
3546

3647
public async static Task<WebServerResponse<string>> Post(string path, Dictionary<string, string> values)
@@ -61,6 +72,20 @@ public async static Task<WebServerResponse<string>> Post(string path, Dictionary
6172
}
6273
}
6374

75+
public static async Task UpdateIsAlive()
76+
{
77+
try
78+
{
79+
var response = await Client.GetAsync($"http://{WEB_SERVER_IP}/api/ping");
80+
await response.Content.ReadAsStringAsync();
81+
ConnectionAlive = true;
82+
}
83+
catch (Exception)
84+
{
85+
ConnectionAlive = false;
86+
}
87+
}
88+
6489
public static async Task<WebServerResponse<T>> Get<T>(string path)
6590
{
6691
try
@@ -91,7 +116,7 @@ public static async Task<WebServerResponse<T>> Get<T>(string path)
91116

92117
public async void OnTimerPingMasterServerEvent(System.Object source, ElapsedEventArgs args)
93118
{
94-
var res = await WebClient.Post("ping", new Dictionary<string, string> { { "Name", SceneLobby.CurrentLobby.Name } });
119+
var res = await WebClient.Post("servers/ping", new Dictionary<string, string> { { "Name", SceneLobby.CurrentLobby.Name } });
95120
if (res.Status == WebServerStatus.ERROR)
96121
{
97122
FailedPingAttempts++;
@@ -104,12 +129,6 @@ public async void OnTimerPingMasterServerEvent(System.Object source, ElapsedEven
104129
FailedPingAttempts = 0; // reset failed ping attempts if a ping gets through
105130
}
106131

107-
public static void GetExternalIp()
108-
{
109-
string externalIpString = new System.Net.WebClient().DownloadString("http://icanhazip.com").Replace("\\r\\n", "").Replace("\\n", "").Trim();
110-
ExternalIp = IPAddress.Parse(externalIpString).ToString();
111-
}
112-
113132
public void Dispose()
114133
{
115134
TimerPingMasterServer.Dispose();

Scripts/Scenes/Game Servers/UIGameServersNavBtns.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ private async void _on_Refresh_pressed()
4242
return;
4343

4444
SceneGameServersScript.ClearServers();
45-
await SceneGameServersScript.ListServers();
45+
46+
if (WebClient.ConnectionAlive)
47+
await SceneGameServersScript.ListServers();
4648
}
4749

4850
private void _on_Direct_Connect_pressed()

0 commit comments

Comments
 (0)