buffered_read_stream.hpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  1. //
  2. // impl/buffered_read_stream.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2021 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/associated_allocator.hpp>
  16. #include <boost/asio/associated_executor.hpp>
  17. #include <boost/asio/detail/handler_alloc_helpers.hpp>
  18. #include <boost/asio/detail/handler_cont_helpers.hpp>
  19. #include <boost/asio/detail/handler_invoke_helpers.hpp>
  20. #include <boost/asio/detail/handler_type_requirements.hpp>
  21. #include <boost/asio/detail/non_const_lvalue.hpp>
  22. #include <boost/asio/detail/type_traits.hpp>
  23. #include <boost/asio/detail/push_options.hpp>
  24. namespace boost {
  25. namespace asio {
  26. template <typename Stream>
  27. std::size_t buffered_read_stream<Stream>::fill()
  28. {
  29. detail::buffer_resize_guard<detail::buffered_stream_storage>
  30. resize_guard(storage_);
  31. std::size_t previous_size = storage_.size();
  32. storage_.resize(storage_.capacity());
  33. storage_.resize(previous_size + next_layer_.read_some(buffer(
  34. storage_.data() + previous_size,
  35. storage_.size() - previous_size)));
  36. resize_guard.commit();
  37. return storage_.size() - previous_size;
  38. }
  39. template <typename Stream>
  40. std::size_t buffered_read_stream<Stream>::fill(boost::system::error_code& ec)
  41. {
  42. detail::buffer_resize_guard<detail::buffered_stream_storage>
  43. resize_guard(storage_);
  44. std::size_t previous_size = storage_.size();
  45. storage_.resize(storage_.capacity());
  46. storage_.resize(previous_size + next_layer_.read_some(buffer(
  47. storage_.data() + previous_size,
  48. storage_.size() - previous_size),
  49. ec));
  50. resize_guard.commit();
  51. return storage_.size() - previous_size;
  52. }
  53. namespace detail
  54. {
  55. template <typename ReadHandler>
  56. class buffered_fill_handler
  57. {
  58. public:
  59. buffered_fill_handler(detail::buffered_stream_storage& storage,
  60. std::size_t previous_size, ReadHandler& handler)
  61. : storage_(storage),
  62. previous_size_(previous_size),
  63. handler_(BOOST_ASIO_MOVE_CAST(ReadHandler)(handler))
  64. {
  65. }
  66. #if defined(BOOST_ASIO_HAS_MOVE)
  67. buffered_fill_handler(const buffered_fill_handler& other)
  68. : storage_(other.storage_),
  69. previous_size_(other.previous_size_),
  70. handler_(other.handler_)
  71. {
  72. }
  73. buffered_fill_handler(buffered_fill_handler&& other)
  74. : storage_(other.storage_),
  75. previous_size_(other.previous_size_),
  76. handler_(BOOST_ASIO_MOVE_CAST(ReadHandler)(other.handler_))
  77. {
  78. }
  79. #endif // defined(BOOST_ASIO_HAS_MOVE)
  80. void operator()(const boost::system::error_code& ec,
  81. const std::size_t bytes_transferred)
  82. {
  83. storage_.resize(previous_size_ + bytes_transferred);
  84. handler_(ec, bytes_transferred);
  85. }
  86. //private:
  87. detail::buffered_stream_storage& storage_;
  88. std::size_t previous_size_;
  89. ReadHandler handler_;
  90. };
  91. template <typename ReadHandler>
  92. inline asio_handler_allocate_is_deprecated
  93. asio_handler_allocate(std::size_t size,
  94. buffered_fill_handler<ReadHandler>* this_handler)
  95. {
  96. #if defined(BOOST_ASIO_NO_DEPRECATED)
  97. boost_asio_handler_alloc_helpers::allocate(size, this_handler->handler_);
  98. return asio_handler_allocate_is_no_longer_used();
  99. #else // defined(BOOST_ASIO_NO_DEPRECATED)
  100. return boost_asio_handler_alloc_helpers::allocate(
  101. size, this_handler->handler_);
  102. #endif // defined(BOOST_ASIO_NO_DEPRECATED)
  103. }
  104. template <typename ReadHandler>
  105. inline asio_handler_deallocate_is_deprecated
  106. asio_handler_deallocate(void* pointer, std::size_t size,
  107. buffered_fill_handler<ReadHandler>* this_handler)
  108. {
  109. boost_asio_handler_alloc_helpers::deallocate(
  110. pointer, size, this_handler->handler_);
  111. #if defined(BOOST_ASIO_NO_DEPRECATED)
  112. return asio_handler_deallocate_is_no_longer_used();
  113. #endif // defined(BOOST_ASIO_NO_DEPRECATED)
  114. }
  115. template <typename ReadHandler>
  116. inline bool asio_handler_is_continuation(
  117. buffered_fill_handler<ReadHandler>* this_handler)
  118. {
  119. return boost_asio_handler_cont_helpers::is_continuation(
  120. this_handler->handler_);
  121. }
  122. template <typename Function, typename ReadHandler>
  123. inline asio_handler_invoke_is_deprecated
  124. asio_handler_invoke(Function& function,
  125. buffered_fill_handler<ReadHandler>* this_handler)
  126. {
  127. boost_asio_handler_invoke_helpers::invoke(
  128. function, this_handler->handler_);
  129. #if defined(BOOST_ASIO_NO_DEPRECATED)
  130. return asio_handler_invoke_is_no_longer_used();
  131. #endif // defined(BOOST_ASIO_NO_DEPRECATED)
  132. }
  133. template <typename Function, typename ReadHandler>
  134. inline asio_handler_invoke_is_deprecated
  135. asio_handler_invoke(const Function& function,
  136. buffered_fill_handler<ReadHandler>* this_handler)
  137. {
  138. boost_asio_handler_invoke_helpers::invoke(
  139. function, this_handler->handler_);
  140. #if defined(BOOST_ASIO_NO_DEPRECATED)
  141. return asio_handler_invoke_is_no_longer_used();
  142. #endif // defined(BOOST_ASIO_NO_DEPRECATED)
  143. }
  144. template <typename Stream>
  145. class initiate_async_buffered_fill
  146. {
  147. public:
  148. typedef typename remove_reference<
  149. Stream>::type::lowest_layer_type::executor_type executor_type;
  150. explicit initiate_async_buffered_fill(
  151. typename remove_reference<Stream>::type& next_layer)
  152. : next_layer_(next_layer)
  153. {
  154. }
  155. executor_type get_executor() const BOOST_ASIO_NOEXCEPT
  156. {
  157. return next_layer_.lowest_layer().get_executor();
  158. }
  159. template <typename ReadHandler>
  160. void operator()(BOOST_ASIO_MOVE_ARG(ReadHandler) handler,
  161. buffered_stream_storage* storage) const
  162. {
  163. // If you get an error on the following line it means that your handler
  164. // does not meet the documented type requirements for a ReadHandler.
  165. BOOST_ASIO_READ_HANDLER_CHECK(ReadHandler, handler) type_check;
  166. non_const_lvalue<ReadHandler> handler2(handler);
  167. std::size_t previous_size = storage->size();
  168. storage->resize(storage->capacity());
  169. next_layer_.async_read_some(
  170. buffer(
  171. storage->data() + previous_size,
  172. storage->size() - previous_size),
  173. buffered_fill_handler<typename decay<ReadHandler>::type>(
  174. *storage, previous_size, handler2.value));
  175. }
  176. private:
  177. typename remove_reference<Stream>::type& next_layer_;
  178. };
  179. } // namespace detail
  180. #if !defined(GENERATING_DOCUMENTATION)
  181. template <typename ReadHandler, typename Allocator>
  182. struct associated_allocator<
  183. detail::buffered_fill_handler<ReadHandler>, Allocator>
  184. {
  185. typedef typename associated_allocator<ReadHandler, Allocator>::type type;
  186. static type get(const detail::buffered_fill_handler<ReadHandler>& h,
  187. const Allocator& a = Allocator()) BOOST_ASIO_NOEXCEPT
  188. {
  189. return associated_allocator<ReadHandler, Allocator>::get(h.handler_, a);
  190. }
  191. };
  192. template <typename ReadHandler, typename Executor>
  193. struct associated_executor<
  194. detail::buffered_fill_handler<ReadHandler>, Executor>
  195. : detail::associated_executor_forwarding_base<ReadHandler, Executor>
  196. {
  197. typedef typename associated_executor<ReadHandler, Executor>::type type;
  198. static type get(const detail::buffered_fill_handler<ReadHandler>& h,
  199. const Executor& ex = Executor()) BOOST_ASIO_NOEXCEPT
  200. {
  201. return associated_executor<ReadHandler, Executor>::get(h.handler_, ex);
  202. }
  203. };
  204. #endif // !defined(GENERATING_DOCUMENTATION)
  205. template <typename Stream>
  206. template <
  207. BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code,
  208. std::size_t)) ReadHandler>
  209. BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(ReadHandler,
  210. void (boost::system::error_code, std::size_t))
  211. buffered_read_stream<Stream>::async_fill(
  212. BOOST_ASIO_MOVE_ARG(ReadHandler) handler)
  213. {
  214. return async_initiate<ReadHandler,
  215. void (boost::system::error_code, std::size_t)>(
  216. detail::initiate_async_buffered_fill<Stream>(next_layer_),
  217. handler, &storage_);
  218. }
  219. template <typename Stream>
  220. template <typename MutableBufferSequence>
  221. std::size_t buffered_read_stream<Stream>::read_some(
  222. const MutableBufferSequence& buffers)
  223. {
  224. using boost::asio::buffer_size;
  225. if (buffer_size(buffers) == 0)
  226. return 0;
  227. if (storage_.empty())
  228. this->fill();
  229. return this->copy(buffers);
  230. }
  231. template <typename Stream>
  232. template <typename MutableBufferSequence>
  233. std::size_t buffered_read_stream<Stream>::read_some(
  234. const MutableBufferSequence& buffers, boost::system::error_code& ec)
  235. {
  236. ec = boost::system::error_code();
  237. using boost::asio::buffer_size;
  238. if (buffer_size(buffers) == 0)
  239. return 0;
  240. if (storage_.empty() && !this->fill(ec))
  241. return 0;
  242. return this->copy(buffers);
  243. }
  244. namespace detail
  245. {
  246. template <typename MutableBufferSequence, typename ReadHandler>
  247. class buffered_read_some_handler
  248. {
  249. public:
  250. buffered_read_some_handler(detail::buffered_stream_storage& storage,
  251. const MutableBufferSequence& buffers, ReadHandler& handler)
  252. : storage_(storage),
  253. buffers_(buffers),
  254. handler_(BOOST_ASIO_MOVE_CAST(ReadHandler)(handler))
  255. {
  256. }
  257. #if defined(BOOST_ASIO_HAS_MOVE)
  258. buffered_read_some_handler(const buffered_read_some_handler& other)
  259. : storage_(other.storage_),
  260. buffers_(other.buffers_),
  261. handler_(other.handler_)
  262. {
  263. }
  264. buffered_read_some_handler(buffered_read_some_handler&& other)
  265. : storage_(other.storage_),
  266. buffers_(other.buffers_),
  267. handler_(BOOST_ASIO_MOVE_CAST(ReadHandler)(other.handler_))
  268. {
  269. }
  270. #endif // defined(BOOST_ASIO_HAS_MOVE)
  271. void operator()(const boost::system::error_code& ec, std::size_t)
  272. {
  273. if (ec || storage_.empty())
  274. {
  275. const std::size_t length = 0;
  276. handler_(ec, length);
  277. }
  278. else
  279. {
  280. const std::size_t bytes_copied = boost::asio::buffer_copy(
  281. buffers_, storage_.data(), storage_.size());
  282. storage_.consume(bytes_copied);
  283. handler_(ec, bytes_copied);
  284. }
  285. }
  286. //private:
  287. detail::buffered_stream_storage& storage_;
  288. MutableBufferSequence buffers_;
  289. ReadHandler handler_;
  290. };
  291. template <typename MutableBufferSequence, typename ReadHandler>
  292. inline asio_handler_allocate_is_deprecated
  293. asio_handler_allocate(std::size_t size,
  294. buffered_read_some_handler<
  295. MutableBufferSequence, ReadHandler>* this_handler)
  296. {
  297. #if defined(BOOST_ASIO_NO_DEPRECATED)
  298. boost_asio_handler_alloc_helpers::allocate(size, this_handler->handler_);
  299. return asio_handler_allocate_is_no_longer_used();
  300. #else // defined(BOOST_ASIO_NO_DEPRECATED)
  301. return boost_asio_handler_alloc_helpers::allocate(
  302. size, this_handler->handler_);
  303. #endif // defined(BOOST_ASIO_NO_DEPRECATED)
  304. }
  305. template <typename MutableBufferSequence, typename ReadHandler>
  306. inline asio_handler_deallocate_is_deprecated
  307. asio_handler_deallocate(void* pointer, std::size_t size,
  308. buffered_read_some_handler<
  309. MutableBufferSequence, ReadHandler>* this_handler)
  310. {
  311. boost_asio_handler_alloc_helpers::deallocate(
  312. pointer, size, this_handler->handler_);
  313. #if defined(BOOST_ASIO_NO_DEPRECATED)
  314. return asio_handler_deallocate_is_no_longer_used();
  315. #endif // defined(BOOST_ASIO_NO_DEPRECATED)
  316. }
  317. template <typename MutableBufferSequence, typename ReadHandler>
  318. inline bool asio_handler_is_continuation(
  319. buffered_read_some_handler<
  320. MutableBufferSequence, ReadHandler>* this_handler)
  321. {
  322. return boost_asio_handler_cont_helpers::is_continuation(
  323. this_handler->handler_);
  324. }
  325. template <typename Function, typename MutableBufferSequence,
  326. typename ReadHandler>
  327. inline asio_handler_invoke_is_deprecated
  328. asio_handler_invoke(Function& function,
  329. buffered_read_some_handler<
  330. MutableBufferSequence, ReadHandler>* this_handler)
  331. {
  332. boost_asio_handler_invoke_helpers::invoke(
  333. function, this_handler->handler_);
  334. #if defined(BOOST_ASIO_NO_DEPRECATED)
  335. return asio_handler_invoke_is_no_longer_used();
  336. #endif // defined(BOOST_ASIO_NO_DEPRECATED)
  337. }
  338. template <typename Function, typename MutableBufferSequence,
  339. typename ReadHandler>
  340. inline asio_handler_invoke_is_deprecated
  341. asio_handler_invoke(const Function& function,
  342. buffered_read_some_handler<
  343. MutableBufferSequence, ReadHandler>* this_handler)
  344. {
  345. boost_asio_handler_invoke_helpers::invoke(
  346. function, this_handler->handler_);
  347. #if defined(BOOST_ASIO_NO_DEPRECATED)
  348. return asio_handler_invoke_is_no_longer_used();
  349. #endif // defined(BOOST_ASIO_NO_DEPRECATED)
  350. }
  351. template <typename Stream>
  352. class initiate_async_buffered_read_some
  353. {
  354. public:
  355. typedef typename remove_reference<
  356. Stream>::type::lowest_layer_type::executor_type executor_type;
  357. explicit initiate_async_buffered_read_some(
  358. typename remove_reference<Stream>::type& next_layer)
  359. : next_layer_(next_layer)
  360. {
  361. }
  362. executor_type get_executor() const BOOST_ASIO_NOEXCEPT
  363. {
  364. return next_layer_.lowest_layer().get_executor();
  365. }
  366. template <typename ReadHandler, typename MutableBufferSequence>
  367. void operator()(BOOST_ASIO_MOVE_ARG(ReadHandler) handler,
  368. buffered_stream_storage* storage,
  369. const MutableBufferSequence& buffers) const
  370. {
  371. // If you get an error on the following line it means that your handler
  372. // does not meet the documented type requirements for a ReadHandler.
  373. BOOST_ASIO_READ_HANDLER_CHECK(ReadHandler, handler) type_check;
  374. using boost::asio::buffer_size;
  375. non_const_lvalue<ReadHandler> handler2(handler);
  376. if (buffer_size(buffers) == 0 || !storage->empty())
  377. {
  378. next_layer_.async_read_some(BOOST_ASIO_MUTABLE_BUFFER(0, 0),
  379. buffered_read_some_handler<MutableBufferSequence,
  380. typename decay<ReadHandler>::type>(
  381. *storage, buffers, handler2.value));
  382. }
  383. else
  384. {
  385. initiate_async_buffered_fill<Stream>(this->next_layer_)(
  386. buffered_read_some_handler<MutableBufferSequence,
  387. typename decay<ReadHandler>::type>(
  388. *storage, buffers, handler2.value),
  389. storage);
  390. }
  391. }
  392. private:
  393. typename remove_reference<Stream>::type& next_layer_;
  394. };
  395. } // namespace detail
  396. #if !defined(GENERATING_DOCUMENTATION)
  397. template <typename MutableBufferSequence,
  398. typename ReadHandler, typename Allocator>
  399. struct associated_allocator<
  400. detail::buffered_read_some_handler<MutableBufferSequence, ReadHandler>,
  401. Allocator>
  402. {
  403. typedef typename associated_allocator<ReadHandler, Allocator>::type type;
  404. static type get(
  405. const detail::buffered_read_some_handler<
  406. MutableBufferSequence, ReadHandler>& h,
  407. const Allocator& a = Allocator()) BOOST_ASIO_NOEXCEPT
  408. {
  409. return associated_allocator<ReadHandler, Allocator>::get(h.handler_, a);
  410. }
  411. };
  412. template <typename MutableBufferSequence,
  413. typename ReadHandler, typename Executor>
  414. struct associated_executor<
  415. detail::buffered_read_some_handler<MutableBufferSequence, ReadHandler>,
  416. Executor>
  417. : detail::associated_executor_forwarding_base<ReadHandler, Executor>
  418. {
  419. typedef typename associated_executor<ReadHandler, Executor>::type type;
  420. static type get(
  421. const detail::buffered_read_some_handler<
  422. MutableBufferSequence, ReadHandler>& h,
  423. const Executor& ex = Executor()) BOOST_ASIO_NOEXCEPT
  424. {
  425. return associated_executor<ReadHandler, Executor>::get(h.handler_, ex);
  426. }
  427. };
  428. #endif // !defined(GENERATING_DOCUMENTATION)
  429. template <typename Stream>
  430. template <typename MutableBufferSequence,
  431. BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code,
  432. std::size_t)) ReadHandler>
  433. BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(ReadHandler,
  434. void (boost::system::error_code, std::size_t))
  435. buffered_read_stream<Stream>::async_read_some(
  436. const MutableBufferSequence& buffers,
  437. BOOST_ASIO_MOVE_ARG(ReadHandler) handler)
  438. {
  439. return async_initiate<ReadHandler,
  440. void (boost::system::error_code, std::size_t)>(
  441. detail::initiate_async_buffered_read_some<Stream>(next_layer_),
  442. handler, &storage_, buffers);
  443. }
  444. template <typename Stream>
  445. template <typename MutableBufferSequence>
  446. std::size_t buffered_read_stream<Stream>::peek(
  447. const MutableBufferSequence& buffers)
  448. {
  449. if (storage_.empty())
  450. this->fill();
  451. return this->peek_copy(buffers);
  452. }
  453. template <typename Stream>
  454. template <typename MutableBufferSequence>
  455. std::size_t buffered_read_stream<Stream>::peek(
  456. const MutableBufferSequence& buffers, boost::system::error_code& ec)
  457. {
  458. ec = boost::system::error_code();
  459. if (storage_.empty() && !this->fill(ec))
  460. return 0;
  461. return this->peek_copy(buffers);
  462. }
  463. } // namespace asio
  464. } // namespace boost
  465. #include <boost/asio/detail/pop_options.hpp>
  466. #endif // BOOST_ASIO_IMPL_BUFFERED_READ_STREAM_HPP