basic_random_access_handle.hpp 12 KB

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