win_iocp_socket_service_base.hpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  1. //
  2. // detail/win_iocp_socket_service_base.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2018 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/io_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(
  76. boost::asio::io_context& io_context);
  77. // Destroy all user-defined handler objects owned by the service.
  78. BOOST_ASIO_DECL void base_shutdown();
  79. // Construct a new socket implementation.
  80. BOOST_ASIO_DECL void construct(base_implementation_type& impl);
  81. // Move-construct a new socket implementation.
  82. BOOST_ASIO_DECL void base_move_construct(base_implementation_type& impl,
  83. base_implementation_type& other_impl);
  84. // Move-assign from another socket implementation.
  85. BOOST_ASIO_DECL void base_move_assign(base_implementation_type& impl,
  86. win_iocp_socket_service_base& other_service,
  87. base_implementation_type& other_impl);
  88. // Destroy a socket implementation.
  89. BOOST_ASIO_DECL void destroy(base_implementation_type& impl);
  90. // Determine whether the socket is open.
  91. bool is_open(const base_implementation_type& impl) const
  92. {
  93. return impl.socket_ != invalid_socket;
  94. }
  95. // Destroy a socket implementation.
  96. BOOST_ASIO_DECL boost::system::error_code close(
  97. base_implementation_type& impl, boost::system::error_code& ec);
  98. // Release ownership of the socket.
  99. BOOST_ASIO_DECL socket_type release(
  100. base_implementation_type& impl, boost::system::error_code& ec);
  101. // Cancel all operations associated with the socket.
  102. BOOST_ASIO_DECL boost::system::error_code cancel(
  103. base_implementation_type& impl, boost::system::error_code& ec);
  104. // Determine whether the socket is at the out-of-band data mark.
  105. bool at_mark(const base_implementation_type& impl,
  106. boost::system::error_code& ec) const
  107. {
  108. return socket_ops::sockatmark(impl.socket_, ec);
  109. }
  110. // Determine the number of bytes available for reading.
  111. std::size_t available(const base_implementation_type& impl,
  112. boost::system::error_code& ec) const
  113. {
  114. return socket_ops::available(impl.socket_, ec);
  115. }
  116. // Place the socket into the state where it will listen for new connections.
  117. boost::system::error_code listen(base_implementation_type& impl,
  118. int backlog, boost::system::error_code& ec)
  119. {
  120. socket_ops::listen(impl.socket_, backlog, ec);
  121. return ec;
  122. }
  123. // Perform an IO control command on the socket.
  124. template <typename IO_Control_Command>
  125. boost::system::error_code io_control(base_implementation_type& impl,
  126. IO_Control_Command& command, boost::system::error_code& ec)
  127. {
  128. socket_ops::ioctl(impl.socket_, impl.state_, command.name(),
  129. static_cast<ioctl_arg_type*>(command.data()), ec);
  130. return ec;
  131. }
  132. // Gets the non-blocking mode of the socket.
  133. bool non_blocking(const base_implementation_type& impl) const
  134. {
  135. return (impl.state_ & socket_ops::user_set_non_blocking) != 0;
  136. }
  137. // Sets the non-blocking mode of the socket.
  138. boost::system::error_code non_blocking(base_implementation_type& impl,
  139. bool mode, boost::system::error_code& ec)
  140. {
  141. socket_ops::set_user_non_blocking(impl.socket_, impl.state_, mode, ec);
  142. return ec;
  143. }
  144. // Gets the non-blocking mode of the native socket implementation.
  145. bool native_non_blocking(const base_implementation_type& impl) const
  146. {
  147. return (impl.state_ & socket_ops::internal_non_blocking) != 0;
  148. }
  149. // Sets the non-blocking mode of the native socket implementation.
  150. boost::system::error_code native_non_blocking(base_implementation_type& impl,
  151. bool mode, boost::system::error_code& ec)
  152. {
  153. socket_ops::set_internal_non_blocking(impl.socket_, impl.state_, mode, ec);
  154. return ec;
  155. }
  156. // Wait for the socket to become ready to read, ready to write, or to have
  157. // pending error conditions.
  158. boost::system::error_code wait(base_implementation_type& impl,
  159. socket_base::wait_type w, boost::system::error_code& ec)
  160. {
  161. switch (w)
  162. {
  163. case socket_base::wait_read:
  164. socket_ops::poll_read(impl.socket_, impl.state_, -1, ec);
  165. break;
  166. case socket_base::wait_write:
  167. socket_ops::poll_write(impl.socket_, impl.state_, -1, ec);
  168. break;
  169. case socket_base::wait_error:
  170. socket_ops::poll_error(impl.socket_, impl.state_, -1, ec);
  171. break;
  172. default:
  173. ec = boost::asio::error::invalid_argument;
  174. break;
  175. }
  176. return ec;
  177. }
  178. // Asynchronously wait for the socket to become ready to read, ready to
  179. // write, or to have pending error conditions.
  180. template <typename Handler>
  181. void async_wait(base_implementation_type& impl,
  182. socket_base::wait_type w, Handler& handler)
  183. {
  184. bool is_continuation =
  185. boost_asio_handler_cont_helpers::is_continuation(handler);
  186. // Allocate and construct an operation to wrap the handler.
  187. typedef win_iocp_wait_op<Handler> op;
  188. typename op::ptr p = { boost::asio::detail::addressof(handler),
  189. op::ptr::allocate(handler), 0 };
  190. p.p = new (p.v) op(impl.cancel_token_, handler);
  191. BOOST_ASIO_HANDLER_CREATION((io_context_, *p.p, "socket",
  192. &impl, impl.socket_, "async_wait"));
  193. switch (w)
  194. {
  195. case socket_base::wait_read:
  196. start_null_buffers_receive_op(impl, 0, p.p);
  197. break;
  198. case socket_base::wait_write:
  199. start_reactor_op(impl, select_reactor::write_op, p.p);
  200. break;
  201. case socket_base::wait_error:
  202. start_reactor_op(impl, select_reactor::except_op, p.p);
  203. break;
  204. default:
  205. p.p->ec_ = boost::asio::error::invalid_argument;
  206. iocp_service_.post_immediate_completion(p.p, is_continuation);
  207. break;
  208. }
  209. p.v = p.p = 0;
  210. }
  211. // Send the given data to the peer. Returns the number of bytes sent.
  212. template <typename ConstBufferSequence>
  213. size_t send(base_implementation_type& impl,
  214. const ConstBufferSequence& buffers,
  215. socket_base::message_flags flags, boost::system::error_code& ec)
  216. {
  217. buffer_sequence_adapter<boost::asio::const_buffer,
  218. ConstBufferSequence> bufs(buffers);
  219. return socket_ops::sync_send(impl.socket_, impl.state_,
  220. bufs.buffers(), bufs.count(), flags, bufs.all_empty(), ec);
  221. }
  222. // Wait until data can be sent without blocking.
  223. size_t send(base_implementation_type& impl, const null_buffers&,
  224. socket_base::message_flags, boost::system::error_code& ec)
  225. {
  226. // Wait for socket to become ready.
  227. socket_ops::poll_write(impl.socket_, impl.state_, -1, ec);
  228. return 0;
  229. }
  230. // Start an asynchronous send. The data being sent must be valid for the
  231. // lifetime of the asynchronous operation.
  232. template <typename ConstBufferSequence, typename Handler>
  233. void async_send(base_implementation_type& impl,
  234. const ConstBufferSequence& buffers,
  235. socket_base::message_flags flags, Handler& handler)
  236. {
  237. // Allocate and construct an operation to wrap the handler.
  238. typedef win_iocp_socket_send_op<ConstBufferSequence, Handler> 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);
  242. BOOST_ASIO_HANDLER_CREATION((io_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>
  253. void async_send(base_implementation_type& impl, const null_buffers&,
  254. socket_base::message_flags, Handler& handler)
  255. {
  256. // Allocate and construct an operation to wrap the handler.
  257. typedef win_iocp_null_buffers_op<Handler> 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);
  261. BOOST_ASIO_HANDLER_CREATION((io_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, typename Handler>
  288. void async_receive(base_implementation_type& impl,
  289. const MutableBufferSequence& buffers,
  290. socket_base::message_flags flags, Handler& handler)
  291. {
  292. // Allocate and construct an operation to wrap the handler.
  293. typedef win_iocp_socket_recv_op<MutableBufferSequence, Handler> op;
  294. typename op::ptr p = { boost::asio::detail::addressof(handler),
  295. op::ptr::allocate(handler), 0 };
  296. p.p = new (p.v) op(impl.state_, impl.cancel_token_, buffers, handler);
  297. BOOST_ASIO_HANDLER_CREATION((io_context_, *p.p, "socket",
  298. &impl, impl.socket_, "async_receive"));
  299. buffer_sequence_adapter<boost::asio::mutable_buffer,
  300. MutableBufferSequence> bufs(buffers);
  301. start_receive_op(impl, bufs.buffers(), bufs.count(), flags,
  302. (impl.state_ & socket_ops::stream_oriented) != 0 && bufs.all_empty(),
  303. p.p);
  304. p.v = p.p = 0;
  305. }
  306. // Wait until data can be received without blocking.
  307. template <typename Handler>
  308. void async_receive(base_implementation_type& impl, const null_buffers&,
  309. socket_base::message_flags flags, Handler& handler)
  310. {
  311. // Allocate and construct an operation to wrap the handler.
  312. typedef win_iocp_null_buffers_op<Handler> op;
  313. typename op::ptr p = { boost::asio::detail::addressof(handler),
  314. op::ptr::allocate(handler), 0 };
  315. p.p = new (p.v) op(impl.cancel_token_, handler);
  316. BOOST_ASIO_HANDLER_CREATION((io_context_, *p.p, "socket",
  317. &impl, impl.socket_, "async_receive(null_buffers)"));
  318. start_null_buffers_receive_op(impl, flags, p.p);
  319. p.v = p.p = 0;
  320. }
  321. // Receive some data with associated flags. Returns the number of bytes
  322. // received.
  323. template <typename MutableBufferSequence>
  324. size_t receive_with_flags(base_implementation_type& impl,
  325. const MutableBufferSequence& buffers,
  326. socket_base::message_flags in_flags,
  327. socket_base::message_flags& out_flags, boost::system::error_code& ec)
  328. {
  329. buffer_sequence_adapter<boost::asio::mutable_buffer,
  330. MutableBufferSequence> bufs(buffers);
  331. return socket_ops::sync_recvmsg(impl.socket_, impl.state_,
  332. bufs.buffers(), bufs.count(), in_flags, out_flags, ec);
  333. }
  334. // Wait until data can be received without blocking.
  335. size_t receive_with_flags(base_implementation_type& impl,
  336. const null_buffers&, socket_base::message_flags,
  337. socket_base::message_flags& out_flags, boost::system::error_code& ec)
  338. {
  339. // Wait for socket to become ready.
  340. socket_ops::poll_read(impl.socket_, impl.state_, -1, ec);
  341. // Clear out_flags, since we cannot give it any other sensible value when
  342. // performing a null_buffers operation.
  343. out_flags = 0;
  344. return 0;
  345. }
  346. // Start an asynchronous receive. The buffer for the data being received
  347. // must be valid for the lifetime of the asynchronous operation.
  348. template <typename MutableBufferSequence, typename Handler>
  349. void async_receive_with_flags(base_implementation_type& impl,
  350. const MutableBufferSequence& buffers, socket_base::message_flags in_flags,
  351. socket_base::message_flags& out_flags, Handler& handler)
  352. {
  353. // Allocate and construct an operation to wrap the handler.
  354. typedef win_iocp_socket_recvmsg_op<MutableBufferSequence, Handler> op;
  355. typename op::ptr p = { boost::asio::detail::addressof(handler),
  356. op::ptr::allocate(handler), 0 };
  357. p.p = new (p.v) op(impl.cancel_token_, buffers, out_flags, handler);
  358. BOOST_ASIO_HANDLER_CREATION((io_context_, *p.p, "socket",
  359. &impl, impl.socket_, "async_receive_with_flags"));
  360. buffer_sequence_adapter<boost::asio::mutable_buffer,
  361. MutableBufferSequence> bufs(buffers);
  362. start_receive_op(impl, bufs.buffers(), bufs.count(), in_flags, false, p.p);
  363. p.v = p.p = 0;
  364. }
  365. // Wait until data can be received without blocking.
  366. template <typename Handler>
  367. void async_receive_with_flags(base_implementation_type& impl,
  368. const null_buffers&, socket_base::message_flags in_flags,
  369. socket_base::message_flags& out_flags, Handler& handler)
  370. {
  371. // Allocate and construct an operation to wrap the handler.
  372. typedef win_iocp_null_buffers_op<Handler> op;
  373. typename op::ptr p = { boost::asio::detail::addressof(handler),
  374. op::ptr::allocate(handler), 0 };
  375. p.p = new (p.v) op(impl.cancel_token_, handler);
  376. BOOST_ASIO_HANDLER_CREATION((io_context_, *p.p, "socket",
  377. &impl, impl.socket_, "async_receive_with_flags(null_buffers)"));
  378. // Reset out_flags since it can be given no sensible value at this time.
  379. out_flags = 0;
  380. start_null_buffers_receive_op(impl, in_flags, p.p);
  381. p.v = p.p = 0;
  382. }
  383. // Helper function to restart an asynchronous accept operation.
  384. BOOST_ASIO_DECL void restart_accept_op(socket_type s,
  385. socket_holder& new_socket, int family, int type, int protocol,
  386. void* output_buffer, DWORD address_length, operation* op);
  387. protected:
  388. // Open a new socket implementation.
  389. BOOST_ASIO_DECL boost::system::error_code do_open(
  390. base_implementation_type& impl, int family, int type,
  391. int protocol, boost::system::error_code& ec);
  392. // Assign a native socket to a socket implementation.
  393. BOOST_ASIO_DECL boost::system::error_code do_assign(
  394. base_implementation_type& impl, int type,
  395. socket_type native_socket, boost::system::error_code& ec);
  396. // Helper function to start an asynchronous send operation.
  397. BOOST_ASIO_DECL void start_send_op(base_implementation_type& impl,
  398. WSABUF* buffers, std::size_t buffer_count,
  399. socket_base::message_flags flags, bool noop, operation* op);
  400. // Helper function to start an asynchronous send_to operation.
  401. BOOST_ASIO_DECL void start_send_to_op(base_implementation_type& impl,
  402. WSABUF* buffers, std::size_t buffer_count,
  403. const socket_addr_type* addr, int addrlen,
  404. socket_base::message_flags flags, operation* op);
  405. // Helper function to start an asynchronous receive operation.
  406. BOOST_ASIO_DECL void start_receive_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 null_buffers receive operation.
  410. BOOST_ASIO_DECL void start_null_buffers_receive_op(
  411. base_implementation_type& impl,
  412. socket_base::message_flags flags, reactor_op* op);
  413. // Helper function to start an asynchronous receive_from operation.
  414. BOOST_ASIO_DECL void start_receive_from_op(base_implementation_type& impl,
  415. WSABUF* buffers, std::size_t buffer_count, socket_addr_type* addr,
  416. socket_base::message_flags flags, int* addrlen, operation* op);
  417. // Helper function to start an asynchronous accept operation.
  418. BOOST_ASIO_DECL void start_accept_op(base_implementation_type& impl,
  419. bool peer_is_open, socket_holder& new_socket, int family, int type,
  420. int protocol, void* output_buffer, DWORD address_length, operation* op);
  421. // Start an asynchronous read or write operation using the reactor.
  422. BOOST_ASIO_DECL void start_reactor_op(base_implementation_type& impl,
  423. int op_type, reactor_op* op);
  424. // Start the asynchronous connect operation using the reactor.
  425. BOOST_ASIO_DECL void start_connect_op(base_implementation_type& impl,
  426. int family, int type, const socket_addr_type* remote_addr,
  427. std::size_t remote_addrlen, win_iocp_socket_connect_op_base* op);
  428. // Helper function to close a socket when the associated object is being
  429. // destroyed.
  430. BOOST_ASIO_DECL void close_for_destruction(base_implementation_type& impl);
  431. // Update the ID of the thread from which cancellation is safe.
  432. BOOST_ASIO_DECL void update_cancellation_thread_id(
  433. base_implementation_type& impl);
  434. // Helper function to get the reactor. If no reactor has been created yet, a
  435. // new one is obtained from the io_context and a pointer to it is cached in
  436. // this service.
  437. BOOST_ASIO_DECL select_reactor& get_reactor();
  438. // The type of a ConnectEx function pointer, as old SDKs may not provide it.
  439. typedef BOOL (PASCAL *connect_ex_fn)(SOCKET,
  440. const socket_addr_type*, int, void*, DWORD, DWORD*, OVERLAPPED*);
  441. // Helper function to get the ConnectEx pointer. If no ConnectEx pointer has
  442. // been obtained yet, one is obtained using WSAIoctl and the pointer is
  443. // cached. Returns a null pointer if ConnectEx is not available.
  444. BOOST_ASIO_DECL connect_ex_fn get_connect_ex(
  445. base_implementation_type& impl, int type);
  446. // The type of a NtSetInformationFile function pointer.
  447. typedef LONG (NTAPI *nt_set_info_fn)(HANDLE, ULONG_PTR*, void*, ULONG, ULONG);
  448. // Helper function to get the NtSetInformationFile function pointer. If no
  449. // NtSetInformationFile pointer has been obtained yet, one is obtained using
  450. // GetProcAddress and the pointer is cached. Returns a null pointer if
  451. // NtSetInformationFile is not available.
  452. BOOST_ASIO_DECL nt_set_info_fn get_nt_set_info();
  453. // Helper function to emulate InterlockedCompareExchangePointer functionality
  454. // for:
  455. // - very old Platform SDKs; and
  456. // - platform SDKs where MSVC's /Wp64 option causes spurious warnings.
  457. BOOST_ASIO_DECL void* interlocked_compare_exchange_pointer(
  458. void** dest, void* exch, void* cmp);
  459. // Helper function to emulate InterlockedExchangePointer functionality for:
  460. // - very old Platform SDKs; and
  461. // - platform SDKs where MSVC's /Wp64 option causes spurious warnings.
  462. BOOST_ASIO_DECL void* interlocked_exchange_pointer(void** dest, void* val);
  463. // The io_context used to obtain the reactor, if required.
  464. boost::asio::io_context& io_context_;
  465. // The IOCP service used for running asynchronous operations and dispatching
  466. // handlers.
  467. win_iocp_io_context& iocp_service_;
  468. // The reactor used for performing connect operations. This object is created
  469. // only if needed.
  470. select_reactor* reactor_;
  471. // Pointer to ConnectEx implementation.
  472. void* connect_ex_;
  473. // Pointer to NtSetInformationFile implementation.
  474. void* nt_set_info_;
  475. // Mutex to protect access to the linked list of implementations.
  476. boost::asio::detail::mutex mutex_;
  477. // The head of a linked list of all implementations.
  478. base_implementation_type* impl_list_;
  479. };
  480. } // namespace detail
  481. } // namespace asio
  482. } // namespace boost
  483. #include <boost/asio/detail/pop_options.hpp>
  484. #if defined(BOOST_ASIO_HEADER_ONLY)
  485. # include <boost/asio/detail/impl/win_iocp_socket_service_base.ipp>
  486. #endif // defined(BOOST_ASIO_HEADER_ONLY)
  487. #endif // defined(BOOST_ASIO_HAS_IOCP)
  488. #endif // BOOST_ASIO_DETAIL_WIN_IOCP_SOCKET_SERVICE_BASE_HPP