|
|
@@ -20,22 +20,35 @@
|
|
|
#include <boost/process/io.hpp>
|
|
|
#endif
|
|
|
|
|
|
+#include <future>
|
|
|
+
|
|
|
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 cfgport, bool connectToLobby, std::shared_ptr<StartInfo> startingInfo)
|
|
|
{
|
|
|
- server = std::make_unique<CVCMIServer>(port, connectToLobby, true);
|
|
|
+ // cfgport may be 0 -- the real port is returned after calling prepare()
|
|
|
+ server = std::make_unique<CVCMIServer>(cfgport, true);
|
|
|
|
|
|
if (startingInfo)
|
|
|
{
|
|
|
server->si = startingInfo; //Else use default
|
|
|
}
|
|
|
|
|
|
- threadRunLocalServer = boost::thread([this]{
|
|
|
+ std::promise<uint16_t> promise;
|
|
|
+
|
|
|
+ threadRunLocalServer = boost::thread([this, connectToLobby, &promise]{
|
|
|
setThreadName("runServer");
|
|
|
+ uint16_t port = server->prepare(connectToLobby);
|
|
|
+ promise.set_value(port);
|
|
|
server->run();
|
|
|
});
|
|
|
+
|
|
|
+ logNetwork->trace("Waiting for server port...");
|
|
|
+ auto srvport = promise.get_future().get();
|
|
|
+ logNetwork->debug("Server port: %d", srvport);
|
|
|
+
|
|
|
+ return srvport;
|
|
|
}
|
|
|
|
|
|
void ServerThreadRunner::shutdown()
|
|
|
@@ -73,7 +86,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 +101,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
|