basic_random_access_handle.hpp 12 KB

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