win_iocp_socket_service_base.hpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833
  1. //
  2. // detail/win_iocp_socket_service_base.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2022 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/handler_invoke_helpers.hpp"
  26. #include "asio/detail/memory.hpp"
  27. #include "asio/detail/mutex.hpp"
  28. #include "asio/detail/operation.hpp"
  29. #include "asio/detail/reactor_op.hpp"
  30. #include "asio/detail/select_reactor.hpp"
  31. #include "asio/detail/socket_holder.hpp"
  32. #include "asio/detail/socket_ops.hpp"
  33. #include "asio/detail/socket_types.hpp"
  34. #include "asio/detail/win_iocp_io_context.hpp"
  35. #include "asio/detail/win_iocp_null_buffers_op.hpp"
  36. #include "asio/detail/win_iocp_socket_connect_op.hpp"
  37. #include "asio/detail/win_iocp_socket_send_op.hpp"
  38. #include "asio/detail/win_iocp_socket_recv_op.hpp"
  39. #include "asio/detail/win_iocp_socket_recvmsg_op.hpp"
  40. #include "asio/detail/win_iocp_wait_op.hpp"
  41. #include "asio/detail/push_options.hpp"
  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(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(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. ASIO_DECL win_iocp_socket_service_base(execution_context& context);
  76. // Destroy all user-defined handler objects owned by the service.
  77. ASIO_DECL void base_shutdown();
  78. // Construct a new socket implementation.
  79. ASIO_DECL void construct(base_implementation_type& impl);
  80. // Move-construct a new socket implementation.
  81. ASIO_DECL void base_move_construct(base_implementation_type& impl,
  82. base_implementation_type& other_impl) ASIO_NOEXCEPT;
  83. // Move-assign from another socket implementation.
  84. 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. 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. ASIO_DECL asio::error_code close(
  96. base_implementation_type& impl, asio::error_code& ec);
  97. // Release ownership of the socket.
  98. ASIO_DECL socket_type release(
  99. base_implementation_type& impl, asio::error_code& ec);
  100. // Cancel all operations associated with the socket.
  101. ASIO_DECL asio::error_code cancel(
  102. base_implementation_type& impl, asio::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. asio::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. asio::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. asio::error_code listen(base_implementation_type& impl,
  117. int backlog, asio::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. asio::error_code io_control(base_implementation_type& impl,
  125. IO_Control_Command& command, asio::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. asio::error_code non_blocking(base_implementation_type& impl,
  138. bool mode, asio::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. asio::error_code native_non_blocking(base_implementation_type& impl,
  150. bool mode, asio::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. asio::error_code wait(base_implementation_type& impl,
  158. socket_base::wait_type w, asio::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 = 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. typename associated_cancellation_slot<Handler>::type slot
  184. = asio::get_associated_cancellation_slot(handler);
  185. bool is_continuation =
  186. 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 = { asio::detail::addressof(handler),
  190. op::ptr::allocate(handler), 0 };
  191. p.p = new (p.v) op(impl.cancel_token_, handler, io_ex);
  192. 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_ = 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, asio::error_code& ec)
  234. {
  235. buffer_sequence_adapter<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, asio::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. typename associated_cancellation_slot<Handler>::type slot
  256. = 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 = { 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. ASIO_HANDLER_CREATION((context_, *p.p, "socket",
  265. &impl, impl.socket_, "async_send"));
  266. buffer_sequence_adapter<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 = { asio::detail::addressof(handler),
  284. op::ptr::allocate(handler), 0 };
  285. p.p = new (p.v) op(impl.cancel_token_, handler, io_ex);
  286. 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, asio::error_code& ec)
  296. {
  297. buffer_sequence_adapter<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, asio::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. typename associated_cancellation_slot<Handler>::type slot
  319. = 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 = { 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. ASIO_HANDLER_CREATION((context_, *p.p, "socket",
  328. &impl, impl.socket_, "async_receive"));
  329. buffer_sequence_adapter<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. typename associated_cancellation_slot<Handler>::type slot
  346. = 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 = { asio::detail::addressof(handler),
  350. op::ptr::allocate(handler), 0 };
  351. p.p = new (p.v) op(impl.cancel_token_, handler, io_ex);
  352. 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, asio::error_code& ec)
  378. {
  379. buffer_sequence_adapter<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, asio::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. typename associated_cancellation_slot<Handler>::type slot
  406. = 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 = { 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. ASIO_HANDLER_CREATION((context_, *p.p, "socket",
  415. &impl, impl.socket_, "async_receive_with_flags"));
  416. buffer_sequence_adapter<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. typename associated_cancellation_slot<Handler>::type slot
  432. = 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 = { asio::detail::addressof(handler),
  436. op::ptr::allocate(handler), 0 };
  437. p.p = new (p.v) op(impl.cancel_token_, handler, io_ex);
  438. 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. 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. ASIO_DECL asio::error_code do_open(
  467. base_implementation_type& impl, int family, int type,
  468. int protocol, asio::error_code& ec);
  469. // Assign a native socket to a socket implementation.
  470. ASIO_DECL asio::error_code do_assign(
  471. base_implementation_type& impl, int type,
  472. socket_type native_socket, asio::error_code& ec);
  473. // Helper function to start an asynchronous send operation.
  474. 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. ASIO_DECL void start_send_to_op(base_implementation_type& impl,
  479. WSABUF* buffers, std::size_t buffer_count,
  480. const socket_addr_type* addr, int addrlen,
  481. socket_base::message_flags flags, operation* op);
  482. // Helper function to start an asynchronous receive operation.
  483. ASIO_DECL void start_receive_op(base_implementation_type& impl,
  484. WSABUF* buffers, std::size_t buffer_count,
  485. socket_base::message_flags flags, bool noop, operation* op);
  486. // Helper function to start an asynchronous null_buffers receive operation.
  487. ASIO_DECL int start_null_buffers_receive_op(
  488. base_implementation_type& impl, socket_base::message_flags flags,
  489. reactor_op* op, operation* iocp_op);
  490. // Helper function to start an asynchronous receive_from operation.
  491. ASIO_DECL void start_receive_from_op(base_implementation_type& impl,
  492. WSABUF* buffers, std::size_t buffer_count, socket_addr_type* addr,
  493. socket_base::message_flags flags, int* addrlen, operation* op);
  494. // Helper function to start an asynchronous accept operation.
  495. ASIO_DECL void start_accept_op(base_implementation_type& impl,
  496. bool peer_is_open, socket_holder& new_socket, int family, int type,
  497. int protocol, void* output_buffer, DWORD address_length, operation* op);
  498. // Start an asynchronous read or write operation using the reactor.
  499. ASIO_DECL void start_reactor_op(base_implementation_type& impl,
  500. int op_type, reactor_op* op);
  501. // Start the asynchronous connect operation using the reactor.
  502. ASIO_DECL int start_connect_op(base_implementation_type& impl,
  503. int family, int type, const socket_addr_type* remote_addr,
  504. std::size_t remote_addrlen, win_iocp_socket_connect_op_base* op,
  505. operation* iocp_op);
  506. // Helper function to close a socket when the associated object is being
  507. // destroyed.
  508. ASIO_DECL void close_for_destruction(base_implementation_type& impl);
  509. // Update the ID of the thread from which cancellation is safe.
  510. ASIO_DECL void update_cancellation_thread_id(
  511. base_implementation_type& impl);
  512. // Helper function to get the reactor. If no reactor has been created yet, a
  513. // new one is obtained from the execution context and a pointer to it is
  514. // cached in this service.
  515. ASIO_DECL select_reactor& get_reactor();
  516. // The type of a ConnectEx function pointer, as old SDKs may not provide it.
  517. typedef BOOL (PASCAL *connect_ex_fn)(SOCKET,
  518. const socket_addr_type*, int, void*, DWORD, DWORD*, OVERLAPPED*);
  519. // Helper function to get the ConnectEx pointer. If no ConnectEx pointer has
  520. // been obtained yet, one is obtained using WSAIoctl and the pointer is
  521. // cached. Returns a null pointer if ConnectEx is not available.
  522. ASIO_DECL connect_ex_fn get_connect_ex(
  523. base_implementation_type& impl, int type);
  524. // The type of a NtSetInformationFile function pointer.
  525. typedef LONG (NTAPI *nt_set_info_fn)(HANDLE, ULONG_PTR*, void*, ULONG, ULONG);
  526. // Helper function to get the NtSetInformationFile function pointer. If no
  527. // NtSetInformationFile pointer has been obtained yet, one is obtained using
  528. // GetProcAddress and the pointer is cached. Returns a null pointer if
  529. // NtSetInformationFile is not available.
  530. ASIO_DECL nt_set_info_fn get_nt_set_info();
  531. // Helper function to emulate InterlockedCompareExchangePointer functionality
  532. // for:
  533. // - very old Platform SDKs; and
  534. // - platform SDKs where MSVC's /Wp64 option causes spurious warnings.
  535. ASIO_DECL void* interlocked_compare_exchange_pointer(
  536. void** dest, void* exch, void* cmp);
  537. // Helper function to emulate InterlockedExchangePointer functionality for:
  538. // - very old Platform SDKs; and
  539. // - platform SDKs where MSVC's /Wp64 option causes spurious warnings.
  540. ASIO_DECL void* interlocked_exchange_pointer(void** dest, void* val);
  541. // Helper class used to implement per operation cancellation.
  542. class iocp_op_cancellation : public operation
  543. {
  544. public:
  545. iocp_op_cancellation(SOCKET s, operation* target)
  546. : operation(&iocp_op_cancellation::do_complete),
  547. socket_(s),
  548. target_(target)
  549. {
  550. }
  551. static void do_complete(void* owner, operation* base,
  552. const asio::error_code& result_ec,
  553. std::size_t bytes_transferred)
  554. {
  555. iocp_op_cancellation* o = static_cast<iocp_op_cancellation*>(base);
  556. o->target_->complete(owner, result_ec, bytes_transferred);
  557. }
  558. void operator()(cancellation_type_t type)
  559. {
  560. #if defined(_WIN32_WINNT) && (_WIN32_WINNT >= 0x0600)
  561. if (!!(type &
  562. (cancellation_type::terminal
  563. | cancellation_type::partial
  564. | cancellation_type::total)))
  565. {
  566. HANDLE sock_as_handle = reinterpret_cast<HANDLE>(socket_);
  567. ::CancelIoEx(sock_as_handle, this);
  568. }
  569. #else // defined(_WIN32_WINNT) && (_WIN32_WINNT >= 0x0600)
  570. (void)type;
  571. #endif // defined(_WIN32_WINNT) && (_WIN32_WINNT >= 0x0600)
  572. }
  573. private:
  574. SOCKET socket_;
  575. operation* target_;
  576. };
  577. // Helper class used to implement per operation cancellation.
  578. class accept_op_cancellation : public operation
  579. {
  580. public:
  581. accept_op_cancellation(SOCKET s, operation* target)
  582. : operation(&iocp_op_cancellation::do_complete),
  583. socket_(s),
  584. target_(target),
  585. cancel_requested_(0)
  586. {
  587. }
  588. static void do_complete(void* owner, operation* base,
  589. const asio::error_code& result_ec,
  590. std::size_t bytes_transferred)
  591. {
  592. accept_op_cancellation* o = static_cast<accept_op_cancellation*>(base);
  593. o->target_->complete(owner, result_ec, bytes_transferred);
  594. }
  595. long* get_cancel_requested()
  596. {
  597. return &cancel_requested_;
  598. }
  599. void operator()(cancellation_type_t type)
  600. {
  601. #if defined(_WIN32_WINNT) && (_WIN32_WINNT >= 0x0600)
  602. if (!!(type &
  603. (cancellation_type::terminal
  604. | cancellation_type::partial
  605. | cancellation_type::total)))
  606. {
  607. HANDLE sock_as_handle = reinterpret_cast<HANDLE>(socket_);
  608. ::CancelIoEx(sock_as_handle, this);
  609. }
  610. #else // defined(_WIN32_WINNT) && (_WIN32_WINNT >= 0x0600)
  611. (void)type;
  612. #endif // defined(_WIN32_WINNT) && (_WIN32_WINNT >= 0x0600)
  613. }
  614. private:
  615. SOCKET socket_;
  616. operation* target_;
  617. long cancel_requested_;
  618. };
  619. // Helper class used to implement per operation cancellation.
  620. class reactor_op_cancellation : public operation
  621. {
  622. public:
  623. reactor_op_cancellation(SOCKET s, operation* base)
  624. : operation(&reactor_op_cancellation::do_complete),
  625. socket_(s),
  626. target_(base),
  627. reactor_(0),
  628. reactor_data_(0),
  629. op_type_(-1)
  630. {
  631. }
  632. void use_reactor(select_reactor* r,
  633. select_reactor::per_descriptor_data* p, int o)
  634. {
  635. reactor_ = r;
  636. reactor_data_ = p;
  637. op_type_ = o;
  638. }
  639. static void do_complete(void* owner, operation* base,
  640. const asio::error_code& result_ec,
  641. std::size_t bytes_transferred)
  642. {
  643. reactor_op_cancellation* o = static_cast<reactor_op_cancellation*>(base);
  644. o->target_->complete(owner, result_ec, bytes_transferred);
  645. }
  646. void operator()(cancellation_type_t type)
  647. {
  648. if (!!(type &
  649. (cancellation_type::terminal
  650. | cancellation_type::partial
  651. | cancellation_type::total)))
  652. {
  653. if (reactor_)
  654. {
  655. reactor_->cancel_ops_by_key(socket_,
  656. *reactor_data_, op_type_, this);
  657. }
  658. else
  659. {
  660. #if defined(_WIN32_WINNT) && (_WIN32_WINNT >= 0x0600)
  661. HANDLE sock_as_handle = reinterpret_cast<HANDLE>(socket_);
  662. ::CancelIoEx(sock_as_handle, this);
  663. #endif // defined(_WIN32_WINNT) && (_WIN32_WINNT >= 0x0600)
  664. }
  665. }
  666. }
  667. private:
  668. SOCKET socket_;
  669. operation* target_;
  670. select_reactor* reactor_;
  671. select_reactor::per_descriptor_data* reactor_data_;
  672. int op_type_;
  673. };
  674. // The execution context used to obtain the reactor, if required.
  675. execution_context& context_;
  676. // The IOCP service used for running asynchronous operations and dispatching
  677. // handlers.
  678. win_iocp_io_context& iocp_service_;
  679. // The reactor used for performing connect operations. This object is created
  680. // only if needed.
  681. select_reactor* reactor_;
  682. // Pointer to ConnectEx implementation.
  683. void* connect_ex_;
  684. // Pointer to NtSetInformationFile implementation.
  685. void* nt_set_info_;
  686. // Mutex to protect access to the linked list of implementations.
  687. asio::detail::mutex mutex_;
  688. // The head of a linked list of all implementations.
  689. base_implementation_type* impl_list_;
  690. };
  691. } // namespace detail
  692. } // namespace asio
  693. #include "asio/detail/pop_options.hpp"
  694. #if defined(ASIO_HEADER_ONLY)
  695. # include "asio/detail/impl/win_iocp_socket_service_base.ipp"
  696. #endif // defined(ASIO_HEADER_ONLY)
  697. #endif // defined(ASIO_HAS_IOCP)
  698. #endif // ASIO_DETAIL_WIN_IOCP_SOCKET_SERVICE_BASE_HPP