public void run() { var executableName = "starbound_server" + (StarryboundServer.IsMono ? "" : ".exe"); try { ProcessStartInfo startInfo = new ProcessStartInfo(executableName) { WindowStyle = ProcessWindowStyle.Hidden, UseShellExecute = false, RedirectStandardOutput = true, CreateNoWindow = true }; process = Process.Start(startInfo); StarryboundServer.parentProcessId = process.Id; File.WriteAllText("starbound_server.pid", process.Id.ToString()); process.OutputDataReceived += (sender, e) => parseOutput(e.Data); process.ErrorDataReceived += (sender, e) => logStarboundError("ErrorDataReceived from starbound_server.exe: " + e.Data); process.BeginOutputReadLine(); process.WaitForExit(); StarryboundServer.changeState(ServerState.Crashed, "ServerThread::run", "starbound_server.exe process has exited."); } catch (ThreadAbortException) { } catch (Exception e) { StarryboundServer.changeState(ServerState.Crashed, "ServerThread::run::Exception", e.ToString()); } }
public void runTcp() { try { IPAddress localAdd = IPAddress.Parse(StarryboundServer.config.proxyIP); tcpSocket = new TcpListener(localAdd, StarryboundServer.config.proxyPort); tcpSocket.Start(); StarryboundServer.logInfo("Proxy server has been started on " + localAdd.ToString() + ":" + StarryboundServer.config.proxyPort); StarryboundServer.changeState(ServerState.ListenerReady, "ListenerThread::runTcp"); try { while (true) { TcpClient clientSocket = tcpSocket.AcceptTcpClient(); clientSocket.ReceiveTimeout = StarryboundServer.config.clientSocketTimeout * 1000; clientSocket.SendTimeout = StarryboundServer.config.internalSocketTimeout * 1000; new Thread(new ThreadStart(new Client(clientSocket).run)).Start(); } } 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."); } catch (ThreadAbortException) { } catch (SocketException e) { StarryboundServer.logFatal("TcpListener has failed to start: " + e.Message); StarryboundServer.serverState = ServerState.Crashed; } }
void parseOutput(string consoleLine) { try { foreach (string line in filterConsole) { if (consoleLine.Contains(line)) { return; } } if (consoleLine.StartsWith("Error: ") || consoleLine.StartsWith("AssetException:")) { this.parseError = true; } else if ((consoleLine.StartsWith("Warn:") || consoleLine.StartsWith("Info:") || consoleLine.StartsWith("Debug:")) && this.parseError) { logStarboundError(" "); this.parseError = false; } else if (String.IsNullOrWhiteSpace(consoleLine) && this.parseError) { logStarboundError(" "); this.parseError = false; return; } if (consoleLine.StartsWith("Warn: Perf: ")) { string[] perf = consoleLine.Remove(0, 12).Split(' '); string function = perf[0]; float millis = Convert.ToSingle(perf[2]); if (millis > 5000) { StarryboundServer.logWarn("Parent Server [" + function + "] lagged for " + (millis / 1000) + " seconds"); } return; } else if (consoleLine.Contains("Info: Server version")) { string[] versionString = consoleLine.Split('\''); string versionName = versionString[1]; int protocolVersion = int.Parse(versionString[3]); StarryboundServer.starboundVersion.Protocol = protocolVersion; StarryboundServer.starboundVersion.Name = versionName; if (protocolVersion != StarryboundServer.ProtocolVersion) { StarryboundServer.logFatal("Detected protcol version [" + protocolVersion + "] != [" + StarryboundServer.ProtocolVersion + "] to expected protocol version!"); Thread.Sleep(5000); Environment.Exit(4); } StarryboundServer.logInfo("Parent Server Version: [" + versionName + "] Protocol: [" + protocolVersion + "]"); return; } else if (consoleLine.Contains("TcpServer will close, listener thread caught exception")) { StarryboundServer.logFatal("Parent Server TcpServer listener thread caught exception, Forcing a restart."); StarryboundServer.changeState(ServerState.Crashed, "ServerThread::parseOutput", "Starbound Tcp listener has crashed"); } else if (consoleLine.Contains("TcpServer listening on: ")) { StarryboundServer.changeState(ServerState.StarboundReady, "ServerThread::parseOutput"); ServerConfig.RemovePrivateConfig(); } else if (consoleLine.StartsWith("Info: Kicking client ")) { string[] kick = consoleLine.Remove(0, 21).Split(' '); string user = kick[0]; string id = kick[1]; string ip = kick[2]; StarryboundServer.logWarn("Parent Server disconnected " + user + " " + ip + " for inactivity."); return; } if (!this.parseError) { Console.WriteLine("[STAR] " + consoleLine); } else { logStarboundError(consoleLine); } } catch (Exception) { } }