win_iocp_socket_service_base.hpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. //
  2. // detail/win_iocp_socket_service_base.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2011 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 ASIO_DETAIL_WIN_IOCP_SOCKET_SERVICE_BASE_HPP
  11. #define ASIO_DETAIL_WIN_IOCP_SOCKET_SERVICE_BASE_HPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include "asio/detail/config.hpp"
  16. #if defined(ASIO_HAS_IOCP)
  17. #include <boost/type_traits/is_same.hpp>
  18. #include <boost/utility/addressof.hpp>
  19. #include "asio/error.hpp"
  20. #include "asio/io_service.hpp"
  21. #include "asio/socket_base.hpp"
  22. #include "asio/detail/bind_handler.hpp"
  23. #include "asio/detail/buffer_sequence_adapter.hpp"
  24. #include "asio/detail/fenced_block.hpp"
  25. #include "asio/detail/handler_alloc_helpers.hpp"
  26. #include "asio/detail/handler_invoke_helpers.hpp"
  27. #include "asio/detail/mutex.hpp"
  28. #include "asio/detail/operation.hpp"
  29. #include "asio/detail/reactor.hpp"
  30. #include "asio/detail/reactor_op.hpp"
  31. #include "asio/detail/socket_holder.hpp"
  32. #include "asio/detail/socket_ops.hpp"
  33. #include "asio/detail/socket_types.hpp"
  34. #include "asio/detail/win_iocp_io_service.hpp"
  35. #include "asio/detail/win_iocp_null_buffers_op.hpp"
  36. #include "asio/detail/win_iocp_socket_send_op.hpp"
  37. #include "asio/detail/win_iocp_socket_recv_op.hpp"
  38. #include "asio/detail/push_options.hpp"
  39. namespace asio {
  40. namespace detail {
  41. class win_iocp_socket_service_base
  42. {
  43. public:
  44. // The implementation type of the socket.
  45. struct base_implementation_type
  46. {
  47. // The native socket representation.
  48. socket_type socket_;
  49. // The current state of the socket.
  50. socket_ops::state_type state_;
  51. // We use a shared pointer as a cancellation token here to work around the
  52. // broken Windows support for cancellation. MSDN says that when you call
  53. // closesocket any outstanding WSARecv or WSASend operations will complete
  54. // with the error ERROR_OPERATION_ABORTED. In practice they complete with
  55. // ERROR_NETNAME_DELETED, which means you can't tell the difference between
  56. // a local cancellation and the socket being hard-closed by the peer.
  57. socket_ops::shared_cancel_token_type cancel_token_;
  58. // Per-descriptor data used by the reactor.
  59. reactor::per_descriptor_data reactor_data_;
  60. #if defined(ASIO_ENABLE_CANCELIO)
  61. // The ID of the thread from which it is safe to cancel asynchronous
  62. // operations. 0 means no asynchronous operations have been started yet.
  63. // ~0 means asynchronous operations have been started from more than one
  64. // thread, and cancellation is not supported for the socket.
  65. DWORD safe_cancellation_thread_id_;
  66. #endif // defined(ASIO_ENABLE_CANCELIO)
  67. // Pointers to adjacent socket implementations in linked list.
  68. base_implementation_type* next_;
  69. base_implementation_type* prev_;
  70. };
  71. // Constructor.
  72. ASIO_DECL win_iocp_socket_service_base(
  73. asio::io_service& io_service);
  74. // Destroy all user-defined handler objects owned by the service.
  75. ASIO_DECL void shutdown_service();
  76. // Construct a new socket implementation.
  77. ASIO_DECL void construct(base_implementation_type& impl);
  78. // Destroy a socket implementation.
  79. ASIO_DECL void destroy(base_implementation_type& impl);
  80. // Determine whether the socket is open.
  81. bool is_open(const base_implementation_type& impl) const
  82. {
  83. return impl.socket_ != invalid_socket;
  84. }
  85. // Destroy a socket implementation.
  86. ASIO_DECL asio::error_code close(
  87. base_implementation_type& impl, asio::error_code& ec);
  88. // Cancel all operations associated with the socket.
  89. ASIO_DECL asio::error_code cancel(
  90. base_implementation_type& impl, asio::error_code& ec);
  91. // Determine whether the socket is at the out-of-band data mark.
  92. bool at_mark(const base_implementation_type& impl,
  93. asio::error_code& ec) const
  94. {
  95. return socket_ops::sockatmark(impl.socket_, ec);
  96. }
  97. // Determine the number of bytes available for reading.
  98. std::size_t available(const base_implementation_type& impl,
  99. asio::error_code& ec) const
  100. {
  101. return socket_ops::available(impl.socket_, ec);
  102. }
  103. // Place the socket into the state where it will listen for new connections.
  104. asio::error_code listen(base_implementation_type& impl,
  105. int backlog, asio::error_code& ec)
  106. {
  107. socket_ops::listen(impl.socket_, backlog, ec);
  108. return ec;
  109. }
  110. // Perform an IO control command on the socket.
  111. template <typename IO_Control_Command>
  112. asio::error_code io_control(base_implementation_type& impl,
  113. IO_Control_Command& command, asio::error_code& ec)
  114. {
  115. socket_ops::ioctl(impl.socket_, impl.state_, command.name(),
  116. static_cast<ioctl_arg_type*>(command.data()), ec);
  117. return ec;
  118. }
  119. /// Disable sends or receives on the socket.
  120. asio::error_code shutdown(base_implementation_type& impl,
  121. socket_base::shutdown_type what, asio::error_code& ec)
  122. {
  123. socket_ops::shutdown(impl.socket_, what, ec);
  124. return ec;
  125. }
  126. // Send the given data to the peer. Returns the number of bytes sent.
  127. template <typename ConstBufferSequence>
  128. size_t send(base_implementation_type& impl,
  129. const ConstBufferSequence& buffers,
  130. socket_base::message_flags flags, asio::error_code& ec)
  131. {
  132. buffer_sequence_adapter<asio::const_buffer,
  133. ConstBufferSequence> bufs(buffers);
  134. return socket_ops::sync_send(impl.socket_, impl.state_,
  135. bufs.buffers(), bufs.count(), flags, bufs.all_empty(), ec);
  136. }
  137. // Wait until data can be sent without blocking.
  138. size_t send(base_implementation_type& impl, const null_buffers&,
  139. socket_base::message_flags, asio::error_code& ec)
  140. {
  141. // Wait for socket to become ready.
  142. socket_ops::poll_write(impl.socket_, ec);
  143. return 0;
  144. }
  145. // Start an asynchronous send. The data being sent must be valid for the
  146. // lifetime of the asynchronous operation.
  147. template <typename ConstBufferSequence, typename Handler>
  148. void async_send(base_implementation_type& impl,
  149. const ConstBufferSequence& buffers,
  150. socket_base::message_flags flags, Handler handler)
  151. {
  152. // Allocate and construct an operation to wrap the handler.
  153. typedef win_iocp_socket_send_op<ConstBufferSequence, Handler> op;
  154. typename op::ptr p = { boost::addressof(handler),
  155. asio_handler_alloc_helpers::allocate(
  156. sizeof(op), handler), 0 };
  157. p.p = new (p.v) op(impl.cancel_token_, buffers, handler);
  158. buffer_sequence_adapter<asio::const_buffer,
  159. ConstBufferSequence> bufs(buffers);
  160. start_send_op(impl, bufs.buffers(), bufs.count(), flags,
  161. (impl.state_ & socket_ops::stream_oriented) != 0 && bufs.all_empty(),
  162. p.p);
  163. p.v = p.p = 0;
  164. }
  165. // Start an asynchronous wait until data can be sent without blocking.
  166. template <typename Handler>
  167. void async_send(base_implementation_type& impl, const null_buffers&,
  168. socket_base::message_flags, Handler handler)
  169. {
  170. // Allocate and construct an operation to wrap the handler.
  171. typedef win_iocp_null_buffers_op<Handler> op;
  172. typename op::ptr p = { boost::addressof(handler),
  173. asio_handler_alloc_helpers::allocate(
  174. sizeof(op), handler), 0 };
  175. p.p = new (p.v) op(impl.cancel_token_, handler);
  176. start_reactor_op(impl, reactor::write_op, p.p);
  177. p.v = p.p = 0;
  178. }
  179. // Receive some data from the peer. Returns the number of bytes received.
  180. template <typename MutableBufferSequence>
  181. size_t receive(base_implementation_type& impl,
  182. const MutableBufferSequence& buffers,
  183. socket_base::message_flags flags, asio::error_code& ec)
  184. {
  185. buffer_sequence_adapter<asio::mutable_buffer,
  186. MutableBufferSequence> bufs(buffers);
  187. return socket_ops::sync_recv(impl.socket_, impl.state_,
  188. bufs.buffers(), bufs.count(), flags, bufs.all_empty(), ec);
  189. }
  190. // Wait until data can be received without blocking.
  191. size_t receive(base_implementation_type& impl, const null_buffers&,
  192. socket_base::message_flags, asio::error_code& ec)
  193. {
  194. // Wait for socket to become ready.
  195. socket_ops::poll_read(impl.socket_, ec);
  196. return 0;
  197. }
  198. // Start an asynchronous receive. The buffer for the data being received
  199. // must be valid for the lifetime of the asynchronous operation.
  200. template <typename MutableBufferSequence, typename Handler>
  201. void async_receive(base_implementation_type& impl,
  202. const MutableBufferSequence& buffers,
  203. socket_base::message_flags flags, Handler handler)
  204. {
  205. // Allocate and construct an operation to wrap the handler.
  206. typedef win_iocp_socket_recv_op<MutableBufferSequence, Handler> op;
  207. typename op::ptr p = { boost::addressof(handler),
  208. asio_handler_alloc_helpers::allocate(
  209. sizeof(op), handler), 0 };
  210. p.p = new (p.v) op(impl.state_, impl.cancel_token_, buffers, handler);
  211. buffer_sequence_adapter<asio::mutable_buffer,
  212. MutableBufferSequence> bufs(buffers);
  213. start_receive_op(impl, bufs.buffers(), bufs.count(), flags,
  214. (impl.state_ & socket_ops::stream_oriented) != 0 && bufs.all_empty(),
  215. p.p);
  216. p.v = p.p = 0;
  217. }
  218. // Wait until data can be received without blocking.
  219. template <typename Handler>
  220. void async_receive(base_implementation_type& impl, const null_buffers&,
  221. socket_base::message_flags flags, Handler handler)
  222. {
  223. // Allocate and construct an operation to wrap the handler.
  224. typedef win_iocp_null_buffers_op<Handler> op;
  225. typename op::ptr p = { boost::addressof(handler),
  226. asio_handler_alloc_helpers::allocate(
  227. sizeof(op), handler), 0 };
  228. p.p = new (p.v) op(impl.cancel_token_, handler);
  229. start_null_buffers_receive_op(impl, flags, p.p);
  230. p.v = p.p = 0;
  231. }
  232. // Helper function to restart an asynchronous accept operation.
  233. ASIO_DECL void restart_accept_op(socket_type s,
  234. socket_holder& new_socket, int family, int type, int protocol,
  235. void* output_buffer, DWORD address_length, operation* op);
  236. protected:
  237. // Open a new socket implementation.
  238. ASIO_DECL asio::error_code do_open(
  239. base_implementation_type& impl, int family, int type,
  240. int protocol, asio::error_code& ec);
  241. // Assign a native socket to a socket implementation.
  242. ASIO_DECL asio::error_code do_assign(
  243. base_implementation_type& impl, int type,
  244. socket_type native_socket, asio::error_code& ec);
  245. // Helper function to start an asynchronous send operation.
  246. ASIO_DECL void start_send_op(base_implementation_type& impl,
  247. WSABUF* buffers, std::size_t buffer_count,
  248. socket_base::message_flags flags, bool noop, operation* op);
  249. // Helper function to start an asynchronous send_to operation.
  250. ASIO_DECL void start_send_to_op(base_implementation_type& impl,
  251. WSABUF* buffers, std::size_t buffer_count,
  252. const socket_addr_type* addr, int addrlen,
  253. socket_base::message_flags flags, operation* op);
  254. // Helper function to start an asynchronous receive operation.
  255. ASIO_DECL void start_receive_op(base_implementation_type& impl,
  256. WSABUF* buffers, std::size_t buffer_count,
  257. socket_base::message_flags flags, bool noop, operation* op);
  258. // Helper function to start an asynchronous null_buffers receive operation.
  259. ASIO_DECL void start_null_buffers_receive_op(
  260. base_implementation_type& impl,
  261. socket_base::message_flags flags, reactor_op* op);
  262. // Helper function to start an asynchronous receive_from operation.
  263. ASIO_DECL void start_receive_from_op(base_implementation_type& impl,
  264. WSABUF* buffers, std::size_t buffer_count, socket_addr_type* addr,
  265. socket_base::message_flags flags, int* addrlen, operation* op);
  266. // Helper function to start an asynchronous accept operation.
  267. ASIO_DECL void start_accept_op(base_implementation_type& impl,
  268. bool peer_is_open, socket_holder& new_socket, int family, int type,
  269. int protocol, void* output_buffer, DWORD address_length, operation* op);
  270. // Start an asynchronous read or write operation using the the reactor.
  271. ASIO_DECL void start_reactor_op(base_implementation_type& impl,
  272. int op_type, reactor_op* op);
  273. // Start the asynchronous connect operation using the reactor.
  274. ASIO_DECL void start_connect_op(base_implementation_type& impl,
  275. reactor_op* op, const socket_addr_type* addr, std::size_t addrlen);
  276. // Helper function to close a socket when the associated object is being
  277. // destroyed.
  278. ASIO_DECL void close_for_destruction(base_implementation_type& impl);
  279. // Update the ID of the thread from which cancellation is safe.
  280. ASIO_DECL void update_cancellation_thread_id(
  281. base_implementation_type& impl);
  282. // Helper function to get the reactor. If no reactor has been created yet, a
  283. // new one is obtained from the io_service and a pointer to it is cached in
  284. // this service.
  285. ASIO_DECL reactor& get_reactor();
  286. // Helper function to emulate InterlockedCompareExchangePointer functionality
  287. // for:
  288. // - very old Platform SDKs; and
  289. // - platform SDKs where MSVC's /Wp64 option causes spurious warnings.
  290. ASIO_DECL void* interlocked_compare_exchange_pointer(
  291. void** dest, void* exch, void* cmp);
  292. // Helper function to emulate InterlockedExchangePointer functionality for:
  293. // - very old Platform SDKs; and
  294. // - platform SDKs where MSVC's /Wp64 option causes spurious warnings.
  295. ASIO_DECL void* interlocked_exchange_pointer(void** dest, void* val);
  296. // The io_service used to obtain the reactor, if required.
  297. asio::io_service& io_service_;
  298. // The IOCP service used for running asynchronous operations and dispatching
  299. // handlers.
  300. win_iocp_io_service& iocp_service_;
  301. // The reactor used for performing connect operations. This object is created
  302. // only if needed.
  303. reactor* reactor_;
  304. // Mutex to protect access to the linked list of implementations.
  305. asio::detail::mutex mutex_;
  306. // The head of a linked list of all implementations.
  307. base_implementation_type* impl_list_;
  308. };
  309. } // namespace detail
  310. } // namespace asio
  311. #include "asio/detail/pop_options.hpp"
  312. #if defined(ASIO_HEADER_ONLY)
  313. # include "asio/detail/impl/win_iocp_socket_service_base.ipp"
  314. #endif // defined(ASIO_HEADER_ONLY)
  315. #endif // defined(ASIO_HAS_IOCP)
  316. #endif // ASIO_DETAIL_WIN_IOCP_SOCKET_SERVICE_BASE_HPP