basic_stream_handle.hpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. //
  2. // windows/basic_stream_handle.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2019 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. #include <boost/asio/windows/basic_overlapped_handle.hpp>
  17. #if defined(BOOST_ASIO_HAS_WINDOWS_STREAM_HANDLE) \
  18. || defined(GENERATING_DOCUMENTATION)
  19. #include <boost/asio/detail/push_options.hpp>
  20. namespace boost {
  21. namespace asio {
  22. namespace windows {
  23. /// Provides stream-oriented handle functionality.
  24. /**
  25. * The windows::basic_stream_handle class provides asynchronous and blocking
  26. * stream-oriented handle functionality.
  27. *
  28. * @par Thread Safety
  29. * @e Distinct @e objects: Safe.@n
  30. * @e Shared @e objects: Unsafe.
  31. *
  32. * @par Concepts:
  33. * AsyncReadStream, AsyncWriteStream, Stream, SyncReadStream, SyncWriteStream.
  34. */
  35. template <typename Executor = executor>
  36. class basic_stream_handle
  37. : public basic_overlapped_handle<Executor>
  38. {
  39. public:
  40. /// The type of the executor associated with the object.
  41. typedef Executor executor_type;
  42. /// Rebinds the handle type to another executor.
  43. template <typename Executor1>
  44. struct rebind_executor
  45. {
  46. /// The handle type when rebound to the specified executor.
  47. typedef basic_stream_handle<Executor1> other;
  48. };
  49. /// The native representation of a handle.
  50. #if defined(GENERATING_DOCUMENTATION)
  51. typedef implementation_defined native_handle_type;
  52. #else
  53. typedef boost::asio::detail::win_iocp_handle_service::native_handle_type
  54. native_handle_type;
  55. #endif
  56. /// Construct a stream handle without opening it.
  57. /**
  58. * This constructor creates a stream handle without opening it.
  59. *
  60. * @param ex The I/O executor that the stream handle will use, by default, to
  61. * dispatch handlers for any asynchronous operations performed on the stream
  62. * handle.
  63. */
  64. explicit basic_stream_handle(const executor_type& ex)
  65. : basic_overlapped_handle<Executor>(ex)
  66. {
  67. }
  68. /// Construct a stream handle without opening it.
  69. /**
  70. * This constructor creates a stream handle without opening it. The handle
  71. * needs to be opened or assigned before data can be sent or received on it.
  72. *
  73. * @param context An execution context which provides the I/O executor that
  74. * the stream handle will use, by default, to dispatch handlers for any
  75. * asynchronous operations performed on the stream handle.
  76. */
  77. template <typename ExecutionContext>
  78. explicit basic_stream_handle(ExecutionContext& context,
  79. typename enable_if<
  80. is_convertible<ExecutionContext&, execution_context&>::value,
  81. basic_stream_handle
  82. >::type* = 0)
  83. : basic_overlapped_handle<Executor>(context)
  84. {
  85. }
  86. /// Construct a stream handle on an existing native handle.
  87. /**
  88. * This constructor creates a stream handle object to hold an existing native
  89. * handle.
  90. *
  91. * @param ex The I/O executor that the stream handle will use, by default, to
  92. * dispatch handlers for any asynchronous operations performed on the stream
  93. * handle.
  94. *
  95. * @param handle The new underlying handle implementation.
  96. *
  97. * @throws boost::system::system_error Thrown on failure.
  98. */
  99. basic_stream_handle(const executor_type& ex, const native_handle_type& handle)
  100. : basic_overlapped_handle<Executor>(ex, handle)
  101. {
  102. }
  103. /// Construct a stream handle on an existing native handle.
  104. /**
  105. * This constructor creates a stream handle object to hold an existing native
  106. * handle.
  107. *
  108. * @param context An execution context which provides the I/O executor that
  109. * the stream handle will use, by default, to dispatch handlers for any
  110. * asynchronous operations performed on the stream handle.
  111. *
  112. * @param handle The new underlying handle implementation.
  113. *
  114. * @throws boost::system::system_error Thrown on failure.
  115. */
  116. template <typename ExecutionContext>
  117. basic_stream_handle(ExecutionContext& context,
  118. const native_handle_type& handle,
  119. typename enable_if<
  120. is_convertible<ExecutionContext&, execution_context&>::value
  121. >::type* = 0)
  122. : basic_overlapped_handle<Executor>(context, handle)
  123. {
  124. }
  125. #if defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
  126. /// Move-construct a stream handle from another.
  127. /**
  128. * This constructor moves a stream handle from one object to another.
  129. *
  130. * @param other The other stream handle object from which the move
  131. * will occur.
  132. *
  133. * @note Following the move, the moved-from object is in the same state as if
  134. * constructed using the @c basic_stream_handle(const executor_type&)
  135. * constructor.
  136. */
  137. basic_stream_handle(basic_stream_handle&& other)
  138. : basic_overlapped_handle<Executor>(std::move(other))
  139. {
  140. }
  141. /// Move-assign a stream handle from another.
  142. /**
  143. * This assignment operator moves a stream handle from one object to
  144. * another.
  145. *
  146. * @param other The other stream handle object from which the move will occur.
  147. *
  148. * @note Following the move, the moved-from object is in the same state as if
  149. * constructed using the @c basic_stream_handle(const executor_type&)
  150. * constructor.
  151. */
  152. basic_stream_handle& operator=(basic_stream_handle&& other)
  153. {
  154. basic_overlapped_handle<Executor>::operator=(std::move(other));
  155. return *this;
  156. }
  157. #endif // defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
  158. /// Write some data to the handle.
  159. /**
  160. * This function is used to write data to the stream handle. The function call
  161. * will block until one or more bytes of the data has been written
  162. * successfully, or until an error occurs.
  163. *
  164. * @param buffers One or more data buffers to be written to the handle.
  165. *
  166. * @returns The number of bytes written.
  167. *
  168. * @throws boost::system::system_error Thrown on failure. An error code of
  169. * boost::asio::error::eof indicates that the connection was closed by the
  170. * peer.
  171. *
  172. * @note The write_some operation may not transmit all of the data to the
  173. * peer. Consider using the @ref write function if you need to ensure that
  174. * all data is written before the blocking operation completes.
  175. *
  176. * @par Example
  177. * To write a single data buffer use the @ref buffer function as follows:
  178. * @code
  179. * handle.write_some(boost::asio::buffer(data, size));
  180. * @endcode
  181. * See the @ref buffer documentation for information on writing multiple
  182. * buffers in one go, and how to use it with arrays, boost::array or
  183. * std::vector.
  184. */
  185. template <typename ConstBufferSequence>
  186. std::size_t write_some(const ConstBufferSequence& buffers)
  187. {
  188. boost::system::error_code ec;
  189. std::size_t s = this->impl_.get_service().write_some(
  190. this->impl_.get_implementation(), buffers, ec);
  191. boost::asio::detail::throw_error(ec, "write_some");
  192. return s;
  193. }
  194. /// Write some data to the handle.
  195. /**
  196. * This function is used to write data to the stream handle. The function call
  197. * will block until one or more bytes of the data has been written
  198. * successfully, or until an error occurs.
  199. *
  200. * @param buffers One or more data buffers to be written to the handle.
  201. *
  202. * @param ec Set to indicate what error occurred, if any.
  203. *
  204. * @returns The number of bytes written. Returns 0 if an error occurred.
  205. *
  206. * @note The write_some operation may not transmit all of the data to the
  207. * peer. Consider using the @ref write function if you need to ensure that
  208. * all data is written before the blocking operation completes.
  209. */
  210. template <typename ConstBufferSequence>
  211. std::size_t write_some(const ConstBufferSequence& buffers,
  212. boost::system::error_code& ec)
  213. {
  214. return this->impl_.get_service().write_some(
  215. this->impl_.get_implementation(), buffers, ec);
  216. }
  217. /// Start an asynchronous write.
  218. /**
  219. * This function is used to asynchronously write data to the stream handle.
  220. * The function call always returns immediately.
  221. *
  222. * @param buffers One or more data buffers to be written to the handle.
  223. * Although the buffers object may be copied as necessary, ownership of the
  224. * underlying memory blocks is retained by the caller, which must guarantee
  225. * that they remain valid until the handler is called.
  226. *
  227. * @param handler The handler to be called when the write operation completes.
  228. * Copies will be made of the handler as required. The function signature of
  229. * the handler must be:
  230. * @code void handler(
  231. * const boost::system::error_code& error, // Result of operation.
  232. * std::size_t bytes_transferred // Number of bytes written.
  233. * ); @endcode
  234. * Regardless of whether the asynchronous operation completes immediately or
  235. * not, the handler will not be invoked from within this function. On
  236. * immediate completion, invocation of the handler will be performed in a
  237. * manner equivalent to using boost::asio::post().
  238. *
  239. * @note The write operation may not transmit all of the data to the peer.
  240. * Consider using the @ref async_write function if you need to ensure that all
  241. * data is written before the asynchronous operation completes.
  242. *
  243. * @par Example
  244. * To write a single data buffer use the @ref buffer function as follows:
  245. * @code
  246. * handle.async_write_some(boost::asio::buffer(data, size), handler);
  247. * @endcode
  248. * See the @ref buffer documentation for information on writing multiple
  249. * buffers in one go, and how to use it with arrays, boost::array or
  250. * std::vector.
  251. */
  252. template <typename ConstBufferSequence,
  253. BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code,
  254. std::size_t)) WriteHandler
  255. BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
  256. BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(WriteHandler,
  257. void (boost::system::error_code, std::size_t))
  258. async_write_some(const ConstBufferSequence& buffers,
  259. BOOST_ASIO_MOVE_ARG(WriteHandler) handler
  260. BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(executor_type))
  261. {
  262. return async_initiate<WriteHandler,
  263. void (boost::system::error_code, std::size_t)>(
  264. initiate_async_write_some(this), handler, buffers);
  265. }
  266. /// Read some data from the handle.
  267. /**
  268. * This function is used to read data from the stream handle. The function
  269. * call will block until one or more bytes of data has been read successfully,
  270. * or until an error occurs.
  271. *
  272. * @param buffers One or more buffers into which the data will be read.
  273. *
  274. * @returns The number of bytes read.
  275. *
  276. * @throws boost::system::system_error Thrown on failure. An error code of
  277. * boost::asio::error::eof indicates that the connection was closed by the
  278. * peer.
  279. *
  280. * @note The read_some operation may not read all of the requested number of
  281. * bytes. Consider using the @ref read function if you need to ensure that
  282. * the requested amount of data is read before the blocking operation
  283. * completes.
  284. *
  285. * @par Example
  286. * To read into a single data buffer use the @ref buffer function as follows:
  287. * @code
  288. * handle.read_some(boost::asio::buffer(data, size));
  289. * @endcode
  290. * See the @ref buffer documentation for information on reading into multiple
  291. * buffers in one go, and how to use it with arrays, boost::array or
  292. * std::vector.
  293. */
  294. template <typename MutableBufferSequence>
  295. std::size_t read_some(const MutableBufferSequence& buffers)
  296. {
  297. boost::system::error_code ec;
  298. std::size_t s = this->impl_.get_service().read_some(
  299. this->impl_.get_implementation(), buffers, ec);
  300. boost::asio::detail::throw_error(ec, "read_some");
  301. return s;
  302. }
  303. /// Read some data from the handle.
  304. /**
  305. * This function is used to read data from the stream handle. The function
  306. * call will block until one or more bytes of data has been read successfully,
  307. * or until an error occurs.
  308. *
  309. * @param buffers One or more buffers into which the data will be read.
  310. *
  311. * @param ec Set to indicate what error occurred, if any.
  312. *
  313. * @returns The number of bytes read. Returns 0 if an error occurred.
  314. *
  315. * @note The read_some operation may not read all of the requested number of
  316. * bytes. Consider using the @ref read function if you need to ensure that
  317. * the requested amount of data is read before the blocking operation
  318. * completes.
  319. */
  320. template <typename MutableBufferSequence>
  321. std::size_t read_some(const MutableBufferSequence& buffers,
  322. boost::system::error_code& ec)
  323. {
  324. return this->impl_.get_service().read_some(
  325. this->impl_.get_implementation(), buffers, ec);
  326. }
  327. /// Start an asynchronous read.
  328. /**
  329. * This function is used to asynchronously read data from the stream handle.
  330. * The function call always returns immediately.
  331. *
  332. * @param buffers One or more buffers into which the data will be read.
  333. * Although the buffers object may be copied as necessary, ownership of the
  334. * underlying memory blocks is retained by the caller, which must guarantee
  335. * that they remain valid until the handler is called.
  336. *
  337. * @param handler The handler to be called when the read operation completes.
  338. * Copies will be made of the handler as required. The function signature of
  339. * the handler must be:
  340. * @code void handler(
  341. * const boost::system::error_code& error, // Result of operation.
  342. * std::size_t bytes_transferred // Number of bytes read.
  343. * ); @endcode
  344. * Regardless of whether the asynchronous operation completes immediately or
  345. * not, the handler will not be invoked from within this function. On
  346. * immediate completion, invocation of the handler will be performed in a
  347. * manner equivalent to using boost::asio::post().
  348. *
  349. * @note The read operation may not read all of the requested number of bytes.
  350. * Consider using the @ref async_read function if you need to ensure that the
  351. * requested amount of data is read before the asynchronous operation
  352. * completes.
  353. *
  354. * @par Example
  355. * To read into a single data buffer use the @ref buffer function as follows:
  356. * @code
  357. * handle.async_read_some(boost::asio::buffer(data, size), handler);
  358. * @endcode
  359. * See the @ref buffer documentation for information on reading into multiple
  360. * buffers in one go, and how to use it with arrays, boost::array or
  361. * std::vector.
  362. */
  363. template <typename MutableBufferSequence,
  364. BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code,
  365. std::size_t)) ReadHandler
  366. BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
  367. BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(ReadHandler,
  368. void (boost::system::error_code, std::size_t))
  369. async_read_some(const MutableBufferSequence& buffers,
  370. BOOST_ASIO_MOVE_ARG(ReadHandler) handler
  371. BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(executor_type))
  372. {
  373. return async_initiate<ReadHandler,
  374. void (boost::system::error_code, std::size_t)>(
  375. initiate_async_read_some(this), handler, buffers);
  376. }
  377. private:
  378. class initiate_async_write_some
  379. {
  380. public:
  381. typedef Executor executor_type;
  382. explicit initiate_async_write_some(basic_stream_handle* self)
  383. : self_(self)
  384. {
  385. }
  386. executor_type get_executor() const BOOST_ASIO_NOEXCEPT
  387. {
  388. return self_->get_executor();
  389. }
  390. template <typename WriteHandler, typename ConstBufferSequence>
  391. void operator()(BOOST_ASIO_MOVE_ARG(WriteHandler) handler,
  392. const ConstBufferSequence& buffers) const
  393. {
  394. // If you get an error on the following line it means that your handler
  395. // does not meet the documented type requirements for a WriteHandler.
  396. BOOST_ASIO_WRITE_HANDLER_CHECK(WriteHandler, handler) type_check;
  397. detail::non_const_lvalue<WriteHandler> handler2(handler);
  398. self_->impl_.get_service().async_write_some(
  399. self_->impl_.get_implementation(), buffers, handler2.value,
  400. self_->impl_.get_implementation_executor());
  401. }
  402. private:
  403. basic_stream_handle* self_;
  404. };
  405. class initiate_async_read_some
  406. {
  407. public:
  408. typedef Executor executor_type;
  409. explicit initiate_async_read_some(basic_stream_handle* self)
  410. : self_(self)
  411. {
  412. }
  413. executor_type get_executor() const BOOST_ASIO_NOEXCEPT
  414. {
  415. return self_->get_executor();
  416. }
  417. template <typename ReadHandler, typename MutableBufferSequence>
  418. void operator()(BOOST_ASIO_MOVE_ARG(ReadHandler) handler,
  419. const MutableBufferSequence& buffers) const
  420. {
  421. // If you get an error on the following line it means that your handler
  422. // does not meet the documented type requirements for a ReadHandler.
  423. BOOST_ASIO_READ_HANDLER_CHECK(ReadHandler, handler) type_check;
  424. detail::non_const_lvalue<ReadHandler> handler2(handler);
  425. self_->impl_.get_service().async_read_some(
  426. self_->impl_.get_implementation(), buffers, handler2.value,
  427. self_->impl_.get_implementation_executor());
  428. }
  429. private:
  430. basic_stream_handle* self_;
  431. };
  432. };
  433. } // namespace windows
  434. } // namespace asio
  435. } // namespace boost
  436. #include <boost/asio/detail/pop_options.hpp>
  437. #endif // defined(BOOST_ASIO_HAS_WINDOWS_STREAM_HANDLE)
  438. // || defined(GENERATING_DOCUMENTATION)
  439. #endif // BOOST_ASIO_WINDOWS_BASIC_STREAM_HANDLE_HPP