123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149 |
- #ifndef BOOST_MYSQL_IMPL_INTERNAL_SANSIO_TOP_LEVEL_ALGO_HPP
- #define BOOST_MYSQL_IMPL_INTERNAL_SANSIO_TOP_LEVEL_ALGO_HPP
- #include <boost/mysql/client_errc.hpp>
- #include <boost/mysql/error_code.hpp>
- #include <boost/mysql/detail/next_action.hpp>
- #include <boost/mysql/impl/internal/coroutine.hpp>
- #include <boost/mysql/impl/internal/sansio/connection_state_data.hpp>
- #include <boost/core/span.hpp>
- #include <cstddef>
- #include <cstdint>
- #ifdef BOOST_USE_VALGRIND
- #include <valgrind/memcheck.h>
- #endif
- namespace boost {
- namespace mysql {
- namespace detail {
- #ifdef BOOST_USE_VALGRIND
- inline void valgrind_make_mem_defined(const void* data, std::size_t size)
- {
- VALGRIND_MAKE_MEM_DEFINED(data, size);
- }
- #else
- inline void valgrind_make_mem_defined(const void*, std::size_t) noexcept {}
- #endif
- template <class InnerAlgo>
- class top_level_algo
- {
- int resume_point_{0};
- connection_state_data* st_;
- InnerAlgo algo_;
- span<const std::uint8_t> bytes_to_write_;
- public:
- template <class... Args>
- top_level_algo(connection_state_data& st, Args&&... args) : st_(&st), algo_(std::forward<Args>(args)...)
- {
- }
- const InnerAlgo& inner_algo() const { return algo_; }
- next_action resume(error_code ec, std::size_t bytes_transferred)
- {
- next_action act;
- switch (resume_point_)
- {
- case 0:
-
- while (true)
- {
-
- act = algo_.resume(*st_, ec);
-
- if (act.is_done())
- {
- return act;
- }
- else if (act.type() == next_action_type::read)
- {
-
-
- while (!st_->reader.done() && !ec)
- {
- ec = st_->reader.prepare_buffer();
- if (ec)
- break;
- BOOST_MYSQL_YIELD(
- resume_point_,
- 1,
- next_action::read({st_->reader.buffer(), st_->ssl_active()})
- )
- valgrind_make_mem_defined(st_->reader.buffer().data(), bytes_transferred);
- st_->reader.resume(bytes_transferred);
- }
-
- if (!ec)
- ec = st_->reader.error();
-
- }
- else if (act.type() == next_action_type::write)
- {
-
- bytes_to_write_ = act.write_args().buffer;
-
-
-
-
-
- if (bytes_to_write_.size() > st_->max_buffer_size())
- {
- ec = client_errc::max_buffer_size_exceeded;
- continue;
- }
- while (!bytes_to_write_.empty() && !ec)
- {
- BOOST_MYSQL_YIELD(
- resume_point_,
- 2,
- next_action::write({bytes_to_write_, st_->ssl_active()})
- )
- bytes_to_write_ = bytes_to_write_.subspan(bytes_transferred);
- }
-
- }
- else
- {
-
- BOOST_MYSQL_YIELD(resume_point_, 3, act)
- }
- }
- }
- return next_action();
- }
- };
- }
- }
- }
- #endif
|