2024-02-10 23:56:02 +02:00
|
|
|
/*
|
|
|
|
* ServerRunner.cpp, part of VCMI engine
|
|
|
|
*
|
|
|
|
* Authors: listed in file AUTHORS in main folder
|
|
|
|
*
|
|
|
|
* License: GNU General Public License v2.0 or later
|
|
|
|
* Full text of license available in license.txt file, in main folder
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
#include "StdInc.h"
|
|
|
|
|
|
|
|
#include "ServerRunner.h"
|
|
|
|
|
|
|
|
#include "../lib/VCMIDirs.h"
|
|
|
|
#include "../lib/CThreadHelper.h"
|
|
|
|
#include "../server/CVCMIServer.h"
|
|
|
|
|
2024-09-06 18:57:57 +02:00
|
|
|
#ifdef ENABLE_SERVER_PROCESS
|
|
|
|
|
|
|
|
#if BOOST_VERSION >= 108600
|
|
|
|
// TODO: upgrade code to use v2 API instead of deprecated v1
|
|
|
|
#include <boost/process/v1/child.hpp>
|
|
|
|
#include <boost/process/v1/io.hpp>
|
|
|
|
#else
|
2024-02-10 23:56:02 +02:00
|
|
|
#include <boost/process/child.hpp>
|
|
|
|
#include <boost/process/io.hpp>
|
|
|
|
#endif
|
|
|
|
|
2024-09-06 18:57:57 +02:00
|
|
|
#endif
|
|
|
|
|
2024-07-12 14:26:31 +02:00
|
|
|
#include <future>
|
|
|
|
|
2024-02-10 23:56:02 +02:00
|
|
|
ServerThreadRunner::ServerThreadRunner() = default;
|
|
|
|
ServerThreadRunner::~ServerThreadRunner() = default;
|
|
|
|
|
2024-07-12 13:54:28 +02:00
|
|
|
uint16_t ServerThreadRunner::start(uint16_t cfgport, bool connectToLobby, std::shared_ptr<StartInfo> startingInfo)
|
2024-02-10 23:56:02 +02:00
|
|
|
{
|
2024-07-12 13:54:28 +02:00
|
|
|
// cfgport may be 0 -- the real port is returned after calling prepare()
|
|
|
|
server = std::make_unique<CVCMIServer>(cfgport, true);
|
2024-02-10 23:56:02 +02:00
|
|
|
|
2024-03-01 11:57:48 +02:00
|
|
|
if (startingInfo)
|
|
|
|
{
|
|
|
|
server->si = startingInfo; //Else use default
|
|
|
|
}
|
|
|
|
|
2024-07-12 13:54:28 +02:00
|
|
|
std::promise<uint16_t> promise;
|
2024-07-11 23:39:36 +02:00
|
|
|
|
2024-07-12 13:54:28 +02:00
|
|
|
threadRunLocalServer = boost::thread([this, connectToLobby, &promise]{
|
2024-02-11 15:38:29 +02:00
|
|
|
setThreadName("runServer");
|
2024-07-12 13:54:28 +02:00
|
|
|
uint16_t port = server->prepare(connectToLobby);
|
|
|
|
promise.set_value(port);
|
2024-02-10 23:56:02 +02:00
|
|
|
server->run();
|
|
|
|
});
|
2024-07-11 23:39:36 +02:00
|
|
|
|
2024-07-12 13:54:28 +02:00
|
|
|
logNetwork->trace("Waiting for server port...");
|
|
|
|
auto srvport = promise.get_future().get();
|
|
|
|
logNetwork->debug("Server port: %d", srvport);
|
2024-07-11 23:39:36 +02:00
|
|
|
|
|
|
|
return srvport;
|
2024-02-10 23:56:02 +02:00
|
|
|
}
|
|
|
|
|
2024-02-11 15:38:29 +02:00
|
|
|
void ServerThreadRunner::shutdown()
|
2024-02-10 23:56:02 +02:00
|
|
|
{
|
|
|
|
server->setState(EServerState::SHUTDOWN);
|
|
|
|
}
|
|
|
|
|
2024-02-11 15:38:29 +02:00
|
|
|
void ServerThreadRunner::wait()
|
2024-02-10 23:56:02 +02:00
|
|
|
{
|
|
|
|
threadRunLocalServer.join();
|
2024-02-11 15:38:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int ServerThreadRunner::exitCode()
|
|
|
|
{
|
2024-02-10 23:56:02 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2024-09-06 18:57:57 +02:00
|
|
|
#ifdef ENABLE_SERVER_PROCESS
|
2024-02-11 15:38:29 +02:00
|
|
|
|
|
|
|
ServerProcessRunner::ServerProcessRunner() = default;
|
|
|
|
ServerProcessRunner::~ServerProcessRunner() = default;
|
|
|
|
|
|
|
|
void ServerProcessRunner::shutdown()
|
2024-02-10 23:56:02 +02:00
|
|
|
{
|
|
|
|
child->terminate();
|
|
|
|
}
|
|
|
|
|
2024-02-11 15:38:29 +02:00
|
|
|
void ServerProcessRunner::wait()
|
2024-02-10 23:56:02 +02:00
|
|
|
{
|
|
|
|
child->wait();
|
2024-02-11 15:38:29 +02:00
|
|
|
}
|
2024-02-10 23:56:02 +02:00
|
|
|
|
2024-02-11 15:38:29 +02:00
|
|
|
int ServerProcessRunner::exitCode()
|
|
|
|
{
|
2024-02-10 23:56:02 +02:00
|
|
|
return child->exit_code();
|
|
|
|
}
|
|
|
|
|
2024-07-11 23:39:36 +02:00
|
|
|
uint16_t ServerProcessRunner::start(uint16_t port, bool connectToLobby, std::shared_ptr<StartInfo> startingInfo)
|
2024-02-10 23:56:02 +02:00
|
|
|
{
|
|
|
|
boost::filesystem::path serverPath = VCMIDirs::get().serverPath();
|
|
|
|
boost::filesystem::path logPath = VCMIDirs::get().userLogsPath() / "server_log.txt";
|
|
|
|
std::vector<std::string> args;
|
|
|
|
args.push_back("--port=" + std::to_string(port));
|
|
|
|
args.push_back("--run-by-client");
|
|
|
|
if(connectToLobby)
|
|
|
|
args.push_back("--lobby");
|
|
|
|
|
|
|
|
std::error_code ec;
|
|
|
|
child = std::make_unique<boost::process::child>(serverPath, args, ec, boost::process::std_out > logPath);
|
|
|
|
|
|
|
|
if (ec)
|
|
|
|
throw std::runtime_error("Failed to start server! Reason: " + ec.message());
|
2024-07-11 23:39:36 +02:00
|
|
|
|
|
|
|
return port;
|
2024-02-10 23:56:02 +02:00
|
|
|
}
|
|
|
|
|
2024-02-11 15:38:29 +02:00
|
|
|
#endif
|