win_iocp_handle_service.hpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. //
  2. // detail/win_iocp_handle_service.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2018 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/io_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 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(boost::asio::io_context& io_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>
  119. void async_write_some(implementation_type& impl,
  120. const ConstBufferSequence& buffers, Handler& handler)
  121. {
  122. // Allocate and construct an operation to wrap the handler.
  123. typedef win_iocp_handle_write_op<ConstBufferSequence, Handler> op;
  124. typename op::ptr p = { boost::asio::detail::addressof(handler),
  125. op::ptr::allocate(handler), 0 };
  126. p.p = new (p.v) op(buffers, handler);
  127. BOOST_ASIO_HANDLER_CREATION((iocp_service_.context(), *p.p, "handle", &impl,
  128. reinterpret_cast<uintmax_t>(impl.handle_), "async_write_some"));
  129. start_write_op(impl, 0,
  130. buffer_sequence_adapter<boost::asio::const_buffer,
  131. ConstBufferSequence>::first(buffers), p.p);
  132. p.v = p.p = 0;
  133. }
  134. // Start an asynchronous write at a specified offset. The data being written
  135. // must be valid for the lifetime of the asynchronous operation.
  136. template <typename ConstBufferSequence, typename Handler>
  137. void async_write_some_at(implementation_type& impl, uint64_t offset,
  138. const ConstBufferSequence& buffers, Handler& handler)
  139. {
  140. // Allocate and construct an operation to wrap the handler.
  141. typedef win_iocp_handle_write_op<ConstBufferSequence, Handler> op;
  142. typename op::ptr p = { boost::asio::detail::addressof(handler),
  143. op::ptr::allocate(handler), 0 };
  144. p.p = new (p.v) op(buffers, handler);
  145. BOOST_ASIO_HANDLER_CREATION((iocp_service_.context(), *p.p, "handle", &impl,
  146. reinterpret_cast<uintmax_t>(impl.handle_), "async_write_some_at"));
  147. start_write_op(impl, offset,
  148. buffer_sequence_adapter<boost::asio::const_buffer,
  149. ConstBufferSequence>::first(buffers), p.p);
  150. p.v = p.p = 0;
  151. }
  152. // Read some data. Returns the number of bytes received.
  153. template <typename MutableBufferSequence>
  154. size_t read_some(implementation_type& impl,
  155. const MutableBufferSequence& buffers, boost::system::error_code& ec)
  156. {
  157. return read_some_at(impl, 0, buffers, ec);
  158. }
  159. // Read some data at a specified offset. Returns the number of bytes received.
  160. template <typename MutableBufferSequence>
  161. size_t read_some_at(implementation_type& impl, uint64_t offset,
  162. const MutableBufferSequence& buffers, boost::system::error_code& ec)
  163. {
  164. boost::asio::mutable_buffer buffer =
  165. buffer_sequence_adapter<boost::asio::mutable_buffer,
  166. MutableBufferSequence>::first(buffers);
  167. return do_read(impl, offset, buffer, ec);
  168. }
  169. // Start an asynchronous read. The buffer for the data being received must be
  170. // valid for the lifetime of the asynchronous operation.
  171. template <typename MutableBufferSequence, typename Handler>
  172. void async_read_some(implementation_type& impl,
  173. const MutableBufferSequence& buffers, Handler& handler)
  174. {
  175. // Allocate and construct an operation to wrap the handler.
  176. typedef win_iocp_handle_read_op<MutableBufferSequence, Handler> op;
  177. typename op::ptr p = { boost::asio::detail::addressof(handler),
  178. op::ptr::allocate(handler), 0 };
  179. p.p = new (p.v) op(buffers, handler);
  180. BOOST_ASIO_HANDLER_CREATION((iocp_service_.context(), *p.p, "handle", &impl,
  181. reinterpret_cast<uintmax_t>(impl.handle_), "async_read_some"));
  182. start_read_op(impl, 0,
  183. buffer_sequence_adapter<boost::asio::mutable_buffer,
  184. MutableBufferSequence>::first(buffers), p.p);
  185. p.v = p.p = 0;
  186. }
  187. // Start an asynchronous read at a specified offset. The buffer for the data
  188. // being received must be valid for the lifetime of the asynchronous
  189. // operation.
  190. template <typename MutableBufferSequence, typename Handler>
  191. void async_read_some_at(implementation_type& impl, uint64_t offset,
  192. const MutableBufferSequence& buffers, Handler& handler)
  193. {
  194. // Allocate and construct an operation to wrap the handler.
  195. typedef win_iocp_handle_read_op<MutableBufferSequence, Handler> op;
  196. typename op::ptr p = { boost::asio::detail::addressof(handler),
  197. op::ptr::allocate(handler), 0 };
  198. p.p = new (p.v) op(buffers, handler);
  199. BOOST_ASIO_HANDLER_CREATION((iocp_service_.context(), *p.p, "handle", &impl,
  200. reinterpret_cast<uintmax_t>(impl.handle_), "async_read_some_at"));
  201. start_read_op(impl, offset,
  202. buffer_sequence_adapter<boost::asio::mutable_buffer,
  203. MutableBufferSequence>::first(buffers), p.p);
  204. p.v = p.p = 0;
  205. }
  206. private:
  207. // Prevent the use of the null_buffers type with this service.
  208. size_t write_some(implementation_type& impl,
  209. const null_buffers& buffers, boost::system::error_code& ec);
  210. size_t write_some_at(implementation_type& impl, uint64_t offset,
  211. const null_buffers& buffers, boost::system::error_code& ec);
  212. template <typename Handler>
  213. void async_write_some(implementation_type& impl,
  214. const null_buffers& buffers, Handler& handler);
  215. template <typename Handler>
  216. void async_write_some_at(implementation_type& impl, uint64_t offset,
  217. const null_buffers& buffers, Handler& handler);
  218. size_t read_some(implementation_type& impl,
  219. const null_buffers& buffers, boost::system::error_code& ec);
  220. size_t read_some_at(implementation_type& impl, uint64_t offset,
  221. const null_buffers& buffers, boost::system::error_code& ec);
  222. template <typename Handler>
  223. void async_read_some(implementation_type& impl,
  224. const null_buffers& buffers, Handler& handler);
  225. template <typename Handler>
  226. void async_read_some_at(implementation_type& impl, uint64_t offset,
  227. const null_buffers& buffers, Handler& handler);
  228. // Helper class for waiting for synchronous operations to complete.
  229. class overlapped_wrapper;
  230. // Helper function to perform a synchronous write operation.
  231. BOOST_ASIO_DECL size_t do_write(implementation_type& impl,
  232. uint64_t offset, const boost::asio::const_buffer& buffer,
  233. boost::system::error_code& ec);
  234. // Helper function to start a write operation.
  235. BOOST_ASIO_DECL void start_write_op(implementation_type& impl,
  236. uint64_t offset, const boost::asio::const_buffer& buffer,
  237. operation* op);
  238. // Helper function to perform a synchronous write operation.
  239. BOOST_ASIO_DECL size_t do_read(implementation_type& impl,
  240. uint64_t offset, const boost::asio::mutable_buffer& buffer,
  241. boost::system::error_code& ec);
  242. // Helper function to start a read operation.
  243. BOOST_ASIO_DECL void start_read_op(implementation_type& impl,
  244. uint64_t offset, const boost::asio::mutable_buffer& buffer,
  245. operation* op);
  246. // Update the ID of the thread from which cancellation is safe.
  247. BOOST_ASIO_DECL void update_cancellation_thread_id(implementation_type& impl);
  248. // Helper function to close a handle when the associated object is being
  249. // destroyed.
  250. BOOST_ASIO_DECL void close_for_destruction(implementation_type& impl);
  251. // The IOCP service used for running asynchronous operations and dispatching
  252. // handlers.
  253. win_iocp_io_context& iocp_service_;
  254. // Mutex to protect access to the linked list of implementations.
  255. mutex mutex_;
  256. // The head of a linked list of all implementations.
  257. implementation_type* impl_list_;
  258. };
  259. } // namespace detail
  260. } // namespace asio
  261. } // namespace boost
  262. #include <boost/asio/detail/pop_options.hpp>
  263. #if defined(BOOST_ASIO_HEADER_ONLY)
  264. # include <boost/asio/detail/impl/win_iocp_handle_service.ipp>
  265. #endif // defined(BOOST_ASIO_HEADER_ONLY)
  266. #endif // defined(BOOST_ASIO_HAS_IOCP)
  267. #endif // BOOST_ASIO_DETAIL_WIN_IOCP_HANDLE_SERVICE_HPP