basic_random_access_handle.hpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. //
  2. // windows/basic_random_access_handle.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_WINDOWS_BASIC_RANDOM_ACCESS_HANDLE_HPP
  11. #define BOOST_ASIO_WINDOWS_BASIC_RANDOM_ACCESS_HANDLE_HPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include <boost/asio/detail/config.hpp>
  16. #include <boost/asio/windows/basic_overlapped_handle.hpp>
  17. #if defined(BOOST_ASIO_HAS_WINDOWS_RANDOM_ACCESS_HANDLE) \
  18. || defined(GENERATING_DOCUMENTATION)
  19. #include <boost/asio/detail/push_options.hpp>
  20. namespace boost {
  21. namespace asio {
  22. namespace windows {
  23. /// Provides random-access handle functionality.
  24. /**
  25. * The windows::basic_random_access_handle class provides asynchronous and
  26. * blocking random-access handle functionality.
  27. *
  28. * @par Thread Safety
  29. * @e Distinct @e objects: Safe.@n
  30. * @e Shared @e objects: Unsafe.
  31. */
  32. template <typename Executor = executor>
  33. class basic_random_access_handle
  34. : public basic_overlapped_handle<Executor>
  35. {
  36. public:
  37. /// The type of the executor associated with the object.
  38. typedef Executor executor_type;
  39. /// Rebinds the handle type to another executor.
  40. template <typename Executor1>
  41. struct rebind_executor
  42. {
  43. /// The handle type when rebound to the specified executor.
  44. typedef basic_random_access_handle<Executor1> other;
  45. };
  46. /// The native representation of a handle.
  47. #if defined(GENERATING_DOCUMENTATION)
  48. typedef implementation_defined native_handle_type;
  49. #else
  50. typedef boost::asio::detail::win_iocp_handle_service::native_handle_type
  51. native_handle_type;
  52. #endif
  53. /// Construct a random-access handle without opening it.
  54. /**
  55. * This constructor creates a random-access handle without opening it.
  56. *
  57. * @param ex The I/O executor that the random-access handle will use, by
  58. * default, to dispatch handlers for any asynchronous operations performed on
  59. * the random-access handle.
  60. */
  61. explicit basic_random_access_handle(const executor_type& ex)
  62. : basic_overlapped_handle<Executor>(ex)
  63. {
  64. }
  65. /// Construct a random-access handle without opening it.
  66. /**
  67. * This constructor creates a random-access handle without opening it. The
  68. * handle needs to be opened or assigned before data can be sent or received
  69. * on it.
  70. *
  71. * @param context An execution context which provides the I/O executor that
  72. * the random-access handle will use, by default, to dispatch handlers for any
  73. * asynchronous operations performed on the random-access handle.
  74. */
  75. template <typename ExecutionContext>
  76. explicit basic_random_access_handle(ExecutionContext& context,
  77. typename enable_if<
  78. is_convertible<ExecutionContext&, execution_context&>::value,
  79. basic_random_access_handle
  80. >::type* = 0)
  81. : basic_overlapped_handle<Executor>(context)
  82. {
  83. }
  84. /// Construct a random-access handle on an existing native handle.
  85. /**
  86. * This constructor creates a random-access handle object to hold an existing
  87. * native handle.
  88. *
  89. * @param ex The I/O executor that the random-access handle will use, by
  90. * default, to dispatch handlers for any asynchronous operations performed on
  91. * the random-access handle.
  92. *
  93. * @param handle The new underlying handle implementation.
  94. *
  95. * @throws boost::system::system_error Thrown on failure.
  96. */
  97. basic_random_access_handle(const executor_type& ex,
  98. const native_handle_type& handle)
  99. : basic_overlapped_handle<Executor>(ex, handle)
  100. {
  101. }
  102. /// Construct a random-access handle on an existing native handle.
  103. /**
  104. * This constructor creates a random-access handle object to hold an existing
  105. * native handle.
  106. *
  107. * @param context An execution context which provides the I/O executor that
  108. * the random-access handle will use, by default, to dispatch handlers for any
  109. * asynchronous operations performed on the random-access handle.
  110. *
  111. * @param handle The new underlying handle implementation.
  112. *
  113. * @throws boost::system::system_error Thrown on failure.
  114. */
  115. template <typename ExecutionContext>
  116. basic_random_access_handle(ExecutionContext& context,
  117. const native_handle_type& handle,
  118. typename enable_if<
  119. is_convertible<ExecutionContext&, execution_context&>::value
  120. >::type* = 0)
  121. : basic_overlapped_handle<Executor>(context, handle)
  122. {
  123. }
  124. #if defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
  125. /// Move-construct a random-access handle from another.
  126. /**
  127. * This constructor moves a random-access handle from one object to another.
  128. *
  129. * @param other The other random-access handle object from which the
  130. * move will occur.
  131. *
  132. * @note Following the move, the moved-from object is in the same state as if
  133. * constructed using the @c basic_random_access_handle(const executor_type&)
  134. * constructor.
  135. */
  136. basic_random_access_handle(basic_random_access_handle&& other)
  137. : basic_overlapped_handle<Executor>(std::move(other))
  138. {
  139. }
  140. /// Move-assign a random-access handle from another.
  141. /**
  142. * This assignment operator moves a random-access handle from one object to
  143. * another.
  144. *
  145. * @param other The other random-access handle object from which the
  146. * move will occur.
  147. *
  148. * @note Following the move, the moved-from object is in the same state as if
  149. * constructed using the @c basic_random_access_handle(const executor_type&)
  150. * constructor.
  151. */
  152. basic_random_access_handle& operator=(basic_random_access_handle&& other)
  153. {
  154. basic_overlapped_handle<Executor>::operator=(std::move(other));
  155. return *this;
  156. }
  157. #endif // defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
  158. /// Write some data to the handle at the specified offset.
  159. /**
  160. * This function is used to write data to the random-access handle. The
  161. * function call will block until one or more bytes of the data has been
  162. * written successfully, or until an error occurs.
  163. *
  164. * @param offset The offset at which the data will be written.
  165. *
  166. * @param buffers One or more data buffers to be written to the handle.
  167. *
  168. * @returns The number of bytes written.
  169. *
  170. * @throws boost::system::system_error Thrown on failure. An error code of
  171. * boost::asio::error::eof indicates that the connection was closed by the
  172. * peer.
  173. *
  174. * @note The write_some_at operation may not write all of the data. Consider
  175. * using the @ref write_at function if you need to ensure that all data is
  176. * written before the blocking operation completes.
  177. *
  178. * @par Example
  179. * To write a single data buffer use the @ref buffer function as follows:
  180. * @code
  181. * handle.write_some_at(42, boost::asio::buffer(data, size));
  182. * @endcode
  183. * See the @ref buffer documentation for information on writing multiple
  184. * buffers in one go, and how to use it with arrays, boost::array or
  185. * std::vector.
  186. */
  187. template <typename ConstBufferSequence>
  188. std::size_t write_some_at(uint64_t offset,
  189. const ConstBufferSequence& buffers)
  190. {
  191. boost::system::error_code ec;
  192. std::size_t s = this->impl_.get_service().write_some_at(
  193. this->impl_.get_implementation(), offset, buffers, ec);
  194. boost::asio::detail::throw_error(ec, "write_some_at");
  195. return s;
  196. }
  197. /// Write some data to the handle at the specified offset.
  198. /**
  199. * This function is used to write data to the random-access handle. The
  200. * function call will block until one or more bytes of the data has been
  201. * written successfully, or until an error occurs.
  202. *
  203. * @param offset The offset at which the data will be written.
  204. *
  205. * @param buffers One or more data buffers to be written to the handle.
  206. *
  207. * @param ec Set to indicate what error occurred, if any.
  208. *
  209. * @returns The number of bytes written. Returns 0 if an error occurred.
  210. *
  211. * @note The write_some operation may not transmit all of the data to the
  212. * peer. Consider using the @ref write_at function if you need to ensure that
  213. * all data is written before the blocking operation completes.
  214. */
  215. template <typename ConstBufferSequence>
  216. std::size_t write_some_at(uint64_t offset,
  217. const ConstBufferSequence& buffers, boost::system::error_code& ec)
  218. {
  219. return this->impl_.get_service().write_some_at(
  220. this->impl_.get_implementation(), offset, buffers, ec);
  221. }
  222. /// Start an asynchronous write at the specified offset.
  223. /**
  224. * This function is used to asynchronously write data to the random-access
  225. * handle. The function call always returns immediately.
  226. *
  227. * @param offset The offset at which the data will be written.
  228. *
  229. * @param buffers One or more data buffers to be written to the handle.
  230. * Although the buffers object may be copied as necessary, ownership of the
  231. * underlying memory blocks is retained by the caller, which must guarantee
  232. * that they remain valid until the handler is called.
  233. *
  234. * @param handler The handler to be called when the write operation completes.
  235. * Copies will be made of the handler as required. The function signature of
  236. * the handler must be:
  237. * @code void handler(
  238. * const boost::system::error_code& error, // Result of operation.
  239. * std::size_t bytes_transferred // Number of bytes written.
  240. * ); @endcode
  241. * Regardless of whether the asynchronous operation completes immediately or
  242. * not, the handler will not be invoked from within this function. On
  243. * immediate completion, invocation of the handler will be performed in a
  244. * manner equivalent to using boost::asio::post().
  245. *
  246. * @note The write operation may not transmit all of the data to the peer.
  247. * Consider using the @ref async_write_at function if you need to ensure that
  248. * all data is written before the asynchronous operation completes.
  249. *
  250. * @par Example
  251. * To write a single data buffer use the @ref buffer function as follows:
  252. * @code
  253. * handle.async_write_some_at(42, boost::asio::buffer(data, size), handler);
  254. * @endcode
  255. * See the @ref buffer documentation for information on writing multiple
  256. * buffers in one go, and how to use it with arrays, boost::array or
  257. * std::vector.
  258. */
  259. template <typename ConstBufferSequence,
  260. BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code,
  261. std::size_t)) WriteHandler
  262. BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
  263. BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(WriteHandler,
  264. void (boost::system::error_code, std::size_t))
  265. async_write_some_at(uint64_t offset,
  266. const ConstBufferSequence& buffers,
  267. BOOST_ASIO_MOVE_ARG(WriteHandler) handler
  268. BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(executor_type))
  269. {
  270. return async_initiate<WriteHandler,
  271. void (boost::system::error_code, std::size_t)>(
  272. initiate_async_write_some_at(this), handler, offset, buffers);
  273. }
  274. /// Read some data from the handle at the specified offset.
  275. /**
  276. * This function is used to read data from the random-access handle. The
  277. * function call will block until one or more bytes of data has been read
  278. * successfully, or until an error occurs.
  279. *
  280. * @param offset The offset at which the data will be read.
  281. *
  282. * @param buffers One or more buffers into which the data will be read.
  283. *
  284. * @returns The number of bytes read.
  285. *
  286. * @throws boost::system::system_error Thrown on failure. An error code of
  287. * boost::asio::error::eof indicates that the connection was closed by the
  288. * peer.
  289. *
  290. * @note The read_some operation may not read all of the requested number of
  291. * bytes. Consider using the @ref read_at function if you need to ensure that
  292. * the requested amount of data is read before the blocking operation
  293. * completes.
  294. *
  295. * @par Example
  296. * To read into a single data buffer use the @ref buffer function as follows:
  297. * @code
  298. * handle.read_some_at(42, boost::asio::buffer(data, size));
  299. * @endcode
  300. * See the @ref buffer documentation for information on reading into multiple
  301. * buffers in one go, and how to use it with arrays, boost::array or
  302. * std::vector.
  303. */
  304. template <typename MutableBufferSequence>
  305. std::size_t read_some_at(uint64_t offset,
  306. const MutableBufferSequence& buffers)
  307. {
  308. boost::system::error_code ec;
  309. std::size_t s = this->impl_.get_service().read_some_at(
  310. this->impl_.get_implementation(), offset, buffers, ec);
  311. boost::asio::detail::throw_error(ec, "read_some_at");
  312. return s;
  313. }
  314. /// Read some data from the handle at the specified offset.
  315. /**
  316. * This function is used to read data from the random-access handle. The
  317. * function call will block until one or more bytes of data has been read
  318. * successfully, or until an error occurs.
  319. *
  320. * @param offset The offset at which the data will be read.
  321. *
  322. * @param buffers One or more buffers into which the data will be read.
  323. *
  324. * @param ec Set to indicate what error occurred, if any.
  325. *
  326. * @returns The number of bytes read. Returns 0 if an error occurred.
  327. *
  328. * @note The read_some operation may not read all of the requested number of
  329. * bytes. Consider using the @ref read_at function if you need to ensure that
  330. * the requested amount of data is read before the blocking operation
  331. * completes.
  332. */
  333. template <typename MutableBufferSequence>
  334. std::size_t read_some_at(uint64_t offset,
  335. const MutableBufferSequence& buffers, boost::system::error_code& ec)
  336. {
  337. return this->impl_.get_service().read_some_at(
  338. this->impl_.get_implementation(), offset, buffers, ec);
  339. }
  340. /// Start an asynchronous read at the specified offset.
  341. /**
  342. * This function is used to asynchronously read data from the random-access
  343. * handle. The function call always returns immediately.
  344. *
  345. * @param offset The offset at which the data will be read.
  346. *
  347. * @param buffers One or more buffers into which the data will be read.
  348. * Although the buffers object may be copied as necessary, ownership of the
  349. * underlying memory blocks is retained by the caller, which must guarantee
  350. * that they remain valid until the handler is called.
  351. *
  352. * @param handler The handler to be called when the read operation completes.
  353. * Copies will be made of the handler as required. The function signature of
  354. * the handler must be:
  355. * @code void handler(
  356. * const boost::system::error_code& error, // Result of operation.
  357. * std::size_t bytes_transferred // Number of bytes read.
  358. * ); @endcode
  359. * Regardless of whether the asynchronous operation completes immediately or
  360. * not, the handler will not be invoked from within this function. On
  361. * immediate completion, invocation of the handler will be performed in a
  362. * manner equivalent to using boost::asio::post().
  363. *
  364. * @note The read operation may not read all of the requested number of bytes.
  365. * Consider using the @ref async_read_at function if you need to ensure that
  366. * the requested amount of data is read before the asynchronous operation
  367. * completes.
  368. *
  369. * @par Example
  370. * To read into a single data buffer use the @ref buffer function as follows:
  371. * @code
  372. * handle.async_read_some_at(42, boost::asio::buffer(data, size), handler);
  373. * @endcode
  374. * See the @ref buffer documentation for information on reading into multiple
  375. * buffers in one go, and how to use it with arrays, boost::array or
  376. * std::vector.
  377. */
  378. template <typename MutableBufferSequence,
  379. BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code,
  380. std::size_t)) ReadHandler
  381. BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
  382. BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(ReadHandler,
  383. void (boost::system::error_code, std::size_t))
  384. async_read_some_at(uint64_t offset,
  385. const MutableBufferSequence& buffers,
  386. BOOST_ASIO_MOVE_ARG(ReadHandler) handler
  387. BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(executor_type))
  388. {
  389. return async_initiate<ReadHandler,
  390. void (boost::system::error_code, std::size_t)>(
  391. initiate_async_read_some_at(this), handler, offset, buffers);
  392. }
  393. private:
  394. class initiate_async_write_some_at
  395. {
  396. public:
  397. typedef Executor executor_type;
  398. explicit initiate_async_write_some_at(basic_random_access_handle* self)
  399. : self_(self)
  400. {
  401. }
  402. executor_type get_executor() const BOOST_ASIO_NOEXCEPT
  403. {
  404. return self_->get_executor();
  405. }
  406. template <typename WriteHandler, typename ConstBufferSequence>
  407. void operator()(BOOST_ASIO_MOVE_ARG(WriteHandler) handler,
  408. uint64_t offset, const ConstBufferSequence& buffers) const
  409. {
  410. // If you get an error on the following line it means that your handler
  411. // does not meet the documented type requirements for a WriteHandler.
  412. BOOST_ASIO_WRITE_HANDLER_CHECK(WriteHandler, handler) type_check;
  413. detail::non_const_lvalue<WriteHandler> handler2(handler);
  414. self_->impl_.get_service().async_write_some_at(
  415. self_->impl_.get_implementation(), offset, buffers, handler2.value,
  416. self_->impl_.get_implementation_executor());
  417. }
  418. private:
  419. basic_random_access_handle* self_;
  420. };
  421. class initiate_async_read_some_at
  422. {
  423. public:
  424. typedef Executor executor_type;
  425. explicit initiate_async_read_some_at(basic_random_access_handle* self)
  426. : self_(self)
  427. {
  428. }
  429. executor_type get_executor() const BOOST_ASIO_NOEXCEPT
  430. {
  431. return self_->get_executor();
  432. }
  433. template <typename ReadHandler, typename MutableBufferSequence>
  434. void operator()(BOOST_ASIO_MOVE_ARG(ReadHandler) handler,
  435. uint64_t offset, const MutableBufferSequence& buffers) const
  436. {
  437. // If you get an error on the following line it means that your handler
  438. // does not meet the documented type requirements for a ReadHandler.
  439. BOOST_ASIO_READ_HANDLER_CHECK(ReadHandler, handler) type_check;
  440. detail::non_const_lvalue<ReadHandler> handler2(handler);
  441. self_->impl_.get_service().async_read_some_at(
  442. self_->impl_.get_implementation(), offset, buffers, handler2.value,
  443. self_->impl_.get_implementation_executor());
  444. }
  445. private:
  446. basic_random_access_handle* self_;
  447. };
  448. };
  449. } // namespace windows
  450. } // namespace asio
  451. } // namespace boost
  452. #include <boost/asio/detail/pop_options.hpp>
  453. #endif // defined(BOOST_ASIO_HAS_WINDOWS_RANDOM_ACCESS_HANDLE)
  454. // || defined(GENERATING_DOCUMENTATION)
  455. #endif // BOOST_ASIO_WINDOWS_BASIC_RANDOM_ACCESS_HANDLE_HPP