Skip to content

Commit

Permalink
Don't try sending heartbeats and improve error message when failing t…
Browse files Browse the repository at this point in the history
…o setup listening socket for connections

Usually this happens because there is already another server or instance of MCGalaxy running on the same port
  • Loading branch information
UnknownShadow200 committed Nov 26, 2022
1 parent 28681d7 commit 5803589
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 21 deletions.
13 changes: 6 additions & 7 deletions MCGalaxy/Network/Heartbeat/ClassiCube.cs
Expand Up @@ -29,7 +29,7 @@ namespace MCGalaxy.Network
public sealed class ClassiCubeBeat : Heartbeat
{
string proxyUrl;
public string ServerHash;
public string ServerHash, ServerUrl;
bool checkedAddr;

void CheckAddress() {
Expand Down Expand Up @@ -103,18 +103,17 @@ public sealed class ClassiCubeBeat : Heartbeat
// only need to do this when contents have changed
if (hash == ServerHash) return;
ServerHash = hash;
Server.URL = text;
ServerUrl = text;

if (!text.Contains("\"errors\":")) {
Server.UpdateUrl(Server.URL);
File.WriteAllText("text/externalurl.txt", Server.URL);
Logger.Log(LogType.SystemActivity, "Server URL found: " + Server.URL);
Server.UpdateUrl(text);
File.WriteAllText("text/externalurl.txt", text);
Logger.Log(LogType.SystemActivity, "Server URL found: " + text);
} else {
string error = GetError(text);
if (error == null) error = "Error while finding URL. Is the port open?";

Server.URL = error;
Server.UpdateUrl(Server.URL);
Server.UpdateUrl(error);
Logger.Log(LogType.Warning, text);
}
}
Expand Down
3 changes: 3 additions & 0 deletions MCGalaxy/Network/Heartbeat/Heartbeat.cs
Expand Up @@ -90,6 +90,9 @@ public abstract class Heartbeat
}

static void OnBeat(SchedulerTask task) {
// no point if can't accept connections anyways
if (!Server.Listener.Listening) return;

foreach (Heartbeat beat in Heartbeats) { beat.Pump(); }
}
}
Expand Down
14 changes: 13 additions & 1 deletion MCGalaxy/Network/Listeners.cs
Expand Up @@ -26,6 +26,8 @@ public abstract class INetListen
public IPAddress IP;
/// <summary> The port this network socket is listening on </summary>
public int Port;
/// <summary> Whether connections are currently being accepted </summary>
public bool Listening;

/// <summary> Begins listening for connections on the given IP and port </summary>
/// <remarks> Client connections are asynchronously accepted </remarks>
Expand Down Expand Up @@ -88,8 +90,13 @@ public sealed class TcpListen : INetListen
} catch (Exception ex) {
Logger.LogError(ex);
Logger.Log(LogType.Warning, "Failed to start listening on port {0} ({1})", port, ex.Message);

string msg = String.Format("Failed to start listening. Is another server or instance of {0} already running on port {1}?",
Server.SoftwareName, port);
Server.UpdateUrl(msg);
socket = null; return;
}
Listening = true;
Logger.Log(LogType.SystemActivity, "Started listening on port {0}... ", port);
}

Expand Down Expand Up @@ -131,7 +138,12 @@ public sealed class TcpListen : INetListen
}

public override void Close() {
if (socket != null) socket.Close();
try {
Listening = false;
if (socket != null) socket.Close();
} catch (Exception ex) {
Logger.LogError(ex);
}
}
}
}
2 changes: 1 addition & 1 deletion MCGalaxy/Network/Utils/HttpUtil.cs
Expand Up @@ -76,7 +76,7 @@ class CustomWebClient : WebClient {

static IPEndPoint BindIPEndPointCallback(ServicePoint servicePoint, IPEndPoint remoteEP, int retryCount) {
IPAddress localIP = null;
if (Server.Listener != null) {
if (Server.Listener.IP != null) {
localIP = Server.Listener.IP;
} else if (!IPAddress.TryParse(Server.Config.ListenIP, out localIP)) {
return null;
Expand Down
3 changes: 0 additions & 3 deletions MCGalaxy/Player/List/PlayerList.cs
Expand Up @@ -67,9 +67,6 @@ public class PlayerList {
}


[Obsolete("Use Add instead")]
public bool AddUnique(string name) { return Add(name); }

internal int IndexOf(string name) {
lock (locker) return names.CaselessIndexOf(name);
}
Expand Down
4 changes: 1 addition & 3 deletions MCGalaxy/Server/Server.Fields.cs
Expand Up @@ -49,9 +49,7 @@ public sealed partial class Server {
set { fullName = value; }
}

// URL for connecting to the server
public static string URL = String.Empty;
public static INetListen Listener;
public static INetListen Listener = new TcpListen();

//Other
public static bool SetupFinished, CLIMode;
Expand Down
3 changes: 1 addition & 2 deletions MCGalaxy/Server/Server.Init.cs
Expand Up @@ -96,8 +96,7 @@ public sealed partial class Server {
}
}

static void SetupSocket(SchedulerTask task) {
Listener = new TcpListen();
static void SetupSocket(SchedulerTask task) {
IPAddress ip;

if (!IPAddress.TryParse(Server.Config.ListenIP, out ip)) {
Expand Down
5 changes: 1 addition & 4 deletions MCGalaxy/Server/Server.cs
Expand Up @@ -228,10 +228,7 @@ public sealed partial class Server
} catch { }

// Stop accepting new connections and disconnect existing sessions
try {
if (Listener != null) Listener.Close();
} catch (Exception ex) { Logger.LogError(ex); }

Listener.Close();
try {
Player[] players = PlayerInfo.Online.Items;
foreach (Player p in players) { p.Leave(msg); }
Expand Down

0 comments on commit 5803589

Please sign in to comment.