cmServer.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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> // IWYU pragma: keep
  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. CM_DISABLE_COPY(cmServer)
  75. public:
  76. class DebugInfo;
  77. cmServer(cmConnection* conn, bool supportExperimental);
  78. ~cmServer() override;
  79. bool Serve(std::string* errorMessage) override;
  80. cmFileMonitor* FileMonitor() const;
  81. private:
  82. void RegisterProtocol(cmServerProtocol* protocol);
  83. // Callbacks from cmServerConnection:
  84. void ProcessRequest(cmConnection* connection,
  85. const std::string& request) override;
  86. std::shared_ptr<cmFileMonitor> fileMonitor;
  87. public:
  88. void OnServeStart() override;
  89. void StartShutDown() override;
  90. public:
  91. void OnConnected(cmConnection* connection) override;
  92. private:
  93. static void reportProgress(const char* msg, float progress, void* data);
  94. static void reportMessage(const char* msg, const char* title, bool& cancel,
  95. void* data);
  96. // Handle requests:
  97. cmServerResponse SetProtocolVersion(const cmServerRequest& request);
  98. void PrintHello(cmConnection* connection) const;
  99. // Write responses:
  100. void WriteProgress(const cmServerRequest& request, int min, int current,
  101. int max, const std::string& message) const;
  102. void WriteMessage(const cmServerRequest& request, const std::string& message,
  103. const std::string& title) const;
  104. void WriteResponse(cmConnection* connection,
  105. const cmServerResponse& response,
  106. const DebugInfo* debug) const;
  107. void WriteParseError(cmConnection* connection,
  108. const std::string& message) const;
  109. void WriteSignal(const std::string& name, const Json::Value& obj) const;
  110. void WriteJsonObject(Json::Value const& jsonValue,
  111. const DebugInfo* debug) const;
  112. void WriteJsonObject(cmConnection* connection, Json::Value const& jsonValue,
  113. const DebugInfo* debug) const;
  114. static cmServerProtocol* FindMatchingProtocol(
  115. const std::vector<cmServerProtocol*>& protocols, int major, int minor);
  116. const bool SupportExperimental;
  117. cmServerProtocol* Protocol = nullptr;
  118. std::vector<cmServerProtocol*> SupportedProtocols;
  119. friend class cmServerProtocol;
  120. friend class cmServerRequest;
  121. };