reactive_socket_service_base.hpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655
  1. //
  2. // detail/reactive_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_REACTIVE_SOCKET_SERVICE_BASE_HPP
  11. #define ASIO_DETAIL_REACTIVE_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. && !defined(ASIO_WINDOWS_RUNTIME) \
  18. && !defined(ASIO_HAS_IO_URING_AS_DEFAULT)
  19. #include "asio/associated_cancellation_slot.hpp"
  20. #include "asio/buffer.hpp"
  21. #include "asio/cancellation_type.hpp"
  22. #include "asio/error.hpp"
  23. #include "asio/execution_context.hpp"
  24. #include "asio/socket_base.hpp"
  25. #include "asio/detail/buffer_sequence_adapter.hpp"
  26. #include "asio/detail/memory.hpp"
  27. #include "asio/detail/reactive_null_buffers_op.hpp"
  28. #include "asio/detail/reactive_socket_recv_op.hpp"
  29. #include "asio/detail/reactive_socket_recvmsg_op.hpp"
  30. #include "asio/detail/reactive_socket_send_op.hpp"
  31. #include "asio/detail/reactive_wait_op.hpp"
  32. #include "asio/detail/reactor.hpp"
  33. #include "asio/detail/reactor_op.hpp"
  34. #include "asio/detail/socket_holder.hpp"
  35. #include "asio/detail/socket_ops.hpp"
  36. #include "asio/detail/socket_types.hpp"
  37. #include "asio/detail/push_options.hpp"
  38. namespace asio {
  39. namespace detail {
  40. class reactive_socket_service_base
  41. {
  42. public:
  43. // The native type of a socket.
  44. typedef socket_type native_handle_type;
  45. // The implementation type of the socket.
  46. struct base_implementation_type
  47. {
  48. // The native socket representation.
  49. socket_type socket_;
  50. // The current state of the socket.
  51. socket_ops::state_type state_;
  52. // Per-descriptor data used by the reactor.
  53. reactor::per_descriptor_data reactor_data_;
  54. };
  55. // Constructor.
  56. ASIO_DECL reactive_socket_service_base(execution_context& context);
  57. // Destroy all user-defined handler objects owned by the service.
  58. ASIO_DECL void base_shutdown();
  59. // Construct a new socket implementation.
  60. ASIO_DECL void construct(base_implementation_type& impl);
  61. // Move-construct a new socket implementation.
  62. ASIO_DECL void base_move_construct(base_implementation_type& impl,
  63. base_implementation_type& other_impl) ASIO_NOEXCEPT;
  64. // Move-assign from another socket implementation.
  65. ASIO_DECL void base_move_assign(base_implementation_type& impl,
  66. reactive_socket_service_base& other_service,
  67. base_implementation_type& other_impl);
  68. // Destroy a socket implementation.
  69. ASIO_DECL void destroy(base_implementation_type& impl);
  70. // Determine whether the socket is open.
  71. bool is_open(const base_implementation_type& impl) const
  72. {
  73. return impl.socket_ != invalid_socket;
  74. }
  75. // Destroy a socket implementation.
  76. ASIO_DECL asio::error_code close(
  77. base_implementation_type& impl, asio::error_code& ec);
  78. // Release ownership of the socket.
  79. ASIO_DECL socket_type release(
  80. base_implementation_type& impl, asio::error_code& ec);
  81. // Get the native socket representation.
  82. native_handle_type native_handle(base_implementation_type& impl)
  83. {
  84. return impl.socket_;
  85. }
  86. // Cancel all operations associated with the socket.
  87. ASIO_DECL asio::error_code cancel(
  88. base_implementation_type& impl, asio::error_code& ec);
  89. // Determine whether the socket is at the out-of-band data mark.
  90. bool at_mark(const base_implementation_type& impl,
  91. asio::error_code& ec) const
  92. {
  93. return socket_ops::sockatmark(impl.socket_, ec);
  94. }
  95. // Determine the number of bytes available for reading.
  96. std::size_t available(const base_implementation_type& impl,
  97. asio::error_code& ec) const
  98. {
  99. return socket_ops::available(impl.socket_, ec);
  100. }
  101. // Place the socket into the state where it will listen for new connections.
  102. asio::error_code listen(base_implementation_type& impl,
  103. int backlog, asio::error_code& ec)
  104. {
  105. socket_ops::listen(impl.socket_, backlog, ec);
  106. return ec;
  107. }
  108. // Perform an IO control command on the socket.
  109. template <typename IO_Control_Command>
  110. asio::error_code io_control(base_implementation_type& impl,
  111. IO_Control_Command& command, asio::error_code& ec)
  112. {
  113. socket_ops::ioctl(impl.socket_, impl.state_, command.name(),
  114. static_cast<ioctl_arg_type*>(command.data()), ec);
  115. return ec;
  116. }
  117. // Gets the non-blocking mode of the socket.
  118. bool non_blocking(const base_implementation_type& impl) const
  119. {
  120. return (impl.state_ & socket_ops::user_set_non_blocking) != 0;
  121. }
  122. // Sets the non-blocking mode of the socket.
  123. asio::error_code non_blocking(base_implementation_type& impl,
  124. bool mode, asio::error_code& ec)
  125. {
  126. socket_ops::set_user_non_blocking(impl.socket_, impl.state_, mode, ec);
  127. return ec;
  128. }
  129. // Gets the non-blocking mode of the native socket implementation.
  130. bool native_non_blocking(const base_implementation_type& impl) const
  131. {
  132. return (impl.state_ & socket_ops::internal_non_blocking) != 0;
  133. }
  134. // Sets the non-blocking mode of the native socket implementation.
  135. asio::error_code native_non_blocking(base_implementation_type& impl,
  136. bool mode, asio::error_code& ec)
  137. {
  138. socket_ops::set_internal_non_blocking(impl.socket_, impl.state_, mode, ec);
  139. return ec;
  140. }
  141. // Wait for the socket to become ready to read, ready to write, or to have
  142. // pending error conditions.
  143. asio::error_code wait(base_implementation_type& impl,
  144. socket_base::wait_type w, asio::error_code& ec)
  145. {
  146. switch (w)
  147. {
  148. case socket_base::wait_read:
  149. socket_ops::poll_read(impl.socket_, impl.state_, -1, ec);
  150. break;
  151. case socket_base::wait_write:
  152. socket_ops::poll_write(impl.socket_, impl.state_, -1, ec);
  153. break;
  154. case socket_base::wait_error:
  155. socket_ops::poll_error(impl.socket_, impl.state_, -1, ec);
  156. break;
  157. default:
  158. ec = asio::error::invalid_argument;
  159. break;
  160. }
  161. return ec;
  162. }
  163. // Asynchronously wait for the socket to become ready to read, ready to
  164. // write, or to have pending error conditions.
  165. template <typename Handler, typename IoExecutor>
  166. void async_wait(base_implementation_type& impl,
  167. socket_base::wait_type w, Handler& handler, const IoExecutor& io_ex)
  168. {
  169. bool is_continuation =
  170. asio_handler_cont_helpers::is_continuation(handler);
  171. typename associated_cancellation_slot<Handler>::type slot
  172. = asio::get_associated_cancellation_slot(handler);
  173. // Allocate and construct an operation to wrap the handler.
  174. typedef reactive_wait_op<Handler, IoExecutor> op;
  175. typename op::ptr p = { asio::detail::addressof(handler),
  176. op::ptr::allocate(handler), 0 };
  177. p.p = new (p.v) op(success_ec_, handler, io_ex);
  178. ASIO_HANDLER_CREATION((reactor_.context(), *p.p, "socket",
  179. &impl, impl.socket_, "async_wait"));
  180. int op_type;
  181. switch (w)
  182. {
  183. case socket_base::wait_read:
  184. op_type = reactor::read_op;
  185. break;
  186. case socket_base::wait_write:
  187. op_type = reactor::write_op;
  188. break;
  189. case socket_base::wait_error:
  190. op_type = reactor::except_op;
  191. break;
  192. default:
  193. p.p->ec_ = asio::error::invalid_argument;
  194. reactor_.post_immediate_completion(p.p, is_continuation);
  195. p.v = p.p = 0;
  196. return;
  197. }
  198. // Optionally register for per-operation cancellation.
  199. if (slot.is_connected())
  200. {
  201. p.p->cancellation_key_ =
  202. &slot.template emplace<reactor_op_cancellation>(
  203. &reactor_, &impl.reactor_data_, impl.socket_, op_type);
  204. }
  205. start_op(impl, op_type, p.p, is_continuation, false, false);
  206. p.v = p.p = 0;
  207. }
  208. // Send the given data to the peer.
  209. template <typename ConstBufferSequence>
  210. size_t send(base_implementation_type& impl,
  211. const ConstBufferSequence& buffers,
  212. socket_base::message_flags flags, asio::error_code& ec)
  213. {
  214. typedef buffer_sequence_adapter<asio::const_buffer,
  215. ConstBufferSequence> bufs_type;
  216. if (bufs_type::is_single_buffer)
  217. {
  218. return socket_ops::sync_send1(impl.socket_,
  219. impl.state_, bufs_type::first(buffers).data(),
  220. bufs_type::first(buffers).size(), flags, ec);
  221. }
  222. else
  223. {
  224. bufs_type bufs(buffers);
  225. return socket_ops::sync_send(impl.socket_, impl.state_,
  226. bufs.buffers(), bufs.count(), flags, bufs.all_empty(), ec);
  227. }
  228. }
  229. // Wait until data can be sent without blocking.
  230. size_t send(base_implementation_type& impl, const null_buffers&,
  231. socket_base::message_flags, asio::error_code& ec)
  232. {
  233. // Wait for socket to become ready.
  234. socket_ops::poll_write(impl.socket_, impl.state_, -1, ec);
  235. return 0;
  236. }
  237. // Start an asynchronous send. The data being sent must be valid for the
  238. // lifetime of the asynchronous operation.
  239. template <typename ConstBufferSequence, typename Handler, typename IoExecutor>
  240. void async_send(base_implementation_type& impl,
  241. const ConstBufferSequence& buffers, socket_base::message_flags flags,
  242. Handler& handler, const IoExecutor& io_ex)
  243. {
  244. bool is_continuation =
  245. asio_handler_cont_helpers::is_continuation(handler);
  246. typename associated_cancellation_slot<Handler>::type slot
  247. = asio::get_associated_cancellation_slot(handler);
  248. // Allocate and construct an operation to wrap the handler.
  249. typedef reactive_socket_send_op<
  250. ConstBufferSequence, Handler, IoExecutor> op;
  251. typename op::ptr p = { asio::detail::addressof(handler),
  252. op::ptr::allocate(handler), 0 };
  253. p.p = new (p.v) op(success_ec_, impl.socket_,
  254. impl.state_, buffers, flags, handler, io_ex);
  255. // Optionally register for per-operation cancellation.
  256. if (slot.is_connected())
  257. {
  258. p.p->cancellation_key_ =
  259. &slot.template emplace<reactor_op_cancellation>(
  260. &reactor_, &impl.reactor_data_, impl.socket_, reactor::write_op);
  261. }
  262. ASIO_HANDLER_CREATION((reactor_.context(), *p.p, "socket",
  263. &impl, impl.socket_, "async_send"));
  264. start_op(impl, reactor::write_op, p.p, is_continuation, true,
  265. ((impl.state_ & socket_ops::stream_oriented)
  266. && buffer_sequence_adapter<asio::const_buffer,
  267. ConstBufferSequence>::all_empty(buffers)));
  268. p.v = p.p = 0;
  269. }
  270. // Start an asynchronous wait until data can be sent without blocking.
  271. template <typename Handler, typename IoExecutor>
  272. void async_send(base_implementation_type& impl, const null_buffers&,
  273. socket_base::message_flags, Handler& handler, const IoExecutor& io_ex)
  274. {
  275. bool is_continuation =
  276. asio_handler_cont_helpers::is_continuation(handler);
  277. typename associated_cancellation_slot<Handler>::type slot
  278. = asio::get_associated_cancellation_slot(handler);
  279. // Allocate and construct an operation to wrap the handler.
  280. typedef reactive_null_buffers_op<Handler, IoExecutor> op;
  281. typename op::ptr p = { asio::detail::addressof(handler),
  282. op::ptr::allocate(handler), 0 };
  283. p.p = new (p.v) op(success_ec_, handler, io_ex);
  284. // Optionally register for per-operation cancellation.
  285. if (slot.is_connected())
  286. {
  287. p.p->cancellation_key_ =
  288. &slot.template emplace<reactor_op_cancellation>(
  289. &reactor_, &impl.reactor_data_, impl.socket_, reactor::write_op);
  290. }
  291. ASIO_HANDLER_CREATION((reactor_.context(), *p.p, "socket",
  292. &impl, impl.socket_, "async_send(null_buffers)"));
  293. start_op(impl, reactor::write_op, p.p, is_continuation, false, false);
  294. p.v = p.p = 0;
  295. }
  296. // Receive some data from the peer. Returns the number of bytes received.
  297. template <typename MutableBufferSequence>
  298. size_t receive(base_implementation_type& impl,
  299. const MutableBufferSequence& buffers,
  300. socket_base::message_flags flags, asio::error_code& ec)
  301. {
  302. typedef buffer_sequence_adapter<asio::mutable_buffer,
  303. MutableBufferSequence> bufs_type;
  304. if (bufs_type::is_single_buffer)
  305. {
  306. return socket_ops::sync_recv1(impl.socket_,
  307. impl.state_, bufs_type::first(buffers).data(),
  308. bufs_type::first(buffers).size(), flags, ec);
  309. }
  310. else
  311. {
  312. bufs_type bufs(buffers);
  313. return socket_ops::sync_recv(impl.socket_, impl.state_,
  314. bufs.buffers(), bufs.count(), flags, bufs.all_empty(), ec);
  315. }
  316. }
  317. // Wait until data can be received without blocking.
  318. size_t receive(base_implementation_type& impl, const null_buffers&,
  319. socket_base::message_flags, asio::error_code& ec)
  320. {
  321. // Wait for socket to become ready.
  322. socket_ops::poll_read(impl.socket_, impl.state_, -1, ec);
  323. return 0;
  324. }
  325. // Start an asynchronous receive. The buffer for the data being received
  326. // must be valid for the lifetime of the asynchronous operation.
  327. template <typename MutableBufferSequence,
  328. typename Handler, typename IoExecutor>
  329. void async_receive(base_implementation_type& impl,
  330. const MutableBufferSequence& buffers, socket_base::message_flags flags,
  331. Handler& handler, const IoExecutor& io_ex)
  332. {
  333. bool is_continuation =
  334. asio_handler_cont_helpers::is_continuation(handler);
  335. typename associated_cancellation_slot<Handler>::type slot
  336. = asio::get_associated_cancellation_slot(handler);
  337. // Allocate and construct an operation to wrap the handler.
  338. typedef reactive_socket_recv_op<
  339. MutableBufferSequence, Handler, IoExecutor> op;
  340. typename op::ptr p = { asio::detail::addressof(handler),
  341. op::ptr::allocate(handler), 0 };
  342. p.p = new (p.v) op(success_ec_, impl.socket_,
  343. impl.state_, buffers, flags, handler, io_ex);
  344. // Optionally register for per-operation cancellation.
  345. if (slot.is_connected())
  346. {
  347. p.p->cancellation_key_ =
  348. &slot.template emplace<reactor_op_cancellation>(
  349. &reactor_, &impl.reactor_data_, impl.socket_, reactor::read_op);
  350. }
  351. ASIO_HANDLER_CREATION((reactor_.context(), *p.p, "socket",
  352. &impl, impl.socket_, "async_receive"));
  353. start_op(impl,
  354. (flags & socket_base::message_out_of_band)
  355. ? reactor::except_op : reactor::read_op,
  356. p.p, is_continuation,
  357. (flags & socket_base::message_out_of_band) == 0,
  358. ((impl.state_ & socket_ops::stream_oriented)
  359. && buffer_sequence_adapter<asio::mutable_buffer,
  360. MutableBufferSequence>::all_empty(buffers)));
  361. p.v = p.p = 0;
  362. }
  363. // Wait until data can be received without blocking.
  364. template <typename Handler, typename IoExecutor>
  365. void async_receive(base_implementation_type& impl,
  366. const null_buffers&, socket_base::message_flags flags,
  367. Handler& handler, const IoExecutor& io_ex)
  368. {
  369. bool is_continuation =
  370. asio_handler_cont_helpers::is_continuation(handler);
  371. typename associated_cancellation_slot<Handler>::type slot
  372. = asio::get_associated_cancellation_slot(handler);
  373. // Allocate and construct an operation to wrap the handler.
  374. typedef reactive_null_buffers_op<Handler, IoExecutor> op;
  375. typename op::ptr p = { asio::detail::addressof(handler),
  376. op::ptr::allocate(handler), 0 };
  377. p.p = new (p.v) op(success_ec_, handler, io_ex);
  378. // Optionally register for per-operation cancellation.
  379. if (slot.is_connected())
  380. {
  381. p.p->cancellation_key_ =
  382. &slot.template emplace<reactor_op_cancellation>(
  383. &reactor_, &impl.reactor_data_, impl.socket_, reactor::read_op);
  384. }
  385. ASIO_HANDLER_CREATION((reactor_.context(), *p.p, "socket",
  386. &impl, impl.socket_, "async_receive(null_buffers)"));
  387. start_op(impl,
  388. (flags & socket_base::message_out_of_band)
  389. ? reactor::except_op : reactor::read_op,
  390. p.p, is_continuation, false, false);
  391. p.v = p.p = 0;
  392. }
  393. // Receive some data with associated flags. Returns the number of bytes
  394. // received.
  395. template <typename MutableBufferSequence>
  396. size_t receive_with_flags(base_implementation_type& impl,
  397. const MutableBufferSequence& buffers,
  398. socket_base::message_flags in_flags,
  399. socket_base::message_flags& out_flags, asio::error_code& ec)
  400. {
  401. buffer_sequence_adapter<asio::mutable_buffer,
  402. MutableBufferSequence> bufs(buffers);
  403. return socket_ops::sync_recvmsg(impl.socket_, impl.state_,
  404. bufs.buffers(), bufs.count(), in_flags, out_flags, ec);
  405. }
  406. // Wait until data can be received without blocking.
  407. size_t receive_with_flags(base_implementation_type& impl,
  408. const null_buffers&, socket_base::message_flags,
  409. socket_base::message_flags& out_flags, asio::error_code& ec)
  410. {
  411. // Wait for socket to become ready.
  412. socket_ops::poll_read(impl.socket_, impl.state_, -1, ec);
  413. // Clear out_flags, since we cannot give it any other sensible value when
  414. // performing a null_buffers operation.
  415. out_flags = 0;
  416. return 0;
  417. }
  418. // Start an asynchronous receive. The buffer for the data being received
  419. // must be valid for the lifetime of the asynchronous operation.
  420. template <typename MutableBufferSequence,
  421. typename Handler, typename IoExecutor>
  422. void async_receive_with_flags(base_implementation_type& impl,
  423. const MutableBufferSequence& buffers, socket_base::message_flags in_flags,
  424. socket_base::message_flags& out_flags, Handler& handler,
  425. const IoExecutor& io_ex)
  426. {
  427. bool is_continuation =
  428. asio_handler_cont_helpers::is_continuation(handler);
  429. typename associated_cancellation_slot<Handler>::type slot
  430. = asio::get_associated_cancellation_slot(handler);
  431. // Allocate and construct an operation to wrap the handler.
  432. typedef reactive_socket_recvmsg_op<
  433. MutableBufferSequence, 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(success_ec_, impl.socket_,
  437. buffers, in_flags, out_flags, handler, io_ex);
  438. // Optionally register for per-operation cancellation.
  439. if (slot.is_connected())
  440. {
  441. p.p->cancellation_key_ =
  442. &slot.template emplace<reactor_op_cancellation>(
  443. &reactor_, &impl.reactor_data_, impl.socket_, reactor::read_op);
  444. }
  445. ASIO_HANDLER_CREATION((reactor_.context(), *p.p, "socket",
  446. &impl, impl.socket_, "async_receive_with_flags"));
  447. start_op(impl,
  448. (in_flags & socket_base::message_out_of_band)
  449. ? reactor::except_op : reactor::read_op,
  450. p.p, is_continuation,
  451. (in_flags & socket_base::message_out_of_band) == 0, false);
  452. p.v = p.p = 0;
  453. }
  454. // Wait until data can be received without blocking.
  455. template <typename Handler, typename IoExecutor>
  456. void async_receive_with_flags(base_implementation_type& impl,
  457. const null_buffers&, socket_base::message_flags in_flags,
  458. socket_base::message_flags& out_flags, Handler& handler,
  459. const IoExecutor& io_ex)
  460. {
  461. bool is_continuation =
  462. asio_handler_cont_helpers::is_continuation(handler);
  463. typename associated_cancellation_slot<Handler>::type slot
  464. = asio::get_associated_cancellation_slot(handler);
  465. // Allocate and construct an operation to wrap the handler.
  466. typedef reactive_null_buffers_op<Handler, IoExecutor> op;
  467. typename op::ptr p = { asio::detail::addressof(handler),
  468. op::ptr::allocate(handler), 0 };
  469. p.p = new (p.v) op(success_ec_, handler, io_ex);
  470. // Optionally register for per-operation cancellation.
  471. if (slot.is_connected())
  472. {
  473. p.p->cancellation_key_ =
  474. &slot.template emplace<reactor_op_cancellation>(
  475. &reactor_, &impl.reactor_data_, impl.socket_, reactor::read_op);
  476. }
  477. ASIO_HANDLER_CREATION((reactor_.context(), *p.p, "socket",
  478. &impl, impl.socket_, "async_receive_with_flags(null_buffers)"));
  479. // Clear out_flags, since we cannot give it any other sensible value when
  480. // performing a null_buffers operation.
  481. out_flags = 0;
  482. start_op(impl,
  483. (in_flags & socket_base::message_out_of_band)
  484. ? reactor::except_op : reactor::read_op,
  485. p.p, is_continuation, false, false);
  486. p.v = p.p = 0;
  487. }
  488. protected:
  489. // Open a new socket implementation.
  490. ASIO_DECL asio::error_code do_open(
  491. base_implementation_type& impl, int af,
  492. int type, int protocol, asio::error_code& ec);
  493. // Assign a native socket to a socket implementation.
  494. ASIO_DECL asio::error_code do_assign(
  495. base_implementation_type& impl, int type,
  496. const native_handle_type& native_socket, asio::error_code& ec);
  497. // Start the asynchronous read or write operation.
  498. ASIO_DECL void start_op(base_implementation_type& impl, int op_type,
  499. reactor_op* op, bool is_continuation, bool is_non_blocking, bool noop);
  500. // Start the asynchronous accept operation.
  501. ASIO_DECL void start_accept_op(base_implementation_type& impl,
  502. reactor_op* op, bool is_continuation, bool peer_is_open);
  503. // Start the asynchronous connect operation.
  504. ASIO_DECL void start_connect_op(base_implementation_type& impl,
  505. reactor_op* op, bool is_continuation,
  506. const socket_addr_type* addr, size_t addrlen);
  507. // Helper class used to implement per-operation cancellation
  508. class reactor_op_cancellation
  509. {
  510. public:
  511. reactor_op_cancellation(reactor* r,
  512. reactor::per_descriptor_data* p, int d, int o)
  513. : reactor_(r),
  514. reactor_data_(p),
  515. descriptor_(d),
  516. op_type_(o)
  517. {
  518. }
  519. void operator()(cancellation_type_t type)
  520. {
  521. if (!!(type &
  522. (cancellation_type::terminal
  523. | cancellation_type::partial
  524. | cancellation_type::total)))
  525. {
  526. reactor_->cancel_ops_by_key(descriptor_,
  527. *reactor_data_, op_type_, this);
  528. }
  529. }
  530. private:
  531. reactor* reactor_;
  532. reactor::per_descriptor_data* reactor_data_;
  533. int descriptor_;
  534. int op_type_;
  535. };
  536. // The selector that performs event demultiplexing for the service.
  537. reactor& reactor_;
  538. // Cached success value to avoid accessing category singleton.
  539. const asio::error_code success_ec_;
  540. };
  541. } // namespace detail
  542. } // namespace asio
  543. #include "asio/detail/pop_options.hpp"
  544. #if defined(ASIO_HEADER_ONLY)
  545. # include "asio/detail/impl/reactive_socket_service_base.ipp"
  546. #endif // defined(ASIO_HEADER_ONLY)
  547. #endif // !defined(ASIO_HAS_IOCP)
  548. // && !defined(ASIO_WINDOWS_RUNTIME)
  549. // && !defined(ASIO_HAS_IO_URING_AS_DEFAULT)
  550. #endif // ASIO_DETAIL_REACTIVE_SOCKET_SERVICE_BASE_HPP