ServerRunner.cpp 2.5 KB

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