basic_object_handle.hpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. //
  2. // windows/basic_object_handle.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2019 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  6. // Copyright (c) 2011 Boris Schaeling (boris@highscore.de)
  7. //
  8. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  9. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  10. //
  11. #ifndef BOOST_ASIO_WINDOWS_BASIC_OBJECT_HANDLE_HPP
  12. #define BOOST_ASIO_WINDOWS_BASIC_OBJECT_HANDLE_HPP
  13. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  14. # pragma once
  15. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  16. #include <boost/asio/detail/config.hpp>
  17. #if defined(BOOST_ASIO_HAS_WINDOWS_OBJECT_HANDLE) \
  18. || defined(GENERATING_DOCUMENTATION)
  19. #include <boost/asio/async_result.hpp>
  20. #include <boost/asio/detail/io_object_impl.hpp>
  21. #include <boost/asio/detail/throw_error.hpp>
  22. #include <boost/asio/detail/win_object_handle_service.hpp>
  23. #include <boost/asio/error.hpp>
  24. #include <boost/asio/execution_context.hpp>
  25. #include <boost/asio/executor.hpp>
  26. #if defined(BOOST_ASIO_HAS_MOVE)
  27. # include <utility>
  28. #endif // defined(BOOST_ASIO_HAS_MOVE)
  29. #include <boost/asio/detail/push_options.hpp>
  30. namespace boost {
  31. namespace asio {
  32. namespace windows {
  33. /// Provides object-oriented handle functionality.
  34. /**
  35. * The windows::basic_object_handle class provides asynchronous and blocking
  36. * object-oriented handle functionality.
  37. *
  38. * @par Thread Safety
  39. * @e Distinct @e objects: Safe.@n
  40. * @e Shared @e objects: Unsafe.
  41. */
  42. template <typename Executor = executor>
  43. class basic_object_handle
  44. {
  45. public:
  46. /// The type of the executor associated with the object.
  47. typedef Executor executor_type;
  48. /// Rebinds the handle type to another executor.
  49. template <typename Executor1>
  50. struct rebind_executor
  51. {
  52. /// The handle type when rebound to the specified executor.
  53. typedef basic_object_handle<Executor1> other;
  54. };
  55. /// The native representation of a handle.
  56. #if defined(GENERATING_DOCUMENTATION)
  57. typedef implementation_defined native_handle_type;
  58. #else
  59. typedef boost::asio::detail::win_object_handle_service::native_handle_type
  60. native_handle_type;
  61. #endif
  62. /// An object handle is always the lowest layer.
  63. typedef basic_object_handle lowest_layer_type;
  64. /// Construct an object handle without opening it.
  65. /**
  66. * This constructor creates an object handle without opening it.
  67. *
  68. * @param ex The I/O executor that the object handle will use, by default, to
  69. * dispatch handlers for any asynchronous operations performed on the
  70. * object handle.
  71. */
  72. explicit basic_object_handle(const executor_type& ex)
  73. : impl_(ex)
  74. {
  75. }
  76. /// Construct an object handle without opening it.
  77. /**
  78. * This constructor creates an object handle without opening it.
  79. *
  80. * @param context An execution context which provides the I/O executor that
  81. * the object handle will use, by default, to dispatch handlers for any
  82. * asynchronous operations performed on the object handle.
  83. */
  84. template <typename ExecutionContext>
  85. explicit basic_object_handle(ExecutionContext& context,
  86. typename enable_if<
  87. is_convertible<ExecutionContext&, execution_context&>::value,
  88. basic_object_handle
  89. >::type* = 0)
  90. : impl_(context)
  91. {
  92. }
  93. /// Construct an object handle on an existing native handle.
  94. /**
  95. * This constructor creates an object handle object to hold an existing native
  96. * handle.
  97. *
  98. * @param ex The I/O executor that the object handle will use, by default, to
  99. * dispatch handlers for any asynchronous operations performed on the
  100. * object handle.
  101. *
  102. * @param native_handle The new underlying handle implementation.
  103. *
  104. * @throws boost::system::system_error Thrown on failure.
  105. */
  106. basic_object_handle(const executor_type& ex,
  107. const native_handle_type& native_handle)
  108. : impl_(ex)
  109. {
  110. boost::system::error_code ec;
  111. impl_.get_service().assign(impl_.get_implementation(), native_handle, ec);
  112. boost::asio::detail::throw_error(ec, "assign");
  113. }
  114. /// Construct an object handle on an existing native handle.
  115. /**
  116. * This constructor creates an object handle object to hold an existing native
  117. * handle.
  118. *
  119. * @param context An execution context which provides the I/O executor that
  120. * the object handle will use, by default, to dispatch handlers for any
  121. * asynchronous operations performed on the object handle.
  122. *
  123. * @param native_handle The new underlying handle implementation.
  124. *
  125. * @throws boost::system::system_error Thrown on failure.
  126. */
  127. template <typename ExecutionContext>
  128. basic_object_handle(ExecutionContext& context,
  129. const native_handle_type& native_handle,
  130. typename enable_if<
  131. is_convertible<ExecutionContext&, execution_context&>::value
  132. >::type* = 0)
  133. : impl_(context)
  134. {
  135. boost::system::error_code ec;
  136. impl_.get_service().assign(impl_.get_implementation(), native_handle, ec);
  137. boost::asio::detail::throw_error(ec, "assign");
  138. }
  139. #if defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
  140. /// Move-construct an object handle from another.
  141. /**
  142. * This constructor moves an object handle from one object to another.
  143. *
  144. * @param other The other object handle object from which the move will
  145. * occur.
  146. *
  147. * @note Following the move, the moved-from object is in the same state as if
  148. * constructed using the @c basic_object_handle(const executor_type&)
  149. * constructor.
  150. */
  151. basic_object_handle(basic_object_handle&& other)
  152. : impl_(std::move(other.impl_))
  153. {
  154. }
  155. /// Move-assign an object handle from another.
  156. /**
  157. * This assignment operator moves an object handle from one object to another.
  158. *
  159. * @param other The other object handle object from which the move will
  160. * occur.
  161. *
  162. * @note Following the move, the moved-from object is in the same state as if
  163. * constructed using the @c basic_object_handle(const executor_type&)
  164. * constructor.
  165. */
  166. basic_object_handle& operator=(basic_object_handle&& other)
  167. {
  168. impl_ = std::move(other.impl_);
  169. return *this;
  170. }
  171. #endif // defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
  172. /// Get the executor associated with the object.
  173. executor_type get_executor() BOOST_ASIO_NOEXCEPT
  174. {
  175. return impl_.get_executor();
  176. }
  177. /// Get a reference to the lowest layer.
  178. /**
  179. * This function returns a reference to the lowest layer in a stack of
  180. * layers. Since an object handle cannot contain any further layers, it simply
  181. * returns a reference to itself.
  182. *
  183. * @return A reference to the lowest layer in the stack of layers. Ownership
  184. * is not transferred to the caller.
  185. */
  186. lowest_layer_type& lowest_layer()
  187. {
  188. return *this;
  189. }
  190. /// Get a const reference to the lowest layer.
  191. /**
  192. * This function returns a const reference to the lowest layer in a stack of
  193. * layers. Since an object handle cannot contain any further layers, it simply
  194. * returns a reference to itself.
  195. *
  196. * @return A const reference to the lowest layer in the stack of layers.
  197. * Ownership is not transferred to the caller.
  198. */
  199. const lowest_layer_type& lowest_layer() const
  200. {
  201. return *this;
  202. }
  203. /// Assign an existing native handle to the handle.
  204. /*
  205. * This function opens the handle to hold an existing native handle.
  206. *
  207. * @param handle A native handle.
  208. *
  209. * @throws boost::system::system_error Thrown on failure.
  210. */
  211. void assign(const native_handle_type& handle)
  212. {
  213. boost::system::error_code ec;
  214. impl_.get_service().assign(impl_.get_implementation(), handle, ec);
  215. boost::asio::detail::throw_error(ec, "assign");
  216. }
  217. /// Assign an existing native handle to the handle.
  218. /*
  219. * This function opens the handle to hold an existing native handle.
  220. *
  221. * @param handle A native handle.
  222. *
  223. * @param ec Set to indicate what error occurred, if any.
  224. */
  225. BOOST_ASIO_SYNC_OP_VOID assign(const native_handle_type& handle,
  226. boost::system::error_code& ec)
  227. {
  228. impl_.get_service().assign(impl_.get_implementation(), handle, ec);
  229. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  230. }
  231. /// Determine whether the handle is open.
  232. bool is_open() const
  233. {
  234. return impl_.get_service().is_open(impl_.get_implementation());
  235. }
  236. /// Close the handle.
  237. /**
  238. * This function is used to close the handle. Any asynchronous read or write
  239. * operations will be cancelled immediately, and will complete with the
  240. * boost::asio::error::operation_aborted error.
  241. *
  242. * @throws boost::system::system_error Thrown on failure.
  243. */
  244. void close()
  245. {
  246. boost::system::error_code ec;
  247. impl_.get_service().close(impl_.get_implementation(), ec);
  248. boost::asio::detail::throw_error(ec, "close");
  249. }
  250. /// Close the handle.
  251. /**
  252. * This function is used to close the handle. Any asynchronous read or write
  253. * operations will be cancelled immediately, and will complete with the
  254. * boost::asio::error::operation_aborted error.
  255. *
  256. * @param ec Set to indicate what error occurred, if any.
  257. */
  258. BOOST_ASIO_SYNC_OP_VOID close(boost::system::error_code& ec)
  259. {
  260. impl_.get_service().close(impl_.get_implementation(), ec);
  261. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  262. }
  263. /// Get the native handle representation.
  264. /**
  265. * This function may be used to obtain the underlying representation of the
  266. * handle. This is intended to allow access to native handle functionality
  267. * that is not otherwise provided.
  268. */
  269. native_handle_type native_handle()
  270. {
  271. return impl_.get_service().native_handle(impl_.get_implementation());
  272. }
  273. /// Cancel all asynchronous operations associated with the handle.
  274. /**
  275. * This function causes all outstanding asynchronous read or write operations
  276. * to finish immediately, and the handlers for cancelled operations will be
  277. * passed the boost::asio::error::operation_aborted error.
  278. *
  279. * @throws boost::system::system_error Thrown on failure.
  280. */
  281. void cancel()
  282. {
  283. boost::system::error_code ec;
  284. impl_.get_service().cancel(impl_.get_implementation(), ec);
  285. boost::asio::detail::throw_error(ec, "cancel");
  286. }
  287. /// Cancel all asynchronous operations associated with the handle.
  288. /**
  289. * This function causes all outstanding asynchronous read or write operations
  290. * to finish immediately, and the handlers for cancelled operations will be
  291. * passed the boost::asio::error::operation_aborted error.
  292. *
  293. * @param ec Set to indicate what error occurred, if any.
  294. */
  295. BOOST_ASIO_SYNC_OP_VOID cancel(boost::system::error_code& ec)
  296. {
  297. impl_.get_service().cancel(impl_.get_implementation(), ec);
  298. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  299. }
  300. /// Perform a blocking wait on the object handle.
  301. /**
  302. * This function is used to wait for the object handle to be set to the
  303. * signalled state. This function blocks and does not return until the object
  304. * handle has been set to the signalled state.
  305. *
  306. * @throws boost::system::system_error Thrown on failure.
  307. */
  308. void wait()
  309. {
  310. boost::system::error_code ec;
  311. impl_.get_service().wait(impl_.get_implementation(), ec);
  312. boost::asio::detail::throw_error(ec, "wait");
  313. }
  314. /// Perform a blocking wait on the object handle.
  315. /**
  316. * This function is used to wait for the object handle to be set to the
  317. * signalled state. This function blocks and does not return until the object
  318. * handle has been set to the signalled state.
  319. *
  320. * @param ec Set to indicate what error occurred, if any.
  321. */
  322. void wait(boost::system::error_code& ec)
  323. {
  324. impl_.get_service().wait(impl_.get_implementation(), ec);
  325. }
  326. /// Start an asynchronous wait on the object handle.
  327. /**
  328. * This function is be used to initiate an asynchronous wait against the
  329. * object handle. It always returns immediately.
  330. *
  331. * @param handler The handler to be called when the object handle is set to
  332. * the signalled state. Copies will be made of the handler as required. The
  333. * function signature of the handler must be:
  334. * @code void handler(
  335. * const boost::system::error_code& error // Result of operation.
  336. * ); @endcode
  337. * Regardless of whether the asynchronous operation completes immediately or
  338. * not, the handler will not be invoked from within this function. On
  339. * immediate completion, invocation of the handler will be performed in a
  340. * manner equivalent to using boost::asio::post().
  341. */
  342. template <
  343. BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code))
  344. WaitHandler BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
  345. BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(WaitHandler,
  346. void (boost::system::error_code))
  347. async_wait(
  348. BOOST_ASIO_MOVE_ARG(WaitHandler) handler
  349. BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(executor_type))
  350. {
  351. return async_initiate<WaitHandler, void (boost::system::error_code)>(
  352. initiate_async_wait(this), handler);
  353. }
  354. private:
  355. // Disallow copying and assignment.
  356. basic_object_handle(const basic_object_handle&) BOOST_ASIO_DELETED;
  357. basic_object_handle& operator=(const basic_object_handle&) BOOST_ASIO_DELETED;
  358. class initiate_async_wait
  359. {
  360. public:
  361. typedef Executor executor_type;
  362. explicit initiate_async_wait(basic_object_handle* self)
  363. : self_(self)
  364. {
  365. }
  366. executor_type get_executor() const BOOST_ASIO_NOEXCEPT
  367. {
  368. return self_->get_executor();
  369. }
  370. template <typename WaitHandler>
  371. void operator()(BOOST_ASIO_MOVE_ARG(WaitHandler) handler) const
  372. {
  373. // If you get an error on the following line it means that your handler
  374. // does not meet the documented type requirements for a WaitHandler.
  375. BOOST_ASIO_WAIT_HANDLER_CHECK(WaitHandler, handler) type_check;
  376. detail::non_const_lvalue<WaitHandler> handler2(handler);
  377. self_->impl_.get_service().async_wait(
  378. self_->impl_.get_implementation(), handler2.value,
  379. self_->impl_.get_implementation_executor());
  380. }
  381. private:
  382. basic_object_handle* self_;
  383. };
  384. boost::asio::detail::io_object_impl<
  385. boost::asio::detail::win_object_handle_service, Executor> impl_;
  386. };
  387. } // namespace windows
  388. } // namespace asio
  389. } // namespace boost
  390. #include <boost/asio/detail/pop_options.hpp>
  391. #endif // defined(BOOST_ASIO_HAS_WINDOWS_OBJECT_HANDLE)
  392. // || defined(GENERATING_DOCUMENTATION)
  393. #endif // BOOST_ASIO_WINDOWS_BASIC_OBJECT_HANDLE_HPP