Skip to content

Commit

Permalink
Update server state change handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Adam Walker committed Jan 28, 2014
1 parent 770c77b commit a2c90a3
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 13 deletions.
4 changes: 2 additions & 2 deletions Commands/Shutdown.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public override bool doProcess(string[] args)
{
if (!hasPermission()) { permissionError(); return false; }

StarryboundServer.serverState = ServerState.GracefulShutdown;
StarryboundServer.changeState(ServerState.GracefulShutdown, "Command::Shutdown", "Shutdown requested by " + this.player.name);
return true;
}
}
Expand All @@ -61,7 +61,7 @@ public override bool doProcess(string[] args)
{
StarryboundServer.sendGlobalMessage("^#f75d5d;The server restart has been aborted by " + this.player.name);
StarryboundServer.logWarn("The server restart has been aborted.");
StarryboundServer.serverState = ServerState.Running;
StarryboundServer.changeState(ServerState.Running, "Command::Restart");
StarryboundServer.restartTime = 0;
}
else
Expand Down
9 changes: 6 additions & 3 deletions ListenerThread.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public void runTcp()
tcpSocket.Start();

StarryboundServer.logInfo("Proxy server has been started on " + localAdd.ToString() + ":" + StarryboundServer.config.proxyPort);
StarryboundServer.serverState = ServerState.ListenerReady;
StarryboundServer.changeState(ServerState.ListenerReady, "ListenerThread::runTcp");

try
{
Expand All @@ -52,16 +52,19 @@ public void runTcp()
new Thread(new ThreadStart(new Client(clientSocket).run)).Start();
}
}
catch (ThreadAbortException) { }
catch (ThreadAbortException)
{
StarryboundServer.changeState(ServerState.Crashed, "ListenerThread::runTcp", "Thread has been aborted");
}
catch (Exception e)
{
if ((int)StarryboundServer.serverState > 3) return;
StarryboundServer.logException("ListenerThread Exception: " + e.ToString());
StarryboundServer.changeState(ServerState.Crashed, "ListenerThread::runTcp", e.ToString());
}

tcpSocket.Stop();
StarryboundServer.logFatal("ListenerThread has failed - No new connections will be possible.");
StarryboundServer.serverState = ServerState.Crashed;
}
catch (ThreadAbortException) { }
catch(SocketException e)
Expand Down
10 changes: 5 additions & 5 deletions ServerThread.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ public void run()
process.ErrorDataReceived += (sender, e) => logStarboundError("ErrorDataReceived from starbound_server.exe: " + e.Data);
process.BeginOutputReadLine();
process.WaitForExit();
StarryboundServer.serverState = ServerState.Crashed;

StarryboundServer.changeState(ServerState.Crashed, "ServerThread::run", "starbound_server.exe process has exited.");
}
catch (ThreadAbortException) { }
catch (Exception e)
{
StarryboundServer.logException("Unable to start starbound_server.exe, is this file in the same directory? " + e.ToString());
StarryboundServer.serverState = ServerState.Crashed;
StarryboundServer.changeState(ServerState.Crashed, "ServerThread::run::Exception", e.ToString());
}
}

Expand Down Expand Up @@ -101,11 +101,11 @@ void parseOutput(string consoleLine)
else if (consoleLine.Contains("TcpServer will close, listener thread caught exception"))
{
StarryboundServer.logFatal("Parent Server TcpServer listener thread caught exception, Forcing a restart.");
StarryboundServer.serverState = ServerState.Crashed;
StarryboundServer.changeState(ServerState.Crashed, "ServerThread::parseOutput", "Starbound Tcp listener has crashed");
}
else if (consoleLine.Contains("TcpServer listening on: "))
{
StarryboundServer.serverState = ServerState.StarboundReady;
StarryboundServer.changeState(ServerState.StarboundReady, "ServerThread::parseOutput");
ServerConfig.RemovePrivateConfig();
}
else if (consoleLine.StartsWith("Info: Kicking client "))
Expand Down
29 changes: 26 additions & 3 deletions StarryboundServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ class StarryboundServer
public static string privatePassword;

public static int failedConnections;
public static ServerState serverState;
private static ServerState aServerState;
public static ServerState serverState { get { return aServerState; } set { return; } }
public static int startTime;
public static int restartTime = 0;
private static bool shuttingDown = false;
Expand Down Expand Up @@ -122,7 +123,7 @@ static void Main(string[] args)
config.logLevel = LogType.Debug;
#endif
startTime = Utils.getTimestamp();
serverState = ServerState.Starting;
changeState(ServerState.Starting, "StarryboundServer::Main");
Console.Title = "Loading... Starrybound Server (" + VersionNum + ") (" + ProtocolVersion + ")";

try
Expand Down Expand Up @@ -215,7 +216,7 @@ static void Main(string[] args)
if ((int)serverState > 3) return;
#endif
logInfo("Parent Starbound server is ready. Starrybound Server now accepting connections.");
serverState = ServerState.Running;
changeState(ServerState.Running, "StarryboundServer::Main");
}

private static void crashMonitor()
Expand Down Expand Up @@ -284,6 +285,28 @@ private static void crashMonitor()
}
}

public static void changeState(ServerState aState, string caller, string reason = "Not Specified")
{
string format = "StateChange requested by {0} to {1}: {2}";

switch (aState)
{
case ServerState.Crashed:
logWarn(string.Format(format, caller, aState, reason));
break;

case ServerState.GracefulShutdown:
logWarn(string.Format(format, caller, aState, reason));
break;

default:
logDebug("StarryboundServer::changeState", string.Format(format, caller, aState, reason));
break;
}

aServerState = aState;
}

private static void doRestart()
{
doShutdown(false);
Expand Down

0 comments on commit a2c90a3

Please sign in to comment.