overlapped_handle.hpp 11 KB

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