ServerRunner.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * ServerRunner.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "ServerRunner.h"
  12. #include "../lib/VCMIDirs.h"
  13. #include "../lib/CThreadHelper.h"
  14. #include "../lib/network/NetworkInterface.h"
  15. #include "../lib/CConfigHandler.h"
  16. #include "../server/CVCMIServer.h"
  17. #ifdef ENABLE_SERVER_PROCESS
  18. #if BOOST_VERSION >= 108600
  19. // TODO: upgrade code to use v2 API instead of deprecated v1
  20. #include <boost/process/v1/child.hpp>
  21. #include <boost/process/v1/io.hpp>
  22. #else
  23. #include <boost/process/child.hpp>
  24. #include <boost/process/io.hpp>
  25. #endif
  26. #endif
  27. #include <future>
  28. ServerThreadRunner::ServerThreadRunner() = default;
  29. ServerThreadRunner::~ServerThreadRunner() = default;
  30. void ServerThreadRunner::start(bool listenForConnections, bool connectToLobby, std::shared_ptr<StartInfo> startingInfo)
  31. {
  32. // cfgport may be 0 -- the real port is returned after calling prepare()
  33. uint16_t port = settings["server"]["localPort"].Integer();
  34. server = std::make_unique<CVCMIServer>(port, true);
  35. if (startingInfo)
  36. {
  37. server->si = startingInfo; //Else use default
  38. }
  39. std::promise<uint16_t> promise;
  40. threadRunLocalServer = boost::thread([this, connectToLobby, listenForConnections, &promise]{
  41. setThreadName("runServer");
  42. uint16_t port = server->prepare(connectToLobby, listenForConnections);
  43. promise.set_value(port);
  44. server->run();
  45. });
  46. logNetwork->trace("Waiting for server port...");
  47. serverPort = promise.get_future().get();
  48. logNetwork->debug("Server port: %d", serverPort);
  49. }
  50. void ServerThreadRunner::shutdown()
  51. {
  52. server->setState(EServerState::SHUTDOWN);
  53. }
  54. void ServerThreadRunner::wait()
  55. {
  56. threadRunLocalServer.join();
  57. }
  58. int ServerThreadRunner::exitCode()
  59. {
  60. return 0;
  61. }
  62. void ServerThreadRunner::connect(INetworkHandler & network, INetworkClientListener & listener)
  63. {
  64. network.createInternalConnection(listener, server->getNetworkServer());
  65. }
  66. #ifdef ENABLE_SERVER_PROCESS
  67. ServerProcessRunner::ServerProcessRunner() = default;
  68. ServerProcessRunner::~ServerProcessRunner() = default;
  69. void ServerProcessRunner::shutdown()
  70. {
  71. child->terminate();
  72. }
  73. void ServerProcessRunner::wait()
  74. {
  75. child->wait();
  76. }
  77. int ServerProcessRunner::exitCode()
  78. {
  79. return child->exit_code();
  80. }
  81. void ServerProcessRunner::start(bool listenForConnections, bool connectToLobby, std::shared_ptr<StartInfo> startingInfo)
  82. {
  83. uint16_t port = settings["server"]["localPort"].Integer();
  84. boost::filesystem::path serverPath = VCMIDirs::get().serverPath();
  85. boost::filesystem::path logPath = VCMIDirs::get().userLogsPath() / "server_log.txt";
  86. std::vector<std::string> args;
  87. args.push_back("--port=" + std::to_string(port));
  88. args.push_back("--run-by-client");
  89. if(connectToLobby)
  90. args.push_back("--lobby");
  91. std::error_code ec;
  92. child = std::make_unique<boost::process::child>(serverPath, args, ec, boost::process::std_out > logPath);
  93. if (ec)
  94. throw std::runtime_error("Failed to start server! Reason: " + ec.message());
  95. }
  96. void ServerProcessRunner::connect(INetworkHandler & network, INetworkClientListener & listener)
  97. {
  98. std::string host = settings["server"]["localHostname"].String();
  99. uint16_t port = settings["server"]["localPort"].Integer();
  100. network.connectToRemote(listener, host, port);
  101. }
  102. #endif