read_buffer.hpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. //
  2. // Copyright (c) 2019-2023 Ruben Perez Hidalgo (rubenperez038 at gmail dot com)
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. #ifndef BHO_MYSQL_IMPL_INTERNAL_CHANNEL_READ_BUFFER_HPP
  8. #define BHO_MYSQL_IMPL_INTERNAL_CHANNEL_READ_BUFFER_HPP
  9. #include <asio2/bho/assert.hpp>
  10. #include <asio2/bho/core/span.hpp>
  11. #include <cstddef>
  12. #include <cstdint>
  13. #include <cstring>
  14. #include <vector>
  15. namespace bho {
  16. namespace mysql {
  17. namespace detail {
  18. // Custom buffer type optimized for read operations performed in the MySQL protocol.
  19. // The buffer is a single, resizable chunk of memory with four areas:
  20. // - Reserved area: messages that have already been read but are kept alive,
  21. // either because we need them or because we haven't cleaned them yet.
  22. // - Current message area: delimits the message we are currently parsing.
  23. // - Pending bytes area: bytes we've read but haven't been parsed into a message yet.
  24. // - Free area: free space for more bytes to be read.
  25. class read_buffer
  26. {
  27. std::vector<std::uint8_t> buffer_;
  28. std::size_t current_message_offset_{0};
  29. std::size_t pending_offset_{0};
  30. std::size_t free_offset_{0};
  31. public:
  32. read_buffer(std::size_t size) : buffer_(size, std::uint8_t(0)) { buffer_.resize(buffer_.capacity()); }
  33. // Whole buffer accessors
  34. const std::uint8_t* first() const noexcept { return buffer_.data(); }
  35. std::size_t size() const noexcept { return buffer_.size(); }
  36. // Area accessors
  37. std::uint8_t* reserved_first() noexcept { return buffer_.data(); }
  38. const std::uint8_t* reserved_first() const noexcept { return buffer_.data(); }
  39. std::uint8_t* current_message_first() noexcept { return buffer_.data() + current_message_offset_; }
  40. const std::uint8_t* current_message_first() const noexcept
  41. {
  42. return buffer_.data() + current_message_offset_;
  43. }
  44. std::uint8_t* pending_first() noexcept { return buffer_.data() + pending_offset_; }
  45. const std::uint8_t* pending_first() const noexcept { return buffer_.data() + pending_offset_; }
  46. std::uint8_t* free_first() noexcept { return buffer_.data() + free_offset_; }
  47. const std::uint8_t* free_first() const noexcept { return buffer_.data() + free_offset_; }
  48. std::size_t reserved_size() const noexcept { return current_message_offset_; }
  49. std::size_t current_message_size() const noexcept { return pending_offset_ - current_message_offset_; }
  50. std::size_t pending_size() const noexcept { return free_offset_ - pending_offset_; }
  51. std::size_t free_size() const noexcept { return buffer_.size() - free_offset_; }
  52. span<const std::uint8_t> reserved_area() const noexcept { return {reserved_first(), reserved_size()}; }
  53. span<const std::uint8_t> current_message() const noexcept
  54. {
  55. return {current_message_first(), current_message_size()};
  56. }
  57. span<const std::uint8_t> pending_area() const noexcept { return {pending_first(), pending_size()}; }
  58. span<std::uint8_t> free_area() noexcept { return {free_first(), free_size()}; }
  59. // Moves n bytes from the free to the processing area (e.g. they've been read)
  60. void move_to_pending(std::size_t length) noexcept
  61. {
  62. BHO_ASSERT(length <= free_size());
  63. free_offset_ += length;
  64. }
  65. // Moves n bytes from the processing to the current message area
  66. void move_to_current_message(std::size_t length) noexcept
  67. {
  68. BHO_ASSERT(length <= pending_size());
  69. pending_offset_ += length;
  70. }
  71. // Removes the last length bytes from the current message area,
  72. // effectively memmove'ing all subsequent bytes backwards.
  73. // Used to remove intermediate headers. length must be > 0
  74. void remove_current_message_last(std::size_t length) noexcept
  75. {
  76. BHO_ASSERT(length <= current_message_size());
  77. BHO_ASSERT(length > 0);
  78. std::memmove(pending_first() - length, pending_first(), pending_size());
  79. pending_offset_ -= length;
  80. free_offset_ -= length;
  81. }
  82. // Moves length bytes from the current message area to the reserved area
  83. // Used to move entire parsed messages or message headers
  84. void move_to_reserved(std::size_t length) noexcept
  85. {
  86. BHO_ASSERT(length <= current_message_size());
  87. current_message_offset_ += length;
  88. }
  89. // Removes the reserved area, effectively memmove'ing evth backwards
  90. void remove_reserved() noexcept
  91. {
  92. if (reserved_size() > 0)
  93. {
  94. std::size_t currmsg_size = current_message_size();
  95. std::size_t pend_size = pending_size();
  96. std::memmove(buffer_.data(), current_message_first(), currmsg_size + pend_size);
  97. current_message_offset_ = 0;
  98. pending_offset_ = currmsg_size;
  99. free_offset_ = currmsg_size + pend_size;
  100. }
  101. }
  102. // Makes sure the free size is at least n bytes long; resizes the buffer if required
  103. void grow_to_fit(std::size_t n)
  104. {
  105. if (free_size() < n)
  106. {
  107. buffer_.resize(buffer_.size() + n - free_size());
  108. buffer_.resize(buffer_.capacity());
  109. }
  110. }
  111. };
  112. } // namespace detail
  113. } // namespace mysql
  114. } // namespace bho
  115. #endif /* INCLUDE_BHO_MYSQL_DETAIL_AUXILIAR_STATIC_STRING_HPP_ */