top_level_algo.hpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. //
  2. // Copyright (c) 2019-2024 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 BOOST_MYSQL_IMPL_INTERNAL_SANSIO_TOP_LEVEL_ALGO_HPP
  8. #define BOOST_MYSQL_IMPL_INTERNAL_SANSIO_TOP_LEVEL_ALGO_HPP
  9. #include <boost/mysql/client_errc.hpp>
  10. #include <boost/mysql/error_code.hpp>
  11. #include <boost/mysql/detail/next_action.hpp>
  12. #include <boost/mysql/impl/internal/coroutine.hpp>
  13. #include <boost/mysql/impl/internal/sansio/connection_state_data.hpp>
  14. #include <boost/core/span.hpp>
  15. #include <cstddef>
  16. #include <cstdint>
  17. #ifdef BOOST_USE_VALGRIND
  18. #include <valgrind/memcheck.h>
  19. #endif
  20. namespace boost {
  21. namespace mysql {
  22. namespace detail {
  23. // Valgrind
  24. #ifdef BOOST_USE_VALGRIND
  25. inline void valgrind_make_mem_defined(const void* data, std::size_t size)
  26. {
  27. VALGRIND_MAKE_MEM_DEFINED(data, size);
  28. }
  29. #else
  30. inline void valgrind_make_mem_defined(const void*, std::size_t) noexcept {}
  31. #endif
  32. // InnerAlgo should have
  33. // Constructible from the args forwarded by the ctor.
  34. // next_action resume(connection_state_data&, error_code);
  35. // AlgoParams::result_type result(const connection_state_data&) const; // if AlgoParams::result_type != void
  36. template <class InnerAlgo>
  37. class top_level_algo
  38. {
  39. int resume_point_{0};
  40. connection_state_data* st_;
  41. InnerAlgo algo_;
  42. span<const std::uint8_t> bytes_to_write_;
  43. public:
  44. template <class... Args>
  45. top_level_algo(connection_state_data& st, Args&&... args) : st_(&st), algo_(std::forward<Args>(args)...)
  46. {
  47. }
  48. const InnerAlgo& inner_algo() const { return algo_; }
  49. next_action resume(error_code ec, std::size_t bytes_transferred)
  50. {
  51. next_action act;
  52. switch (resume_point_)
  53. {
  54. case 0:
  55. // Run until completion
  56. while (true)
  57. {
  58. // Run the op
  59. act = algo_.resume(*st_, ec);
  60. // Check next action
  61. if (act.is_done())
  62. {
  63. return act;
  64. }
  65. else if (act.type() == next_action_type::read)
  66. {
  67. // Read until a complete message is received
  68. // (may be zero times if cached)
  69. while (!st_->reader.done() && !ec)
  70. {
  71. ec = st_->reader.prepare_buffer();
  72. if (ec)
  73. break;
  74. BOOST_MYSQL_YIELD(
  75. resume_point_,
  76. 1,
  77. next_action::read({st_->reader.buffer(), st_->ssl_active()})
  78. )
  79. valgrind_make_mem_defined(st_->reader.buffer().data(), bytes_transferred);
  80. st_->reader.resume(bytes_transferred);
  81. }
  82. // Check for errors
  83. if (!ec)
  84. ec = st_->reader.error();
  85. // We've got a message, continue
  86. }
  87. else if (act.type() == next_action_type::write)
  88. {
  89. // Write until a complete message was written
  90. bytes_to_write_ = act.write_args().buffer;
  91. // Check buffer size. We should check this before
  92. // resizing the buffer, but requires non-trivial changes.
  93. // For now, this yields the right user-facing behavior.
  94. // https://github.com/boostorg/mysql/issues/297
  95. // https://github.com/boostorg/mysql/issues/279
  96. if (bytes_to_write_.size() > st_->max_buffer_size())
  97. {
  98. ec = client_errc::max_buffer_size_exceeded;
  99. continue;
  100. }
  101. while (!bytes_to_write_.empty() && !ec)
  102. {
  103. BOOST_MYSQL_YIELD(
  104. resume_point_,
  105. 2,
  106. next_action::write({bytes_to_write_, st_->ssl_active()})
  107. )
  108. bytes_to_write_ = bytes_to_write_.subspan(bytes_transferred);
  109. }
  110. // We fully wrote a message, continue
  111. }
  112. else
  113. {
  114. // Other ops always require I/O
  115. BOOST_MYSQL_YIELD(resume_point_, 3, act)
  116. }
  117. }
  118. }
  119. return next_action();
  120. }
  121. };
  122. } // namespace detail
  123. } // namespace mysql
  124. } // namespace boost
  125. #endif