cmConnection.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 "cmUVHandlePtr.h"
  6. #include "cm_uv.h"
  7. #include <cstddef>
  8. #include <memory>
  9. #include <string>
  10. class cmServerBase;
  11. /***
  12. * Given a sequence of bytes with any kind of buffering, instances of this
  13. * class arrange logical chunks according to whatever the use case is for
  14. * the connection.
  15. */
  16. class cmConnectionBufferStrategy
  17. {
  18. public:
  19. virtual ~cmConnectionBufferStrategy();
  20. /***
  21. * Called whenever with an active raw buffer. If a logical chunk
  22. * becomes available, that chunk is returned and that portion is
  23. * removed from the rawBuffer
  24. *
  25. * @param rawBuffer in/out parameter. Receive buffer; the buffer strategy is
  26. * free to manipulate this buffer anyway it needs to.
  27. *
  28. * @return Next chunk from the stream. Returns the empty string if a chunk
  29. * isn't ready yet. Users of this interface should repeatedly call this
  30. * function until an empty string is returned since its entirely possible
  31. * multiple chunks come in a single raw buffer.
  32. */
  33. virtual std::string BufferMessage(std::string& rawBuffer) = 0;
  34. /***
  35. * Called to properly buffer an outgoing message.
  36. *
  37. * @param rawBuffer Message to format in the correct way
  38. *
  39. * @return Formatted message
  40. */
  41. virtual std::string BufferOutMessage(const std::string& rawBuffer) const
  42. {
  43. return rawBuffer;
  44. };
  45. /***
  46. * Resets the internal state of the buffering
  47. */
  48. virtual void clear();
  49. // TODO: There should be a callback / flag set for errors
  50. };
  51. class cmConnection
  52. {
  53. CM_DISABLE_COPY(cmConnection)
  54. public:
  55. cmConnection() {}
  56. virtual void WriteData(const std::string& data) = 0;
  57. virtual ~cmConnection();
  58. virtual bool OnConnectionShuttingDown();
  59. virtual bool IsOpen() const = 0;
  60. virtual void SetServer(cmServerBase* s);
  61. virtual void ProcessRequest(const std::string& request);
  62. virtual bool OnServeStart(std::string* pString);
  63. protected:
  64. cmServerBase* Server = nullptr;
  65. };
  66. /***
  67. * Abstraction of a connection; ties in event callbacks from libuv and notifies
  68. * the server when appropriate
  69. */
  70. class cmEventBasedConnection : public cmConnection
  71. {
  72. public:
  73. /***
  74. * @param bufferStrategy If no strategy is given, it will process the raw
  75. * chunks as they come in. The connection
  76. * owns the pointer given.
  77. */
  78. cmEventBasedConnection(cmConnectionBufferStrategy* bufferStrategy = nullptr);
  79. virtual void Connect(uv_stream_t* server);
  80. virtual void ReadData(const std::string& data);
  81. bool IsOpen() const override;
  82. void WriteData(const std::string& data) override;
  83. bool OnConnectionShuttingDown() override;
  84. virtual void OnDisconnect(int errorCode);
  85. static void on_close(uv_handle_t* handle);
  86. template <typename T>
  87. static void on_close_delete(uv_handle_t* handle)
  88. {
  89. delete reinterpret_cast<T*>(handle);
  90. }
  91. protected:
  92. cm::uv_stream_ptr WriteStream;
  93. std::string RawReadBuffer;
  94. std::unique_ptr<cmConnectionBufferStrategy> BufferStrategy;
  95. static void on_read(uv_stream_t* stream, ssize_t nread, const uv_buf_t* buf);
  96. static void on_write(uv_write_t* req, int status);
  97. static void on_new_connection(uv_stream_t* stream, int status);
  98. static void on_alloc_buffer(uv_handle_t* handle, size_t suggested_size,
  99. uv_buf_t* buf);
  100. };