read_at.hpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701
  1. //
  2. // impl/read_at.hpp
  3. // ~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2019 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_READ_AT_HPP
  11. #define BOOST_ASIO_IMPL_READ_AT_HPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include <algorithm>
  16. #include <boost/asio/associated_allocator.hpp>
  17. #include <boost/asio/associated_executor.hpp>
  18. #include <boost/asio/buffer.hpp>
  19. #include <boost/asio/completion_condition.hpp>
  20. #include <boost/asio/detail/array_fwd.hpp>
  21. #include <boost/asio/detail/base_from_completion_cond.hpp>
  22. #include <boost/asio/detail/bind_handler.hpp>
  23. #include <boost/asio/detail/consuming_buffers.hpp>
  24. #include <boost/asio/detail/dependent_type.hpp>
  25. #include <boost/asio/detail/handler_alloc_helpers.hpp>
  26. #include <boost/asio/detail/handler_cont_helpers.hpp>
  27. #include <boost/asio/detail/handler_invoke_helpers.hpp>
  28. #include <boost/asio/detail/handler_type_requirements.hpp>
  29. #include <boost/asio/detail/non_const_lvalue.hpp>
  30. #include <boost/asio/detail/throw_error.hpp>
  31. #include <boost/asio/error.hpp>
  32. #include <boost/asio/detail/push_options.hpp>
  33. namespace boost {
  34. namespace asio {
  35. namespace detail
  36. {
  37. template <typename SyncRandomAccessReadDevice, typename MutableBufferSequence,
  38. typename MutableBufferIterator, typename CompletionCondition>
  39. std::size_t read_at_buffer_sequence(SyncRandomAccessReadDevice& d,
  40. uint64_t offset, const MutableBufferSequence& buffers,
  41. const MutableBufferIterator&, CompletionCondition completion_condition,
  42. boost::system::error_code& ec)
  43. {
  44. ec = boost::system::error_code();
  45. boost::asio::detail::consuming_buffers<mutable_buffer,
  46. MutableBufferSequence, MutableBufferIterator> tmp(buffers);
  47. while (!tmp.empty())
  48. {
  49. if (std::size_t max_size = detail::adapt_completion_condition_result(
  50. completion_condition(ec, tmp.total_consumed())))
  51. {
  52. tmp.consume(d.read_some_at(offset + tmp.total_consumed(),
  53. tmp.prepare(max_size), ec));
  54. }
  55. else
  56. break;
  57. }
  58. return tmp.total_consumed();;
  59. }
  60. } // namespace detail
  61. template <typename SyncRandomAccessReadDevice, typename MutableBufferSequence,
  62. typename CompletionCondition>
  63. std::size_t read_at(SyncRandomAccessReadDevice& d,
  64. uint64_t offset, const MutableBufferSequence& buffers,
  65. CompletionCondition completion_condition, boost::system::error_code& ec)
  66. {
  67. return detail::read_at_buffer_sequence(d, offset, buffers,
  68. boost::asio::buffer_sequence_begin(buffers),
  69. BOOST_ASIO_MOVE_CAST(CompletionCondition)(completion_condition), ec);
  70. }
  71. template <typename SyncRandomAccessReadDevice, typename MutableBufferSequence>
  72. inline std::size_t read_at(SyncRandomAccessReadDevice& d,
  73. uint64_t offset, const MutableBufferSequence& buffers)
  74. {
  75. boost::system::error_code ec;
  76. std::size_t bytes_transferred = read_at(
  77. d, offset, buffers, transfer_all(), ec);
  78. boost::asio::detail::throw_error(ec, "read_at");
  79. return bytes_transferred;
  80. }
  81. template <typename SyncRandomAccessReadDevice, typename MutableBufferSequence>
  82. inline std::size_t read_at(SyncRandomAccessReadDevice& d,
  83. uint64_t offset, const MutableBufferSequence& buffers,
  84. boost::system::error_code& ec)
  85. {
  86. return read_at(d, offset, buffers, transfer_all(), ec);
  87. }
  88. template <typename SyncRandomAccessReadDevice, typename MutableBufferSequence,
  89. typename CompletionCondition>
  90. inline std::size_t read_at(SyncRandomAccessReadDevice& d,
  91. uint64_t offset, const MutableBufferSequence& buffers,
  92. CompletionCondition completion_condition)
  93. {
  94. boost::system::error_code ec;
  95. std::size_t bytes_transferred = read_at(d, offset, buffers,
  96. BOOST_ASIO_MOVE_CAST(CompletionCondition)(completion_condition), ec);
  97. boost::asio::detail::throw_error(ec, "read_at");
  98. return bytes_transferred;
  99. }
  100. #if !defined(BOOST_ASIO_NO_EXTENSIONS)
  101. #if !defined(BOOST_ASIO_NO_IOSTREAM)
  102. template <typename SyncRandomAccessReadDevice, typename Allocator,
  103. typename CompletionCondition>
  104. std::size_t read_at(SyncRandomAccessReadDevice& d,
  105. uint64_t offset, boost::asio::basic_streambuf<Allocator>& b,
  106. CompletionCondition completion_condition, boost::system::error_code& ec)
  107. {
  108. ec = boost::system::error_code();
  109. std::size_t total_transferred = 0;
  110. std::size_t max_size = detail::adapt_completion_condition_result(
  111. completion_condition(ec, total_transferred));
  112. std::size_t bytes_available = read_size_helper(b, max_size);
  113. while (bytes_available > 0)
  114. {
  115. std::size_t bytes_transferred = d.read_some_at(
  116. offset + total_transferred, b.prepare(bytes_available), ec);
  117. b.commit(bytes_transferred);
  118. total_transferred += bytes_transferred;
  119. max_size = detail::adapt_completion_condition_result(
  120. completion_condition(ec, total_transferred));
  121. bytes_available = read_size_helper(b, max_size);
  122. }
  123. return total_transferred;
  124. }
  125. template <typename SyncRandomAccessReadDevice, typename Allocator>
  126. inline std::size_t read_at(SyncRandomAccessReadDevice& d,
  127. uint64_t offset, boost::asio::basic_streambuf<Allocator>& b)
  128. {
  129. boost::system::error_code ec;
  130. std::size_t bytes_transferred = read_at(
  131. d, offset, b, transfer_all(), ec);
  132. boost::asio::detail::throw_error(ec, "read_at");
  133. return bytes_transferred;
  134. }
  135. template <typename SyncRandomAccessReadDevice, typename Allocator>
  136. inline std::size_t read_at(SyncRandomAccessReadDevice& d,
  137. uint64_t offset, boost::asio::basic_streambuf<Allocator>& b,
  138. boost::system::error_code& ec)
  139. {
  140. return read_at(d, offset, b, transfer_all(), ec);
  141. }
  142. template <typename SyncRandomAccessReadDevice, typename Allocator,
  143. typename CompletionCondition>
  144. inline std::size_t read_at(SyncRandomAccessReadDevice& d,
  145. uint64_t offset, boost::asio::basic_streambuf<Allocator>& b,
  146. CompletionCondition completion_condition)
  147. {
  148. boost::system::error_code ec;
  149. std::size_t bytes_transferred = read_at(d, offset, b,
  150. BOOST_ASIO_MOVE_CAST(CompletionCondition)(completion_condition), ec);
  151. boost::asio::detail::throw_error(ec, "read_at");
  152. return bytes_transferred;
  153. }
  154. #endif // !defined(BOOST_ASIO_NO_IOSTREAM)
  155. #endif // !defined(BOOST_ASIO_NO_EXTENSIONS)
  156. namespace detail
  157. {
  158. template <typename AsyncRandomAccessReadDevice,
  159. typename MutableBufferSequence, typename MutableBufferIterator,
  160. typename CompletionCondition, typename ReadHandler>
  161. class read_at_op
  162. : detail::base_from_completion_cond<CompletionCondition>
  163. {
  164. public:
  165. read_at_op(AsyncRandomAccessReadDevice& device,
  166. uint64_t offset, const MutableBufferSequence& buffers,
  167. CompletionCondition& completion_condition, ReadHandler& handler)
  168. : detail::base_from_completion_cond<
  169. CompletionCondition>(completion_condition),
  170. device_(device),
  171. offset_(offset),
  172. buffers_(buffers),
  173. start_(0),
  174. handler_(BOOST_ASIO_MOVE_CAST(ReadHandler)(handler))
  175. {
  176. }
  177. #if defined(BOOST_ASIO_HAS_MOVE)
  178. read_at_op(const read_at_op& other)
  179. : detail::base_from_completion_cond<CompletionCondition>(other),
  180. device_(other.device_),
  181. offset_(other.offset_),
  182. buffers_(other.buffers_),
  183. start_(other.start_),
  184. handler_(other.handler_)
  185. {
  186. }
  187. read_at_op(read_at_op&& other)
  188. : detail::base_from_completion_cond<CompletionCondition>(
  189. BOOST_ASIO_MOVE_CAST(detail::base_from_completion_cond<
  190. CompletionCondition>)(other)),
  191. device_(other.device_),
  192. offset_(other.offset_),
  193. buffers_(BOOST_ASIO_MOVE_CAST(buffers_type)(other.buffers_)),
  194. start_(other.start_),
  195. handler_(BOOST_ASIO_MOVE_CAST(ReadHandler)(other.handler_))
  196. {
  197. }
  198. #endif // defined(BOOST_ASIO_HAS_MOVE)
  199. void operator()(const boost::system::error_code& ec,
  200. std::size_t bytes_transferred, int start = 0)
  201. {
  202. std::size_t max_size;
  203. switch (start_ = start)
  204. {
  205. case 1:
  206. max_size = this->check_for_completion(ec, buffers_.total_consumed());
  207. do
  208. {
  209. device_.async_read_some_at(
  210. offset_ + buffers_.total_consumed(), buffers_.prepare(max_size),
  211. BOOST_ASIO_MOVE_CAST(read_at_op)(*this));
  212. return; default:
  213. buffers_.consume(bytes_transferred);
  214. if ((!ec && bytes_transferred == 0) || buffers_.empty())
  215. break;
  216. max_size = this->check_for_completion(ec, buffers_.total_consumed());
  217. } while (max_size > 0);
  218. handler_(ec, buffers_.total_consumed());
  219. }
  220. }
  221. //private:
  222. typedef boost::asio::detail::consuming_buffers<mutable_buffer,
  223. MutableBufferSequence, MutableBufferIterator> buffers_type;
  224. AsyncRandomAccessReadDevice& device_;
  225. uint64_t offset_;
  226. buffers_type buffers_;
  227. int start_;
  228. ReadHandler handler_;
  229. };
  230. template <typename AsyncRandomAccessReadDevice,
  231. typename MutableBufferSequence, typename MutableBufferIterator,
  232. typename CompletionCondition, typename ReadHandler>
  233. inline void* asio_handler_allocate(std::size_t size,
  234. read_at_op<AsyncRandomAccessReadDevice, MutableBufferSequence,
  235. MutableBufferIterator, CompletionCondition, ReadHandler>* this_handler)
  236. {
  237. return boost_asio_handler_alloc_helpers::allocate(
  238. size, this_handler->handler_);
  239. }
  240. template <typename AsyncRandomAccessReadDevice,
  241. typename MutableBufferSequence, typename MutableBufferIterator,
  242. typename CompletionCondition, typename ReadHandler>
  243. inline void asio_handler_deallocate(void* pointer, std::size_t size,
  244. read_at_op<AsyncRandomAccessReadDevice, MutableBufferSequence,
  245. MutableBufferIterator, CompletionCondition, ReadHandler>* this_handler)
  246. {
  247. boost_asio_handler_alloc_helpers::deallocate(
  248. pointer, size, this_handler->handler_);
  249. }
  250. template <typename AsyncRandomAccessReadDevice,
  251. typename MutableBufferSequence, typename MutableBufferIterator,
  252. typename CompletionCondition, typename ReadHandler>
  253. inline bool asio_handler_is_continuation(
  254. read_at_op<AsyncRandomAccessReadDevice, MutableBufferSequence,
  255. MutableBufferIterator, CompletionCondition, ReadHandler>* this_handler)
  256. {
  257. return this_handler->start_ == 0 ? true
  258. : boost_asio_handler_cont_helpers::is_continuation(
  259. this_handler->handler_);
  260. }
  261. template <typename Function, typename AsyncRandomAccessReadDevice,
  262. typename MutableBufferSequence, typename MutableBufferIterator,
  263. typename CompletionCondition, typename ReadHandler>
  264. inline void asio_handler_invoke(Function& function,
  265. read_at_op<AsyncRandomAccessReadDevice, MutableBufferSequence,
  266. MutableBufferIterator, CompletionCondition, ReadHandler>* this_handler)
  267. {
  268. boost_asio_handler_invoke_helpers::invoke(
  269. function, this_handler->handler_);
  270. }
  271. template <typename Function, typename AsyncRandomAccessReadDevice,
  272. typename MutableBufferSequence, typename MutableBufferIterator,
  273. typename CompletionCondition, typename ReadHandler>
  274. inline void asio_handler_invoke(const Function& function,
  275. read_at_op<AsyncRandomAccessReadDevice, MutableBufferSequence,
  276. MutableBufferIterator, CompletionCondition, ReadHandler>* this_handler)
  277. {
  278. boost_asio_handler_invoke_helpers::invoke(
  279. function, this_handler->handler_);
  280. }
  281. template <typename AsyncRandomAccessReadDevice,
  282. typename MutableBufferSequence, typename MutableBufferIterator,
  283. typename CompletionCondition, typename ReadHandler>
  284. inline void start_read_at_buffer_sequence_op(AsyncRandomAccessReadDevice& d,
  285. uint64_t offset, const MutableBufferSequence& buffers,
  286. const MutableBufferIterator&, CompletionCondition& completion_condition,
  287. ReadHandler& handler)
  288. {
  289. detail::read_at_op<AsyncRandomAccessReadDevice, MutableBufferSequence,
  290. MutableBufferIterator, CompletionCondition, ReadHandler>(
  291. d, offset, buffers, completion_condition, handler)(
  292. boost::system::error_code(), 0, 1);
  293. }
  294. template <typename AsyncRandomAccessReadDevice>
  295. class initiate_async_read_at_buffer_sequence
  296. {
  297. public:
  298. typedef typename AsyncRandomAccessReadDevice::executor_type executor_type;
  299. explicit initiate_async_read_at_buffer_sequence(
  300. AsyncRandomAccessReadDevice& device)
  301. : device_(device)
  302. {
  303. }
  304. executor_type get_executor() const BOOST_ASIO_NOEXCEPT
  305. {
  306. return device_.get_executor();
  307. }
  308. template <typename ReadHandler, typename MutableBufferSequence,
  309. typename CompletionCondition>
  310. void operator()(BOOST_ASIO_MOVE_ARG(ReadHandler) handler,
  311. uint64_t offset, const MutableBufferSequence& buffers,
  312. BOOST_ASIO_MOVE_ARG(CompletionCondition) completion_cond) const
  313. {
  314. // If you get an error on the following line it means that your handler
  315. // does not meet the documented type requirements for a ReadHandler.
  316. BOOST_ASIO_READ_HANDLER_CHECK(ReadHandler, handler) type_check;
  317. non_const_lvalue<ReadHandler> handler2(handler);
  318. non_const_lvalue<CompletionCondition> completion_cond2(completion_cond);
  319. start_read_at_buffer_sequence_op(device_, offset, buffers,
  320. boost::asio::buffer_sequence_begin(buffers),
  321. completion_cond2.value, handler2.value);
  322. }
  323. private:
  324. AsyncRandomAccessReadDevice& device_;
  325. };
  326. } // namespace detail
  327. #if !defined(GENERATING_DOCUMENTATION)
  328. template <typename AsyncRandomAccessReadDevice,
  329. typename MutableBufferSequence, typename MutableBufferIterator,
  330. typename CompletionCondition, typename ReadHandler, typename Allocator>
  331. struct associated_allocator<
  332. detail::read_at_op<AsyncRandomAccessReadDevice, MutableBufferSequence,
  333. MutableBufferIterator, CompletionCondition, ReadHandler>,
  334. Allocator>
  335. {
  336. typedef typename associated_allocator<ReadHandler, Allocator>::type type;
  337. static type get(
  338. const detail::read_at_op<AsyncRandomAccessReadDevice,
  339. MutableBufferSequence, MutableBufferIterator,
  340. CompletionCondition, ReadHandler>& h,
  341. const Allocator& a = Allocator()) BOOST_ASIO_NOEXCEPT
  342. {
  343. return associated_allocator<ReadHandler, Allocator>::get(h.handler_, a);
  344. }
  345. };
  346. template <typename AsyncRandomAccessReadDevice,
  347. typename MutableBufferSequence, typename MutableBufferIterator,
  348. typename CompletionCondition, typename ReadHandler, typename Executor>
  349. struct associated_executor<
  350. detail::read_at_op<AsyncRandomAccessReadDevice, MutableBufferSequence,
  351. MutableBufferIterator, CompletionCondition, ReadHandler>,
  352. Executor>
  353. {
  354. typedef typename associated_executor<ReadHandler, Executor>::type type;
  355. static type get(
  356. const detail::read_at_op<AsyncRandomAccessReadDevice,
  357. MutableBufferSequence, MutableBufferIterator,
  358. CompletionCondition, ReadHandler>& h,
  359. const Executor& ex = Executor()) BOOST_ASIO_NOEXCEPT
  360. {
  361. return associated_executor<ReadHandler, Executor>::get(h.handler_, ex);
  362. }
  363. };
  364. #endif // !defined(GENERATING_DOCUMENTATION)
  365. template <typename AsyncRandomAccessReadDevice,
  366. typename MutableBufferSequence, typename CompletionCondition,
  367. BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code,
  368. std::size_t)) ReadHandler>
  369. inline BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(ReadHandler,
  370. void (boost::system::error_code, std::size_t))
  371. async_read_at(AsyncRandomAccessReadDevice& d,
  372. uint64_t offset, const MutableBufferSequence& buffers,
  373. CompletionCondition completion_condition,
  374. BOOST_ASIO_MOVE_ARG(ReadHandler) handler)
  375. {
  376. return async_initiate<ReadHandler,
  377. void (boost::system::error_code, std::size_t)>(
  378. detail::initiate_async_read_at_buffer_sequence<
  379. AsyncRandomAccessReadDevice>(d),
  380. handler, offset, buffers,
  381. BOOST_ASIO_MOVE_CAST(CompletionCondition)(completion_condition));
  382. }
  383. template <typename AsyncRandomAccessReadDevice, typename MutableBufferSequence,
  384. BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code,
  385. std::size_t)) ReadHandler>
  386. inline BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(ReadHandler,
  387. void (boost::system::error_code, std::size_t))
  388. async_read_at(AsyncRandomAccessReadDevice& d,
  389. uint64_t offset, const MutableBufferSequence& buffers,
  390. BOOST_ASIO_MOVE_ARG(ReadHandler) handler)
  391. {
  392. return async_initiate<ReadHandler,
  393. void (boost::system::error_code, std::size_t)>(
  394. detail::initiate_async_read_at_buffer_sequence<
  395. AsyncRandomAccessReadDevice>(d),
  396. handler, offset, buffers, transfer_all());
  397. }
  398. #if !defined(BOOST_ASIO_NO_EXTENSIONS)
  399. #if !defined(BOOST_ASIO_NO_IOSTREAM)
  400. namespace detail
  401. {
  402. template <typename AsyncRandomAccessReadDevice, typename Allocator,
  403. typename CompletionCondition, typename ReadHandler>
  404. class read_at_streambuf_op
  405. : detail::base_from_completion_cond<CompletionCondition>
  406. {
  407. public:
  408. read_at_streambuf_op(AsyncRandomAccessReadDevice& device,
  409. uint64_t offset, basic_streambuf<Allocator>& streambuf,
  410. CompletionCondition& completion_condition, ReadHandler& handler)
  411. : detail::base_from_completion_cond<
  412. CompletionCondition>(completion_condition),
  413. device_(device),
  414. offset_(offset),
  415. streambuf_(streambuf),
  416. start_(0),
  417. total_transferred_(0),
  418. handler_(BOOST_ASIO_MOVE_CAST(ReadHandler)(handler))
  419. {
  420. }
  421. #if defined(BOOST_ASIO_HAS_MOVE)
  422. read_at_streambuf_op(const read_at_streambuf_op& other)
  423. : detail::base_from_completion_cond<CompletionCondition>(other),
  424. device_(other.device_),
  425. offset_(other.offset_),
  426. streambuf_(other.streambuf_),
  427. start_(other.start_),
  428. total_transferred_(other.total_transferred_),
  429. handler_(other.handler_)
  430. {
  431. }
  432. read_at_streambuf_op(read_at_streambuf_op&& other)
  433. : detail::base_from_completion_cond<CompletionCondition>(
  434. BOOST_ASIO_MOVE_CAST(detail::base_from_completion_cond<
  435. CompletionCondition>)(other)),
  436. device_(other.device_),
  437. offset_(other.offset_),
  438. streambuf_(other.streambuf_),
  439. start_(other.start_),
  440. total_transferred_(other.total_transferred_),
  441. handler_(BOOST_ASIO_MOVE_CAST(ReadHandler)(other.handler_))
  442. {
  443. }
  444. #endif // defined(BOOST_ASIO_HAS_MOVE)
  445. void operator()(const boost::system::error_code& ec,
  446. std::size_t bytes_transferred, int start = 0)
  447. {
  448. std::size_t max_size, bytes_available;
  449. switch (start_ = start)
  450. {
  451. case 1:
  452. max_size = this->check_for_completion(ec, total_transferred_);
  453. bytes_available = read_size_helper(streambuf_, max_size);
  454. for (;;)
  455. {
  456. device_.async_read_some_at(offset_ + total_transferred_,
  457. streambuf_.prepare(bytes_available),
  458. BOOST_ASIO_MOVE_CAST(read_at_streambuf_op)(*this));
  459. return; default:
  460. total_transferred_ += bytes_transferred;
  461. streambuf_.commit(bytes_transferred);
  462. max_size = this->check_for_completion(ec, total_transferred_);
  463. bytes_available = read_size_helper(streambuf_, max_size);
  464. if ((!ec && bytes_transferred == 0) || bytes_available == 0)
  465. break;
  466. }
  467. handler_(ec, static_cast<const std::size_t&>(total_transferred_));
  468. }
  469. }
  470. //private:
  471. AsyncRandomAccessReadDevice& device_;
  472. uint64_t offset_;
  473. boost::asio::basic_streambuf<Allocator>& streambuf_;
  474. int start_;
  475. std::size_t total_transferred_;
  476. ReadHandler handler_;
  477. };
  478. template <typename AsyncRandomAccessReadDevice, typename Allocator,
  479. typename CompletionCondition, typename ReadHandler>
  480. inline void* asio_handler_allocate(std::size_t size,
  481. read_at_streambuf_op<AsyncRandomAccessReadDevice, Allocator,
  482. CompletionCondition, ReadHandler>* this_handler)
  483. {
  484. return boost_asio_handler_alloc_helpers::allocate(
  485. size, this_handler->handler_);
  486. }
  487. template <typename AsyncRandomAccessReadDevice, typename Allocator,
  488. typename CompletionCondition, typename ReadHandler>
  489. inline void asio_handler_deallocate(void* pointer, std::size_t size,
  490. read_at_streambuf_op<AsyncRandomAccessReadDevice, Allocator,
  491. CompletionCondition, ReadHandler>* this_handler)
  492. {
  493. boost_asio_handler_alloc_helpers::deallocate(
  494. pointer, size, this_handler->handler_);
  495. }
  496. template <typename AsyncRandomAccessReadDevice, typename Allocator,
  497. typename CompletionCondition, typename ReadHandler>
  498. inline bool asio_handler_is_continuation(
  499. read_at_streambuf_op<AsyncRandomAccessReadDevice, Allocator,
  500. CompletionCondition, ReadHandler>* this_handler)
  501. {
  502. return this_handler->start_ == 0 ? true
  503. : boost_asio_handler_cont_helpers::is_continuation(
  504. this_handler->handler_);
  505. }
  506. template <typename Function, typename AsyncRandomAccessReadDevice,
  507. typename Allocator, typename CompletionCondition, typename ReadHandler>
  508. inline void asio_handler_invoke(Function& function,
  509. read_at_streambuf_op<AsyncRandomAccessReadDevice, Allocator,
  510. CompletionCondition, ReadHandler>* this_handler)
  511. {
  512. boost_asio_handler_invoke_helpers::invoke(
  513. function, this_handler->handler_);
  514. }
  515. template <typename Function, typename AsyncRandomAccessReadDevice,
  516. typename Allocator, typename CompletionCondition, typename ReadHandler>
  517. inline void asio_handler_invoke(const Function& function,
  518. read_at_streambuf_op<AsyncRandomAccessReadDevice, Allocator,
  519. CompletionCondition, ReadHandler>* this_handler)
  520. {
  521. boost_asio_handler_invoke_helpers::invoke(
  522. function, this_handler->handler_);
  523. }
  524. template <typename AsyncRandomAccessReadDevice>
  525. class initiate_async_read_at_streambuf
  526. {
  527. public:
  528. typedef typename AsyncRandomAccessReadDevice::executor_type executor_type;
  529. explicit initiate_async_read_at_streambuf(
  530. AsyncRandomAccessReadDevice& device)
  531. : device_(device)
  532. {
  533. }
  534. executor_type get_executor() const BOOST_ASIO_NOEXCEPT
  535. {
  536. return device_.get_executor();
  537. }
  538. template <typename ReadHandler,
  539. typename Allocator, typename CompletionCondition>
  540. void operator()(BOOST_ASIO_MOVE_ARG(ReadHandler) handler,
  541. uint64_t offset, basic_streambuf<Allocator>* b,
  542. BOOST_ASIO_MOVE_ARG(CompletionCondition) completion_cond) const
  543. {
  544. // If you get an error on the following line it means that your handler
  545. // does not meet the documented type requirements for a ReadHandler.
  546. BOOST_ASIO_READ_HANDLER_CHECK(ReadHandler, handler) type_check;
  547. non_const_lvalue<ReadHandler> handler2(handler);
  548. non_const_lvalue<CompletionCondition> completion_cond2(completion_cond);
  549. read_at_streambuf_op<AsyncRandomAccessReadDevice, Allocator,
  550. CompletionCondition, typename decay<ReadHandler>::type>(
  551. device_, offset, *b, completion_cond2.value, handler2.value)(
  552. boost::system::error_code(), 0, 1);
  553. }
  554. private:
  555. AsyncRandomAccessReadDevice& device_;
  556. };
  557. } // namespace detail
  558. #if !defined(GENERATING_DOCUMENTATION)
  559. template <typename AsyncRandomAccessReadDevice, typename Allocator,
  560. typename CompletionCondition, typename ReadHandler, typename Allocator1>
  561. struct associated_allocator<
  562. detail::read_at_streambuf_op<AsyncRandomAccessReadDevice,
  563. Allocator, CompletionCondition, ReadHandler>,
  564. Allocator1>
  565. {
  566. typedef typename associated_allocator<ReadHandler, Allocator1>::type type;
  567. static type get(
  568. const detail::read_at_streambuf_op<AsyncRandomAccessReadDevice,
  569. Allocator, CompletionCondition, ReadHandler>& h,
  570. const Allocator1& a = Allocator1()) BOOST_ASIO_NOEXCEPT
  571. {
  572. return associated_allocator<ReadHandler, Allocator1>::get(h.handler_, a);
  573. }
  574. };
  575. template <typename AsyncRandomAccessReadDevice, typename Executor,
  576. typename CompletionCondition, typename ReadHandler, typename Executor1>
  577. struct associated_executor<
  578. detail::read_at_streambuf_op<AsyncRandomAccessReadDevice,
  579. Executor, CompletionCondition, ReadHandler>,
  580. Executor1>
  581. {
  582. typedef typename associated_executor<ReadHandler, Executor1>::type type;
  583. static type get(
  584. const detail::read_at_streambuf_op<AsyncRandomAccessReadDevice,
  585. Executor, CompletionCondition, ReadHandler>& h,
  586. const Executor1& ex = Executor1()) BOOST_ASIO_NOEXCEPT
  587. {
  588. return associated_executor<ReadHandler, Executor1>::get(h.handler_, ex);
  589. }
  590. };
  591. #endif // !defined(GENERATING_DOCUMENTATION)
  592. template <typename AsyncRandomAccessReadDevice,
  593. typename Allocator, typename CompletionCondition,
  594. BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code,
  595. std::size_t)) ReadHandler>
  596. inline BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(ReadHandler,
  597. void (boost::system::error_code, std::size_t))
  598. async_read_at(AsyncRandomAccessReadDevice& d,
  599. uint64_t offset, boost::asio::basic_streambuf<Allocator>& b,
  600. CompletionCondition completion_condition,
  601. BOOST_ASIO_MOVE_ARG(ReadHandler) handler)
  602. {
  603. return async_initiate<ReadHandler,
  604. void (boost::system::error_code, std::size_t)>(
  605. detail::initiate_async_read_at_streambuf<AsyncRandomAccessReadDevice>(d),
  606. handler, offset, &b,
  607. BOOST_ASIO_MOVE_CAST(CompletionCondition)(completion_condition));
  608. }
  609. template <typename AsyncRandomAccessReadDevice, typename Allocator,
  610. BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code,
  611. std::size_t)) ReadHandler>
  612. inline BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(ReadHandler,
  613. void (boost::system::error_code, std::size_t))
  614. async_read_at(AsyncRandomAccessReadDevice& d,
  615. uint64_t offset, boost::asio::basic_streambuf<Allocator>& b,
  616. BOOST_ASIO_MOVE_ARG(ReadHandler) handler)
  617. {
  618. return async_initiate<ReadHandler,
  619. void (boost::system::error_code, std::size_t)>(
  620. detail::initiate_async_read_at_streambuf<AsyncRandomAccessReadDevice>(d),
  621. handler, offset, &b, transfer_all());
  622. }
  623. #endif // !defined(BOOST_ASIO_NO_IOSTREAM)
  624. #endif // !defined(BOOST_ASIO_NO_EXTENSIONS)
  625. } // namespace asio
  626. } // namespace boost
  627. #include <boost/asio/detail/pop_options.hpp>
  628. #endif // BOOST_ASIO_IMPL_READ_AT_HPP