basic_overlapped_handle.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. //
  2. // windows/basic_overlapped_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_OVERLAPPED_HANDLE_HPP
  11. #define BOOST_ASIO_WINDOWS_BASIC_OVERLAPPED_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_RANDOM_ACCESS_HANDLE) \
  17. || defined(BOOST_ASIO_HAS_WINDOWS_STREAM_HANDLE) \
  18. || defined(GENERATING_DOCUMENTATION)
  19. #include <cstddef>
  20. #include <boost/asio/async_result.hpp>
  21. #include <boost/asio/detail/io_object_impl.hpp>
  22. #include <boost/asio/detail/throw_error.hpp>
  23. #include <boost/asio/detail/win_iocp_handle_service.hpp>
  24. #include <boost/asio/error.hpp>
  25. #include <boost/asio/execution_context.hpp>
  26. #include <boost/asio/executor.hpp>
  27. #if defined(BOOST_ASIO_HAS_MOVE)
  28. # include <utility>
  29. #endif // defined(BOOST_ASIO_HAS_MOVE)
  30. #include <boost/asio/detail/push_options.hpp>
  31. namespace boost {
  32. namespace asio {
  33. namespace windows {
  34. /// Provides Windows handle functionality for objects that support
  35. /// overlapped I/O.
  36. /**
  37. * The windows::overlapped_handle class provides the ability to wrap a Windows
  38. * handle. The underlying object referred to by the handle must support
  39. * overlapped I/O.
  40. *
  41. * @par Thread Safety
  42. * @e Distinct @e objects: Safe.@n
  43. * @e Shared @e objects: Unsafe.
  44. */
  45. template <typename Executor = executor>
  46. class basic_overlapped_handle
  47. {
  48. public:
  49. /// The type of the executor associated with the object.
  50. typedef Executor executor_type;
  51. /// The native representation of a handle.
  52. #if defined(GENERATING_DOCUMENTATION)
  53. typedef implementation_defined native_handle_type;
  54. #else
  55. typedef boost::asio::detail::win_iocp_handle_service::native_handle_type
  56. native_handle_type;
  57. #endif
  58. /// An overlapped_handle is always the lowest layer.
  59. typedef basic_overlapped_handle lowest_layer_type;
  60. /// Construct an overlapped handle without opening it.
  61. /**
  62. * This constructor creates an overlapped handle without opening it.
  63. *
  64. * @param ex The I/O executor that the overlapped handle will use, by default,
  65. * to dispatch handlers for any asynchronous operations performed on the
  66. * overlapped handle.
  67. */
  68. explicit basic_overlapped_handle(const executor_type& ex)
  69. : impl_(ex)
  70. {
  71. }
  72. /// Construct an overlapped handle without opening it.
  73. /**
  74. * This constructor creates an overlapped handle without opening it.
  75. *
  76. * @param context An execution context which provides the I/O executor that
  77. * the overlapped handle will use, by default, to dispatch handlers for any
  78. * asynchronous operations performed on the overlapped handle.
  79. */
  80. template <typename ExecutionContext>
  81. explicit basic_overlapped_handle(ExecutionContext& context,
  82. typename enable_if<
  83. is_convertible<ExecutionContext&, execution_context&>::value,
  84. basic_overlapped_handle
  85. >::type* = 0)
  86. : impl_(context)
  87. {
  88. }
  89. /// Construct an overlapped handle on an existing native handle.
  90. /**
  91. * This constructor creates an overlapped handle object to hold an existing
  92. * native handle.
  93. *
  94. * @param ex The I/O executor that the overlapped handle will use, by default,
  95. * to dispatch handlers for any asynchronous operations performed on the
  96. * overlapped handle.
  97. *
  98. * @param native_handle The new underlying handle implementation.
  99. *
  100. * @throws boost::system::system_error Thrown on failure.
  101. */
  102. basic_overlapped_handle(const executor_type& ex,
  103. const native_handle_type& native_handle)
  104. : impl_(ex)
  105. {
  106. boost::system::error_code ec;
  107. impl_.get_service().assign(impl_.get_implementation(), native_handle, ec);
  108. boost::asio::detail::throw_error(ec, "assign");
  109. }
  110. /// Construct an overlapped handle on an existing native handle.
  111. /**
  112. * This constructor creates an overlapped handle object to hold an existing
  113. * native handle.
  114. *
  115. * @param context An execution context which provides the I/O executor that
  116. * the overlapped handle will use, by default, to dispatch handlers for any
  117. * asynchronous operations performed on the overlapped handle.
  118. *
  119. * @param native_handle The new underlying handle implementation.
  120. *
  121. * @throws boost::system::system_error Thrown on failure.
  122. */
  123. template <typename ExecutionContext>
  124. basic_overlapped_handle(ExecutionContext& context,
  125. const native_handle_type& native_handle,
  126. typename enable_if<
  127. is_convertible<ExecutionContext&, execution_context&>::value
  128. >::type* = 0)
  129. : impl_(context)
  130. {
  131. boost::system::error_code ec;
  132. impl_.get_service().assign(impl_.get_implementation(), native_handle, ec);
  133. boost::asio::detail::throw_error(ec, "assign");
  134. }
  135. #if defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
  136. /// Move-construct an overlapped handle from another.
  137. /**
  138. * This constructor moves a handle from one object to another.
  139. *
  140. * @param other The other overlapped handle object from which the move will
  141. * occur.
  142. *
  143. * @note Following the move, the moved-from object is in the same state as if
  144. * constructed using the @c overlapped_handle(const executor_type&)
  145. * constructor.
  146. */
  147. basic_overlapped_handle(basic_overlapped_handle&& other)
  148. : impl_(std::move(other.impl_))
  149. {
  150. }
  151. /// Move-assign an overlapped handle from another.
  152. /**
  153. * This assignment operator moves a handle from one object to another.
  154. *
  155. * @param other The other overlapped handle object from which the move will
  156. * occur.
  157. *
  158. * @note Following the move, the moved-from object is in the same state as if
  159. * constructed using the @c overlapped_handle(const executor_type&)
  160. * constructor.
  161. */
  162. basic_overlapped_handle& operator=(basic_overlapped_handle&& other)
  163. {
  164. impl_ = std::move(other.impl_);
  165. return *this;
  166. }
  167. #endif // defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
  168. /// Get the executor associated with the object.
  169. executor_type get_executor() BOOST_ASIO_NOEXCEPT
  170. {
  171. return impl_.get_executor();
  172. }
  173. /// Get a reference to the lowest layer.
  174. /**
  175. * This function returns a reference to the lowest layer in a stack of
  176. * layers. Since an overlapped_handle cannot contain any further layers, it
  177. * simply returns a reference to itself.
  178. *
  179. * @return A reference to the lowest layer in the stack of layers. Ownership
  180. * is not transferred to the caller.
  181. */
  182. lowest_layer_type& lowest_layer()
  183. {
  184. return *this;
  185. }
  186. /// Get a const reference to the lowest layer.
  187. /**
  188. * This function returns a const reference to the lowest layer in a stack of
  189. * layers. Since an overlapped_handle cannot contain any further layers, it
  190. * simply returns a reference to itself.
  191. *
  192. * @return A const reference to the lowest layer in the stack of layers.
  193. * Ownership is not transferred to the caller.
  194. */
  195. const lowest_layer_type& lowest_layer() const
  196. {
  197. return *this;
  198. }
  199. /// Assign an existing native handle to the handle.
  200. /*
  201. * This function opens the handle to hold an existing native handle.
  202. *
  203. * @param handle A native handle.
  204. *
  205. * @throws boost::system::system_error Thrown on failure.
  206. */
  207. void assign(const native_handle_type& handle)
  208. {
  209. boost::system::error_code ec;
  210. impl_.get_service().assign(impl_.get_implementation(), handle, ec);
  211. boost::asio::detail::throw_error(ec, "assign");
  212. }
  213. /// Assign an existing native handle to the handle.
  214. /*
  215. * This function opens the handle to hold an existing native handle.
  216. *
  217. * @param handle A native handle.
  218. *
  219. * @param ec Set to indicate what error occurred, if any.
  220. */
  221. BOOST_ASIO_SYNC_OP_VOID assign(const native_handle_type& handle,
  222. boost::system::error_code& ec)
  223. {
  224. impl_.get_service().assign(impl_.get_implementation(), handle, ec);
  225. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  226. }
  227. /// Determine whether the handle is open.
  228. bool is_open() const
  229. {
  230. return impl_.get_service().is_open(impl_.get_implementation());
  231. }
  232. /// Close the handle.
  233. /**
  234. * This function is used to close the handle. Any asynchronous read or write
  235. * operations will be cancelled immediately, and will complete with the
  236. * boost::asio::error::operation_aborted error.
  237. *
  238. * @throws boost::system::system_error Thrown on failure.
  239. */
  240. void close()
  241. {
  242. boost::system::error_code ec;
  243. impl_.get_service().close(impl_.get_implementation(), ec);
  244. boost::asio::detail::throw_error(ec, "close");
  245. }
  246. /// Close the handle.
  247. /**
  248. * This function is used to close the handle. Any asynchronous read or write
  249. * operations will be cancelled immediately, and will complete with the
  250. * boost::asio::error::operation_aborted error.
  251. *
  252. * @param ec Set to indicate what error occurred, if any.
  253. */
  254. BOOST_ASIO_SYNC_OP_VOID close(boost::system::error_code& ec)
  255. {
  256. impl_.get_service().close(impl_.get_implementation(), ec);
  257. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  258. }
  259. /// Get the native handle representation.
  260. /**
  261. * This function may be used to obtain the underlying representation of the
  262. * handle. This is intended to allow access to native handle functionality
  263. * that is not otherwise provided.
  264. */
  265. native_handle_type native_handle()
  266. {
  267. return impl_.get_service().native_handle(impl_.get_implementation());
  268. }
  269. /// Cancel all asynchronous operations associated with the handle.
  270. /**
  271. * This function causes all outstanding asynchronous read or write operations
  272. * to finish immediately, and the handlers for cancelled operations will be
  273. * passed the boost::asio::error::operation_aborted error.
  274. *
  275. * @throws boost::system::system_error Thrown on failure.
  276. */
  277. void cancel()
  278. {
  279. boost::system::error_code ec;
  280. impl_.get_service().cancel(impl_.get_implementation(), ec);
  281. boost::asio::detail::throw_error(ec, "cancel");
  282. }
  283. /// Cancel all asynchronous operations associated with the handle.
  284. /**
  285. * This function causes all outstanding asynchronous read or write operations
  286. * to finish immediately, and the handlers for cancelled operations will be
  287. * passed the boost::asio::error::operation_aborted error.
  288. *
  289. * @param ec Set to indicate what error occurred, if any.
  290. */
  291. BOOST_ASIO_SYNC_OP_VOID cancel(boost::system::error_code& ec)
  292. {
  293. impl_.get_service().cancel(impl_.get_implementation(), ec);
  294. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  295. }
  296. protected:
  297. /// Protected destructor to prevent deletion through this type.
  298. /**
  299. * This function destroys the handle, cancelling any outstanding asynchronous
  300. * wait operations associated with the handle as if by calling @c cancel.
  301. */
  302. ~basic_overlapped_handle()
  303. {
  304. }
  305. boost::asio::detail::io_object_impl<
  306. boost::asio::detail::win_iocp_handle_service, Executor> impl_;
  307. private:
  308. // Disallow copying and assignment.
  309. basic_overlapped_handle(const basic_overlapped_handle&) BOOST_ASIO_DELETED;
  310. basic_overlapped_handle& operator=(
  311. const basic_overlapped_handle&) BOOST_ASIO_DELETED;
  312. };
  313. } // namespace windows
  314. } // namespace asio
  315. } // namespace boost
  316. #include <boost/asio/detail/pop_options.hpp>
  317. #endif // defined(BOOST_ASIO_HAS_WINDOWS_RANDOM_ACCESS_HANDLE)
  318. // || defined(BOOST_ASIO_HAS_WINDOWS_STREAM_HANDLE)
  319. // || defined(GENERATING_DOCUMENTATION)
  320. #endif // BOOST_ASIO_WINDOWS_BASIC_OVERLAPPED_HANDLE_HPP