win_iocp_socket_service_base.hpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829
  1. //
  2. // detail/win_iocp_socket_service_base.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2023 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 "asio/associated_cancellation_slot.hpp"
  18. #include "asio/error.hpp"
  19. #include "asio/execution_context.hpp"
  20. #include "asio/socket_base.hpp"
  21. #include "asio/detail/bind_handler.hpp"
  22. #include "asio/detail/buffer_sequence_adapter.hpp"
  23. #include "asio/detail/fenced_block.hpp"
  24. #include "asio/detail/handler_alloc_helpers.hpp"
  25. #include "asio/detail/memory.hpp"
  26. #include "asio/detail/mutex.hpp"
  27. #include "asio/detail/operation.hpp"
  28. #include "asio/detail/reactor_op.hpp"
  29. #include "asio/detail/select_reactor.hpp"
  30. #include "asio/detail/socket_holder.hpp"
  31. #include "asio/detail/socket_ops.hpp"
  32. #include "asio/detail/socket_types.hpp"
  33. #include "asio/detail/win_iocp_io_context.hpp"
  34. #include "asio/detail/win_iocp_null_buffers_op.hpp"
  35. #include "asio/detail/win_iocp_socket_connect_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/win_iocp_socket_recvmsg_op.hpp"
  39. #include "asio/detail/win_iocp_wait_op.hpp"
  40. #include "asio/detail/push_options.hpp"
  41. namespace asio {
  42. namespace detail {
  43. class win_iocp_socket_service_base
  44. {
  45. public:
  46. // The implementation type of the socket.
  47. struct base_implementation_type
  48. {
  49. // The native socket representation.
  50. socket_type socket_;
  51. // The current state of the socket.
  52. socket_ops::state_type state_;
  53. // We use a shared pointer as a cancellation token here to work around the
  54. // broken Windows support for cancellation. MSDN says that when you call
  55. // closesocket any outstanding WSARecv or WSASend operations will complete
  56. // with the error ERROR_OPERATION_ABORTED. In practice they complete with
  57. // ERROR_NETNAME_DELETED, which means you can't tell the difference between
  58. // a local cancellation and the socket being hard-closed by the peer.
  59. socket_ops::shared_cancel_token_type cancel_token_;
  60. // Per-descriptor data used by the reactor.
  61. select_reactor::per_descriptor_data reactor_data_;
  62. #if defined(ASIO_ENABLE_CANCELIO)
  63. // The ID of the thread from which it is safe to cancel asynchronous
  64. // operations. 0 means no asynchronous operations have been started yet.
  65. // ~0 means asynchronous operations have been started from more than one
  66. // thread, and cancellation is not supported for the socket.
  67. DWORD safe_cancellation_thread_id_;
  68. #endif // defined(ASIO_ENABLE_CANCELIO)
  69. // Pointers to adjacent socket implementations in linked list.
  70. base_implementation_type* next_;
  71. base_implementation_type* prev_;
  72. };
  73. // Constructor.
  74. ASIO_DECL win_iocp_socket_service_base(execution_context& context);
  75. // Destroy all user-defined handler objects owned by the service.
  76. ASIO_DECL void base_shutdown();
  77. // Construct a new socket implementation.
  78. ASIO_DECL void construct(base_implementation_type& impl);
  79. // Move-construct a new socket implementation.
  80. ASIO_DECL void base_move_construct(base_implementation_type& impl,
  81. base_implementation_type& other_impl) noexcept;
  82. // Move-assign from another socket implementation.
  83. ASIO_DECL void base_move_assign(base_implementation_type& impl,
  84. win_iocp_socket_service_base& other_service,
  85. base_implementation_type& other_impl);
  86. // Destroy a socket implementation.
  87. ASIO_DECL void destroy(base_implementation_type& impl);
  88. // Determine whether the socket is open.
  89. bool is_open(const base_implementation_type& impl) const
  90. {
  91. return impl.socket_ != invalid_socket;
  92. }
  93. // Destroy a socket implementation.
  94. ASIO_DECL asio::error_code close(
  95. base_implementation_type& impl, asio::error_code& ec);
  96. // Release ownership of the socket.
  97. ASIO_DECL socket_type release(
  98. base_implementation_type& impl, asio::error_code& ec);
  99. // Cancel all operations associated with the socket.
  100. ASIO_DECL asio::error_code cancel(
  101. base_implementation_type& impl, asio::error_code& ec);
  102. // Determine whether the socket is at the out-of-band data mark.
  103. bool at_mark(const base_implementation_type& impl,
  104. asio::error_code& ec) const
  105. {
  106. return socket_ops::sockatmark(impl.socket_, ec);
  107. }
  108. // Determine the number of bytes available for reading.
  109. std::size_t available(const base_implementation_type& impl,
  110. asio::error_code& ec) const
  111. {
  112. return socket_ops::available(impl.socket_, ec);
  113. }
  114. // Place the socket into the state where it will listen for new connections.
  115. asio::error_code listen(base_implementation_type& impl,
  116. int backlog, asio::error_code& ec)
  117. {
  118. socket_ops::listen(impl.socket_, backlog, ec);
  119. return ec;
  120. }
  121. // Perform an IO control command on the socket.
  122. template <typename IO_Control_Command>
  123. asio::error_code io_control(base_implementation_type& impl,
  124. IO_Control_Command& command, asio::error_code& ec)
  125. {
  126. socket_ops::ioctl(impl.socket_, impl.state_, command.name(),
  127. static_cast<ioctl_arg_type*>(command.data()), ec);
  128. return ec;
  129. }
  130. // Gets the non-blocking mode of the socket.
  131. bool non_blocking(const base_implementation_type& impl) const
  132. {
  133. return (impl.state_ & socket_ops::user_set_non_blocking) != 0;
  134. }
  135. // Sets the non-blocking mode of the socket.
  136. asio::error_code non_blocking(base_implementation_type& impl,
  137. bool mode, asio::error_code& ec)
  138. {
  139. socket_ops::set_user_non_blocking(impl.socket_, impl.state_, mode, ec);
  140. return ec;
  141. }
  142. // Gets the non-blocking mode of the native socket implementation.
  143. bool native_non_blocking(const base_implementation_type& impl) const
  144. {
  145. return (impl.state_ & socket_ops::internal_non_blocking) != 0;
  146. }
  147. // Sets the non-blocking mode of the native socket implementation.
  148. asio::error_code native_non_blocking(base_implementation_type& impl,
  149. bool mode, asio::error_code& ec)
  150. {
  151. socket_ops::set_internal_non_blocking(impl.socket_, impl.state_, mode, ec);
  152. return ec;
  153. }
  154. // Wait for the socket to become ready to read, ready to write, or to have
  155. // pending error conditions.
  156. asio::error_code wait(base_implementation_type& impl,
  157. socket_base::wait_type w, asio::error_code& ec)
  158. {
  159. switch (w)
  160. {
  161. case socket_base::wait_read:
  162. socket_ops::poll_read(impl.socket_, impl.state_, -1, ec);
  163. break;
  164. case socket_base::wait_write:
  165. socket_ops::poll_write(impl.socket_, impl.state_, -1, ec);
  166. break;
  167. case socket_base::wait_error:
  168. socket_ops::poll_error(impl.socket_, impl.state_, -1, ec);
  169. break;
  170. default:
  171. ec = asio::error::invalid_argument;
  172. break;
  173. }
  174. return ec;
  175. }
  176. // Asynchronously wait for the socket to become ready to read, ready to
  177. // write, or to have pending error conditions.
  178. template <typename Handler, typename IoExecutor>
  179. void async_wait(base_implementation_type& impl,
  180. socket_base::wait_type w, Handler& handler, const IoExecutor& io_ex)
  181. {
  182. associated_cancellation_slot_t<Handler> slot
  183. = asio::get_associated_cancellation_slot(handler);
  184. bool is_continuation =
  185. asio_handler_cont_helpers::is_continuation(handler);
  186. // Allocate and construct an operation to wrap the handler.
  187. typedef win_iocp_wait_op<Handler, IoExecutor> op;
  188. typename op::ptr p = { asio::detail::addressof(handler),
  189. op::ptr::allocate(handler), 0 };
  190. p.p = new (p.v) op(impl.cancel_token_, handler, io_ex);
  191. ASIO_HANDLER_CREATION((context_, *p.p, "socket",
  192. &impl, impl.socket_, "async_wait"));
  193. // Optionally register for per-operation cancellation.
  194. operation* iocp_op = p.p;
  195. if (slot.is_connected())
  196. {
  197. p.p->cancellation_key_ = iocp_op =
  198. &slot.template emplace<reactor_op_cancellation>(
  199. impl.socket_, iocp_op);
  200. }
  201. int op_type = -1;
  202. switch (w)
  203. {
  204. case socket_base::wait_read:
  205. op_type = start_null_buffers_receive_op(impl, 0, p.p, iocp_op);
  206. break;
  207. case socket_base::wait_write:
  208. op_type = select_reactor::write_op;
  209. start_reactor_op(impl, select_reactor::write_op, p.p);
  210. break;
  211. case socket_base::wait_error:
  212. op_type = select_reactor::read_op;
  213. start_reactor_op(impl, select_reactor::except_op, p.p);
  214. break;
  215. default:
  216. p.p->ec_ = asio::error::invalid_argument;
  217. iocp_service_.post_immediate_completion(p.p, is_continuation);
  218. break;
  219. }
  220. p.v = p.p = 0;
  221. // Update cancellation method if the reactor was used.
  222. if (slot.is_connected() && op_type != -1)
  223. {
  224. static_cast<reactor_op_cancellation*>(iocp_op)->use_reactor(
  225. &get_reactor(), &impl.reactor_data_, op_type);
  226. }
  227. }
  228. // Send the given data to the peer. Returns the number of bytes sent.
  229. template <typename ConstBufferSequence>
  230. size_t send(base_implementation_type& impl,
  231. const ConstBufferSequence& buffers,
  232. socket_base::message_flags flags, asio::error_code& ec)
  233. {
  234. buffer_sequence_adapter<asio::const_buffer,
  235. ConstBufferSequence> bufs(buffers);
  236. return socket_ops::sync_send(impl.socket_, impl.state_,
  237. bufs.buffers(), bufs.count(), flags, bufs.all_empty(), ec);
  238. }
  239. // Wait until data can be sent without blocking.
  240. size_t send(base_implementation_type& impl, const null_buffers&,
  241. socket_base::message_flags, asio::error_code& ec)
  242. {
  243. // Wait for socket to become ready.
  244. socket_ops::poll_write(impl.socket_, impl.state_, -1, ec);
  245. return 0;
  246. }
  247. // Start an asynchronous send. The data being sent must be valid for the
  248. // lifetime of the asynchronous operation.
  249. template <typename ConstBufferSequence, typename Handler, typename IoExecutor>
  250. void async_send(base_implementation_type& impl,
  251. const ConstBufferSequence& buffers, socket_base::message_flags flags,
  252. Handler& handler, const IoExecutor& io_ex)
  253. {
  254. associated_cancellation_slot_t<Handler> slot
  255. = asio::get_associated_cancellation_slot(handler);
  256. // Allocate and construct an operation to wrap the handler.
  257. typedef win_iocp_socket_send_op<
  258. ConstBufferSequence, Handler, IoExecutor> op;
  259. typename op::ptr p = { asio::detail::addressof(handler),
  260. op::ptr::allocate(handler), 0 };
  261. operation* o = p.p = new (p.v) op(
  262. impl.cancel_token_, buffers, handler, io_ex);
  263. ASIO_HANDLER_CREATION((context_, *p.p, "socket",
  264. &impl, impl.socket_, "async_send"));
  265. buffer_sequence_adapter<asio::const_buffer,
  266. ConstBufferSequence> bufs(buffers);
  267. // Optionally register for per-operation cancellation.
  268. if (slot.is_connected())
  269. o = &slot.template emplace<iocp_op_cancellation>(impl.socket_, o);
  270. start_send_op(impl, bufs.buffers(), bufs.count(), flags,
  271. (impl.state_ & socket_ops::stream_oriented) != 0 && bufs.all_empty(),
  272. o);
  273. p.v = p.p = 0;
  274. }
  275. // Start an asynchronous wait until data can be sent without blocking.
  276. template <typename Handler, typename IoExecutor>
  277. void async_send(base_implementation_type& impl, const null_buffers&,
  278. socket_base::message_flags, Handler& handler, const IoExecutor& io_ex)
  279. {
  280. // Allocate and construct an operation to wrap the handler.
  281. typedef win_iocp_null_buffers_op<Handler, IoExecutor> op;
  282. typename op::ptr p = { asio::detail::addressof(handler),
  283. op::ptr::allocate(handler), 0 };
  284. p.p = new (p.v) op(impl.cancel_token_, handler, io_ex);
  285. ASIO_HANDLER_CREATION((context_, *p.p, "socket",
  286. &impl, impl.socket_, "async_send(null_buffers)"));
  287. start_reactor_op(impl, select_reactor::write_op, p.p);
  288. p.v = p.p = 0;
  289. }
  290. // Receive some data from the peer. Returns the number of bytes received.
  291. template <typename MutableBufferSequence>
  292. size_t receive(base_implementation_type& impl,
  293. const MutableBufferSequence& buffers,
  294. socket_base::message_flags flags, asio::error_code& ec)
  295. {
  296. buffer_sequence_adapter<asio::mutable_buffer,
  297. MutableBufferSequence> bufs(buffers);
  298. return socket_ops::sync_recv(impl.socket_, impl.state_,
  299. bufs.buffers(), bufs.count(), flags, bufs.all_empty(), ec);
  300. }
  301. // Wait until data can be received without blocking.
  302. size_t receive(base_implementation_type& impl, const null_buffers&,
  303. socket_base::message_flags, asio::error_code& ec)
  304. {
  305. // Wait for socket to become ready.
  306. socket_ops::poll_read(impl.socket_, impl.state_, -1, ec);
  307. return 0;
  308. }
  309. // Start an asynchronous receive. The buffer for the data being received
  310. // must be valid for the lifetime of the asynchronous operation.
  311. template <typename MutableBufferSequence,
  312. typename Handler, typename IoExecutor>
  313. void async_receive(base_implementation_type& impl,
  314. const MutableBufferSequence& buffers, socket_base::message_flags flags,
  315. Handler& handler, const IoExecutor& io_ex)
  316. {
  317. associated_cancellation_slot_t<Handler> slot
  318. = asio::get_associated_cancellation_slot(handler);
  319. // Allocate and construct an operation to wrap the handler.
  320. typedef win_iocp_socket_recv_op<
  321. MutableBufferSequence, Handler, IoExecutor> op;
  322. typename op::ptr p = { asio::detail::addressof(handler),
  323. op::ptr::allocate(handler), 0 };
  324. operation* o = p.p = new (p.v) op(impl.state_,
  325. impl.cancel_token_, buffers, handler, io_ex);
  326. ASIO_HANDLER_CREATION((context_, *p.p, "socket",
  327. &impl, impl.socket_, "async_receive"));
  328. buffer_sequence_adapter<asio::mutable_buffer,
  329. MutableBufferSequence> bufs(buffers);
  330. // Optionally register for per-operation cancellation.
  331. if (slot.is_connected())
  332. o = &slot.template emplace<iocp_op_cancellation>(impl.socket_, o);
  333. start_receive_op(impl, bufs.buffers(), bufs.count(), flags,
  334. (impl.state_ & socket_ops::stream_oriented) != 0 && bufs.all_empty(),
  335. o);
  336. p.v = p.p = 0;
  337. }
  338. // Wait until data can be received without blocking.
  339. template <typename Handler, typename IoExecutor>
  340. void async_receive(base_implementation_type& impl,
  341. const null_buffers&, socket_base::message_flags flags,
  342. Handler& handler, const IoExecutor& io_ex)
  343. {
  344. associated_cancellation_slot_t<Handler> slot
  345. = asio::get_associated_cancellation_slot(handler);
  346. // Allocate and construct an operation to wrap the handler.
  347. typedef win_iocp_null_buffers_op<Handler, IoExecutor> op;
  348. typename op::ptr p = { asio::detail::addressof(handler),
  349. op::ptr::allocate(handler), 0 };
  350. p.p = new (p.v) op(impl.cancel_token_, handler, io_ex);
  351. ASIO_HANDLER_CREATION((context_, *p.p, "socket",
  352. &impl, impl.socket_, "async_receive(null_buffers)"));
  353. // Optionally register for per-operation cancellation.
  354. operation* iocp_op = p.p;
  355. if (slot.is_connected())
  356. {
  357. p.p->cancellation_key_ = iocp_op =
  358. &slot.template emplace<reactor_op_cancellation>(
  359. impl.socket_, iocp_op);
  360. }
  361. int op_type = start_null_buffers_receive_op(impl, flags, p.p, iocp_op);
  362. p.v = p.p = 0;
  363. // Update cancellation method if the reactor was used.
  364. if (slot.is_connected() && op_type != -1)
  365. {
  366. static_cast<reactor_op_cancellation*>(iocp_op)->use_reactor(
  367. &get_reactor(), &impl.reactor_data_, op_type);
  368. }
  369. }
  370. // Receive some data with associated flags. Returns the number of bytes
  371. // received.
  372. template <typename MutableBufferSequence>
  373. size_t receive_with_flags(base_implementation_type& impl,
  374. const MutableBufferSequence& buffers,
  375. socket_base::message_flags in_flags,
  376. socket_base::message_flags& out_flags, asio::error_code& ec)
  377. {
  378. buffer_sequence_adapter<asio::mutable_buffer,
  379. MutableBufferSequence> bufs(buffers);
  380. return socket_ops::sync_recvmsg(impl.socket_, impl.state_,
  381. bufs.buffers(), bufs.count(), in_flags, out_flags, ec);
  382. }
  383. // Wait until data can be received without blocking.
  384. size_t receive_with_flags(base_implementation_type& impl,
  385. const null_buffers&, socket_base::message_flags,
  386. socket_base::message_flags& out_flags, asio::error_code& ec)
  387. {
  388. // Wait for socket to become ready.
  389. socket_ops::poll_read(impl.socket_, impl.state_, -1, ec);
  390. // Clear out_flags, since we cannot give it any other sensible value when
  391. // performing a null_buffers operation.
  392. out_flags = 0;
  393. return 0;
  394. }
  395. // Start an asynchronous receive. The buffer for the data being received
  396. // must be valid for the lifetime of the asynchronous operation.
  397. template <typename MutableBufferSequence,
  398. typename Handler, typename IoExecutor>
  399. void async_receive_with_flags(base_implementation_type& impl,
  400. const MutableBufferSequence& buffers, socket_base::message_flags in_flags,
  401. socket_base::message_flags& out_flags, Handler& handler,
  402. const IoExecutor& io_ex)
  403. {
  404. associated_cancellation_slot_t<Handler> slot
  405. = asio::get_associated_cancellation_slot(handler);
  406. // Allocate and construct an operation to wrap the handler.
  407. typedef win_iocp_socket_recvmsg_op<
  408. MutableBufferSequence, Handler, IoExecutor> op;
  409. typename op::ptr p = { asio::detail::addressof(handler),
  410. op::ptr::allocate(handler), 0 };
  411. operation* o = p.p = new (p.v) op(impl.cancel_token_,
  412. buffers, out_flags, handler, io_ex);
  413. ASIO_HANDLER_CREATION((context_, *p.p, "socket",
  414. &impl, impl.socket_, "async_receive_with_flags"));
  415. buffer_sequence_adapter<asio::mutable_buffer,
  416. MutableBufferSequence> bufs(buffers);
  417. // Optionally register for per-operation cancellation.
  418. if (slot.is_connected())
  419. o = &slot.template emplace<iocp_op_cancellation>(impl.socket_, o);
  420. start_receive_op(impl, bufs.buffers(), bufs.count(), in_flags, false, o);
  421. p.v = p.p = 0;
  422. }
  423. // Wait until data can be received without blocking.
  424. template <typename Handler, typename IoExecutor>
  425. void async_receive_with_flags(base_implementation_type& impl,
  426. const null_buffers&, socket_base::message_flags in_flags,
  427. socket_base::message_flags& out_flags, Handler& handler,
  428. const IoExecutor& io_ex)
  429. {
  430. associated_cancellation_slot_t<Handler> slot
  431. = asio::get_associated_cancellation_slot(handler);
  432. // Allocate and construct an operation to wrap the handler.
  433. typedef win_iocp_null_buffers_op<Handler, IoExecutor> op;
  434. typename op::ptr p = { asio::detail::addressof(handler),
  435. op::ptr::allocate(handler), 0 };
  436. p.p = new (p.v) op(impl.cancel_token_, handler, io_ex);
  437. ASIO_HANDLER_CREATION((context_, *p.p, "socket",
  438. &impl, impl.socket_, "async_receive_with_flags(null_buffers)"));
  439. // Reset out_flags since it can be given no sensible value at this time.
  440. out_flags = 0;
  441. // Optionally register for per-operation cancellation.
  442. operation* iocp_op = p.p;
  443. if (slot.is_connected())
  444. {
  445. p.p->cancellation_key_ = iocp_op =
  446. &slot.template emplace<reactor_op_cancellation>(
  447. impl.socket_, iocp_op);
  448. }
  449. int op_type = start_null_buffers_receive_op(impl, in_flags, p.p, iocp_op);
  450. p.v = p.p = 0;
  451. // Update cancellation method if the reactor was used.
  452. if (slot.is_connected() && op_type != -1)
  453. {
  454. static_cast<reactor_op_cancellation*>(iocp_op)->use_reactor(
  455. &get_reactor(), &impl.reactor_data_, op_type);
  456. }
  457. }
  458. // Helper function to restart an asynchronous accept operation.
  459. ASIO_DECL void restart_accept_op(socket_type s,
  460. socket_holder& new_socket, int family, int type,
  461. int protocol, void* output_buffer, DWORD address_length,
  462. long* cancel_requested, operation* op);
  463. protected:
  464. // Open a new socket implementation.
  465. ASIO_DECL asio::error_code do_open(
  466. base_implementation_type& impl, int family, int type,
  467. int protocol, asio::error_code& ec);
  468. // Assign a native socket to a socket implementation.
  469. ASIO_DECL asio::error_code do_assign(
  470. base_implementation_type& impl, int type,
  471. socket_type native_socket, asio::error_code& ec);
  472. // Helper function to start an asynchronous send operation.
  473. ASIO_DECL void start_send_op(base_implementation_type& impl,
  474. WSABUF* buffers, std::size_t buffer_count,
  475. socket_base::message_flags flags, bool noop, operation* op);
  476. // Helper function to start an asynchronous send_to operation.
  477. ASIO_DECL void start_send_to_op(base_implementation_type& impl,
  478. WSABUF* buffers, std::size_t buffer_count, const void* addr,
  479. int addrlen, socket_base::message_flags flags, operation* op);
  480. // Helper function to start an asynchronous receive operation.
  481. ASIO_DECL void start_receive_op(base_implementation_type& impl,
  482. WSABUF* buffers, std::size_t buffer_count,
  483. socket_base::message_flags flags, bool noop, operation* op);
  484. // Helper function to start an asynchronous null_buffers receive operation.
  485. ASIO_DECL int start_null_buffers_receive_op(
  486. base_implementation_type& impl, socket_base::message_flags flags,
  487. reactor_op* op, operation* iocp_op);
  488. // Helper function to start an asynchronous receive_from operation.
  489. ASIO_DECL void start_receive_from_op(base_implementation_type& impl,
  490. WSABUF* buffers, std::size_t buffer_count, void* addr,
  491. socket_base::message_flags flags, int* addrlen, operation* op);
  492. // Helper function to start an asynchronous accept operation.
  493. ASIO_DECL void start_accept_op(base_implementation_type& impl,
  494. bool peer_is_open, socket_holder& new_socket, int family, int type,
  495. int protocol, void* output_buffer, DWORD address_length, operation* op);
  496. // Start an asynchronous read or write operation using the reactor.
  497. ASIO_DECL void start_reactor_op(base_implementation_type& impl,
  498. int op_type, reactor_op* op);
  499. // Start the asynchronous connect operation using the reactor.
  500. ASIO_DECL int start_connect_op(base_implementation_type& impl,
  501. int family, int type, const void* remote_addr, std::size_t remote_addrlen,
  502. win_iocp_socket_connect_op_base* op, operation* iocp_op);
  503. // Helper function to close a socket when the associated object is being
  504. // destroyed.
  505. ASIO_DECL void close_for_destruction(base_implementation_type& impl);
  506. // Update the ID of the thread from which cancellation is safe.
  507. ASIO_DECL void update_cancellation_thread_id(
  508. base_implementation_type& impl);
  509. // Helper function to get the reactor. If no reactor has been created yet, a
  510. // new one is obtained from the execution context and a pointer to it is
  511. // cached in this service.
  512. ASIO_DECL select_reactor& get_reactor();
  513. // The type of a ConnectEx function pointer, as old SDKs may not provide it.
  514. typedef BOOL (PASCAL *connect_ex_fn)(SOCKET,
  515. const socket_addr_type*, int, void*, DWORD, DWORD*, OVERLAPPED*);
  516. // Helper function to get the ConnectEx pointer. If no ConnectEx pointer has
  517. // been obtained yet, one is obtained using WSAIoctl and the pointer is
  518. // cached. Returns a null pointer if ConnectEx is not available.
  519. ASIO_DECL connect_ex_fn get_connect_ex(
  520. base_implementation_type& impl, int type);
  521. // The type of a NtSetInformationFile function pointer.
  522. typedef LONG (NTAPI *nt_set_info_fn)(HANDLE, ULONG_PTR*, void*, ULONG, ULONG);
  523. // Helper function to get the NtSetInformationFile function pointer. If no
  524. // NtSetInformationFile pointer has been obtained yet, one is obtained using
  525. // GetProcAddress and the pointer is cached. Returns a null pointer if
  526. // NtSetInformationFile is not available.
  527. ASIO_DECL nt_set_info_fn get_nt_set_info();
  528. // Helper function to emulate InterlockedCompareExchangePointer functionality
  529. // for:
  530. // - very old Platform SDKs; and
  531. // - platform SDKs where MSVC's /Wp64 option causes spurious warnings.
  532. ASIO_DECL void* interlocked_compare_exchange_pointer(
  533. void** dest, void* exch, void* cmp);
  534. // Helper function to emulate InterlockedExchangePointer functionality for:
  535. // - very old Platform SDKs; and
  536. // - platform SDKs where MSVC's /Wp64 option causes spurious warnings.
  537. ASIO_DECL void* interlocked_exchange_pointer(void** dest, void* val);
  538. // Helper class used to implement per operation cancellation.
  539. class iocp_op_cancellation : public operation
  540. {
  541. public:
  542. iocp_op_cancellation(SOCKET s, operation* target)
  543. : operation(&iocp_op_cancellation::do_complete),
  544. socket_(s),
  545. target_(target)
  546. {
  547. }
  548. static void do_complete(void* owner, operation* base,
  549. const asio::error_code& result_ec,
  550. std::size_t bytes_transferred)
  551. {
  552. iocp_op_cancellation* o = static_cast<iocp_op_cancellation*>(base);
  553. o->target_->complete(owner, result_ec, bytes_transferred);
  554. }
  555. void operator()(cancellation_type_t type)
  556. {
  557. #if defined(_WIN32_WINNT) && (_WIN32_WINNT >= 0x0600)
  558. if (!!(type &
  559. (cancellation_type::terminal
  560. | cancellation_type::partial
  561. | cancellation_type::total)))
  562. {
  563. HANDLE sock_as_handle = reinterpret_cast<HANDLE>(socket_);
  564. ::CancelIoEx(sock_as_handle, this);
  565. }
  566. #else // defined(_WIN32_WINNT) && (_WIN32_WINNT >= 0x0600)
  567. (void)type;
  568. #endif // defined(_WIN32_WINNT) && (_WIN32_WINNT >= 0x0600)
  569. }
  570. private:
  571. SOCKET socket_;
  572. operation* target_;
  573. };
  574. // Helper class used to implement per operation cancellation.
  575. class accept_op_cancellation : public operation
  576. {
  577. public:
  578. accept_op_cancellation(SOCKET s, operation* target)
  579. : operation(&iocp_op_cancellation::do_complete),
  580. socket_(s),
  581. target_(target),
  582. cancel_requested_(0)
  583. {
  584. }
  585. static void do_complete(void* owner, operation* base,
  586. const asio::error_code& result_ec,
  587. std::size_t bytes_transferred)
  588. {
  589. accept_op_cancellation* o = static_cast<accept_op_cancellation*>(base);
  590. o->target_->complete(owner, result_ec, bytes_transferred);
  591. }
  592. long* get_cancel_requested()
  593. {
  594. return &cancel_requested_;
  595. }
  596. void operator()(cancellation_type_t type)
  597. {
  598. #if defined(_WIN32_WINNT) && (_WIN32_WINNT >= 0x0600)
  599. if (!!(type &
  600. (cancellation_type::terminal
  601. | cancellation_type::partial
  602. | cancellation_type::total)))
  603. {
  604. HANDLE sock_as_handle = reinterpret_cast<HANDLE>(socket_);
  605. ::CancelIoEx(sock_as_handle, this);
  606. }
  607. #else // defined(_WIN32_WINNT) && (_WIN32_WINNT >= 0x0600)
  608. (void)type;
  609. #endif // defined(_WIN32_WINNT) && (_WIN32_WINNT >= 0x0600)
  610. }
  611. private:
  612. SOCKET socket_;
  613. operation* target_;
  614. long cancel_requested_;
  615. };
  616. // Helper class used to implement per operation cancellation.
  617. class reactor_op_cancellation : public operation
  618. {
  619. public:
  620. reactor_op_cancellation(SOCKET s, operation* base)
  621. : operation(&reactor_op_cancellation::do_complete),
  622. socket_(s),
  623. target_(base),
  624. reactor_(0),
  625. reactor_data_(0),
  626. op_type_(-1)
  627. {
  628. }
  629. void use_reactor(select_reactor* r,
  630. select_reactor::per_descriptor_data* p, int o)
  631. {
  632. reactor_ = r;
  633. reactor_data_ = p;
  634. op_type_ = o;
  635. }
  636. static void do_complete(void* owner, operation* base,
  637. const asio::error_code& result_ec,
  638. std::size_t bytes_transferred)
  639. {
  640. reactor_op_cancellation* o = static_cast<reactor_op_cancellation*>(base);
  641. o->target_->complete(owner, result_ec, bytes_transferred);
  642. }
  643. void operator()(cancellation_type_t type)
  644. {
  645. if (!!(type &
  646. (cancellation_type::terminal
  647. | cancellation_type::partial
  648. | cancellation_type::total)))
  649. {
  650. if (reactor_)
  651. {
  652. reactor_->cancel_ops_by_key(socket_,
  653. *reactor_data_, op_type_, this);
  654. }
  655. else
  656. {
  657. #if defined(_WIN32_WINNT) && (_WIN32_WINNT >= 0x0600)
  658. HANDLE sock_as_handle = reinterpret_cast<HANDLE>(socket_);
  659. ::CancelIoEx(sock_as_handle, this);
  660. #endif // defined(_WIN32_WINNT) && (_WIN32_WINNT >= 0x0600)
  661. }
  662. }
  663. }
  664. private:
  665. SOCKET socket_;
  666. operation* target_;
  667. select_reactor* reactor_;
  668. select_reactor::per_descriptor_data* reactor_data_;
  669. int op_type_;
  670. };
  671. // The execution context used to obtain the reactor, if required.
  672. execution_context& context_;
  673. // The IOCP service used for running asynchronous operations and dispatching
  674. // handlers.
  675. win_iocp_io_context& iocp_service_;
  676. // The reactor used for performing connect operations. This object is created
  677. // only if needed.
  678. select_reactor* reactor_;
  679. // Pointer to ConnectEx implementation.
  680. void* connect_ex_;
  681. // Pointer to NtSetInformationFile implementation.
  682. void* nt_set_info_;
  683. // Mutex to protect access to the linked list of implementations.
  684. asio::detail::mutex mutex_;
  685. // The head of a linked list of all implementations.
  686. base_implementation_type* impl_list_;
  687. };
  688. } // namespace detail
  689. } // namespace asio
  690. #include "asio/detail/pop_options.hpp"
  691. #if defined(ASIO_HEADER_ONLY)
  692. # include "asio/detail/impl/win_iocp_socket_service_base.ipp"
  693. #endif // defined(ASIO_HEADER_ONLY)
  694. #endif // defined(ASIO_HAS_IOCP)
  695. #endif // ASIO_DETAIL_WIN_IOCP_SOCKET_SERVICE_BASE_HPP