osb/source/server/main.cpp

95 lines
2.8 KiB
C++
Raw Normal View History

2023-06-20 04:33:09 +00:00
#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;
}