win_iocp_handle_service.hpp 11 KB

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