win_iocp_socket_service_base.hpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  1. //
  2. // detail/win_iocp_socket_service_base.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2019 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 BOOST_ASIO_DETAIL_WIN_IOCP_SOCKET_SERVICE_BASE_HPP
  11. #define BOOST_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 <boost/asio/detail/config.hpp>
  16. #if defined(BOOST_ASIO_HAS_IOCP)
  17. #include <boost/asio/error.hpp>
  18. #include <boost/asio/execution_context.hpp>
  19. #include <boost/asio/socket_base.hpp>
  20. #include <boost/asio/detail/bind_handler.hpp>
  21. #include <boost/asio/detail/buffer_sequence_adapter.hpp>
  22. #include <boost/asio/detail/fenced_block.hpp>
  23. #include <boost/asio/detail/handler_alloc_helpers.hpp>
  24. #include <boost/asio/detail/handler_invoke_helpers.hpp>
  25. #include <boost/asio/detail/memory.hpp>
  26. #include <boost/asio/detail/mutex.hpp>
  27. #include <boost/asio/detail/operation.hpp>
  28. #include <boost/asio/detail/reactor_op.hpp>
  29. #include <boost/asio/detail/select_reactor.hpp>
  30. #include <boost/asio/detail/socket_holder.hpp>
  31. #include <boost/asio/detail/socket_ops.hpp>
  32. #include <boost/asio/detail/socket_types.hpp>
  33. #include <boost/asio/detail/win_iocp_io_context.hpp>
  34. #include <boost/asio/detail/win_iocp_null_buffers_op.hpp>
  35. #include <boost/asio/detail/win_iocp_socket_connect_op.hpp>
  36. #include <boost/asio/detail/win_iocp_socket_send_op.hpp>
  37. #include <boost/asio/detail/win_iocp_socket_recv_op.hpp>
  38. #include <boost/asio/detail/win_iocp_socket_recvmsg_op.hpp>
  39. #include <boost/asio/detail/win_iocp_wait_op.hpp>
  40. #include <boost/asio/detail/push_options.hpp>
  41. namespace boost {
  42. namespace asio {
  43. namespace detail {
  44. class win_iocp_socket_service_base
  45. {
  46. public:
  47. // The implementation type of the socket.
  48. struct base_implementation_type
  49. {
  50. // The native socket representation.
  51. socket_type socket_;
  52. // The current state of the socket.
  53. socket_ops::state_type state_;
  54. // We use a shared pointer as a cancellation token here to work around the
  55. // broken Windows support for cancellation. MSDN says that when you call
  56. // closesocket any outstanding WSARecv or WSASend operations will complete
  57. // with the error ERROR_OPERATION_ABORTED. In practice they complete with
  58. // ERROR_NETNAME_DELETED, which means you can't tell the difference between
  59. // a local cancellation and the socket being hard-closed by the peer.
  60. socket_ops::shared_cancel_token_type cancel_token_;
  61. // Per-descriptor data used by the reactor.
  62. select_reactor::per_descriptor_data reactor_data_;
  63. #if defined(BOOST_ASIO_ENABLE_CANCELIO)
  64. // The ID of the thread from which it is safe to cancel asynchronous
  65. // operations. 0 means no asynchronous operations have been started yet.
  66. // ~0 means asynchronous operations have been started from more than one
  67. // thread, and cancellation is not supported for the socket.
  68. DWORD safe_cancellation_thread_id_;
  69. #endif // defined(BOOST_ASIO_ENABLE_CANCELIO)
  70. // Pointers to adjacent socket implementations in linked list.
  71. base_implementation_type* next_;
  72. base_implementation_type* prev_;
  73. };
  74. // Constructor.
  75. BOOST_ASIO_DECL win_iocp_socket_service_base(execution_context& context);
  76. // Destroy all user-defined handler objects owned by the service.
  77. BOOST_ASIO_DECL void base_shutdown();
  78. // Construct a new socket implementation.
  79. BOOST_ASIO_DECL void construct(base_implementation_type& impl);
  80. // Move-construct a new socket implementation.
  81. BOOST_ASIO_DECL void base_move_construct(base_implementation_type& impl,
  82. base_implementation_type& other_impl) BOOST_ASIO_NOEXCEPT;
  83. // Move-assign from another socket implementation.
  84. BOOST_ASIO_DECL void base_move_assign(base_implementation_type& impl,
  85. win_iocp_socket_service_base& other_service,
  86. base_implementation_type& other_impl);
  87. // Destroy a socket implementation.
  88. BOOST_ASIO_DECL void destroy(base_implementation_type& impl);
  89. // Determine whether the socket is open.
  90. bool is_open(const base_implementation_type& impl) const
  91. {
  92. return impl.socket_ != invalid_socket;
  93. }
  94. // Destroy a socket implementation.
  95. BOOST_ASIO_DECL boost::system::error_code close(
  96. base_implementation_type& impl, boost::system::error_code& ec);
  97. // Release ownership of the socket.
  98. BOOST_ASIO_DECL socket_type release(
  99. base_implementation_type& impl, boost::system::error_code& ec);
  100. // Cancel all operations associated with the socket.
  101. BOOST_ASIO_DECL boost::system::error_code cancel(
  102. base_implementation_type& impl, boost::system::error_code& ec);
  103. // Determine whether the socket is at the out-of-band data mark.
  104. bool at_mark(const base_implementation_type& impl,
  105. boost::system::error_code& ec) const
  106. {
  107. return socket_ops::sockatmark(impl.socket_, ec);
  108. }
  109. // Determine the number of bytes available for reading.
  110. std::size_t available(const base_implementation_type& impl,
  111. boost::system::error_code& ec) const
  112. {
  113. return socket_ops::available(impl.socket_, ec);
  114. }
  115. // Place the socket into the state where it will listen for new connections.
  116. boost::system::error_code listen(base_implementation_type& impl,
  117. int backlog, boost::system::error_code& ec)
  118. {
  119. socket_ops::listen(impl.socket_, backlog, ec);
  120. return ec;
  121. }
  122. // Perform an IO control command on the socket.
  123. template <typename IO_Control_Command>
  124. boost::system::error_code io_control(base_implementation_type& impl,
  125. IO_Control_Command& command, boost::system::error_code& ec)
  126. {
  127. socket_ops::ioctl(impl.socket_, impl.state_, command.name(),
  128. static_cast<ioctl_arg_type*>(command.data()), ec);
  129. return ec;
  130. }
  131. // Gets the non-blocking mode of the socket.
  132. bool non_blocking(const base_implementation_type& impl) const
  133. {
  134. return (impl.state_ & socket_ops::user_set_non_blocking) != 0;
  135. }
  136. // Sets the non-blocking mode of the socket.
  137. boost::system::error_code non_blocking(base_implementation_type& impl,
  138. bool mode, boost::system::error_code& ec)
  139. {
  140. socket_ops::set_user_non_blocking(impl.socket_, impl.state_, mode, ec);
  141. return ec;
  142. }
  143. // Gets the non-blocking mode of the native socket implementation.
  144. bool native_non_blocking(const base_implementation_type& impl) const
  145. {
  146. return (impl.state_ & socket_ops::internal_non_blocking) != 0;
  147. }
  148. // Sets the non-blocking mode of the native socket implementation.
  149. boost::system::error_code native_non_blocking(base_implementation_type& impl,
  150. bool mode, boost::system::error_code& ec)
  151. {
  152. socket_ops::set_internal_non_blocking(impl.socket_, impl.state_, mode, ec);
  153. return ec;
  154. }
  155. // Wait for the socket to become ready to read, ready to write, or to have
  156. // pending error conditions.
  157. boost::system::error_code wait(base_implementation_type& impl,
  158. socket_base::wait_type w, boost::system::error_code& ec)
  159. {
  160. switch (w)
  161. {
  162. case socket_base::wait_read:
  163. socket_ops::poll_read(impl.socket_, impl.state_, -1, ec);
  164. break;
  165. case socket_base::wait_write:
  166. socket_ops::poll_write(impl.socket_, impl.state_, -1, ec);
  167. break;
  168. case socket_base::wait_error:
  169. socket_ops::poll_error(impl.socket_, impl.state_, -1, ec);
  170. break;
  171. default:
  172. ec = boost::asio::error::invalid_argument;
  173. break;
  174. }
  175. return ec;
  176. }
  177. // Asynchronously wait for the socket to become ready to read, ready to
  178. // write, or to have pending error conditions.
  179. template <typename Handler, typename IoExecutor>
  180. void async_wait(base_implementation_type& impl,
  181. socket_base::wait_type w, Handler& handler, const IoExecutor& io_ex)
  182. {
  183. bool is_continuation =
  184. boost_asio_handler_cont_helpers::is_continuation(handler);
  185. // Allocate and construct an operation to wrap the handler.
  186. typedef win_iocp_wait_op<Handler, IoExecutor> op;
  187. typename op::ptr p = { boost::asio::detail::addressof(handler),
  188. op::ptr::allocate(handler), 0 };
  189. p.p = new (p.v) op(impl.cancel_token_, handler, io_ex);
  190. BOOST_ASIO_HANDLER_CREATION((context_, *p.p, "socket",
  191. &impl, impl.socket_, "async_wait"));
  192. switch (w)
  193. {
  194. case socket_base::wait_read:
  195. start_null_buffers_receive_op(impl, 0, p.p);
  196. break;
  197. case socket_base::wait_write:
  198. start_reactor_op(impl, select_reactor::write_op, p.p);
  199. break;
  200. case socket_base::wait_error:
  201. start_reactor_op(impl, select_reactor::except_op, p.p);
  202. break;
  203. default:
  204. p.p->ec_ = boost::asio::error::invalid_argument;
  205. iocp_service_.post_immediate_completion(p.p, is_continuation);
  206. break;
  207. }
  208. p.v = p.p = 0;
  209. }
  210. // Send the given data to the peer. Returns the number of bytes sent.
  211. template <typename ConstBufferSequence>
  212. size_t send(base_implementation_type& impl,
  213. const ConstBufferSequence& buffers,
  214. socket_base::message_flags flags, boost::system::error_code& ec)
  215. {
  216. buffer_sequence_adapter<boost::asio::const_buffer,
  217. ConstBufferSequence> bufs(buffers);
  218. return socket_ops::sync_send(impl.socket_, impl.state_,
  219. bufs.buffers(), bufs.count(), flags, bufs.all_empty(), ec);
  220. }
  221. // Wait until data can be sent without blocking.
  222. size_t send(base_implementation_type& impl, const null_buffers&,
  223. socket_base::message_flags, boost::system::error_code& ec)
  224. {
  225. // Wait for socket to become ready.
  226. socket_ops::poll_write(impl.socket_, impl.state_, -1, ec);
  227. return 0;
  228. }
  229. // Start an asynchronous send. The data being sent must be valid for the
  230. // lifetime of the asynchronous operation.
  231. template <typename ConstBufferSequence, typename Handler, typename IoExecutor>
  232. void async_send(base_implementation_type& impl,
  233. const ConstBufferSequence& buffers, socket_base::message_flags flags,
  234. Handler& handler, const IoExecutor& io_ex)
  235. {
  236. // Allocate and construct an operation to wrap the handler.
  237. typedef win_iocp_socket_send_op<
  238. ConstBufferSequence, Handler, IoExecutor> op;
  239. typename op::ptr p = { boost::asio::detail::addressof(handler),
  240. op::ptr::allocate(handler), 0 };
  241. p.p = new (p.v) op(impl.cancel_token_, buffers, handler, io_ex);
  242. BOOST_ASIO_HANDLER_CREATION((context_, *p.p, "socket",
  243. &impl, impl.socket_, "async_send"));
  244. buffer_sequence_adapter<boost::asio::const_buffer,
  245. ConstBufferSequence> bufs(buffers);
  246. start_send_op(impl, bufs.buffers(), bufs.count(), flags,
  247. (impl.state_ & socket_ops::stream_oriented) != 0 && bufs.all_empty(),
  248. p.p);
  249. p.v = p.p = 0;
  250. }
  251. // Start an asynchronous wait until data can be sent without blocking.
  252. template <typename Handler, typename IoExecutor>
  253. void async_send(base_implementation_type& impl, const null_buffers&,
  254. socket_base::message_flags, Handler& handler, const IoExecutor& io_ex)
  255. {
  256. // Allocate and construct an operation to wrap the handler.
  257. typedef win_iocp_null_buffers_op<Handler, IoExecutor> op;
  258. typename op::ptr p = { boost::asio::detail::addressof(handler),
  259. op::ptr::allocate(handler), 0 };
  260. p.p = new (p.v) op(impl.cancel_token_, handler, io_ex);
  261. BOOST_ASIO_HANDLER_CREATION((context_, *p.p, "socket",
  262. &impl, impl.socket_, "async_send(null_buffers)"));
  263. start_reactor_op(impl, select_reactor::write_op, p.p);
  264. p.v = p.p = 0;
  265. }
  266. // Receive some data from the peer. Returns the number of bytes received.
  267. template <typename MutableBufferSequence>
  268. size_t receive(base_implementation_type& impl,
  269. const MutableBufferSequence& buffers,
  270. socket_base::message_flags flags, boost::system::error_code& ec)
  271. {
  272. buffer_sequence_adapter<boost::asio::mutable_buffer,
  273. MutableBufferSequence> bufs(buffers);
  274. return socket_ops::sync_recv(impl.socket_, impl.state_,
  275. bufs.buffers(), bufs.count(), flags, bufs.all_empty(), ec);
  276. }
  277. // Wait until data can be received without blocking.
  278. size_t receive(base_implementation_type& impl, const null_buffers&,
  279. socket_base::message_flags, boost::system::error_code& ec)
  280. {
  281. // Wait for socket to become ready.
  282. socket_ops::poll_read(impl.socket_, impl.state_, -1, ec);
  283. return 0;
  284. }
  285. // Start an asynchronous receive. The buffer for the data being received
  286. // must be valid for the lifetime of the asynchronous operation.
  287. template <typename MutableBufferSequence,
  288. typename Handler, typename IoExecutor>
  289. void async_receive(base_implementation_type& impl,
  290. const MutableBufferSequence& buffers, socket_base::message_flags flags,
  291. Handler& handler, const IoExecutor& io_ex)
  292. {
  293. // Allocate and construct an operation to wrap the handler.
  294. typedef win_iocp_socket_recv_op<
  295. MutableBufferSequence, Handler, IoExecutor> op;
  296. typename op::ptr p = { boost::asio::detail::addressof(handler),
  297. op::ptr::allocate(handler), 0 };
  298. p.p = new (p.v) op(impl.state_, impl.cancel_token_,
  299. buffers, handler, io_ex);
  300. BOOST_ASIO_HANDLER_CREATION((context_, *p.p, "socket",
  301. &impl, impl.socket_, "async_receive"));
  302. buffer_sequence_adapter<boost::asio::mutable_buffer,
  303. MutableBufferSequence> bufs(buffers);
  304. start_receive_op(impl, bufs.buffers(), bufs.count(), flags,
  305. (impl.state_ & socket_ops::stream_oriented) != 0 && bufs.all_empty(),
  306. p.p);
  307. p.v = p.p = 0;
  308. }
  309. // Wait until data can be received without blocking.
  310. template <typename Handler, typename IoExecutor>
  311. void async_receive(base_implementation_type& impl,
  312. const null_buffers&, socket_base::message_flags flags,
  313. Handler& handler, const IoExecutor& io_ex)
  314. {
  315. // Allocate and construct an operation to wrap the handler.
  316. typedef win_iocp_null_buffers_op<Handler, IoExecutor> op;
  317. typename op::ptr p = { boost::asio::detail::addressof(handler),
  318. op::ptr::allocate(handler), 0 };
  319. p.p = new (p.v) op(impl.cancel_token_, handler, io_ex);
  320. BOOST_ASIO_HANDLER_CREATION((context_, *p.p, "socket",
  321. &impl, impl.socket_, "async_receive(null_buffers)"));
  322. start_null_buffers_receive_op(impl, flags, p.p);
  323. p.v = p.p = 0;
  324. }
  325. // Receive some data with associated flags. Returns the number of bytes
  326. // received.
  327. template <typename MutableBufferSequence>
  328. size_t receive_with_flags(base_implementation_type& impl,
  329. const MutableBufferSequence& buffers,
  330. socket_base::message_flags in_flags,
  331. socket_base::message_flags& out_flags, boost::system::error_code& ec)
  332. {
  333. buffer_sequence_adapter<boost::asio::mutable_buffer,
  334. MutableBufferSequence> bufs(buffers);
  335. return socket_ops::sync_recvmsg(impl.socket_, impl.state_,
  336. bufs.buffers(), bufs.count(), in_flags, out_flags, ec);
  337. }
  338. // Wait until data can be received without blocking.
  339. size_t receive_with_flags(base_implementation_type& impl,
  340. const null_buffers&, socket_base::message_flags,
  341. socket_base::message_flags& out_flags, boost::system::error_code& ec)
  342. {
  343. // Wait for socket to become ready.
  344. socket_ops::poll_read(impl.socket_, impl.state_, -1, ec);
  345. // Clear out_flags, since we cannot give it any other sensible value when
  346. // performing a null_buffers operation.
  347. out_flags = 0;
  348. return 0;
  349. }
  350. // Start an asynchronous receive. The buffer for the data being received
  351. // must be valid for the lifetime of the asynchronous operation.
  352. template <typename MutableBufferSequence,
  353. typename Handler, typename IoExecutor>
  354. void async_receive_with_flags(base_implementation_type& impl,
  355. const MutableBufferSequence& buffers, socket_base::message_flags in_flags,
  356. socket_base::message_flags& out_flags, Handler& handler,
  357. const IoExecutor& io_ex)
  358. {
  359. // Allocate and construct an operation to wrap the handler.
  360. typedef win_iocp_socket_recvmsg_op<
  361. MutableBufferSequence, Handler, IoExecutor> op;
  362. typename op::ptr p = { boost::asio::detail::addressof(handler),
  363. op::ptr::allocate(handler), 0 };
  364. p.p = new (p.v) op(impl.cancel_token_,
  365. buffers, out_flags, handler, io_ex);
  366. BOOST_ASIO_HANDLER_CREATION((context_, *p.p, "socket",
  367. &impl, impl.socket_, "async_receive_with_flags"));
  368. buffer_sequence_adapter<boost::asio::mutable_buffer,
  369. MutableBufferSequence> bufs(buffers);
  370. start_receive_op(impl, bufs.buffers(), bufs.count(), in_flags, false, p.p);
  371. p.v = p.p = 0;
  372. }
  373. // Wait until data can be received without blocking.
  374. template <typename Handler, typename IoExecutor>
  375. void async_receive_with_flags(base_implementation_type& impl,
  376. const null_buffers&, socket_base::message_flags in_flags,
  377. socket_base::message_flags& out_flags, Handler& handler,
  378. const IoExecutor& io_ex)
  379. {
  380. // Allocate and construct an operation to wrap the handler.
  381. typedef win_iocp_null_buffers_op<Handler, IoExecutor> op;
  382. typename op::ptr p = { boost::asio::detail::addressof(handler),
  383. op::ptr::allocate(handler), 0 };
  384. p.p = new (p.v) op(impl.cancel_token_, handler, io_ex);
  385. BOOST_ASIO_HANDLER_CREATION((context_, *p.p, "socket",
  386. &impl, impl.socket_, "async_receive_with_flags(null_buffers)"));
  387. // Reset out_flags since it can be given no sensible value at this time.
  388. out_flags = 0;
  389. start_null_buffers_receive_op(impl, in_flags, p.p);
  390. p.v = p.p = 0;
  391. }
  392. // Helper function to restart an asynchronous accept operation.
  393. BOOST_ASIO_DECL void restart_accept_op(socket_type s,
  394. socket_holder& new_socket, int family, int type, int protocol,
  395. void* output_buffer, DWORD address_length, operation* op);
  396. protected:
  397. // Open a new socket implementation.
  398. BOOST_ASIO_DECL boost::system::error_code do_open(
  399. base_implementation_type& impl, int family, int type,
  400. int protocol, boost::system::error_code& ec);
  401. // Assign a native socket to a socket implementation.
  402. BOOST_ASIO_DECL boost::system::error_code do_assign(
  403. base_implementation_type& impl, int type,
  404. socket_type native_socket, boost::system::error_code& ec);
  405. // Helper function to start an asynchronous send operation.
  406. BOOST_ASIO_DECL void start_send_op(base_implementation_type& impl,
  407. WSABUF* buffers, std::size_t buffer_count,
  408. socket_base::message_flags flags, bool noop, operation* op);
  409. // Helper function to start an asynchronous send_to operation.
  410. BOOST_ASIO_DECL void start_send_to_op(base_implementation_type& impl,
  411. WSABUF* buffers, std::size_t buffer_count,
  412. const socket_addr_type* addr, int addrlen,
  413. socket_base::message_flags flags, operation* op);
  414. // Helper function to start an asynchronous receive operation.
  415. BOOST_ASIO_DECL void start_receive_op(base_implementation_type& impl,
  416. WSABUF* buffers, std::size_t buffer_count,
  417. socket_base::message_flags flags, bool noop, operation* op);
  418. // Helper function to start an asynchronous null_buffers receive operation.
  419. BOOST_ASIO_DECL void start_null_buffers_receive_op(
  420. base_implementation_type& impl,
  421. socket_base::message_flags flags, reactor_op* op);
  422. // Helper function to start an asynchronous receive_from operation.
  423. BOOST_ASIO_DECL void start_receive_from_op(base_implementation_type& impl,
  424. WSABUF* buffers, std::size_t buffer_count, socket_addr_type* addr,
  425. socket_base::message_flags flags, int* addrlen, operation* op);
  426. // Helper function to start an asynchronous accept operation.
  427. BOOST_ASIO_DECL void start_accept_op(base_implementation_type& impl,
  428. bool peer_is_open, socket_holder& new_socket, int family, int type,
  429. int protocol, void* output_buffer, DWORD address_length, operation* op);
  430. // Start an asynchronous read or write operation using the reactor.
  431. BOOST_ASIO_DECL void start_reactor_op(base_implementation_type& impl,
  432. int op_type, reactor_op* op);
  433. // Start the asynchronous connect operation using the reactor.
  434. BOOST_ASIO_DECL void start_connect_op(base_implementation_type& impl,
  435. int family, int type, const socket_addr_type* remote_addr,
  436. std::size_t remote_addrlen, win_iocp_socket_connect_op_base* op);
  437. // Helper function to close a socket when the associated object is being
  438. // destroyed.
  439. BOOST_ASIO_DECL void close_for_destruction(base_implementation_type& impl);
  440. // Update the ID of the thread from which cancellation is safe.
  441. BOOST_ASIO_DECL void update_cancellation_thread_id(
  442. base_implementation_type& impl);
  443. // Helper function to get the reactor. If no reactor has been created yet, a
  444. // new one is obtained from the execution context and a pointer to it is
  445. // cached in this service.
  446. BOOST_ASIO_DECL select_reactor& get_reactor();
  447. // The type of a ConnectEx function pointer, as old SDKs may not provide it.
  448. typedef BOOL (PASCAL *connect_ex_fn)(SOCKET,
  449. const socket_addr_type*, int, void*, DWORD, DWORD*, OVERLAPPED*);
  450. // Helper function to get the ConnectEx pointer. If no ConnectEx pointer has
  451. // been obtained yet, one is obtained using WSAIoctl and the pointer is
  452. // cached. Returns a null pointer if ConnectEx is not available.
  453. BOOST_ASIO_DECL connect_ex_fn get_connect_ex(
  454. base_implementation_type& impl, int type);
  455. // The type of a NtSetInformationFile function pointer.
  456. typedef LONG (NTAPI *nt_set_info_fn)(HANDLE, ULONG_PTR*, void*, ULONG, ULONG);
  457. // Helper function to get the NtSetInformationFile function pointer. If no
  458. // NtSetInformationFile pointer has been obtained yet, one is obtained using
  459. // GetProcAddress and the pointer is cached. Returns a null pointer if
  460. // NtSetInformationFile is not available.
  461. BOOST_ASIO_DECL nt_set_info_fn get_nt_set_info();
  462. // Helper function to emulate InterlockedCompareExchangePointer functionality
  463. // for:
  464. // - very old Platform SDKs; and
  465. // - platform SDKs where MSVC's /Wp64 option causes spurious warnings.
  466. BOOST_ASIO_DECL void* interlocked_compare_exchange_pointer(
  467. void** dest, void* exch, void* cmp);
  468. // Helper function to emulate InterlockedExchangePointer functionality for:
  469. // - very old Platform SDKs; and
  470. // - platform SDKs where MSVC's /Wp64 option causes spurious warnings.
  471. BOOST_ASIO_DECL void* interlocked_exchange_pointer(void** dest, void* val);
  472. // The execution context used to obtain the reactor, if required.
  473. execution_context& context_;
  474. // The IOCP service used for running asynchronous operations and dispatching
  475. // handlers.
  476. win_iocp_io_context& iocp_service_;
  477. // The reactor used for performing connect operations. This object is created
  478. // only if needed.
  479. select_reactor* reactor_;
  480. // Pointer to ConnectEx implementation.
  481. void* connect_ex_;
  482. // Pointer to NtSetInformationFile implementation.
  483. void* nt_set_info_;
  484. // Mutex to protect access to the linked list of implementations.
  485. boost::asio::detail::mutex mutex_;
  486. // The head of a linked list of all implementations.
  487. base_implementation_type* impl_list_;
  488. };
  489. } // namespace detail
  490. } // namespace asio
  491. } // namespace boost
  492. #include <boost/asio/detail/pop_options.hpp>
  493. #if defined(BOOST_ASIO_HEADER_ONLY)
  494. # include <boost/asio/detail/impl/win_iocp_socket_service_base.ipp>
  495. #endif // defined(BOOST_ASIO_HEADER_ONLY)
  496. #endif // defined(BOOST_ASIO_HAS_IOCP)
  497. #endif // BOOST_ASIO_DETAIL_WIN_IOCP_SOCKET_SERVICE_BASE_HPP