reactive_socket_service_base.hpp 27 KB

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