buffered_read_stream.hpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. //
  2. // impl/buffered_read_stream.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2024 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  6. //
  7. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  8. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. //
  10. #ifndef BOOST_ASIO_IMPL_BUFFERED_READ_STREAM_HPP
  11. #define BOOST_ASIO_IMPL_BUFFERED_READ_STREAM_HPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include <boost/asio/associator.hpp>
  16. #include <boost/asio/detail/handler_cont_helpers.hpp>
  17. #include <boost/asio/detail/handler_type_requirements.hpp>
  18. #include <boost/asio/detail/non_const_lvalue.hpp>
  19. #include <boost/asio/detail/type_traits.hpp>
  20. #include <boost/asio/detail/push_options.hpp>
  21. namespace boost {
  22. namespace asio {
  23. template <typename Stream>
  24. std::size_t buffered_read_stream<Stream>::fill()
  25. {
  26. detail::buffer_resize_guard<detail::buffered_stream_storage>
  27. resize_guard(storage_);
  28. std::size_t previous_size = storage_.size();
  29. storage_.resize(storage_.capacity());
  30. storage_.resize(previous_size + next_layer_.read_some(buffer(
  31. storage_.data() + previous_size,
  32. storage_.size() - previous_size)));
  33. resize_guard.commit();
  34. return storage_.size() - previous_size;
  35. }
  36. template <typename Stream>
  37. std::size_t buffered_read_stream<Stream>::fill(boost::system::error_code& ec)
  38. {
  39. detail::buffer_resize_guard<detail::buffered_stream_storage>
  40. resize_guard(storage_);
  41. std::size_t previous_size = storage_.size();
  42. storage_.resize(storage_.capacity());
  43. storage_.resize(previous_size + next_layer_.read_some(buffer(
  44. storage_.data() + previous_size,
  45. storage_.size() - previous_size),
  46. ec));
  47. resize_guard.commit();
  48. return storage_.size() - previous_size;
  49. }
  50. namespace detail
  51. {
  52. template <typename ReadHandler>
  53. class buffered_fill_handler
  54. {
  55. public:
  56. buffered_fill_handler(detail::buffered_stream_storage& storage,
  57. std::size_t previous_size, ReadHandler& handler)
  58. : storage_(storage),
  59. previous_size_(previous_size),
  60. handler_(static_cast<ReadHandler&&>(handler))
  61. {
  62. }
  63. buffered_fill_handler(const buffered_fill_handler& other)
  64. : storage_(other.storage_),
  65. previous_size_(other.previous_size_),
  66. handler_(other.handler_)
  67. {
  68. }
  69. buffered_fill_handler(buffered_fill_handler&& other)
  70. : storage_(other.storage_),
  71. previous_size_(other.previous_size_),
  72. handler_(static_cast<ReadHandler&&>(other.handler_))
  73. {
  74. }
  75. void operator()(const boost::system::error_code& ec,
  76. const std::size_t bytes_transferred)
  77. {
  78. storage_.resize(previous_size_ + bytes_transferred);
  79. static_cast<ReadHandler&&>(handler_)(ec, bytes_transferred);
  80. }
  81. //private:
  82. detail::buffered_stream_storage& storage_;
  83. std::size_t previous_size_;
  84. ReadHandler handler_;
  85. };
  86. template <typename ReadHandler>
  87. inline bool asio_handler_is_continuation(
  88. buffered_fill_handler<ReadHandler>* this_handler)
  89. {
  90. return boost_asio_handler_cont_helpers::is_continuation(
  91. this_handler->handler_);
  92. }
  93. template <typename Stream>
  94. class initiate_async_buffered_fill
  95. {
  96. public:
  97. typedef typename remove_reference_t<
  98. Stream>::lowest_layer_type::executor_type executor_type;
  99. explicit initiate_async_buffered_fill(
  100. remove_reference_t<Stream>& next_layer)
  101. : next_layer_(next_layer)
  102. {
  103. }
  104. executor_type get_executor() const noexcept
  105. {
  106. return next_layer_.lowest_layer().get_executor();
  107. }
  108. template <typename ReadHandler>
  109. void operator()(ReadHandler&& handler,
  110. buffered_stream_storage* storage) const
  111. {
  112. // If you get an error on the following line it means that your handler
  113. // does not meet the documented type requirements for a ReadHandler.
  114. BOOST_ASIO_READ_HANDLER_CHECK(ReadHandler, handler) type_check;
  115. non_const_lvalue<ReadHandler> handler2(handler);
  116. std::size_t previous_size = storage->size();
  117. storage->resize(storage->capacity());
  118. next_layer_.async_read_some(
  119. buffer(
  120. storage->data() + previous_size,
  121. storage->size() - previous_size),
  122. buffered_fill_handler<decay_t<ReadHandler>>(
  123. *storage, previous_size, handler2.value));
  124. }
  125. private:
  126. remove_reference_t<Stream>& next_layer_;
  127. };
  128. } // namespace detail
  129. #if !defined(GENERATING_DOCUMENTATION)
  130. template <template <typename, typename> class Associator,
  131. typename ReadHandler, typename DefaultCandidate>
  132. struct associator<Associator,
  133. detail::buffered_fill_handler<ReadHandler>,
  134. DefaultCandidate>
  135. : Associator<ReadHandler, DefaultCandidate>
  136. {
  137. static typename Associator<ReadHandler, DefaultCandidate>::type get(
  138. const detail::buffered_fill_handler<ReadHandler>& h) noexcept
  139. {
  140. return Associator<ReadHandler, DefaultCandidate>::get(h.handler_);
  141. }
  142. static auto get(const detail::buffered_fill_handler<ReadHandler>& h,
  143. const DefaultCandidate& c) noexcept
  144. -> decltype(Associator<ReadHandler, DefaultCandidate>::get(h.handler_, c))
  145. {
  146. return Associator<ReadHandler, DefaultCandidate>::get(h.handler_, c);
  147. }
  148. };
  149. #endif // !defined(GENERATING_DOCUMENTATION)
  150. template <typename Stream>
  151. template <
  152. BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code,
  153. std::size_t)) ReadHandler>
  154. inline auto buffered_read_stream<Stream>::async_fill(ReadHandler&& handler)
  155. -> decltype(
  156. async_initiate<ReadHandler,
  157. void (boost::system::error_code, std::size_t)>(
  158. declval<detail::initiate_async_buffered_fill<Stream>>(),
  159. handler, declval<detail::buffered_stream_storage*>()))
  160. {
  161. return async_initiate<ReadHandler,
  162. void (boost::system::error_code, std::size_t)>(
  163. detail::initiate_async_buffered_fill<Stream>(next_layer_),
  164. handler, &storage_);
  165. }
  166. template <typename Stream>
  167. template <typename MutableBufferSequence>
  168. std::size_t buffered_read_stream<Stream>::read_some(
  169. const MutableBufferSequence& buffers)
  170. {
  171. using boost::asio::buffer_size;
  172. if (buffer_size(buffers) == 0)
  173. return 0;
  174. if (storage_.empty())
  175. this->fill();
  176. return this->copy(buffers);
  177. }
  178. template <typename Stream>
  179. template <typename MutableBufferSequence>
  180. std::size_t buffered_read_stream<Stream>::read_some(
  181. const MutableBufferSequence& buffers, boost::system::error_code& ec)
  182. {
  183. ec = boost::system::error_code();
  184. using boost::asio::buffer_size;
  185. if (buffer_size(buffers) == 0)
  186. return 0;
  187. if (storage_.empty() && !this->fill(ec))
  188. return 0;
  189. return this->copy(buffers);
  190. }
  191. namespace detail
  192. {
  193. template <typename MutableBufferSequence, typename ReadHandler>
  194. class buffered_read_some_handler
  195. {
  196. public:
  197. buffered_read_some_handler(detail::buffered_stream_storage& storage,
  198. const MutableBufferSequence& buffers, ReadHandler& handler)
  199. : storage_(storage),
  200. buffers_(buffers),
  201. handler_(static_cast<ReadHandler&&>(handler))
  202. {
  203. }
  204. buffered_read_some_handler(const buffered_read_some_handler& other)
  205. : storage_(other.storage_),
  206. buffers_(other.buffers_),
  207. handler_(other.handler_)
  208. {
  209. }
  210. buffered_read_some_handler(buffered_read_some_handler&& other)
  211. : storage_(other.storage_),
  212. buffers_(other.buffers_),
  213. handler_(static_cast<ReadHandler&&>(other.handler_))
  214. {
  215. }
  216. void operator()(const boost::system::error_code& ec, std::size_t)
  217. {
  218. if (ec || storage_.empty())
  219. {
  220. const std::size_t length = 0;
  221. static_cast<ReadHandler&&>(handler_)(ec, length);
  222. }
  223. else
  224. {
  225. const std::size_t bytes_copied = boost::asio::buffer_copy(
  226. buffers_, storage_.data(), storage_.size());
  227. storage_.consume(bytes_copied);
  228. static_cast<ReadHandler&&>(handler_)(ec, bytes_copied);
  229. }
  230. }
  231. //private:
  232. detail::buffered_stream_storage& storage_;
  233. MutableBufferSequence buffers_;
  234. ReadHandler handler_;
  235. };
  236. template <typename MutableBufferSequence, typename ReadHandler>
  237. inline bool asio_handler_is_continuation(
  238. buffered_read_some_handler<
  239. MutableBufferSequence, ReadHandler>* this_handler)
  240. {
  241. return boost_asio_handler_cont_helpers::is_continuation(
  242. this_handler->handler_);
  243. }
  244. template <typename Stream>
  245. class initiate_async_buffered_read_some
  246. {
  247. public:
  248. typedef typename remove_reference_t<
  249. Stream>::lowest_layer_type::executor_type executor_type;
  250. explicit initiate_async_buffered_read_some(
  251. remove_reference_t<Stream>& next_layer)
  252. : next_layer_(next_layer)
  253. {
  254. }
  255. executor_type get_executor() const noexcept
  256. {
  257. return next_layer_.lowest_layer().get_executor();
  258. }
  259. template <typename ReadHandler, typename MutableBufferSequence>
  260. void operator()(ReadHandler&& handler,
  261. buffered_stream_storage* storage,
  262. const MutableBufferSequence& buffers) const
  263. {
  264. // If you get an error on the following line it means that your handler
  265. // does not meet the documented type requirements for a ReadHandler.
  266. BOOST_ASIO_READ_HANDLER_CHECK(ReadHandler, handler) type_check;
  267. using boost::asio::buffer_size;
  268. non_const_lvalue<ReadHandler> handler2(handler);
  269. if (buffer_size(buffers) == 0 || !storage->empty())
  270. {
  271. next_layer_.async_read_some(BOOST_ASIO_MUTABLE_BUFFER(0, 0),
  272. buffered_read_some_handler<MutableBufferSequence,
  273. decay_t<ReadHandler>>(
  274. *storage, buffers, handler2.value));
  275. }
  276. else
  277. {
  278. initiate_async_buffered_fill<Stream>(this->next_layer_)(
  279. buffered_read_some_handler<MutableBufferSequence,
  280. decay_t<ReadHandler>>(
  281. *storage, buffers, handler2.value),
  282. storage);
  283. }
  284. }
  285. private:
  286. remove_reference_t<Stream>& next_layer_;
  287. };
  288. } // namespace detail
  289. #if !defined(GENERATING_DOCUMENTATION)
  290. template <template <typename, typename> class Associator,
  291. typename MutableBufferSequence, typename ReadHandler,
  292. typename DefaultCandidate>
  293. struct associator<Associator,
  294. detail::buffered_read_some_handler<MutableBufferSequence, ReadHandler>,
  295. DefaultCandidate>
  296. : Associator<ReadHandler, DefaultCandidate>
  297. {
  298. static typename Associator<ReadHandler, DefaultCandidate>::type get(
  299. const detail::buffered_read_some_handler<
  300. MutableBufferSequence, ReadHandler>& h) noexcept
  301. {
  302. return Associator<ReadHandler, DefaultCandidate>::get(h.handler_);
  303. }
  304. static auto get(
  305. const detail::buffered_read_some_handler<
  306. MutableBufferSequence, ReadHandler>& h,
  307. const DefaultCandidate& c) noexcept
  308. -> decltype(Associator<ReadHandler, DefaultCandidate>::get(h.handler_, c))
  309. {
  310. return Associator<ReadHandler, DefaultCandidate>::get(h.handler_, c);
  311. }
  312. };
  313. #endif // !defined(GENERATING_DOCUMENTATION)
  314. template <typename Stream>
  315. template <typename MutableBufferSequence,
  316. BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code,
  317. std::size_t)) ReadHandler>
  318. inline auto buffered_read_stream<Stream>::async_read_some(
  319. const MutableBufferSequence& buffers, ReadHandler&& handler)
  320. -> decltype(
  321. async_initiate<ReadHandler,
  322. void (boost::system::error_code, std::size_t)>(
  323. declval<detail::initiate_async_buffered_read_some<Stream>>(),
  324. handler, declval<detail::buffered_stream_storage*>(), buffers))
  325. {
  326. return async_initiate<ReadHandler,
  327. void (boost::system::error_code, std::size_t)>(
  328. detail::initiate_async_buffered_read_some<Stream>(next_layer_),
  329. handler, &storage_, buffers);
  330. }
  331. template <typename Stream>
  332. template <typename MutableBufferSequence>
  333. std::size_t buffered_read_stream<Stream>::peek(
  334. const MutableBufferSequence& buffers)
  335. {
  336. if (storage_.empty())
  337. this->fill();
  338. return this->peek_copy(buffers);
  339. }
  340. template <typename Stream>
  341. template <typename MutableBufferSequence>
  342. std::size_t buffered_read_stream<Stream>::peek(
  343. const MutableBufferSequence& buffers, boost::system::error_code& ec)
  344. {
  345. ec = boost::system::error_code();
  346. if (storage_.empty() && !this->fill(ec))
  347. return 0;
  348. return this->peek_copy(buffers);
  349. }
  350. } // namespace asio
  351. } // namespace boost
  352. #include <boost/asio/detail/pop_options.hpp>
  353. #endif // BOOST_ASIO_IMPL_BUFFERED_READ_STREAM_HPP