ServerRunner.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. boost::thread threadRunLocalServer;
  32. uint16_t serverPort = 0;
  33. public:
  34. void start(bool listenForConnections, bool connectToLobby, std::shared_ptr<StartInfo> startingInfo) override;
  35. void shutdown() override;
  36. void wait() override;
  37. int exitCode() override;
  38. void connect(INetworkHandler & network, INetworkClientListener & listener) override;
  39. ServerThreadRunner();
  40. ~ServerThreadRunner();
  41. };
  42. #ifndef VCMI_MOBILE
  43. // Enable support for running vcmiserver as separate process. Unavailable on mobile systems
  44. #define ENABLE_SERVER_PROCESS
  45. #endif
  46. #ifdef ENABLE_SERVER_PROCESS
  47. #if BOOST_VERSION >= 108600
  48. namespace boost::process {
  49. inline namespace v1 {
  50. class child;
  51. }
  52. }
  53. #else
  54. namespace boost::process {
  55. class child;
  56. }
  57. #endif
  58. /// Class that runs server instance as a child process
  59. /// Available only on desktop systems where process management is allowed
  60. class ServerProcessRunner final : public IServerRunner, boost::noncopyable
  61. {
  62. std::unique_ptr<boost::process::child> child;
  63. public:
  64. void start(bool listenForConnections, bool connectToLobby, std::shared_ptr<StartInfo> startingInfo) override;
  65. void shutdown() override;
  66. void wait() override;
  67. int exitCode() override;
  68. void connect(INetworkHandler & network, INetworkClientListener & listener) override;
  69. ServerProcessRunner();
  70. ~ServerProcessRunner();
  71. };
  72. #endif