123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- /*
- * 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"
- #ifndef VCMI_MOBILE
- #include <boost/process/child.hpp>
- #include <boost/process/io.hpp>
- #endif
- #include <future>
- ServerThreadRunner::ServerThreadRunner() = default;
- ServerThreadRunner::~ServerThreadRunner() = default;
- uint16_t ServerThreadRunner::start(uint16_t cfgport, bool connectToLobby, std::shared_ptr<StartInfo> startingInfo)
- {
- // 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
- }
- std::promise<uint16_t> promise;
- threadRunLocalServer = boost::thread([this, connectToLobby, &promise]{
- setThreadName("runServer");
- std::this_thread::sleep_for(std::chrono::seconds(5));
- 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()
- {
- server->setState(EServerState::SHUTDOWN);
- }
- void ServerThreadRunner::wait()
- {
- threadRunLocalServer.join();
- }
- int ServerThreadRunner::exitCode()
- {
- return 0;
- }
- #ifndef VCMI_MOBILE
- ServerProcessRunner::ServerProcessRunner() = default;
- ServerProcessRunner::~ServerProcessRunner() = default;
- void ServerProcessRunner::shutdown()
- {
- child->terminate();
- }
- void ServerProcessRunner::wait()
- {
- child->wait();
- }
- int ServerProcessRunner::exitCode()
- {
- return child->exit_code();
- }
- 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";
- 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());
- return port;
- }
- #endif
|