basic_overlapped_handle.hpp 12 KB

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