basic_random_access_handle.hpp 14 KB

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