basic_stream_handle.hpp 13 KB

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