ServerRunner.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. lobbyMode = connectToLobby;
  36. if (startingInfo)
  37. {
  38. server->si = startingInfo; //Else use default
  39. }
  40. std::promise<uint16_t> promise;
  41. threadRunLocalServer = std::thread([this, connectToLobby, listenForConnections, &promise]{
  42. setThreadName("runServer");
  43. uint16_t port = server->prepare(connectToLobby, listenForConnections);
  44. promise.set_value(port);
  45. server->run();
  46. });
  47. logNetwork->trace("Waiting for server port...");
  48. serverPort = promise.get_future().get();
  49. logNetwork->debug("Server port: %d", serverPort);
  50. }
  51. void ServerThreadRunner::shutdown()
  52. {
  53. server->setState(EServerState::SHUTDOWN);
  54. }
  55. void ServerThreadRunner::wait()
  56. {
  57. threadRunLocalServer.join();
  58. }
  59. int ServerThreadRunner::exitCode()
  60. {
  61. return 0;
  62. }
  63. void ServerThreadRunner::connect(INetworkHandler & network, INetworkClientListener & listener)
  64. {
  65. if (lobbyMode)
  66. {
  67. std::string host = settings["server"]["localHostname"].String();
  68. uint16_t port = settings["server"]["localPort"].Integer();
  69. logNetwork->info("Establishing connection to %s:%d...", host, port);
  70. network.connectToRemote(listener, host, port);
  71. }
  72. else
  73. {
  74. network.createInternalConnection(listener, server->getNetworkServer());
  75. }
  76. }
  77. #ifdef ENABLE_SERVER_PROCESS
  78. ServerProcessRunner::ServerProcessRunner() = default;
  79. ServerProcessRunner::~ServerProcessRunner() = default;
  80. void ServerProcessRunner::shutdown()
  81. {
  82. child->terminate();
  83. }
  84. void ServerProcessRunner::wait()
  85. {
  86. child->wait();
  87. }
  88. int ServerProcessRunner::exitCode()
  89. {
  90. return child->exit_code();
  91. }
  92. void ServerProcessRunner::start(bool listenForConnections, bool connectToLobby, std::shared_ptr<StartInfo> startingInfo)
  93. {
  94. uint16_t port = settings["server"]["localPort"].Integer();
  95. boost::filesystem::path serverPath = VCMIDirs::get().serverPath();
  96. boost::filesystem::path logPath = VCMIDirs::get().userLogsPath() / "server_log.txt";
  97. std::vector<std::string> args;
  98. args.push_back("--port=" + std::to_string(port));
  99. args.push_back("--run-by-client");
  100. if(connectToLobby)
  101. args.push_back("--lobby");
  102. std::error_code ec;
  103. child = std::make_unique<boost::process::child>(serverPath, args, ec, boost::process::std_out > logPath);
  104. if (ec)
  105. throw std::runtime_error("Failed to start server! Reason: " + ec.message());
  106. }
  107. void ServerProcessRunner::connect(INetworkHandler & network, INetworkClientListener & listener)
  108. {
  109. std::string host = settings["server"]["localHostname"].String();
  110. uint16_t port = settings["server"]["localPort"].Integer();
  111. logNetwork->info("Establishing connection to %s:%d...", host, port);
  112. network.connectToRemote(listener, host, port);
  113. }
  114. #endif