ServerRunner.cpp 2.5 KB

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