object_handle.hpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. //
  2. // windows/object_handle.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2018 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_OBJECT_HANDLE_HPP
  12. #define BOOST_ASIO_WINDOWS_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/basic_io_object.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/io_context.hpp>
  25. #if defined(BOOST_ASIO_HAS_MOVE)
  26. # include <utility>
  27. #endif // defined(BOOST_ASIO_HAS_MOVE)
  28. #if defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
  29. # include <boost/asio/windows/basic_object_handle.hpp>
  30. #endif // defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
  31. #define BOOST_ASIO_SVC_T boost::asio::detail::win_object_handle_service
  32. #include <boost/asio/detail/push_options.hpp>
  33. namespace boost {
  34. namespace asio {
  35. namespace windows {
  36. #if defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
  37. // Typedef for the typical usage of an object handle.
  38. typedef basic_object_handle<> object_handle;
  39. #else // defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
  40. /// Provides object-oriented handle functionality.
  41. /**
  42. * The windows::object_handle class provides asynchronous and blocking
  43. * object-oriented handle functionality.
  44. *
  45. * @par Thread Safety
  46. * @e Distinct @e objects: Safe.@n
  47. * @e Shared @e objects: Unsafe.
  48. */
  49. class object_handle
  50. : BOOST_ASIO_SVC_ACCESS basic_io_object<BOOST_ASIO_SVC_T>
  51. {
  52. public:
  53. /// The type of the executor associated with the object.
  54. typedef io_context::executor_type executor_type;
  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_SVC_T::native_handle_type native_handle_type;
  60. #endif
  61. /// An object_handle is always the lowest layer.
  62. typedef object_handle lowest_layer_type;
  63. /// Construct an object_handle without opening it.
  64. /**
  65. * This constructor creates an object handle without opening it.
  66. *
  67. * @param io_context The io_context object that the object handle will use to
  68. * dispatch handlers for any asynchronous operations performed on the handle.
  69. */
  70. explicit object_handle(boost::asio::io_context& io_context)
  71. : basic_io_object<BOOST_ASIO_SVC_T>(io_context)
  72. {
  73. }
  74. /// Construct an object_handle on an existing native handle.
  75. /**
  76. * This constructor creates an object handle object to hold an existing native
  77. * handle.
  78. *
  79. * @param io_context The io_context object that the object handle will use to
  80. * dispatch handlers for any asynchronous operations performed on the handle.
  81. *
  82. * @param native_handle The new underlying handle implementation.
  83. *
  84. * @throws boost::system::system_error Thrown on failure.
  85. */
  86. object_handle(boost::asio::io_context& io_context,
  87. const native_handle_type& native_handle)
  88. : basic_io_object<BOOST_ASIO_SVC_T>(io_context)
  89. {
  90. boost::system::error_code ec;
  91. this->get_service().assign(this->get_implementation(), native_handle, ec);
  92. boost::asio::detail::throw_error(ec, "assign");
  93. }
  94. #if defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
  95. /// Move-construct an object_handle from another.
  96. /**
  97. * This constructor moves an object handle from one object to another.
  98. *
  99. * @param other The other object_handle object from which the move will
  100. * occur.
  101. *
  102. * @note Following the move, the moved-from object is in the same state as if
  103. * constructed using the @c object_handle(io_context&) constructor.
  104. */
  105. object_handle(object_handle&& other)
  106. : basic_io_object<BOOST_ASIO_SVC_T>(std::move(other))
  107. {
  108. }
  109. /// Move-assign an object_handle from another.
  110. /**
  111. * This assignment operator moves an object handle from one object to another.
  112. *
  113. * @param other The other object_handle object from which the move will
  114. * occur.
  115. *
  116. * @note Following the move, the moved-from object is in the same state as if
  117. * constructed using the @c object_handle(io_context&) constructor.
  118. */
  119. object_handle& operator=(object_handle&& other)
  120. {
  121. basic_io_object<BOOST_ASIO_SVC_T>::operator=(std::move(other));
  122. return *this;
  123. }
  124. #endif // defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
  125. #if !defined(BOOST_ASIO_NO_DEPRECATED)
  126. /// (Deprecated: Use get_executor().) Get the io_context associated with the
  127. /// object.
  128. /**
  129. * This function may be used to obtain the io_context object that the I/O
  130. * object uses to dispatch handlers for asynchronous operations.
  131. *
  132. * @return A reference to the io_context object that the I/O object will use
  133. * to dispatch handlers. Ownership is not transferred to the caller.
  134. */
  135. boost::asio::io_context& get_io_context()
  136. {
  137. return basic_io_object<BOOST_ASIO_SVC_T>::get_io_context();
  138. }
  139. /// (Deprecated: Use get_executor().) Get the io_context associated with the
  140. /// object.
  141. /**
  142. * This function may be used to obtain the io_context object that the I/O
  143. * object uses to dispatch handlers for asynchronous operations.
  144. *
  145. * @return A reference to the io_context object that the I/O object will use
  146. * to dispatch handlers. Ownership is not transferred to the caller.
  147. */
  148. boost::asio::io_context& get_io_service()
  149. {
  150. return basic_io_object<BOOST_ASIO_SVC_T>::get_io_service();
  151. }
  152. #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
  153. /// Get the executor associated with the object.
  154. executor_type get_executor() BOOST_ASIO_NOEXCEPT
  155. {
  156. return basic_io_object<BOOST_ASIO_SVC_T>::get_executor();
  157. }
  158. /// Get a reference to the lowest layer.
  159. /**
  160. * This function returns a reference to the lowest layer in a stack of
  161. * layers. Since an object_handle cannot contain any further layers, it simply
  162. * returns a reference to itself.
  163. *
  164. * @return A reference to the lowest layer in the stack of layers. Ownership
  165. * is not transferred to the caller.
  166. */
  167. lowest_layer_type& lowest_layer()
  168. {
  169. return *this;
  170. }
  171. /// Get a const reference to the lowest layer.
  172. /**
  173. * This function returns a const reference to the lowest layer in a stack of
  174. * layers. Since an object_handle cannot contain any further layers, it simply
  175. * returns a reference to itself.
  176. *
  177. * @return A const reference to the lowest layer in the stack of layers.
  178. * Ownership is not transferred to the caller.
  179. */
  180. const lowest_layer_type& lowest_layer() const
  181. {
  182. return *this;
  183. }
  184. /// Assign an existing native handle to the handle.
  185. /*
  186. * This function opens the handle to hold an existing native handle.
  187. *
  188. * @param handle A native handle.
  189. *
  190. * @throws boost::system::system_error Thrown on failure.
  191. */
  192. void assign(const native_handle_type& handle)
  193. {
  194. boost::system::error_code ec;
  195. this->get_service().assign(this->get_implementation(), handle, ec);
  196. boost::asio::detail::throw_error(ec, "assign");
  197. }
  198. /// Assign an existing native handle to the handle.
  199. /*
  200. * This function opens the handle to hold an existing native handle.
  201. *
  202. * @param handle A native handle.
  203. *
  204. * @param ec Set to indicate what error occurred, if any.
  205. */
  206. BOOST_ASIO_SYNC_OP_VOID assign(const native_handle_type& handle,
  207. boost::system::error_code& ec)
  208. {
  209. this->get_service().assign(this->get_implementation(), handle, ec);
  210. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  211. }
  212. /// Determine whether the handle is open.
  213. bool is_open() const
  214. {
  215. return this->get_service().is_open(this->get_implementation());
  216. }
  217. /// Close the handle.
  218. /**
  219. * This function is used to close the handle. Any asynchronous read or write
  220. * operations will be cancelled immediately, and will complete with the
  221. * boost::asio::error::operation_aborted error.
  222. *
  223. * @throws boost::system::system_error Thrown on failure.
  224. */
  225. void close()
  226. {
  227. boost::system::error_code ec;
  228. this->get_service().close(this->get_implementation(), ec);
  229. boost::asio::detail::throw_error(ec, "close");
  230. }
  231. /// Close the handle.
  232. /**
  233. * This function is used to close the handle. Any asynchronous read or write
  234. * operations will be cancelled immediately, and will complete with the
  235. * boost::asio::error::operation_aborted error.
  236. *
  237. * @param ec Set to indicate what error occurred, if any.
  238. */
  239. BOOST_ASIO_SYNC_OP_VOID close(boost::system::error_code& ec)
  240. {
  241. this->get_service().close(this->get_implementation(), ec);
  242. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  243. }
  244. /// Get the native handle representation.
  245. /**
  246. * This function may be used to obtain the underlying representation of the
  247. * handle. This is intended to allow access to native handle functionality
  248. * that is not otherwise provided.
  249. */
  250. native_handle_type native_handle()
  251. {
  252. return this->get_service().native_handle(this->get_implementation());
  253. }
  254. /// Cancel all asynchronous operations associated with the handle.
  255. /**
  256. * This function causes all outstanding asynchronous read or write operations
  257. * to finish immediately, and the handlers for cancelled operations will be
  258. * passed the boost::asio::error::operation_aborted error.
  259. *
  260. * @throws boost::system::system_error Thrown on failure.
  261. */
  262. void cancel()
  263. {
  264. boost::system::error_code ec;
  265. this->get_service().cancel(this->get_implementation(), ec);
  266. boost::asio::detail::throw_error(ec, "cancel");
  267. }
  268. /// Cancel all asynchronous operations associated with the handle.
  269. /**
  270. * This function causes all outstanding asynchronous read or write operations
  271. * to finish immediately, and the handlers for cancelled operations will be
  272. * passed the boost::asio::error::operation_aborted error.
  273. *
  274. * @param ec Set to indicate what error occurred, if any.
  275. */
  276. BOOST_ASIO_SYNC_OP_VOID cancel(boost::system::error_code& ec)
  277. {
  278. this->get_service().cancel(this->get_implementation(), ec);
  279. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  280. }
  281. /// Perform a blocking wait on the object handle.
  282. /**
  283. * This function is used to wait for the object handle to be set to the
  284. * signalled state. This function blocks and does not return until the object
  285. * handle has been set to the signalled state.
  286. *
  287. * @throws boost::system::system_error Thrown on failure.
  288. */
  289. void wait()
  290. {
  291. boost::system::error_code ec;
  292. this->get_service().wait(this->get_implementation(), ec);
  293. boost::asio::detail::throw_error(ec, "wait");
  294. }
  295. /// Perform a blocking wait on the object handle.
  296. /**
  297. * This function is used to wait for the object handle to be set to the
  298. * signalled state. This function blocks and does not return until the object
  299. * handle has been set to the signalled state.
  300. *
  301. * @param ec Set to indicate what error occurred, if any.
  302. */
  303. void wait(boost::system::error_code& ec)
  304. {
  305. this->get_service().wait(this->get_implementation(), ec);
  306. }
  307. /// Start an asynchronous wait on the object handle.
  308. /**
  309. * This function is be used to initiate an asynchronous wait against the
  310. * object handle. It always returns immediately.
  311. *
  312. * @param handler The handler to be called when the object handle is set to
  313. * the signalled state. Copies will be made of the handler as required. The
  314. * function signature of the handler must be:
  315. * @code void handler(
  316. * const boost::system::error_code& error // Result of operation.
  317. * ); @endcode
  318. * Regardless of whether the asynchronous operation completes immediately or
  319. * not, the handler will not be invoked from within this function. Invocation
  320. * of the handler will be performed in a manner equivalent to using
  321. * boost::asio::io_context::post().
  322. */
  323. template <typename WaitHandler>
  324. BOOST_ASIO_INITFN_RESULT_TYPE(WaitHandler,
  325. void (boost::system::error_code))
  326. async_wait(BOOST_ASIO_MOVE_ARG(WaitHandler) handler)
  327. {
  328. boost::asio::async_completion<WaitHandler,
  329. void (boost::system::error_code)> init(handler);
  330. this->get_service().async_wait(this->get_implementation(),
  331. init.completion_handler);
  332. return init.result.get();
  333. }
  334. };
  335. #endif // defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
  336. } // namespace windows
  337. } // namespace asio
  338. } // namespace boost
  339. #include <boost/asio/detail/pop_options.hpp>
  340. #undef BOOST_ASIO_SVC_T
  341. #endif // defined(BOOST_ASIO_HAS_WINDOWS_OBJECT_HANDLE)
  342. // || defined(GENERATING_DOCUMENTATION)
  343. #endif // BOOST_ASIO_WINDOWS_OBJECT_HANDLE_HPP