win_iocp_handle_service.hpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. //
  2. // detail/win_iocp_handle_service.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2019 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  6. // Copyright (c) 2008 Rep Invariant Systems, Inc. (info@repinvariant.com)
  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_DETAIL_WIN_IOCP_HANDLE_SERVICE_HPP
  12. #define BOOST_ASIO_DETAIL_WIN_IOCP_HANDLE_SERVICE_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_IOCP)
  18. #include <boost/asio/error.hpp>
  19. #include <boost/asio/execution_context.hpp>
  20. #include <boost/asio/detail/buffer_sequence_adapter.hpp>
  21. #include <boost/asio/detail/cstdint.hpp>
  22. #include <boost/asio/detail/handler_alloc_helpers.hpp>
  23. #include <boost/asio/detail/memory.hpp>
  24. #include <boost/asio/detail/mutex.hpp>
  25. #include <boost/asio/detail/operation.hpp>
  26. #include <boost/asio/detail/win_iocp_handle_read_op.hpp>
  27. #include <boost/asio/detail/win_iocp_handle_write_op.hpp>
  28. #include <boost/asio/detail/win_iocp_io_context.hpp>
  29. #include <boost/asio/detail/push_options.hpp>
  30. namespace boost {
  31. namespace asio {
  32. namespace detail {
  33. class win_iocp_handle_service :
  34. public execution_context_service_base<win_iocp_handle_service>
  35. {
  36. public:
  37. // The native type of a stream handle.
  38. typedef HANDLE native_handle_type;
  39. // The implementation type of the stream handle.
  40. class implementation_type
  41. {
  42. public:
  43. // Default constructor.
  44. implementation_type()
  45. : handle_(INVALID_HANDLE_VALUE),
  46. safe_cancellation_thread_id_(0),
  47. next_(0),
  48. prev_(0)
  49. {
  50. }
  51. private:
  52. // Only this service will have access to the internal values.
  53. friend class win_iocp_handle_service;
  54. // The native stream handle representation.
  55. native_handle_type handle_;
  56. // The ID of the thread from which it is safe to cancel asynchronous
  57. // operations. 0 means no asynchronous operations have been started yet.
  58. // ~0 means asynchronous operations have been started from more than one
  59. // thread, and cancellation is not supported for the handle.
  60. DWORD safe_cancellation_thread_id_;
  61. // Pointers to adjacent handle implementations in linked list.
  62. implementation_type* next_;
  63. implementation_type* prev_;
  64. };
  65. BOOST_ASIO_DECL win_iocp_handle_service(execution_context& context);
  66. // Destroy all user-defined handler objects owned by the service.
  67. BOOST_ASIO_DECL void shutdown();
  68. // Construct a new handle implementation.
  69. BOOST_ASIO_DECL void construct(implementation_type& impl);
  70. // Move-construct a new handle implementation.
  71. BOOST_ASIO_DECL void move_construct(implementation_type& impl,
  72. implementation_type& other_impl);
  73. // Move-assign from another handle implementation.
  74. BOOST_ASIO_DECL void move_assign(implementation_type& impl,
  75. win_iocp_handle_service& other_service,
  76. implementation_type& other_impl);
  77. // Destroy a handle implementation.
  78. BOOST_ASIO_DECL void destroy(implementation_type& impl);
  79. // Assign a native handle to a handle implementation.
  80. BOOST_ASIO_DECL boost::system::error_code assign(implementation_type& impl,
  81. const native_handle_type& handle, boost::system::error_code& ec);
  82. // Determine whether the handle is open.
  83. bool is_open(const implementation_type& impl) const
  84. {
  85. return impl.handle_ != INVALID_HANDLE_VALUE;
  86. }
  87. // Destroy a handle implementation.
  88. BOOST_ASIO_DECL boost::system::error_code close(implementation_type& impl,
  89. boost::system::error_code& ec);
  90. // Get the native handle representation.
  91. native_handle_type native_handle(const implementation_type& impl) const
  92. {
  93. return impl.handle_;
  94. }
  95. // Cancel all operations associated with the handle.
  96. BOOST_ASIO_DECL boost::system::error_code cancel(implementation_type& impl,
  97. boost::system::error_code& ec);
  98. // Write the given data. Returns the number of bytes written.
  99. template <typename ConstBufferSequence>
  100. size_t write_some(implementation_type& impl,
  101. const ConstBufferSequence& buffers, boost::system::error_code& ec)
  102. {
  103. return write_some_at(impl, 0, buffers, ec);
  104. }
  105. // Write the given data at the specified offset. Returns the number of bytes
  106. // written.
  107. template <typename ConstBufferSequence>
  108. size_t write_some_at(implementation_type& impl, uint64_t offset,
  109. const ConstBufferSequence& buffers, boost::system::error_code& ec)
  110. {
  111. boost::asio::const_buffer buffer =
  112. buffer_sequence_adapter<boost::asio::const_buffer,
  113. ConstBufferSequence>::first(buffers);
  114. return do_write(impl, offset, buffer, ec);
  115. }
  116. // Start an asynchronous write. The data being written must be valid for the
  117. // lifetime of the asynchronous operation.
  118. template <typename ConstBufferSequence, typename Handler, typename IoExecutor>
  119. void async_write_some(implementation_type& impl,
  120. const ConstBufferSequence& buffers,
  121. Handler& handler, const IoExecutor& io_ex)
  122. {
  123. // Allocate and construct an operation to wrap the handler.
  124. typedef win_iocp_handle_write_op<
  125. ConstBufferSequence, Handler, IoExecutor> op;
  126. typename op::ptr p = { boost::asio::detail::addressof(handler),
  127. op::ptr::allocate(handler), 0 };
  128. p.p = new (p.v) op(buffers, handler, io_ex);
  129. BOOST_ASIO_HANDLER_CREATION((iocp_service_.context(), *p.p, "handle", &impl,
  130. reinterpret_cast<uintmax_t>(impl.handle_), "async_write_some"));
  131. start_write_op(impl, 0,
  132. buffer_sequence_adapter<boost::asio::const_buffer,
  133. ConstBufferSequence>::first(buffers), p.p);
  134. p.v = p.p = 0;
  135. }
  136. // Start an asynchronous write at a specified offset. The data being written
  137. // must be valid for the lifetime of the asynchronous operation.
  138. template <typename ConstBufferSequence, typename Handler, typename IoExecutor>
  139. void async_write_some_at(implementation_type& impl,
  140. uint64_t offset, const ConstBufferSequence& buffers,
  141. Handler& handler, const IoExecutor& io_ex)
  142. {
  143. // Allocate and construct an operation to wrap the handler.
  144. typedef win_iocp_handle_write_op<
  145. ConstBufferSequence, Handler, IoExecutor> op;
  146. typename op::ptr p = { boost::asio::detail::addressof(handler),
  147. op::ptr::allocate(handler), 0 };
  148. p.p = new (p.v) op(buffers, handler, io_ex);
  149. BOOST_ASIO_HANDLER_CREATION((iocp_service_.context(), *p.p, "handle", &impl,
  150. reinterpret_cast<uintmax_t>(impl.handle_), "async_write_some_at"));
  151. start_write_op(impl, offset,
  152. buffer_sequence_adapter<boost::asio::const_buffer,
  153. ConstBufferSequence>::first(buffers), p.p);
  154. p.v = p.p = 0;
  155. }
  156. // Read some data. Returns the number of bytes received.
  157. template <typename MutableBufferSequence>
  158. size_t read_some(implementation_type& impl,
  159. const MutableBufferSequence& buffers, boost::system::error_code& ec)
  160. {
  161. return read_some_at(impl, 0, buffers, ec);
  162. }
  163. // Read some data at a specified offset. Returns the number of bytes received.
  164. template <typename MutableBufferSequence>
  165. size_t read_some_at(implementation_type& impl, uint64_t offset,
  166. const MutableBufferSequence& buffers, boost::system::error_code& ec)
  167. {
  168. boost::asio::mutable_buffer buffer =
  169. buffer_sequence_adapter<boost::asio::mutable_buffer,
  170. MutableBufferSequence>::first(buffers);
  171. return do_read(impl, offset, buffer, ec);
  172. }
  173. // Start an asynchronous read. The buffer for the data being received must be
  174. // valid for the lifetime of the asynchronous operation.
  175. template <typename MutableBufferSequence,
  176. typename Handler, typename IoExecutor>
  177. void async_read_some(implementation_type& impl,
  178. const MutableBufferSequence& buffers,
  179. Handler& handler, const IoExecutor& io_ex)
  180. {
  181. // Allocate and construct an operation to wrap the handler.
  182. typedef win_iocp_handle_read_op<
  183. MutableBufferSequence, Handler, IoExecutor> op;
  184. typename op::ptr p = { boost::asio::detail::addressof(handler),
  185. op::ptr::allocate(handler), 0 };
  186. p.p = new (p.v) op(buffers, handler, io_ex);
  187. BOOST_ASIO_HANDLER_CREATION((iocp_service_.context(), *p.p, "handle", &impl,
  188. reinterpret_cast<uintmax_t>(impl.handle_), "async_read_some"));
  189. start_read_op(impl, 0,
  190. buffer_sequence_adapter<boost::asio::mutable_buffer,
  191. MutableBufferSequence>::first(buffers), p.p);
  192. p.v = p.p = 0;
  193. }
  194. // Start an asynchronous read at a specified offset. The buffer for the data
  195. // being received must be valid for the lifetime of the asynchronous
  196. // operation.
  197. template <typename MutableBufferSequence,
  198. typename Handler, typename IoExecutor>
  199. void async_read_some_at(implementation_type& impl,
  200. uint64_t offset, const MutableBufferSequence& buffers,
  201. Handler& handler, const IoExecutor& io_ex)
  202. {
  203. // Allocate and construct an operation to wrap the handler.
  204. typedef win_iocp_handle_read_op<
  205. MutableBufferSequence, Handler, IoExecutor> op;
  206. typename op::ptr p = { boost::asio::detail::addressof(handler),
  207. op::ptr::allocate(handler), 0 };
  208. p.p = new (p.v) op(buffers, handler, io_ex);
  209. BOOST_ASIO_HANDLER_CREATION((iocp_service_.context(), *p.p, "handle", &impl,
  210. reinterpret_cast<uintmax_t>(impl.handle_), "async_read_some_at"));
  211. start_read_op(impl, offset,
  212. buffer_sequence_adapter<boost::asio::mutable_buffer,
  213. MutableBufferSequence>::first(buffers), p.p);
  214. p.v = p.p = 0;
  215. }
  216. private:
  217. // Prevent the use of the null_buffers type with this service.
  218. size_t write_some(implementation_type& impl,
  219. const null_buffers& buffers, boost::system::error_code& ec);
  220. size_t write_some_at(implementation_type& impl, uint64_t offset,
  221. const null_buffers& buffers, boost::system::error_code& ec);
  222. template <typename Handler, typename IoExecutor>
  223. void async_write_some(implementation_type& impl,
  224. const null_buffers& buffers, Handler& handler,
  225. const IoExecutor& io_ex);
  226. template <typename Handler, typename IoExecutor>
  227. void async_write_some_at(implementation_type& impl, uint64_t offset,
  228. const null_buffers& buffers, Handler& handler, const IoExecutor& io_ex);
  229. size_t read_some(implementation_type& impl,
  230. const null_buffers& buffers, boost::system::error_code& ec);
  231. size_t read_some_at(implementation_type& impl, uint64_t offset,
  232. const null_buffers& buffers, boost::system::error_code& ec);
  233. template <typename Handler, typename IoExecutor>
  234. void async_read_some(implementation_type& impl,
  235. const null_buffers& buffers, Handler& handler,
  236. const IoExecutor& io_ex);
  237. template <typename Handler, typename IoExecutor>
  238. void async_read_some_at(implementation_type& impl, uint64_t offset,
  239. const null_buffers& buffers, Handler& handler, const IoExecutor& io_ex);
  240. // Helper class for waiting for synchronous operations to complete.
  241. class overlapped_wrapper;
  242. // Helper function to perform a synchronous write operation.
  243. BOOST_ASIO_DECL size_t do_write(implementation_type& impl,
  244. uint64_t offset, const boost::asio::const_buffer& buffer,
  245. boost::system::error_code& ec);
  246. // Helper function to start a write operation.
  247. BOOST_ASIO_DECL void start_write_op(implementation_type& impl,
  248. uint64_t offset, const boost::asio::const_buffer& buffer,
  249. operation* op);
  250. // Helper function to perform a synchronous write operation.
  251. BOOST_ASIO_DECL size_t do_read(implementation_type& impl,
  252. uint64_t offset, const boost::asio::mutable_buffer& buffer,
  253. boost::system::error_code& ec);
  254. // Helper function to start a read operation.
  255. BOOST_ASIO_DECL void start_read_op(implementation_type& impl,
  256. uint64_t offset, const boost::asio::mutable_buffer& buffer,
  257. operation* op);
  258. // Update the ID of the thread from which cancellation is safe.
  259. BOOST_ASIO_DECL void update_cancellation_thread_id(implementation_type& impl);
  260. // Helper function to close a handle when the associated object is being
  261. // destroyed.
  262. BOOST_ASIO_DECL void close_for_destruction(implementation_type& impl);
  263. // The IOCP service used for running asynchronous operations and dispatching
  264. // handlers.
  265. win_iocp_io_context& iocp_service_;
  266. // Mutex to protect access to the linked list of implementations.
  267. mutex mutex_;
  268. // The head of a linked list of all implementations.
  269. implementation_type* impl_list_;
  270. };
  271. } // namespace detail
  272. } // namespace asio
  273. } // namespace boost
  274. #include <boost/asio/detail/pop_options.hpp>
  275. #if defined(BOOST_ASIO_HEADER_ONLY)
  276. # include <boost/asio/detail/impl/win_iocp_handle_service.ipp>
  277. #endif // defined(BOOST_ASIO_HEADER_ONLY)
  278. #endif // defined(BOOST_ASIO_HAS_IOCP)
  279. #endif // BOOST_ASIO_DETAIL_WIN_IOCP_HANDLE_SERVICE_HPP