ServerRunner.h 1.8 KB

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