basic_random_access_handle.hpp 15 KB

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