win_iocp_socket_service_base.hpp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831
  1. //
  2. // detail/win_iocp_socket_service_base.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2024 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/associated_cancellation_slot.hpp>
  18. #include <boost/asio/error.hpp>
  19. #include <boost/asio/execution_context.hpp>
  20. #include <boost/asio/socket_base.hpp>
  21. #include <boost/asio/detail/bind_handler.hpp>
  22. #include <boost/asio/detail/buffer_sequence_adapter.hpp>
  23. #include <boost/asio/detail/fenced_block.hpp>
  24. #include <boost/asio/detail/handler_alloc_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) 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. associated_cancellation_slot_t<Handler> slot
  184. = boost::asio::get_associated_cancellation_slot(handler);
  185. bool is_continuation =
  186. boost_asio_handler_cont_helpers::is_continuation(handler);
  187. // Allocate and construct an operation to wrap the handler.
  188. typedef win_iocp_wait_op<Handler, IoExecutor> op;
  189. typename op::ptr p = { boost::asio::detail::addressof(handler),
  190. op::ptr::allocate(handler), 0 };
  191. p.p = new (p.v) op(impl.cancel_token_, handler, io_ex);
  192. BOOST_ASIO_HANDLER_CREATION((context_, *p.p, "socket",
  193. &impl, impl.socket_, "async_wait"));
  194. // Optionally register for per-operation cancellation.
  195. operation* iocp_op = p.p;
  196. if (slot.is_connected())
  197. {
  198. p.p->cancellation_key_ = iocp_op =
  199. &slot.template emplace<reactor_op_cancellation>(
  200. impl.socket_, iocp_op);
  201. }
  202. int op_type = -1;
  203. switch (w)
  204. {
  205. case socket_base::wait_read:
  206. op_type = start_null_buffers_receive_op(impl, 0, p.p, iocp_op);
  207. break;
  208. case socket_base::wait_write:
  209. op_type = select_reactor::write_op;
  210. start_reactor_op(impl, select_reactor::write_op, p.p);
  211. break;
  212. case socket_base::wait_error:
  213. op_type = select_reactor::read_op;
  214. start_reactor_op(impl, select_reactor::except_op, p.p);
  215. break;
  216. default:
  217. p.p->ec_ = boost::asio::error::invalid_argument;
  218. iocp_service_.post_immediate_completion(p.p, is_continuation);
  219. break;
  220. }
  221. p.v = p.p = 0;
  222. // Update cancellation method if the reactor was used.
  223. if (slot.is_connected() && op_type != -1)
  224. {
  225. static_cast<reactor_op_cancellation*>(iocp_op)->use_reactor(
  226. &get_reactor(), &impl.reactor_data_, op_type);
  227. }
  228. }
  229. // Send the given data to the peer. Returns the number of bytes sent.
  230. template <typename ConstBufferSequence>
  231. size_t send(base_implementation_type& impl,
  232. const ConstBufferSequence& buffers,
  233. socket_base::message_flags flags, boost::system::error_code& ec)
  234. {
  235. buffer_sequence_adapter<boost::asio::const_buffer,
  236. ConstBufferSequence> bufs(buffers);
  237. return socket_ops::sync_send(impl.socket_, impl.state_,
  238. bufs.buffers(), bufs.count(), flags, bufs.all_empty(), ec);
  239. }
  240. // Wait until data can be sent without blocking.
  241. size_t send(base_implementation_type& impl, const null_buffers&,
  242. socket_base::message_flags, boost::system::error_code& ec)
  243. {
  244. // Wait for socket to become ready.
  245. socket_ops::poll_write(impl.socket_, impl.state_, -1, ec);
  246. return 0;
  247. }
  248. // Start an asynchronous send. The data being sent must be valid for the
  249. // lifetime of the asynchronous operation.
  250. template <typename ConstBufferSequence, typename Handler, typename IoExecutor>
  251. void async_send(base_implementation_type& impl,
  252. const ConstBufferSequence& buffers, socket_base::message_flags flags,
  253. Handler& handler, const IoExecutor& io_ex)
  254. {
  255. associated_cancellation_slot_t<Handler> slot
  256. = boost::asio::get_associated_cancellation_slot(handler);
  257. // Allocate and construct an operation to wrap the handler.
  258. typedef win_iocp_socket_send_op<
  259. ConstBufferSequence, Handler, IoExecutor> op;
  260. typename op::ptr p = { boost::asio::detail::addressof(handler),
  261. op::ptr::allocate(handler), 0 };
  262. operation* o = p.p = new (p.v) op(
  263. impl.cancel_token_, buffers, handler, io_ex);
  264. BOOST_ASIO_HANDLER_CREATION((context_, *p.p, "socket",
  265. &impl, impl.socket_, "async_send"));
  266. buffer_sequence_adapter<boost::asio::const_buffer,
  267. ConstBufferSequence> bufs(buffers);
  268. // Optionally register for per-operation cancellation.
  269. if (slot.is_connected())
  270. o = &slot.template emplace<iocp_op_cancellation>(impl.socket_, o);
  271. start_send_op(impl, bufs.buffers(), bufs.count(), flags,
  272. (impl.state_ & socket_ops::stream_oriented) != 0 && bufs.all_empty(),
  273. o);
  274. p.v = p.p = 0;
  275. }
  276. // Start an asynchronous wait until data can be sent without blocking.
  277. template <typename Handler, typename IoExecutor>
  278. void async_send(base_implementation_type& impl, const null_buffers&,
  279. socket_base::message_flags, Handler& handler, const IoExecutor& io_ex)
  280. {
  281. // Allocate and construct an operation to wrap the handler.
  282. typedef win_iocp_null_buffers_op<Handler, IoExecutor> op;
  283. typename op::ptr p = { boost::asio::detail::addressof(handler),
  284. op::ptr::allocate(handler), 0 };
  285. p.p = new (p.v) op(impl.cancel_token_, handler, io_ex);
  286. BOOST_ASIO_HANDLER_CREATION((context_, *p.p, "socket",
  287. &impl, impl.socket_, "async_send(null_buffers)"));
  288. start_reactor_op(impl, select_reactor::write_op, p.p);
  289. p.v = p.p = 0;
  290. }
  291. // Receive some data from the peer. Returns the number of bytes received.
  292. template <typename MutableBufferSequence>
  293. size_t receive(base_implementation_type& impl,
  294. const MutableBufferSequence& buffers,
  295. socket_base::message_flags flags, boost::system::error_code& ec)
  296. {
  297. buffer_sequence_adapter<boost::asio::mutable_buffer,
  298. MutableBufferSequence> bufs(buffers);
  299. return socket_ops::sync_recv(impl.socket_, impl.state_,
  300. bufs.buffers(), bufs.count(), flags, bufs.all_empty(), ec);
  301. }
  302. // Wait until data can be received without blocking.
  303. size_t receive(base_implementation_type& impl, const null_buffers&,
  304. socket_base::message_flags, boost::system::error_code& ec)
  305. {
  306. // Wait for socket to become ready.
  307. socket_ops::poll_read(impl.socket_, impl.state_, -1, ec);
  308. return 0;
  309. }
  310. // Start an asynchronous receive. The buffer for the data being received
  311. // must be valid for the lifetime of the asynchronous operation.
  312. template <typename MutableBufferSequence,
  313. typename Handler, typename IoExecutor>
  314. void async_receive(base_implementation_type& impl,
  315. const MutableBufferSequence& buffers, socket_base::message_flags flags,
  316. Handler& handler, const IoExecutor& io_ex)
  317. {
  318. associated_cancellation_slot_t<Handler> slot
  319. = boost::asio::get_associated_cancellation_slot(handler);
  320. // Allocate and construct an operation to wrap the handler.
  321. typedef win_iocp_socket_recv_op<
  322. MutableBufferSequence, Handler, IoExecutor> op;
  323. typename op::ptr p = { boost::asio::detail::addressof(handler),
  324. op::ptr::allocate(handler), 0 };
  325. operation* o = p.p = new (p.v) op(impl.state_,
  326. impl.cancel_token_, buffers, handler, io_ex);
  327. BOOST_ASIO_HANDLER_CREATION((context_, *p.p, "socket",
  328. &impl, impl.socket_, "async_receive"));
  329. buffer_sequence_adapter<boost::asio::mutable_buffer,
  330. MutableBufferSequence> bufs(buffers);
  331. // Optionally register for per-operation cancellation.
  332. if (slot.is_connected())
  333. o = &slot.template emplace<iocp_op_cancellation>(impl.socket_, o);
  334. start_receive_op(impl, bufs.buffers(), bufs.count(), flags,
  335. (impl.state_ & socket_ops::stream_oriented) != 0 && bufs.all_empty(),
  336. o);
  337. p.v = p.p = 0;
  338. }
  339. // Wait until data can be received without blocking.
  340. template <typename Handler, typename IoExecutor>
  341. void async_receive(base_implementation_type& impl,
  342. const null_buffers&, socket_base::message_flags flags,
  343. Handler& handler, const IoExecutor& io_ex)
  344. {
  345. associated_cancellation_slot_t<Handler> slot
  346. = boost::asio::get_associated_cancellation_slot(handler);
  347. // Allocate and construct an operation to wrap the handler.
  348. typedef win_iocp_null_buffers_op<Handler, IoExecutor> op;
  349. typename op::ptr p = { boost::asio::detail::addressof(handler),
  350. op::ptr::allocate(handler), 0 };
  351. p.p = new (p.v) op(impl.cancel_token_, handler, io_ex);
  352. BOOST_ASIO_HANDLER_CREATION((context_, *p.p, "socket",
  353. &impl, impl.socket_, "async_receive(null_buffers)"));
  354. // Optionally register for per-operation cancellation.
  355. operation* iocp_op = p.p;
  356. if (slot.is_connected())
  357. {
  358. p.p->cancellation_key_ = iocp_op =
  359. &slot.template emplace<reactor_op_cancellation>(
  360. impl.socket_, iocp_op);
  361. }
  362. int op_type = start_null_buffers_receive_op(impl, flags, p.p, iocp_op);
  363. p.v = p.p = 0;
  364. // Update cancellation method if the reactor was used.
  365. if (slot.is_connected() && op_type != -1)
  366. {
  367. static_cast<reactor_op_cancellation*>(iocp_op)->use_reactor(
  368. &get_reactor(), &impl.reactor_data_, op_type);
  369. }
  370. }
  371. // Receive some data with associated flags. Returns the number of bytes
  372. // received.
  373. template <typename MutableBufferSequence>
  374. size_t receive_with_flags(base_implementation_type& impl,
  375. const MutableBufferSequence& buffers,
  376. socket_base::message_flags in_flags,
  377. socket_base::message_flags& out_flags, boost::system::error_code& ec)
  378. {
  379. buffer_sequence_adapter<boost::asio::mutable_buffer,
  380. MutableBufferSequence> bufs(buffers);
  381. return socket_ops::sync_recvmsg(impl.socket_, impl.state_,
  382. bufs.buffers(), bufs.count(), in_flags, out_flags, ec);
  383. }
  384. // Wait until data can be received without blocking.
  385. size_t receive_with_flags(base_implementation_type& impl,
  386. const null_buffers&, socket_base::message_flags,
  387. socket_base::message_flags& out_flags, boost::system::error_code& ec)
  388. {
  389. // Wait for socket to become ready.
  390. socket_ops::poll_read(impl.socket_, impl.state_, -1, ec);
  391. // Clear out_flags, since we cannot give it any other sensible value when
  392. // performing a null_buffers operation.
  393. out_flags = 0;
  394. return 0;
  395. }
  396. // Start an asynchronous receive. The buffer for the data being received
  397. // must be valid for the lifetime of the asynchronous operation.
  398. template <typename MutableBufferSequence,
  399. typename Handler, typename IoExecutor>
  400. void async_receive_with_flags(base_implementation_type& impl,
  401. const MutableBufferSequence& buffers, socket_base::message_flags in_flags,
  402. socket_base::message_flags& out_flags, Handler& handler,
  403. const IoExecutor& io_ex)
  404. {
  405. associated_cancellation_slot_t<Handler> slot
  406. = boost::asio::get_associated_cancellation_slot(handler);
  407. // Allocate and construct an operation to wrap the handler.
  408. typedef win_iocp_socket_recvmsg_op<
  409. MutableBufferSequence, Handler, IoExecutor> op;
  410. typename op::ptr p = { boost::asio::detail::addressof(handler),
  411. op::ptr::allocate(handler), 0 };
  412. operation* o = p.p = new (p.v) op(impl.cancel_token_,
  413. buffers, out_flags, handler, io_ex);
  414. BOOST_ASIO_HANDLER_CREATION((context_, *p.p, "socket",
  415. &impl, impl.socket_, "async_receive_with_flags"));
  416. buffer_sequence_adapter<boost::asio::mutable_buffer,
  417. MutableBufferSequence> bufs(buffers);
  418. // Optionally register for per-operation cancellation.
  419. if (slot.is_connected())
  420. o = &slot.template emplace<iocp_op_cancellation>(impl.socket_, o);
  421. start_receive_op(impl, bufs.buffers(), bufs.count(), in_flags, false, o);
  422. p.v = p.p = 0;
  423. }
  424. // Wait until data can be received without blocking.
  425. template <typename Handler, typename IoExecutor>
  426. void async_receive_with_flags(base_implementation_type& impl,
  427. const null_buffers&, socket_base::message_flags in_flags,
  428. socket_base::message_flags& out_flags, Handler& handler,
  429. const IoExecutor& io_ex)
  430. {
  431. associated_cancellation_slot_t<Handler> slot
  432. = boost::asio::get_associated_cancellation_slot(handler);
  433. // Allocate and construct an operation to wrap the handler.
  434. typedef win_iocp_null_buffers_op<Handler, IoExecutor> op;
  435. typename op::ptr p = { boost::asio::detail::addressof(handler),
  436. op::ptr::allocate(handler), 0 };
  437. p.p = new (p.v) op(impl.cancel_token_, handler, io_ex);
  438. BOOST_ASIO_HANDLER_CREATION((context_, *p.p, "socket",
  439. &impl, impl.socket_, "async_receive_with_flags(null_buffers)"));
  440. // Reset out_flags since it can be given no sensible value at this time.
  441. out_flags = 0;
  442. // Optionally register for per-operation cancellation.
  443. operation* iocp_op = p.p;
  444. if (slot.is_connected())
  445. {
  446. p.p->cancellation_key_ = iocp_op =
  447. &slot.template emplace<reactor_op_cancellation>(
  448. impl.socket_, iocp_op);
  449. }
  450. int op_type = start_null_buffers_receive_op(impl, in_flags, p.p, iocp_op);
  451. p.v = p.p = 0;
  452. // Update cancellation method if the reactor was used.
  453. if (slot.is_connected() && op_type != -1)
  454. {
  455. static_cast<reactor_op_cancellation*>(iocp_op)->use_reactor(
  456. &get_reactor(), &impl.reactor_data_, op_type);
  457. }
  458. }
  459. // Helper function to restart an asynchronous accept operation.
  460. BOOST_ASIO_DECL void restart_accept_op(socket_type s,
  461. socket_holder& new_socket, int family, int type,
  462. int protocol, void* output_buffer, DWORD address_length,
  463. long* cancel_requested, operation* op);
  464. protected:
  465. // Open a new socket implementation.
  466. BOOST_ASIO_DECL boost::system::error_code do_open(
  467. base_implementation_type& impl, int family, int type,
  468. int protocol, boost::system::error_code& ec);
  469. // Assign a native socket to a socket implementation.
  470. BOOST_ASIO_DECL boost::system::error_code do_assign(
  471. base_implementation_type& impl, int type,
  472. socket_type native_socket, boost::system::error_code& ec);
  473. // Helper function to start an asynchronous send operation.
  474. BOOST_ASIO_DECL void start_send_op(base_implementation_type& impl,
  475. WSABUF* buffers, std::size_t buffer_count,
  476. socket_base::message_flags flags, bool noop, operation* op);
  477. // Helper function to start an asynchronous send_to operation.
  478. BOOST_ASIO_DECL void start_send_to_op(base_implementation_type& impl,
  479. WSABUF* buffers, std::size_t buffer_count, const void* addr,
  480. int addrlen, socket_base::message_flags flags, operation* op);
  481. // Helper function to start an asynchronous receive operation.
  482. BOOST_ASIO_DECL void start_receive_op(base_implementation_type& impl,
  483. WSABUF* buffers, std::size_t buffer_count,
  484. socket_base::message_flags flags, bool noop, operation* op);
  485. // Helper function to start an asynchronous null_buffers receive operation.
  486. BOOST_ASIO_DECL int start_null_buffers_receive_op(
  487. base_implementation_type& impl, socket_base::message_flags flags,
  488. reactor_op* op, operation* iocp_op);
  489. // Helper function to start an asynchronous receive_from operation.
  490. BOOST_ASIO_DECL void start_receive_from_op(base_implementation_type& impl,
  491. WSABUF* buffers, std::size_t buffer_count, void* addr,
  492. socket_base::message_flags flags, int* addrlen, operation* op);
  493. // Helper function to start an asynchronous accept operation.
  494. BOOST_ASIO_DECL void start_accept_op(base_implementation_type& impl,
  495. bool peer_is_open, socket_holder& new_socket, int family, int type,
  496. int protocol, void* output_buffer, DWORD address_length, operation* op);
  497. // Start an asynchronous read or write operation using the reactor.
  498. BOOST_ASIO_DECL void start_reactor_op(base_implementation_type& impl,
  499. int op_type, reactor_op* op);
  500. // Start the asynchronous connect operation using the reactor.
  501. BOOST_ASIO_DECL int start_connect_op(base_implementation_type& impl,
  502. int family, int type, const void* remote_addr, std::size_t remote_addrlen,
  503. win_iocp_socket_connect_op_base* op, operation* iocp_op);
  504. // Helper function to close a socket when the associated object is being
  505. // destroyed.
  506. BOOST_ASIO_DECL void close_for_destruction(base_implementation_type& impl);
  507. // Update the ID of the thread from which cancellation is safe.
  508. BOOST_ASIO_DECL void update_cancellation_thread_id(
  509. base_implementation_type& impl);
  510. // Helper function to get the reactor. If no reactor has been created yet, a
  511. // new one is obtained from the execution context and a pointer to it is
  512. // cached in this service.
  513. BOOST_ASIO_DECL select_reactor& get_reactor();
  514. // The type of a ConnectEx function pointer, as old SDKs may not provide it.
  515. typedef BOOL (PASCAL *connect_ex_fn)(SOCKET,
  516. const socket_addr_type*, int, void*, DWORD, DWORD*, OVERLAPPED*);
  517. // Helper function to get the ConnectEx pointer. If no ConnectEx pointer has
  518. // been obtained yet, one is obtained using WSAIoctl and the pointer is
  519. // cached. Returns a null pointer if ConnectEx is not available.
  520. BOOST_ASIO_DECL connect_ex_fn get_connect_ex(
  521. base_implementation_type& impl, int type);
  522. // The type of a NtSetInformationFile function pointer.
  523. typedef LONG (NTAPI *nt_set_info_fn)(HANDLE, ULONG_PTR*, void*, ULONG, ULONG);
  524. // Helper function to get the NtSetInformationFile function pointer. If no
  525. // NtSetInformationFile pointer has been obtained yet, one is obtained using
  526. // GetProcAddress and the pointer is cached. Returns a null pointer if
  527. // NtSetInformationFile is not available.
  528. BOOST_ASIO_DECL nt_set_info_fn get_nt_set_info();
  529. // Helper function to emulate InterlockedCompareExchangePointer functionality
  530. // for:
  531. // - very old Platform SDKs; and
  532. // - platform SDKs where MSVC's /Wp64 option causes spurious warnings.
  533. BOOST_ASIO_DECL void* interlocked_compare_exchange_pointer(
  534. void** dest, void* exch, void* cmp);
  535. // Helper function to emulate InterlockedExchangePointer functionality for:
  536. // - very old Platform SDKs; and
  537. // - platform SDKs where MSVC's /Wp64 option causes spurious warnings.
  538. BOOST_ASIO_DECL void* interlocked_exchange_pointer(void** dest, void* val);
  539. // Helper class used to implement per operation cancellation.
  540. class iocp_op_cancellation : public operation
  541. {
  542. public:
  543. iocp_op_cancellation(SOCKET s, operation* target)
  544. : operation(&iocp_op_cancellation::do_complete),
  545. socket_(s),
  546. target_(target)
  547. {
  548. }
  549. static void do_complete(void* owner, operation* base,
  550. const boost::system::error_code& result_ec,
  551. std::size_t bytes_transferred)
  552. {
  553. iocp_op_cancellation* o = static_cast<iocp_op_cancellation*>(base);
  554. o->target_->complete(owner, result_ec, bytes_transferred);
  555. }
  556. void operator()(cancellation_type_t type)
  557. {
  558. #if defined(_WIN32_WINNT) && (_WIN32_WINNT >= 0x0600)
  559. if (!!(type &
  560. (cancellation_type::terminal
  561. | cancellation_type::partial
  562. | cancellation_type::total)))
  563. {
  564. HANDLE sock_as_handle = reinterpret_cast<HANDLE>(socket_);
  565. ::CancelIoEx(sock_as_handle, this);
  566. }
  567. #else // defined(_WIN32_WINNT) && (_WIN32_WINNT >= 0x0600)
  568. (void)type;
  569. #endif // defined(_WIN32_WINNT) && (_WIN32_WINNT >= 0x0600)
  570. }
  571. private:
  572. SOCKET socket_;
  573. operation* target_;
  574. };
  575. // Helper class used to implement per operation cancellation.
  576. class accept_op_cancellation : public operation
  577. {
  578. public:
  579. accept_op_cancellation(SOCKET s, operation* target)
  580. : operation(&iocp_op_cancellation::do_complete),
  581. socket_(s),
  582. target_(target),
  583. cancel_requested_(0)
  584. {
  585. }
  586. static void do_complete(void* owner, operation* base,
  587. const boost::system::error_code& result_ec,
  588. std::size_t bytes_transferred)
  589. {
  590. accept_op_cancellation* o = static_cast<accept_op_cancellation*>(base);
  591. o->target_->complete(owner, result_ec, bytes_transferred);
  592. }
  593. long* get_cancel_requested()
  594. {
  595. return &cancel_requested_;
  596. }
  597. void operator()(cancellation_type_t type)
  598. {
  599. #if defined(_WIN32_WINNT) && (_WIN32_WINNT >= 0x0600)
  600. if (!!(type &
  601. (cancellation_type::terminal
  602. | cancellation_type::partial
  603. | cancellation_type::total)))
  604. {
  605. HANDLE sock_as_handle = reinterpret_cast<HANDLE>(socket_);
  606. ::CancelIoEx(sock_as_handle, this);
  607. }
  608. #else // defined(_WIN32_WINNT) && (_WIN32_WINNT >= 0x0600)
  609. (void)type;
  610. #endif // defined(_WIN32_WINNT) && (_WIN32_WINNT >= 0x0600)
  611. }
  612. private:
  613. SOCKET socket_;
  614. operation* target_;
  615. long cancel_requested_;
  616. };
  617. // Helper class used to implement per operation cancellation.
  618. class reactor_op_cancellation : public operation
  619. {
  620. public:
  621. reactor_op_cancellation(SOCKET s, operation* base)
  622. : operation(&reactor_op_cancellation::do_complete),
  623. socket_(s),
  624. target_(base),
  625. reactor_(0),
  626. reactor_data_(0),
  627. op_type_(-1)
  628. {
  629. }
  630. void use_reactor(select_reactor* r,
  631. select_reactor::per_descriptor_data* p, int o)
  632. {
  633. reactor_ = r;
  634. reactor_data_ = p;
  635. op_type_ = o;
  636. }
  637. static void do_complete(void* owner, operation* base,
  638. const boost::system::error_code& result_ec,
  639. std::size_t bytes_transferred)
  640. {
  641. reactor_op_cancellation* o = static_cast<reactor_op_cancellation*>(base);
  642. o->target_->complete(owner, result_ec, bytes_transferred);
  643. }
  644. void operator()(cancellation_type_t type)
  645. {
  646. if (!!(type &
  647. (cancellation_type::terminal
  648. | cancellation_type::partial
  649. | cancellation_type::total)))
  650. {
  651. if (reactor_)
  652. {
  653. reactor_->cancel_ops_by_key(socket_,
  654. *reactor_data_, op_type_, this);
  655. }
  656. else
  657. {
  658. #if defined(_WIN32_WINNT) && (_WIN32_WINNT >= 0x0600)
  659. HANDLE sock_as_handle = reinterpret_cast<HANDLE>(socket_);
  660. ::CancelIoEx(sock_as_handle, this);
  661. #endif // defined(_WIN32_WINNT) && (_WIN32_WINNT >= 0x0600)
  662. }
  663. }
  664. }
  665. private:
  666. SOCKET socket_;
  667. operation* target_;
  668. select_reactor* reactor_;
  669. select_reactor::per_descriptor_data* reactor_data_;
  670. int op_type_;
  671. };
  672. // The execution context used to obtain the reactor, if required.
  673. execution_context& context_;
  674. // The IOCP service used for running asynchronous operations and dispatching
  675. // handlers.
  676. win_iocp_io_context& iocp_service_;
  677. // The reactor used for performing connect operations. This object is created
  678. // only if needed.
  679. select_reactor* reactor_;
  680. // Pointer to ConnectEx implementation.
  681. void* connect_ex_;
  682. // Pointer to NtSetInformationFile implementation.
  683. void* nt_set_info_;
  684. // Mutex to protect access to the linked list of implementations.
  685. boost::asio::detail::mutex mutex_;
  686. // The head of a linked list of all implementations.
  687. base_implementation_type* impl_list_;
  688. };
  689. } // namespace detail
  690. } // namespace asio
  691. } // namespace boost
  692. #include <boost/asio/detail/pop_options.hpp>
  693. #if defined(BOOST_ASIO_HEADER_ONLY)
  694. # include <boost/asio/detail/impl/win_iocp_socket_service_base.ipp>
  695. #endif // defined(BOOST_ASIO_HEADER_ONLY)
  696. #endif // defined(BOOST_ASIO_HAS_IOCP)
  697. #endif // BOOST_ASIO_DETAIL_WIN_IOCP_SOCKET_SERVICE_BASE_HPP