ServerRunner.h 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. * ServerRunner.h, 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. #pragma once
  11. VCMI_LIB_NAMESPACE_BEGIN
  12. struct StartInfo;
  13. class INetworkHandler;
  14. class INetworkClientListener;
  15. VCMI_LIB_NAMESPACE_END
  16. class CVCMIServer;
  17. class IServerRunner
  18. {
  19. public:
  20. virtual void start(bool listenForConnections, bool connectToLobby, std::shared_ptr<StartInfo> startingInfo) = 0;
  21. virtual void shutdown() = 0;
  22. virtual void wait() = 0;
  23. virtual int exitCode() = 0;
  24. virtual void connect(INetworkHandler & network, INetworkClientListener & listener) = 0;
  25. virtual ~IServerRunner() = default;
  26. };
  27. /// Class that runs server instance as a thread of client process
  28. class ServerThreadRunner final : public IServerRunner, boost::noncopyable
  29. {
  30. std::unique_ptr<CVCMIServer> server;
  31. std::thread threadRunLocalServer;
  32. uint16_t serverPort = 0;
  33. bool lobbyMode = false;
  34. public:
  35. void start(bool listenForConnections, bool connectToLobby, std::shared_ptr<StartInfo> startingInfo) override;
  36. void shutdown() override;
  37. void wait() override;
  38. int exitCode() override;
  39. void connect(INetworkHandler & network, INetworkClientListener & listener) override;
  40. ServerThreadRunner();
  41. ~ServerThreadRunner();
  42. };
  43. #ifndef VCMI_MOBILE
  44. // Enable support for running vcmiserver as separate process. Unavailable on mobile systems
  45. #define ENABLE_SERVER_PROCESS
  46. #endif
  47. #ifdef ENABLE_SERVER_PROCESS
  48. #if BOOST_VERSION >= 108600
  49. namespace boost::process {
  50. inline namespace v1 {
  51. class child;
  52. }
  53. }
  54. #else
  55. namespace boost::process {
  56. class child;
  57. }
  58. #endif
  59. /// Class that runs server instance as a child process
  60. /// Available only on desktop systems where process management is allowed
  61. class ServerProcessRunner final : public IServerRunner, boost::noncopyable
  62. {
  63. std::unique_ptr<boost::process::child> child;
  64. public:
  65. void start(bool listenForConnections, bool connectToLobby, std::shared_ptr<StartInfo> startingInfo) override;
  66. void shutdown() override;
  67. void wait() override;
  68. int exitCode() override;
  69. void connect(INetworkHandler & network, INetworkClientListener & listener) override;
  70. ServerProcessRunner();
  71. ~ServerProcessRunner();
  72. };
  73. #endif