2
0

ServerRunner.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 "../server/CVCMIServer.h"
  15. #ifdef ENABLE_SERVER_PROCESS
  16. #if BOOST_VERSION >= 108600
  17. // TODO: upgrade code to use v2 API instead of deprecated v1
  18. #include <boost/process/v1/child.hpp>
  19. #include <boost/process/v1/io.hpp>
  20. #else
  21. #include <boost/process/child.hpp>
  22. #include <boost/process/io.hpp>
  23. #endif
  24. #endif
  25. #include <future>
  26. ServerThreadRunner::ServerThreadRunner() = default;
  27. ServerThreadRunner::~ServerThreadRunner() = default;
  28. uint16_t ServerThreadRunner::start(uint16_t cfgport, bool connectToLobby, std::shared_ptr<StartInfo> startingInfo)
  29. {
  30. // cfgport may be 0 -- the real port is returned after calling prepare()
  31. server = std::make_unique<CVCMIServer>(cfgport, true);
  32. if (startingInfo)
  33. {
  34. server->si = startingInfo; //Else use default
  35. }
  36. std::promise<uint16_t> promise;
  37. threadRunLocalServer = boost::thread([this, connectToLobby, &promise]{
  38. setThreadName("runServer");
  39. uint16_t port = server->prepare(connectToLobby);
  40. promise.set_value(port);
  41. server->run();
  42. });
  43. logNetwork->trace("Waiting for server port...");
  44. auto srvport = promise.get_future().get();
  45. logNetwork->debug("Server port: %d", srvport);
  46. return srvport;
  47. }
  48. void ServerThreadRunner::shutdown()
  49. {
  50. server->setState(EServerState::SHUTDOWN);
  51. }
  52. void ServerThreadRunner::wait()
  53. {
  54. threadRunLocalServer.join();
  55. }
  56. int ServerThreadRunner::exitCode()
  57. {
  58. return 0;
  59. }
  60. #ifdef ENABLE_SERVER_PROCESS
  61. ServerProcessRunner::ServerProcessRunner() = default;
  62. ServerProcessRunner::~ServerProcessRunner() = default;
  63. void ServerProcessRunner::shutdown()
  64. {
  65. child->terminate();
  66. }
  67. void ServerProcessRunner::wait()
  68. {
  69. child->wait();
  70. }
  71. int ServerProcessRunner::exitCode()
  72. {
  73. return child->exit_code();
  74. }
  75. uint16_t ServerProcessRunner::start(uint16_t port, bool connectToLobby, std::shared_ptr<StartInfo> startingInfo)
  76. {
  77. boost::filesystem::path serverPath = VCMIDirs::get().serverPath();
  78. boost::filesystem::path logPath = VCMIDirs::get().userLogsPath() / "server_log.txt";
  79. std::vector<std::string> args;
  80. args.push_back("--port=" + std::to_string(port));
  81. args.push_back("--run-by-client");
  82. if(connectToLobby)
  83. args.push_back("--lobby");
  84. std::error_code ec;
  85. child = std::make_unique<boost::process::child>(serverPath, args, ec, boost::process::std_out > logPath);
  86. if (ec)
  87. throw std::runtime_error("Failed to start server! Reason: " + ec.message());
  88. return port;
  89. }
  90. #endif