Bind server to a randomly assigned port

This commit is contained in:
Simeon Manolov
2024-07-12 01:06:36 +03:00
parent 719f920914
commit 9d73b50979
9 changed files with 57 additions and 32 deletions
+17 -4
View File
@@ -23,19 +23,30 @@
ServerThreadRunner::ServerThreadRunner() = default;
ServerThreadRunner::~ServerThreadRunner() = default;
void ServerThreadRunner::start(uint16_t port, bool connectToLobby, std::shared_ptr<StartInfo> startingInfo)
uint16_t ServerThreadRunner::start(uint16_t port, bool connectToLobby, std::shared_ptr<StartInfo> startingInfo)
{
server = std::make_unique<CVCMIServer>(port, connectToLobby, true);
server = std::make_unique<CVCMIServer>(port, true);
if (startingInfo)
{
server->si = startingInfo; //Else use default
}
threadRunLocalServer = boost::thread([this]{
uint16_t srvport = port;
threadRunLocalServer = boost::thread([this, connectToLobby, &srvport]{
setThreadName("runServer");
srvport = server->prepare(connectToLobby);
server->run();
});
while(srvport == 0) {
logNetwork->trace("Waiting for server port...");
boost::this_thread::sleep(boost::posix_time::milliseconds(100));
logNetwork->debug("Server port: %d", srvport);
}
return srvport;
}
void ServerThreadRunner::shutdown()
@@ -73,7 +84,7 @@ int ServerProcessRunner::exitCode()
return child->exit_code();
}
void ServerProcessRunner::start(uint16_t port, bool connectToLobby, std::shared_ptr<StartInfo> startingInfo)
uint16_t ServerProcessRunner::start(uint16_t port, bool connectToLobby, std::shared_ptr<StartInfo> startingInfo)
{
boost::filesystem::path serverPath = VCMIDirs::get().serverPath();
boost::filesystem::path logPath = VCMIDirs::get().userLogsPath() / "server_log.txt";
@@ -88,6 +99,8 @@ void ServerProcessRunner::start(uint16_t port, bool connectToLobby, std::shared_
if (ec)
throw std::runtime_error("Failed to start server! Reason: " + ec.message());
return port;
}
#endif