write_at.hpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. //
  2. // impl/write_at.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_WRITE_AT_HPP
  11. #define BOOST_ASIO_IMPL_WRITE_AT_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/buffer.hpp>
  17. #include <boost/asio/detail/array_fwd.hpp>
  18. #include <boost/asio/detail/base_from_cancellation_state.hpp>
  19. #include <boost/asio/detail/base_from_completion_cond.hpp>
  20. #include <boost/asio/detail/bind_handler.hpp>
  21. #include <boost/asio/detail/consuming_buffers.hpp>
  22. #include <boost/asio/detail/dependent_type.hpp>
  23. #include <boost/asio/detail/handler_cont_helpers.hpp>
  24. #include <boost/asio/detail/handler_tracking.hpp>
  25. #include <boost/asio/detail/handler_type_requirements.hpp>
  26. #include <boost/asio/detail/non_const_lvalue.hpp>
  27. #include <boost/asio/detail/throw_error.hpp>
  28. #include <boost/asio/detail/push_options.hpp>
  29. namespace boost {
  30. namespace asio {
  31. namespace detail
  32. {
  33. template <typename SyncRandomAccessWriteDevice, typename ConstBufferSequence,
  34. typename ConstBufferIterator, typename CompletionCondition>
  35. std::size_t write_at_buffer_sequence(SyncRandomAccessWriteDevice& d,
  36. uint64_t offset, const ConstBufferSequence& buffers,
  37. const ConstBufferIterator&, CompletionCondition completion_condition,
  38. boost::system::error_code& ec)
  39. {
  40. ec = boost::system::error_code();
  41. boost::asio::detail::consuming_buffers<const_buffer,
  42. ConstBufferSequence, ConstBufferIterator> tmp(buffers);
  43. while (!tmp.empty())
  44. {
  45. if (std::size_t max_size = detail::adapt_completion_condition_result(
  46. completion_condition(ec, tmp.total_consumed())))
  47. {
  48. tmp.consume(d.write_some_at(offset + tmp.total_consumed(),
  49. tmp.prepare(max_size), ec));
  50. }
  51. else
  52. break;
  53. }
  54. return tmp.total_consumed();
  55. }
  56. } // namespace detail
  57. template <typename SyncRandomAccessWriteDevice, typename ConstBufferSequence,
  58. typename CompletionCondition>
  59. std::size_t write_at(SyncRandomAccessWriteDevice& d,
  60. uint64_t offset, const ConstBufferSequence& buffers,
  61. CompletionCondition completion_condition, boost::system::error_code& ec,
  62. constraint_t<
  63. is_completion_condition<CompletionCondition>::value
  64. >)
  65. {
  66. return detail::write_at_buffer_sequence(d, offset, buffers,
  67. boost::asio::buffer_sequence_begin(buffers),
  68. static_cast<CompletionCondition&&>(completion_condition), ec);
  69. }
  70. template <typename SyncRandomAccessWriteDevice, typename ConstBufferSequence>
  71. inline std::size_t write_at(SyncRandomAccessWriteDevice& d,
  72. uint64_t offset, const ConstBufferSequence& buffers)
  73. {
  74. boost::system::error_code ec;
  75. std::size_t bytes_transferred = write_at(
  76. d, offset, buffers, transfer_all(), ec);
  77. boost::asio::detail::throw_error(ec, "write_at");
  78. return bytes_transferred;
  79. }
  80. template <typename SyncRandomAccessWriteDevice, typename ConstBufferSequence>
  81. inline std::size_t write_at(SyncRandomAccessWriteDevice& d,
  82. uint64_t offset, const ConstBufferSequence& buffers,
  83. boost::system::error_code& ec)
  84. {
  85. return write_at(d, offset, buffers, transfer_all(), ec);
  86. }
  87. template <typename SyncRandomAccessWriteDevice, typename ConstBufferSequence,
  88. typename CompletionCondition>
  89. inline std::size_t write_at(SyncRandomAccessWriteDevice& d,
  90. uint64_t offset, const ConstBufferSequence& buffers,
  91. CompletionCondition completion_condition,
  92. constraint_t<
  93. is_completion_condition<CompletionCondition>::value
  94. >)
  95. {
  96. boost::system::error_code ec;
  97. std::size_t bytes_transferred = write_at(d, offset, buffers,
  98. static_cast<CompletionCondition&&>(completion_condition), ec);
  99. boost::asio::detail::throw_error(ec, "write_at");
  100. return bytes_transferred;
  101. }
  102. #if !defined(BOOST_ASIO_NO_EXTENSIONS)
  103. #if !defined(BOOST_ASIO_NO_IOSTREAM)
  104. template <typename SyncRandomAccessWriteDevice, typename Allocator,
  105. typename CompletionCondition>
  106. std::size_t write_at(SyncRandomAccessWriteDevice& d,
  107. uint64_t offset, boost::asio::basic_streambuf<Allocator>& b,
  108. CompletionCondition completion_condition, boost::system::error_code& ec,
  109. constraint_t<
  110. is_completion_condition<CompletionCondition>::value
  111. >)
  112. {
  113. std::size_t bytes_transferred = write_at(d, offset, b.data(),
  114. static_cast<CompletionCondition&&>(completion_condition), ec);
  115. b.consume(bytes_transferred);
  116. return bytes_transferred;
  117. }
  118. template <typename SyncRandomAccessWriteDevice, typename Allocator>
  119. inline std::size_t write_at(SyncRandomAccessWriteDevice& d,
  120. uint64_t offset, boost::asio::basic_streambuf<Allocator>& b)
  121. {
  122. boost::system::error_code ec;
  123. std::size_t bytes_transferred = write_at(d, offset, b, transfer_all(), ec);
  124. boost::asio::detail::throw_error(ec, "write_at");
  125. return bytes_transferred;
  126. }
  127. template <typename SyncRandomAccessWriteDevice, typename Allocator>
  128. inline std::size_t write_at(SyncRandomAccessWriteDevice& d,
  129. uint64_t offset, boost::asio::basic_streambuf<Allocator>& b,
  130. boost::system::error_code& ec)
  131. {
  132. return write_at(d, offset, b, transfer_all(), ec);
  133. }
  134. template <typename SyncRandomAccessWriteDevice, typename Allocator,
  135. typename CompletionCondition>
  136. inline std::size_t write_at(SyncRandomAccessWriteDevice& d,
  137. uint64_t offset, boost::asio::basic_streambuf<Allocator>& b,
  138. CompletionCondition completion_condition,
  139. constraint_t<
  140. is_completion_condition<CompletionCondition>::value
  141. >)
  142. {
  143. boost::system::error_code ec;
  144. std::size_t bytes_transferred = write_at(d, offset, b,
  145. static_cast<CompletionCondition&&>(completion_condition), ec);
  146. boost::asio::detail::throw_error(ec, "write_at");
  147. return bytes_transferred;
  148. }
  149. #endif // !defined(BOOST_ASIO_NO_IOSTREAM)
  150. #endif // !defined(BOOST_ASIO_NO_EXTENSIONS)
  151. namespace detail
  152. {
  153. template <typename AsyncRandomAccessWriteDevice,
  154. typename ConstBufferSequence, typename ConstBufferIterator,
  155. typename CompletionCondition, typename WriteHandler>
  156. class write_at_op
  157. : public base_from_cancellation_state<WriteHandler>,
  158. base_from_completion_cond<CompletionCondition>
  159. {
  160. public:
  161. write_at_op(AsyncRandomAccessWriteDevice& device,
  162. uint64_t offset, const ConstBufferSequence& buffers,
  163. CompletionCondition& completion_condition, WriteHandler& handler)
  164. : base_from_cancellation_state<WriteHandler>(
  165. handler, enable_partial_cancellation()),
  166. base_from_completion_cond<CompletionCondition>(completion_condition),
  167. device_(device),
  168. offset_(offset),
  169. buffers_(buffers),
  170. start_(0),
  171. handler_(static_cast<WriteHandler&&>(handler))
  172. {
  173. }
  174. write_at_op(const write_at_op& other)
  175. : base_from_cancellation_state<WriteHandler>(other),
  176. base_from_completion_cond<CompletionCondition>(other),
  177. device_(other.device_),
  178. offset_(other.offset_),
  179. buffers_(other.buffers_),
  180. start_(other.start_),
  181. handler_(other.handler_)
  182. {
  183. }
  184. write_at_op(write_at_op&& other)
  185. : base_from_cancellation_state<WriteHandler>(
  186. static_cast<base_from_cancellation_state<WriteHandler>&&>(other)),
  187. base_from_completion_cond<CompletionCondition>(
  188. static_cast<base_from_completion_cond<CompletionCondition>&&>(other)),
  189. device_(other.device_),
  190. offset_(other.offset_),
  191. buffers_(static_cast<buffers_type&&>(other.buffers_)),
  192. start_(other.start_),
  193. handler_(static_cast<WriteHandler&&>(other.handler_))
  194. {
  195. }
  196. void operator()(boost::system::error_code ec,
  197. std::size_t bytes_transferred, int start = 0)
  198. {
  199. std::size_t max_size;
  200. switch (start_ = start)
  201. {
  202. case 1:
  203. max_size = this->check_for_completion(ec, buffers_.total_consumed());
  204. for (;;)
  205. {
  206. {
  207. BOOST_ASIO_HANDLER_LOCATION((__FILE__, __LINE__, "async_write_at"));
  208. device_.async_write_some_at(
  209. offset_ + buffers_.total_consumed(), buffers_.prepare(max_size),
  210. static_cast<write_at_op&&>(*this));
  211. }
  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. if (max_size == 0)
  218. break;
  219. if (this->cancelled() != cancellation_type::none)
  220. {
  221. ec = boost::asio::error::operation_aborted;
  222. break;
  223. }
  224. }
  225. static_cast<WriteHandler&&>(handler_)(
  226. static_cast<const boost::system::error_code&>(ec),
  227. static_cast<const std::size_t&>(buffers_.total_consumed()));
  228. }
  229. }
  230. //private:
  231. typedef boost::asio::detail::consuming_buffers<const_buffer,
  232. ConstBufferSequence, ConstBufferIterator> buffers_type;
  233. AsyncRandomAccessWriteDevice& device_;
  234. uint64_t offset_;
  235. buffers_type buffers_;
  236. int start_;
  237. WriteHandler handler_;
  238. };
  239. template <typename AsyncRandomAccessWriteDevice,
  240. typename ConstBufferSequence, typename ConstBufferIterator,
  241. typename CompletionCondition, typename WriteHandler>
  242. inline bool asio_handler_is_continuation(
  243. write_at_op<AsyncRandomAccessWriteDevice, ConstBufferSequence,
  244. ConstBufferIterator, CompletionCondition, WriteHandler>* this_handler)
  245. {
  246. return this_handler->start_ == 0 ? true
  247. : boost_asio_handler_cont_helpers::is_continuation(
  248. this_handler->handler_);
  249. }
  250. template <typename AsyncRandomAccessWriteDevice,
  251. typename ConstBufferSequence, typename ConstBufferIterator,
  252. typename CompletionCondition, typename WriteHandler>
  253. inline void start_write_at_op(AsyncRandomAccessWriteDevice& d,
  254. uint64_t offset, const ConstBufferSequence& buffers,
  255. const ConstBufferIterator&, CompletionCondition& completion_condition,
  256. WriteHandler& handler)
  257. {
  258. detail::write_at_op<AsyncRandomAccessWriteDevice, ConstBufferSequence,
  259. ConstBufferIterator, CompletionCondition, WriteHandler>(
  260. d, offset, buffers, completion_condition, handler)(
  261. boost::system::error_code(), 0, 1);
  262. }
  263. template <typename AsyncRandomAccessWriteDevice>
  264. class initiate_async_write_at
  265. {
  266. public:
  267. typedef typename AsyncRandomAccessWriteDevice::executor_type executor_type;
  268. explicit initiate_async_write_at(AsyncRandomAccessWriteDevice& device)
  269. : device_(device)
  270. {
  271. }
  272. executor_type get_executor() const noexcept
  273. {
  274. return device_.get_executor();
  275. }
  276. template <typename WriteHandler, typename ConstBufferSequence,
  277. typename CompletionCondition>
  278. void operator()(WriteHandler&& handler,
  279. uint64_t offset, const ConstBufferSequence& buffers,
  280. CompletionCondition&& completion_cond) const
  281. {
  282. // If you get an error on the following line it means that your handler
  283. // does not meet the documented type requirements for a WriteHandler.
  284. BOOST_ASIO_WRITE_HANDLER_CHECK(WriteHandler, handler) type_check;
  285. non_const_lvalue<WriteHandler> handler2(handler);
  286. non_const_lvalue<CompletionCondition> completion_cond2(completion_cond);
  287. start_write_at_op(device_, offset, buffers,
  288. boost::asio::buffer_sequence_begin(buffers),
  289. completion_cond2.value, handler2.value);
  290. }
  291. private:
  292. AsyncRandomAccessWriteDevice& device_;
  293. };
  294. } // namespace detail
  295. #if !defined(GENERATING_DOCUMENTATION)
  296. template <template <typename, typename> class Associator,
  297. typename AsyncRandomAccessWriteDevice, typename ConstBufferSequence,
  298. typename ConstBufferIterator, typename CompletionCondition,
  299. typename WriteHandler, typename DefaultCandidate>
  300. struct associator<Associator,
  301. detail::write_at_op<AsyncRandomAccessWriteDevice, ConstBufferSequence,
  302. ConstBufferIterator, CompletionCondition, WriteHandler>,
  303. DefaultCandidate>
  304. : Associator<WriteHandler, DefaultCandidate>
  305. {
  306. static typename Associator<WriteHandler, DefaultCandidate>::type get(
  307. const detail::write_at_op<AsyncRandomAccessWriteDevice,
  308. ConstBufferSequence, ConstBufferIterator,
  309. CompletionCondition, WriteHandler>& h) noexcept
  310. {
  311. return Associator<WriteHandler, DefaultCandidate>::get(h.handler_);
  312. }
  313. static auto get(
  314. const detail::write_at_op<AsyncRandomAccessWriteDevice,
  315. ConstBufferSequence, ConstBufferIterator,
  316. CompletionCondition, WriteHandler>& h,
  317. const DefaultCandidate& c) noexcept
  318. -> decltype(Associator<WriteHandler, DefaultCandidate>::get(h.handler_, c))
  319. {
  320. return Associator<WriteHandler, DefaultCandidate>::get(h.handler_, c);
  321. }
  322. };
  323. #endif // !defined(GENERATING_DOCUMENTATION)
  324. #if !defined(BOOST_ASIO_NO_EXTENSIONS)
  325. #if !defined(BOOST_ASIO_NO_IOSTREAM)
  326. namespace detail
  327. {
  328. template <typename Allocator, typename WriteHandler>
  329. class write_at_streambuf_op
  330. {
  331. public:
  332. write_at_streambuf_op(
  333. boost::asio::basic_streambuf<Allocator>& streambuf,
  334. WriteHandler& handler)
  335. : streambuf_(streambuf),
  336. handler_(static_cast<WriteHandler&&>(handler))
  337. {
  338. }
  339. write_at_streambuf_op(const write_at_streambuf_op& other)
  340. : streambuf_(other.streambuf_),
  341. handler_(other.handler_)
  342. {
  343. }
  344. write_at_streambuf_op(write_at_streambuf_op&& other)
  345. : streambuf_(other.streambuf_),
  346. handler_(static_cast<WriteHandler&&>(other.handler_))
  347. {
  348. }
  349. void operator()(const boost::system::error_code& ec,
  350. const std::size_t bytes_transferred)
  351. {
  352. streambuf_.consume(bytes_transferred);
  353. static_cast<WriteHandler&&>(handler_)(ec, bytes_transferred);
  354. }
  355. //private:
  356. boost::asio::basic_streambuf<Allocator>& streambuf_;
  357. WriteHandler handler_;
  358. };
  359. template <typename Allocator, typename WriteHandler>
  360. inline bool asio_handler_is_continuation(
  361. write_at_streambuf_op<Allocator, WriteHandler>* this_handler)
  362. {
  363. return boost_asio_handler_cont_helpers::is_continuation(
  364. this_handler->handler_);
  365. }
  366. template <typename AsyncRandomAccessWriteDevice>
  367. class initiate_async_write_at_streambuf
  368. {
  369. public:
  370. typedef typename AsyncRandomAccessWriteDevice::executor_type executor_type;
  371. explicit initiate_async_write_at_streambuf(
  372. AsyncRandomAccessWriteDevice& device)
  373. : device_(device)
  374. {
  375. }
  376. executor_type get_executor() const noexcept
  377. {
  378. return device_.get_executor();
  379. }
  380. template <typename WriteHandler,
  381. typename Allocator, typename CompletionCondition>
  382. void operator()(WriteHandler&& handler,
  383. uint64_t offset, basic_streambuf<Allocator>* b,
  384. CompletionCondition&& completion_condition) const
  385. {
  386. // If you get an error on the following line it means that your handler
  387. // does not meet the documented type requirements for a WriteHandler.
  388. BOOST_ASIO_WRITE_HANDLER_CHECK(WriteHandler, handler) type_check;
  389. non_const_lvalue<WriteHandler> handler2(handler);
  390. async_write_at(device_, offset, b->data(),
  391. static_cast<CompletionCondition&&>(completion_condition),
  392. write_at_streambuf_op<Allocator, decay_t<WriteHandler>>(
  393. *b, handler2.value));
  394. }
  395. private:
  396. AsyncRandomAccessWriteDevice& device_;
  397. };
  398. } // namespace detail
  399. #if !defined(GENERATING_DOCUMENTATION)
  400. template <template <typename, typename> class Associator,
  401. typename Executor, typename WriteHandler, typename DefaultCandidate>
  402. struct associator<Associator,
  403. detail::write_at_streambuf_op<Executor, WriteHandler>,
  404. DefaultCandidate>
  405. : Associator<WriteHandler, DefaultCandidate>
  406. {
  407. static typename Associator<WriteHandler, DefaultCandidate>::type get(
  408. const detail::write_at_streambuf_op<Executor, WriteHandler>& h) noexcept
  409. {
  410. return Associator<WriteHandler, DefaultCandidate>::get(h.handler_);
  411. }
  412. static auto get(
  413. const detail::write_at_streambuf_op<Executor, WriteHandler>& h,
  414. const DefaultCandidate& c) noexcept
  415. -> decltype(Associator<WriteHandler, DefaultCandidate>::get(h.handler_, c))
  416. {
  417. return Associator<WriteHandler, DefaultCandidate>::get(h.handler_, c);
  418. }
  419. };
  420. #endif // !defined(GENERATING_DOCUMENTATION)
  421. #endif // !defined(BOOST_ASIO_NO_IOSTREAM)
  422. #endif // !defined(BOOST_ASIO_NO_EXTENSIONS)
  423. } // namespace asio
  424. } // namespace boost
  425. #include <boost/asio/detail/pop_options.hpp>
  426. #endif // BOOST_ASIO_IMPL_WRITE_AT_HPP