Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

basic_overlapped_handle.hpp 11KB

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