basic_random_access_handle.hpp 16 KB

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