win_iocp_handle_service.hpp 12 KB

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