random_access_handle.hpp 14 KB

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