reactive_socket_service_base.hpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750
  1. //
  2. // detail/reactive_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_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) 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. associated_cancellation_slot_t<Handler> 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. start_op(impl, reactor::read_op, p.p,
  195. is_continuation, false, true, &io_ex, 0);
  196. p.v = p.p = 0;
  197. return;
  198. }
  199. // Optionally register for per-operation cancellation.
  200. if (slot.is_connected())
  201. {
  202. p.p->cancellation_key_ =
  203. &slot.template emplace<reactor_op_cancellation>(
  204. &reactor_, &impl.reactor_data_, impl.socket_, op_type);
  205. }
  206. start_op(impl, op_type, p.p, is_continuation, false, false, &io_ex, 0);
  207. p.v = p.p = 0;
  208. }
  209. // Send the given data to the peer.
  210. template <typename ConstBufferSequence>
  211. size_t send(base_implementation_type& impl,
  212. const ConstBufferSequence& buffers,
  213. socket_base::message_flags flags, asio::error_code& ec)
  214. {
  215. typedef buffer_sequence_adapter<asio::const_buffer,
  216. ConstBufferSequence> bufs_type;
  217. if (bufs_type::is_single_buffer)
  218. {
  219. return socket_ops::sync_send1(impl.socket_,
  220. impl.state_, bufs_type::first(buffers).data(),
  221. bufs_type::first(buffers).size(), flags, ec);
  222. }
  223. else
  224. {
  225. bufs_type bufs(buffers);
  226. return socket_ops::sync_send(impl.socket_, impl.state_,
  227. bufs.buffers(), bufs.count(), flags, bufs.all_empty(), ec);
  228. }
  229. }
  230. // Wait until data can be sent without blocking.
  231. size_t send(base_implementation_type& impl, const null_buffers&,
  232. socket_base::message_flags, asio::error_code& ec)
  233. {
  234. // Wait for socket to become ready.
  235. socket_ops::poll_write(impl.socket_, impl.state_, -1, ec);
  236. return 0;
  237. }
  238. // Start an asynchronous send. The data being sent must be valid for the
  239. // lifetime of the asynchronous operation.
  240. template <typename ConstBufferSequence, typename Handler, typename IoExecutor>
  241. void async_send(base_implementation_type& impl,
  242. const ConstBufferSequence& buffers, socket_base::message_flags flags,
  243. Handler& handler, const IoExecutor& io_ex)
  244. {
  245. bool is_continuation =
  246. asio_handler_cont_helpers::is_continuation(handler);
  247. associated_cancellation_slot_t<Handler> slot
  248. = asio::get_associated_cancellation_slot(handler);
  249. // Allocate and construct an operation to wrap the handler.
  250. typedef reactive_socket_send_op<
  251. ConstBufferSequence, Handler, IoExecutor> op;
  252. typename op::ptr p = { asio::detail::addressof(handler),
  253. op::ptr::allocate(handler), 0 };
  254. p.p = new (p.v) op(success_ec_, impl.socket_,
  255. impl.state_, buffers, flags, handler, io_ex);
  256. // Optionally register for per-operation cancellation.
  257. if (slot.is_connected())
  258. {
  259. p.p->cancellation_key_ =
  260. &slot.template emplace<reactor_op_cancellation>(
  261. &reactor_, &impl.reactor_data_, impl.socket_, reactor::write_op);
  262. }
  263. ASIO_HANDLER_CREATION((reactor_.context(), *p.p, "socket",
  264. &impl, impl.socket_, "async_send"));
  265. start_op(impl, reactor::write_op, p.p, is_continuation, true,
  266. ((impl.state_ & socket_ops::stream_oriented)
  267. && buffer_sequence_adapter<asio::const_buffer,
  268. ConstBufferSequence>::all_empty(buffers)), &io_ex, 0);
  269. p.v = p.p = 0;
  270. }
  271. // Start an asynchronous wait until data can be sent without blocking.
  272. template <typename Handler, typename IoExecutor>
  273. void async_send(base_implementation_type& impl, const null_buffers&,
  274. socket_base::message_flags, Handler& handler, const IoExecutor& io_ex)
  275. {
  276. bool is_continuation =
  277. asio_handler_cont_helpers::is_continuation(handler);
  278. associated_cancellation_slot_t<Handler> slot
  279. = asio::get_associated_cancellation_slot(handler);
  280. // Allocate and construct an operation to wrap the handler.
  281. typedef reactive_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(success_ec_, handler, io_ex);
  285. // Optionally register for per-operation cancellation.
  286. if (slot.is_connected())
  287. {
  288. p.p->cancellation_key_ =
  289. &slot.template emplace<reactor_op_cancellation>(
  290. &reactor_, &impl.reactor_data_, impl.socket_, reactor::write_op);
  291. }
  292. ASIO_HANDLER_CREATION((reactor_.context(), *p.p, "socket",
  293. &impl, impl.socket_, "async_send(null_buffers)"));
  294. start_op(impl, reactor::write_op, p.p,
  295. is_continuation, false, false, &io_ex, 0);
  296. p.v = p.p = 0;
  297. }
  298. // Receive some data from the peer. Returns the number of bytes received.
  299. template <typename MutableBufferSequence>
  300. size_t receive(base_implementation_type& impl,
  301. const MutableBufferSequence& buffers,
  302. socket_base::message_flags flags, asio::error_code& ec)
  303. {
  304. typedef buffer_sequence_adapter<asio::mutable_buffer,
  305. MutableBufferSequence> bufs_type;
  306. if (bufs_type::is_single_buffer)
  307. {
  308. return socket_ops::sync_recv1(impl.socket_,
  309. impl.state_, bufs_type::first(buffers).data(),
  310. bufs_type::first(buffers).size(), flags, ec);
  311. }
  312. else
  313. {
  314. bufs_type bufs(buffers);
  315. return socket_ops::sync_recv(impl.socket_, impl.state_,
  316. bufs.buffers(), bufs.count(), flags, bufs.all_empty(), ec);
  317. }
  318. }
  319. // Wait until data can be received without blocking.
  320. size_t receive(base_implementation_type& impl, const null_buffers&,
  321. socket_base::message_flags, asio::error_code& ec)
  322. {
  323. // Wait for socket to become ready.
  324. socket_ops::poll_read(impl.socket_, impl.state_, -1, ec);
  325. return 0;
  326. }
  327. // Start an asynchronous receive. The buffer for the data being received
  328. // must be valid for the lifetime of the asynchronous operation.
  329. template <typename MutableBufferSequence,
  330. typename Handler, typename IoExecutor>
  331. void async_receive(base_implementation_type& impl,
  332. const MutableBufferSequence& buffers, socket_base::message_flags flags,
  333. Handler& handler, const IoExecutor& io_ex)
  334. {
  335. bool is_continuation =
  336. asio_handler_cont_helpers::is_continuation(handler);
  337. associated_cancellation_slot_t<Handler> slot
  338. = asio::get_associated_cancellation_slot(handler);
  339. // Allocate and construct an operation to wrap the handler.
  340. typedef reactive_socket_recv_op<
  341. MutableBufferSequence, Handler, IoExecutor> op;
  342. typename op::ptr p = { asio::detail::addressof(handler),
  343. op::ptr::allocate(handler), 0 };
  344. p.p = new (p.v) op(success_ec_, impl.socket_,
  345. impl.state_, buffers, flags, handler, io_ex);
  346. // Optionally register for per-operation cancellation.
  347. if (slot.is_connected())
  348. {
  349. p.p->cancellation_key_ =
  350. &slot.template emplace<reactor_op_cancellation>(
  351. &reactor_, &impl.reactor_data_, impl.socket_, reactor::read_op);
  352. }
  353. ASIO_HANDLER_CREATION((reactor_.context(), *p.p, "socket",
  354. &impl, impl.socket_, "async_receive"));
  355. start_op(impl,
  356. (flags & socket_base::message_out_of_band)
  357. ? reactor::except_op : reactor::read_op,
  358. p.p, is_continuation,
  359. (flags & socket_base::message_out_of_band) == 0,
  360. ((impl.state_ & socket_ops::stream_oriented)
  361. && buffer_sequence_adapter<asio::mutable_buffer,
  362. MutableBufferSequence>::all_empty(buffers)), &io_ex, 0);
  363. p.v = p.p = 0;
  364. }
  365. // Wait until data can be received without blocking.
  366. template <typename Handler, typename IoExecutor>
  367. void async_receive(base_implementation_type& impl,
  368. const null_buffers&, socket_base::message_flags flags,
  369. Handler& handler, const IoExecutor& io_ex)
  370. {
  371. bool is_continuation =
  372. asio_handler_cont_helpers::is_continuation(handler);
  373. associated_cancellation_slot_t<Handler> slot
  374. = asio::get_associated_cancellation_slot(handler);
  375. // Allocate and construct an operation to wrap the handler.
  376. typedef reactive_null_buffers_op<Handler, IoExecutor> op;
  377. typename op::ptr p = { asio::detail::addressof(handler),
  378. op::ptr::allocate(handler), 0 };
  379. p.p = new (p.v) op(success_ec_, handler, io_ex);
  380. // Optionally register for per-operation cancellation.
  381. if (slot.is_connected())
  382. {
  383. p.p->cancellation_key_ =
  384. &slot.template emplace<reactor_op_cancellation>(
  385. &reactor_, &impl.reactor_data_, impl.socket_, reactor::read_op);
  386. }
  387. ASIO_HANDLER_CREATION((reactor_.context(), *p.p, "socket",
  388. &impl, impl.socket_, "async_receive(null_buffers)"));
  389. start_op(impl,
  390. (flags & socket_base::message_out_of_band)
  391. ? reactor::except_op : reactor::read_op,
  392. p.p, is_continuation, false, false, &io_ex, 0);
  393. p.v = p.p = 0;
  394. }
  395. // Receive some data with associated flags. Returns the number of bytes
  396. // received.
  397. template <typename MutableBufferSequence>
  398. size_t receive_with_flags(base_implementation_type& impl,
  399. const MutableBufferSequence& buffers,
  400. socket_base::message_flags in_flags,
  401. socket_base::message_flags& out_flags, asio::error_code& ec)
  402. {
  403. buffer_sequence_adapter<asio::mutable_buffer,
  404. MutableBufferSequence> bufs(buffers);
  405. return socket_ops::sync_recvmsg(impl.socket_, impl.state_,
  406. bufs.buffers(), bufs.count(), in_flags, out_flags, ec);
  407. }
  408. // Wait until data can be received without blocking.
  409. size_t receive_with_flags(base_implementation_type& impl,
  410. const null_buffers&, socket_base::message_flags,
  411. socket_base::message_flags& out_flags, asio::error_code& ec)
  412. {
  413. // Wait for socket to become ready.
  414. socket_ops::poll_read(impl.socket_, impl.state_, -1, ec);
  415. // Clear out_flags, since we cannot give it any other sensible value when
  416. // performing a null_buffers operation.
  417. out_flags = 0;
  418. return 0;
  419. }
  420. // Start an asynchronous receive. The buffer for the data being received
  421. // must be valid for the lifetime of the asynchronous operation.
  422. template <typename MutableBufferSequence,
  423. typename Handler, typename IoExecutor>
  424. void async_receive_with_flags(base_implementation_type& impl,
  425. const MutableBufferSequence& buffers, socket_base::message_flags in_flags,
  426. socket_base::message_flags& out_flags, Handler& handler,
  427. const IoExecutor& io_ex)
  428. {
  429. bool is_continuation =
  430. asio_handler_cont_helpers::is_continuation(handler);
  431. associated_cancellation_slot_t<Handler> slot
  432. = asio::get_associated_cancellation_slot(handler);
  433. // Allocate and construct an operation to wrap the handler.
  434. typedef reactive_socket_recvmsg_op<
  435. MutableBufferSequence, Handler, IoExecutor> op;
  436. typename op::ptr p = { asio::detail::addressof(handler),
  437. op::ptr::allocate(handler), 0 };
  438. p.p = new (p.v) op(success_ec_, impl.socket_,
  439. buffers, in_flags, out_flags, handler, io_ex);
  440. // Optionally register for per-operation cancellation.
  441. if (slot.is_connected())
  442. {
  443. p.p->cancellation_key_ =
  444. &slot.template emplace<reactor_op_cancellation>(
  445. &reactor_, &impl.reactor_data_, impl.socket_, reactor::read_op);
  446. }
  447. ASIO_HANDLER_CREATION((reactor_.context(), *p.p, "socket",
  448. &impl, impl.socket_, "async_receive_with_flags"));
  449. start_op(impl,
  450. (in_flags & socket_base::message_out_of_band)
  451. ? reactor::except_op : reactor::read_op,
  452. p.p, is_continuation,
  453. (in_flags & socket_base::message_out_of_band) == 0, false, &io_ex, 0);
  454. p.v = p.p = 0;
  455. }
  456. // Wait until data can be received without blocking.
  457. template <typename Handler, typename IoExecutor>
  458. void async_receive_with_flags(base_implementation_type& impl,
  459. const null_buffers&, socket_base::message_flags in_flags,
  460. socket_base::message_flags& out_flags, Handler& handler,
  461. const IoExecutor& io_ex)
  462. {
  463. bool is_continuation =
  464. asio_handler_cont_helpers::is_continuation(handler);
  465. associated_cancellation_slot_t<Handler> slot
  466. = asio::get_associated_cancellation_slot(handler);
  467. // Allocate and construct an operation to wrap the handler.
  468. typedef reactive_null_buffers_op<Handler, IoExecutor> op;
  469. typename op::ptr p = { asio::detail::addressof(handler),
  470. op::ptr::allocate(handler), 0 };
  471. p.p = new (p.v) op(success_ec_, handler, io_ex);
  472. // Optionally register for per-operation cancellation.
  473. if (slot.is_connected())
  474. {
  475. p.p->cancellation_key_ =
  476. &slot.template emplace<reactor_op_cancellation>(
  477. &reactor_, &impl.reactor_data_, impl.socket_, reactor::read_op);
  478. }
  479. ASIO_HANDLER_CREATION((reactor_.context(), *p.p, "socket",
  480. &impl, impl.socket_, "async_receive_with_flags(null_buffers)"));
  481. // Clear out_flags, since we cannot give it any other sensible value when
  482. // performing a null_buffers operation.
  483. out_flags = 0;
  484. start_op(impl,
  485. (in_flags & socket_base::message_out_of_band)
  486. ? reactor::except_op : reactor::read_op,
  487. p.p, is_continuation, false, false, &io_ex, 0);
  488. p.v = p.p = 0;
  489. }
  490. protected:
  491. // Open a new socket implementation.
  492. ASIO_DECL asio::error_code do_open(
  493. base_implementation_type& impl, int af,
  494. int type, int protocol, asio::error_code& ec);
  495. // Assign a native socket to a socket implementation.
  496. ASIO_DECL asio::error_code do_assign(
  497. base_implementation_type& impl, int type,
  498. const native_handle_type& native_socket, asio::error_code& ec);
  499. // Start the asynchronous read or write operation.
  500. ASIO_DECL void do_start_op(base_implementation_type& impl, int op_type,
  501. reactor_op* op, bool is_continuation, bool is_non_blocking, bool noop,
  502. void (*on_immediate)(operation* op, bool, const void*),
  503. const void* immediate_arg);
  504. // Start the asynchronous operation for handlers that are specialised for
  505. // immediate completion.
  506. template <typename Op>
  507. void start_op(base_implementation_type& impl, int op_type, Op* op,
  508. bool is_continuation, bool is_non_blocking, bool noop,
  509. const void* io_ex, ...)
  510. {
  511. return do_start_op(impl, op_type, op, is_continuation,
  512. is_non_blocking, noop, &Op::do_immediate, io_ex);
  513. }
  514. // Start the asynchronous operation for handlers that are not specialised for
  515. // immediate completion.
  516. template <typename Op>
  517. void start_op(base_implementation_type& impl, int op_type, Op* op,
  518. bool is_continuation, bool is_non_blocking, bool noop, const void*,
  519. enable_if_t<
  520. is_same<
  521. typename associated_immediate_executor<
  522. typename Op::handler_type,
  523. typename Op::io_executor_type
  524. >::asio_associated_immediate_executor_is_unspecialised,
  525. void
  526. >::value
  527. >*)
  528. {
  529. return do_start_op(impl, op_type, op, is_continuation, is_non_blocking,
  530. noop, &reactor::call_post_immediate_completion, &reactor_);
  531. }
  532. // Start the asynchronous accept operation.
  533. ASIO_DECL void do_start_accept_op(base_implementation_type& impl,
  534. reactor_op* op, bool is_continuation, bool peer_is_open,
  535. void (*on_immediate)(operation* op, bool, const void*),
  536. const void* immediate_arg);
  537. // Start the asynchronous accept operation for handlers that are specialised
  538. // for immediate completion.
  539. template <typename Op>
  540. void start_accept_op(base_implementation_type& impl, Op* op,
  541. bool is_continuation, bool peer_is_open, const void* io_ex, ...)
  542. {
  543. return do_start_accept_op(impl, op, is_continuation,
  544. peer_is_open, &Op::do_immediate, io_ex);
  545. }
  546. // Start the asynchronous operation for handlers that are not specialised for
  547. // immediate completion.
  548. template <typename Op>
  549. void start_accept_op(base_implementation_type& impl, Op* op,
  550. bool is_continuation, bool peer_is_open, const void*,
  551. enable_if_t<
  552. is_same<
  553. typename associated_immediate_executor<
  554. typename Op::handler_type,
  555. typename Op::io_executor_type
  556. >::asio_associated_immediate_executor_is_unspecialised,
  557. void
  558. >::value
  559. >*)
  560. {
  561. return do_start_accept_op(impl, op, is_continuation, peer_is_open,
  562. &reactor::call_post_immediate_completion, &reactor_);
  563. }
  564. // Start the asynchronous connect operation.
  565. ASIO_DECL void do_start_connect_op(base_implementation_type& impl,
  566. reactor_op* op, bool is_continuation, const void* addr, size_t addrlen,
  567. void (*on_immediate)(operation* op, bool, const void*),
  568. const void* immediate_arg);
  569. // Start the asynchronous operation for handlers that are specialised for
  570. // immediate completion.
  571. template <typename Op>
  572. void start_connect_op(base_implementation_type& impl,
  573. Op* op, bool is_continuation, const void* addr,
  574. size_t addrlen, const void* io_ex, ...)
  575. {
  576. return do_start_connect_op(impl, op, is_continuation,
  577. addr, addrlen, &Op::do_immediate, io_ex);
  578. }
  579. // Start the asynchronous operation for handlers that are not specialised for
  580. // immediate completion.
  581. template <typename Op>
  582. void start_connect_op(base_implementation_type& impl, Op* op,
  583. bool is_continuation, const void* addr, size_t addrlen, const void*,
  584. enable_if_t<
  585. is_same<
  586. typename associated_immediate_executor<
  587. typename Op::handler_type,
  588. typename Op::io_executor_type
  589. >::asio_associated_immediate_executor_is_unspecialised,
  590. void
  591. >::value
  592. >*)
  593. {
  594. return do_start_connect_op(impl, op, is_continuation, addr,
  595. addrlen, &reactor::call_post_immediate_completion, &reactor_);
  596. }
  597. // Helper class used to implement per-operation cancellation
  598. class reactor_op_cancellation
  599. {
  600. public:
  601. reactor_op_cancellation(reactor* r,
  602. reactor::per_descriptor_data* p, socket_type d, int o)
  603. : reactor_(r),
  604. reactor_data_(p),
  605. descriptor_(d),
  606. op_type_(o)
  607. {
  608. }
  609. void operator()(cancellation_type_t type)
  610. {
  611. if (!!(type &
  612. (cancellation_type::terminal
  613. | cancellation_type::partial
  614. | cancellation_type::total)))
  615. {
  616. reactor_->cancel_ops_by_key(descriptor_,
  617. *reactor_data_, op_type_, this);
  618. }
  619. }
  620. private:
  621. reactor* reactor_;
  622. reactor::per_descriptor_data* reactor_data_;
  623. socket_type descriptor_;
  624. int op_type_;
  625. };
  626. // The selector that performs event demultiplexing for the service.
  627. reactor& reactor_;
  628. // Cached success value to avoid accessing category singleton.
  629. const asio::error_code success_ec_;
  630. };
  631. } // namespace detail
  632. } // namespace asio
  633. #include "asio/detail/pop_options.hpp"
  634. #if defined(ASIO_HEADER_ONLY)
  635. # include "asio/detail/impl/reactive_socket_service_base.ipp"
  636. #endif // defined(ASIO_HEADER_ONLY)
  637. #endif // !defined(ASIO_HAS_IOCP)
  638. // && !defined(ASIO_WINDOWS_RUNTIME)
  639. // && !defined(ASIO_HAS_IO_URING_AS_DEFAULT)
  640. #endif // ASIO_DETAIL_REACTIVE_SOCKET_SERVICE_BASE_HPP