basic_stream_handle.hpp 14 KB

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