cmServer.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #pragma once
  4. #include "cmConfigure.h" // IWYU pragma: keep
  5. #include "cm_jsoncpp_value.h"
  6. #include "cm_thread.hxx"
  7. #include "cm_uv.h"
  8. #include "cmUVHandlePtr.h"
  9. #include <memory>
  10. #include <string>
  11. #include <vector>
  12. class cmConnection;
  13. class cmFileMonitor;
  14. class cmServerProtocol;
  15. class cmServerRequest;
  16. class cmServerResponse;
  17. /***
  18. * This essentially hold and manages a libuv event queue and responds to
  19. * messages
  20. * on any of its connections.
  21. */
  22. class cmServerBase
  23. {
  24. public:
  25. cmServerBase(cmConnection* connection);
  26. virtual ~cmServerBase();
  27. virtual void AddNewConnection(cmConnection* ownedConnection);
  28. /***
  29. * The main override responsible for tailoring behavior towards
  30. * whatever the given server is supposed to do
  31. *
  32. * This should almost always be called by the given connections
  33. * directly.
  34. *
  35. * @param connection The connection the request was received on
  36. * @param request The actual request
  37. */
  38. virtual void ProcessRequest(cmConnection* connection,
  39. const std::string& request) = 0;
  40. virtual void OnConnected(cmConnection* connection);
  41. /***
  42. * Start a dedicated thread. If this is used to start the server, it will
  43. * join on the
  44. * servers dtor.
  45. */
  46. virtual bool StartServeThread();
  47. virtual bool Serve(std::string* errorMessage);
  48. virtual void OnServeStart();
  49. virtual void StartShutDown();
  50. virtual bool OnSignal(int signum);
  51. uv_loop_t* GetLoop();
  52. void Close();
  53. void OnDisconnect(cmConnection* pConnection);
  54. protected:
  55. mutable cm::shared_mutex ConnectionsMutex;
  56. std::vector<std::unique_ptr<cmConnection>> Connections;
  57. bool ServeThreadRunning = false;
  58. uv_thread_t ServeThread;
  59. cm::uv_async_ptr ShutdownSignal;
  60. #ifndef NDEBUG
  61. public:
  62. // When the server starts it will mark down it's current thread ID,
  63. // which is useful in other contexts to just assert that operations
  64. // are performed on that same thread.
  65. uv_thread_t ServeThreadId = {};
  66. protected:
  67. #endif
  68. uv_loop_t Loop;
  69. cm::uv_signal_ptr SIGINTHandler;
  70. cm::uv_signal_ptr SIGHUPHandler;
  71. };
  72. class cmServer : public cmServerBase
  73. {
  74. public:
  75. class DebugInfo;
  76. cmServer(cmConnection* conn, bool supportExperimental);
  77. ~cmServer() override;
  78. cmServer(cmServer const&) = delete;
  79. cmServer& operator=(cmServer const&) = delete;
  80. bool Serve(std::string* errorMessage) override;
  81. cmFileMonitor* FileMonitor() const;
  82. private:
  83. void RegisterProtocol(cmServerProtocol* protocol);
  84. // Callbacks from cmServerConnection:
  85. void ProcessRequest(cmConnection* connection,
  86. const std::string& request) override;
  87. std::shared_ptr<cmFileMonitor> fileMonitor;
  88. public:
  89. void OnServeStart() override;
  90. void StartShutDown() override;
  91. public:
  92. void OnConnected(cmConnection* connection) override;
  93. private:
  94. static void reportProgress(const std::string& msg, float progress,
  95. const cmServerRequest& request);
  96. static void reportMessage(const std::string& msg, const char* title,
  97. const cmServerRequest& request);
  98. // Handle requests:
  99. cmServerResponse SetProtocolVersion(const cmServerRequest& request);
  100. void PrintHello(cmConnection* connection) const;
  101. // Write responses:
  102. void WriteProgress(const cmServerRequest& request, int min, int current,
  103. int max, const std::string& message) const;
  104. void WriteMessage(const cmServerRequest& request, const std::string& message,
  105. const std::string& title) const;
  106. void WriteResponse(cmConnection* connection,
  107. const cmServerResponse& response,
  108. const DebugInfo* debug) const;
  109. void WriteParseError(cmConnection* connection,
  110. const std::string& message) const;
  111. void WriteSignal(const std::string& name, const Json::Value& obj) const;
  112. void WriteJsonObject(Json::Value const& jsonValue,
  113. const DebugInfo* debug) const;
  114. void WriteJsonObject(cmConnection* connection, Json::Value const& jsonValue,
  115. const DebugInfo* debug) const;
  116. static cmServerProtocol* FindMatchingProtocol(
  117. const std::vector<cmServerProtocol*>& protocols, int major, int minor);
  118. const bool SupportExperimental;
  119. cmServerProtocol* Protocol = nullptr;
  120. std::vector<cmServerProtocol*> SupportedProtocols;
  121. friend class cmServerProtocol;
  122. friend class cmServerRequest;
  123. };