basic_random_access_handle.hpp 12 KB

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