reactive_socket_service_base.hpp 27 KB

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