diff options
author | Kae <80987908+Novaenia@users.noreply.github.com> | 2023-06-20 14:33:09 +1000 |
---|---|---|
committer | Kae <80987908+Novaenia@users.noreply.github.com> | 2023-06-20 14:33:09 +1000 |
commit | 6352e8e3196f78388b6c771073f9e03eaa612673 (patch) | |
tree | e23772f79a7fbc41bc9108951e9e136857484bf4 /source/server/main.cpp | |
parent | 6741a057e5639280d85d0f88ba26f000baa58f61 (diff) |
everything everywhere
all at once
Diffstat (limited to 'source/server/main.cpp')
-rw-r--r-- | source/server/main.cpp | 94 |
1 files changed, 94 insertions, 0 deletions
diff --git a/source/server/main.cpp b/source/server/main.cpp new file mode 100644 index 0000000..6522acf --- /dev/null +++ b/source/server/main.cpp @@ -0,0 +1,94 @@ +#include "StarFile.hpp" +#include "StarRandom.hpp" +#include "StarLexicalCast.hpp" +#include "StarLogging.hpp" +#include "StarUniverseServer.hpp" +#include "StarRootLoader.hpp" +#include "StarConfiguration.hpp" +#include "StarVersion.hpp" +#include "StarServerQueryThread.hpp" +#include "StarServerRconThread.hpp" +#include "StarSignalHandler.hpp" + +using namespace Star; + +Json const AdditionalDefaultConfiguration = Json::parseJson(R"JSON( + { + "configurationVersion" : { + "server" : 4 + }, + + "runQueryServer" : false, + "queryServerPort" : 21025, + "queryServerBind" : "::", + + "runRconServer" : false, + "rconServerPort" : 21026, + "rconServerBind" : "::", + "rconServerPassword" : "", + "rconServerTimeout" : 1000, + + "allowAssetsMismatch" : true, + "serverOverrideAssetsDigest" : null + } + )JSON"); + +int main(int argc, char** argv) { + try { + RootLoader rootLoader({{}, AdditionalDefaultConfiguration, String("starbound_server.log"), LogLevel::Info, false, String("starbound_server.config")}); + RootUPtr root = rootLoader.commandInitOrDie(argc, argv).first; + root->fullyLoad(); + + SignalHandler signalHandler; + signalHandler.setHandleFatal(true); + signalHandler.setHandleInterrupt(true); + + auto configuration = root->configuration(); + { + Logger::info("Server Version %s (%s) Source ID: %s Protocol: %s", StarVersionString, StarArchitectureString, StarSourceIdentifierString, StarProtocolVersion); + + UniverseServerUPtr server = make_unique<UniverseServer>(root->toStoragePath("universe")); + server->setListeningTcp(true); + server->start(); + + ServerQueryThreadUPtr queryServer; + if (configuration->get("runQueryServer").toBool()) { + queryServer = make_unique<ServerQueryThread>(server.get(), HostAddressWithPort(configuration->get("queryServerBind").toString(), configuration->get("queryServerPort").toInt())); + queryServer->start(); + } + + ServerRconThreadUPtr rconServer; + if (configuration->get("runRconServer").toBool()) { + rconServer = make_unique<ServerRconThread>(server.get(), HostAddressWithPort(configuration->get("rconServerBind").toString(), configuration->get("rconServerPort").toInt())); + rconServer->start(); + } + + while (server->isRunning()) { + if (signalHandler.interruptCaught()) { + Logger::info("Interrupt caught!"); + server->stop(); + break; + } + Thread::sleep(100); + } + + server->join(); + + if (queryServer) { + queryServer->stop(); + queryServer->join(); + } + + if (rconServer) { + rconServer->stop(); + rconServer->join(); + } + } + + Logger::info("Server shutdown gracefully"); + } catch (std::exception const& e) { + fatalException(e, true); + } + + return 0; +} |