reactive_descriptor_service.hpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  1. //
  2. // detail/reactive_descriptor_service.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_DESCRIPTOR_SERVICE_HPP
  11. #define BOOST_ASIO_DETAIL_REACTIVE_DESCRIPTOR_SERVICE_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_WINDOWS) \
  17. && !defined(BOOST_ASIO_WINDOWS_RUNTIME) \
  18. && !defined(__CYGWIN__) \
  19. && !defined(BOOST_ASIO_HAS_IO_URING_AS_DEFAULT)
  20. #include <boost/asio/associated_cancellation_slot.hpp>
  21. #include <boost/asio/associated_immediate_executor.hpp>
  22. #include <boost/asio/buffer.hpp>
  23. #include <boost/asio/cancellation_type.hpp>
  24. #include <boost/asio/execution_context.hpp>
  25. #include <boost/asio/detail/bind_handler.hpp>
  26. #include <boost/asio/detail/buffer_sequence_adapter.hpp>
  27. #include <boost/asio/detail/descriptor_ops.hpp>
  28. #include <boost/asio/detail/descriptor_read_op.hpp>
  29. #include <boost/asio/detail/descriptor_write_op.hpp>
  30. #include <boost/asio/detail/fenced_block.hpp>
  31. #include <boost/asio/detail/memory.hpp>
  32. #include <boost/asio/detail/noncopyable.hpp>
  33. #include <boost/asio/detail/reactive_null_buffers_op.hpp>
  34. #include <boost/asio/detail/reactive_wait_op.hpp>
  35. #include <boost/asio/detail/reactor.hpp>
  36. #include <boost/asio/posix/descriptor_base.hpp>
  37. #include <boost/asio/detail/push_options.hpp>
  38. namespace boost {
  39. namespace asio {
  40. namespace detail {
  41. class reactive_descriptor_service :
  42. public execution_context_service_base<reactive_descriptor_service>
  43. {
  44. public:
  45. // The native type of a descriptor.
  46. typedef int native_handle_type;
  47. // The implementation type of the descriptor.
  48. class implementation_type
  49. : private boost::asio::detail::noncopyable
  50. {
  51. public:
  52. // Default constructor.
  53. implementation_type()
  54. : descriptor_(-1),
  55. state_(0)
  56. {
  57. }
  58. private:
  59. // Only this service will have access to the internal values.
  60. friend class reactive_descriptor_service;
  61. // The native descriptor representation.
  62. int descriptor_;
  63. // The current state of the descriptor.
  64. descriptor_ops::state_type state_;
  65. // Per-descriptor data used by the reactor.
  66. reactor::per_descriptor_data reactor_data_;
  67. };
  68. // Constructor.
  69. BOOST_ASIO_DECL reactive_descriptor_service(execution_context& context);
  70. // Destroy all user-defined handler objects owned by the service.
  71. BOOST_ASIO_DECL void shutdown();
  72. // Construct a new descriptor implementation.
  73. BOOST_ASIO_DECL void construct(implementation_type& impl);
  74. // Move-construct a new descriptor implementation.
  75. BOOST_ASIO_DECL void move_construct(implementation_type& impl,
  76. implementation_type& other_impl) noexcept;
  77. // Move-assign from another descriptor implementation.
  78. BOOST_ASIO_DECL void move_assign(implementation_type& impl,
  79. reactive_descriptor_service& other_service,
  80. implementation_type& other_impl);
  81. // Destroy a descriptor implementation.
  82. BOOST_ASIO_DECL void destroy(implementation_type& impl);
  83. // Assign a native descriptor to a descriptor implementation.
  84. BOOST_ASIO_DECL boost::system::error_code assign(implementation_type& impl,
  85. const native_handle_type& native_descriptor,
  86. boost::system::error_code& ec);
  87. // Determine whether the descriptor is open.
  88. bool is_open(const implementation_type& impl) const
  89. {
  90. return impl.descriptor_ != -1;
  91. }
  92. // Destroy a descriptor implementation.
  93. BOOST_ASIO_DECL boost::system::error_code close(implementation_type& impl,
  94. boost::system::error_code& ec);
  95. // Get the native descriptor representation.
  96. native_handle_type native_handle(const implementation_type& impl) const
  97. {
  98. return impl.descriptor_;
  99. }
  100. // Release ownership of the native descriptor representation.
  101. BOOST_ASIO_DECL native_handle_type release(implementation_type& impl);
  102. // Release ownership of the native descriptor representation.
  103. native_handle_type release(implementation_type& impl,
  104. boost::system::error_code& ec)
  105. {
  106. ec = success_ec_;
  107. return release(impl);
  108. }
  109. // Cancel all operations associated with the descriptor.
  110. BOOST_ASIO_DECL boost::system::error_code cancel(implementation_type& impl,
  111. boost::system::error_code& ec);
  112. // Perform an IO control command on the descriptor.
  113. template <typename IO_Control_Command>
  114. boost::system::error_code io_control(implementation_type& impl,
  115. IO_Control_Command& command, boost::system::error_code& ec)
  116. {
  117. descriptor_ops::ioctl(impl.descriptor_, impl.state_,
  118. command.name(), static_cast<ioctl_arg_type*>(command.data()), ec);
  119. BOOST_ASIO_ERROR_LOCATION(ec);
  120. return ec;
  121. }
  122. // Gets the non-blocking mode of the descriptor.
  123. bool non_blocking(const implementation_type& impl) const
  124. {
  125. return (impl.state_ & descriptor_ops::user_set_non_blocking) != 0;
  126. }
  127. // Sets the non-blocking mode of the descriptor.
  128. boost::system::error_code non_blocking(implementation_type& impl,
  129. bool mode, boost::system::error_code& ec)
  130. {
  131. descriptor_ops::set_user_non_blocking(
  132. impl.descriptor_, impl.state_, mode, ec);
  133. BOOST_ASIO_ERROR_LOCATION(ec);
  134. return ec;
  135. }
  136. // Gets the non-blocking mode of the native descriptor implementation.
  137. bool native_non_blocking(const implementation_type& impl) const
  138. {
  139. return (impl.state_ & descriptor_ops::internal_non_blocking) != 0;
  140. }
  141. // Sets the non-blocking mode of the native descriptor implementation.
  142. boost::system::error_code native_non_blocking(implementation_type& impl,
  143. bool mode, boost::system::error_code& ec)
  144. {
  145. descriptor_ops::set_internal_non_blocking(
  146. impl.descriptor_, impl.state_, mode, ec);
  147. return ec;
  148. }
  149. // Wait for the descriptor to become ready to read, ready to write, or to have
  150. // pending error conditions.
  151. boost::system::error_code wait(implementation_type& impl,
  152. posix::descriptor_base::wait_type w, boost::system::error_code& ec)
  153. {
  154. switch (w)
  155. {
  156. case posix::descriptor_base::wait_read:
  157. descriptor_ops::poll_read(impl.descriptor_, impl.state_, ec);
  158. break;
  159. case posix::descriptor_base::wait_write:
  160. descriptor_ops::poll_write(impl.descriptor_, impl.state_, ec);
  161. break;
  162. case posix::descriptor_base::wait_error:
  163. descriptor_ops::poll_error(impl.descriptor_, impl.state_, ec);
  164. break;
  165. default:
  166. ec = boost::asio::error::invalid_argument;
  167. break;
  168. }
  169. BOOST_ASIO_ERROR_LOCATION(ec);
  170. return ec;
  171. }
  172. // Asynchronously wait for the descriptor to become ready to read, ready to
  173. // write, or to have pending error conditions.
  174. template <typename Handler, typename IoExecutor>
  175. void async_wait(implementation_type& impl,
  176. posix::descriptor_base::wait_type w,
  177. Handler& handler, const IoExecutor& io_ex)
  178. {
  179. bool is_continuation =
  180. boost_asio_handler_cont_helpers::is_continuation(handler);
  181. associated_cancellation_slot_t<Handler> slot
  182. = boost::asio::get_associated_cancellation_slot(handler);
  183. // Allocate and construct an operation to wrap the handler.
  184. typedef reactive_wait_op<Handler, IoExecutor> op;
  185. typename op::ptr p = { boost::asio::detail::addressof(handler),
  186. op::ptr::allocate(handler), 0 };
  187. p.p = new (p.v) op(success_ec_, handler, io_ex);
  188. BOOST_ASIO_HANDLER_CREATION((reactor_.context(), *p.p, "descriptor",
  189. &impl, impl.descriptor_, "async_wait"));
  190. int op_type;
  191. switch (w)
  192. {
  193. case posix::descriptor_base::wait_read:
  194. op_type = reactor::read_op;
  195. break;
  196. case posix::descriptor_base::wait_write:
  197. op_type = reactor::write_op;
  198. break;
  199. case posix::descriptor_base::wait_error:
  200. op_type = reactor::except_op;
  201. break;
  202. default:
  203. p.p->ec_ = boost::asio::error::invalid_argument;
  204. start_op(impl, reactor::read_op, p.p,
  205. is_continuation, false, true, false, &io_ex, 0);
  206. p.v = p.p = 0;
  207. return;
  208. }
  209. // Optionally register for per-operation cancellation.
  210. if (slot.is_connected())
  211. {
  212. p.p->cancellation_key_ =
  213. &slot.template emplace<reactor_op_cancellation>(
  214. &reactor_, &impl.reactor_data_, impl.descriptor_, op_type);
  215. }
  216. start_op(impl, op_type, p.p, is_continuation,
  217. false, false, false, &io_ex, 0);
  218. p.v = p.p = 0;
  219. }
  220. // Write some data to the descriptor.
  221. template <typename ConstBufferSequence>
  222. size_t write_some(implementation_type& impl,
  223. const ConstBufferSequence& buffers, boost::system::error_code& ec)
  224. {
  225. typedef buffer_sequence_adapter<boost::asio::const_buffer,
  226. ConstBufferSequence> bufs_type;
  227. size_t n;
  228. if (bufs_type::is_single_buffer)
  229. {
  230. n = descriptor_ops::sync_write1(impl.descriptor_,
  231. impl.state_, bufs_type::first(buffers).data(),
  232. bufs_type::first(buffers).size(), ec);
  233. }
  234. else
  235. {
  236. bufs_type bufs(buffers);
  237. n = descriptor_ops::sync_write(impl.descriptor_, impl.state_,
  238. bufs.buffers(), bufs.count(), bufs.all_empty(), ec);
  239. }
  240. BOOST_ASIO_ERROR_LOCATION(ec);
  241. return n;
  242. }
  243. // Wait until data can be written without blocking.
  244. size_t write_some(implementation_type& impl,
  245. const null_buffers&, boost::system::error_code& ec)
  246. {
  247. // Wait for descriptor to become ready.
  248. descriptor_ops::poll_write(impl.descriptor_, impl.state_, ec);
  249. BOOST_ASIO_ERROR_LOCATION(ec);
  250. return 0;
  251. }
  252. // Start an asynchronous write. The data being sent must be valid for the
  253. // lifetime of the asynchronous operation.
  254. template <typename ConstBufferSequence, typename Handler, typename IoExecutor>
  255. void async_write_some(implementation_type& impl,
  256. const ConstBufferSequence& buffers, Handler& handler,
  257. const IoExecutor& io_ex)
  258. {
  259. bool is_continuation =
  260. boost_asio_handler_cont_helpers::is_continuation(handler);
  261. associated_cancellation_slot_t<Handler> slot
  262. = boost::asio::get_associated_cancellation_slot(handler);
  263. // Allocate and construct an operation to wrap the handler.
  264. typedef descriptor_write_op<ConstBufferSequence, Handler, IoExecutor> op;
  265. typename op::ptr p = { boost::asio::detail::addressof(handler),
  266. op::ptr::allocate(handler), 0 };
  267. p.p = new (p.v) op(success_ec_, impl.descriptor_, buffers, handler, io_ex);
  268. // Optionally register for per-operation cancellation.
  269. if (slot.is_connected())
  270. {
  271. p.p->cancellation_key_ =
  272. &slot.template emplace<reactor_op_cancellation>(
  273. &reactor_, &impl.reactor_data_,
  274. impl.descriptor_, reactor::write_op);
  275. }
  276. BOOST_ASIO_HANDLER_CREATION((reactor_.context(), *p.p, "descriptor",
  277. &impl, impl.descriptor_, "async_write_some"));
  278. start_op(impl, reactor::write_op, p.p, is_continuation, true,
  279. buffer_sequence_adapter<boost::asio::const_buffer,
  280. ConstBufferSequence>::all_empty(buffers), true, &io_ex, 0);
  281. p.v = p.p = 0;
  282. }
  283. // Start an asynchronous wait until data can be written without blocking.
  284. template <typename Handler, typename IoExecutor>
  285. void async_write_some(implementation_type& impl,
  286. const null_buffers&, Handler& handler, const IoExecutor& io_ex)
  287. {
  288. bool is_continuation =
  289. boost_asio_handler_cont_helpers::is_continuation(handler);
  290. associated_cancellation_slot_t<Handler> slot
  291. = boost::asio::get_associated_cancellation_slot(handler);
  292. // Allocate and construct an operation to wrap the handler.
  293. typedef reactive_null_buffers_op<Handler, IoExecutor> op;
  294. typename op::ptr p = { boost::asio::detail::addressof(handler),
  295. op::ptr::allocate(handler), 0 };
  296. p.p = new (p.v) op(success_ec_, handler, io_ex);
  297. // Optionally register for per-operation cancellation.
  298. if (slot.is_connected())
  299. {
  300. p.p->cancellation_key_ =
  301. &slot.template emplace<reactor_op_cancellation>(
  302. &reactor_, &impl.reactor_data_,
  303. impl.descriptor_, reactor::write_op);
  304. }
  305. BOOST_ASIO_HANDLER_CREATION((reactor_.context(), *p.p, "descriptor",
  306. &impl, impl.descriptor_, "async_write_some(null_buffers)"));
  307. start_op(impl, reactor::write_op, p.p,
  308. is_continuation, false, false, false, &io_ex, 0);
  309. p.v = p.p = 0;
  310. }
  311. // Read some data from the stream. Returns the number of bytes read.
  312. template <typename MutableBufferSequence>
  313. size_t read_some(implementation_type& impl,
  314. const MutableBufferSequence& buffers, boost::system::error_code& ec)
  315. {
  316. typedef buffer_sequence_adapter<boost::asio::mutable_buffer,
  317. MutableBufferSequence> bufs_type;
  318. size_t n;
  319. if (bufs_type::is_single_buffer)
  320. {
  321. n = descriptor_ops::sync_read1(impl.descriptor_,
  322. impl.state_, bufs_type::first(buffers).data(),
  323. bufs_type::first(buffers).size(), ec);
  324. }
  325. else
  326. {
  327. bufs_type bufs(buffers);
  328. n = descriptor_ops::sync_read(impl.descriptor_, impl.state_,
  329. bufs.buffers(), bufs.count(), bufs.all_empty(), ec);
  330. }
  331. BOOST_ASIO_ERROR_LOCATION(ec);
  332. return n;
  333. }
  334. // Wait until data can be read without blocking.
  335. size_t read_some(implementation_type& impl,
  336. const null_buffers&, boost::system::error_code& ec)
  337. {
  338. // Wait for descriptor to become ready.
  339. descriptor_ops::poll_read(impl.descriptor_, impl.state_, ec);
  340. BOOST_ASIO_ERROR_LOCATION(ec);
  341. return 0;
  342. }
  343. // Start an asynchronous read. The buffer for the data being read must be
  344. // valid for the lifetime of the asynchronous operation.
  345. template <typename MutableBufferSequence,
  346. typename Handler, typename IoExecutor>
  347. void async_read_some(implementation_type& impl,
  348. const MutableBufferSequence& buffers,
  349. Handler& handler, const IoExecutor& io_ex)
  350. {
  351. bool is_continuation =
  352. boost_asio_handler_cont_helpers::is_continuation(handler);
  353. associated_cancellation_slot_t<Handler> slot
  354. = boost::asio::get_associated_cancellation_slot(handler);
  355. // Allocate and construct an operation to wrap the handler.
  356. typedef descriptor_read_op<MutableBufferSequence, Handler, IoExecutor> op;
  357. typename op::ptr p = { boost::asio::detail::addressof(handler),
  358. op::ptr::allocate(handler), 0 };
  359. p.p = new (p.v) op(success_ec_, impl.descriptor_, buffers, handler, io_ex);
  360. // Optionally register for per-operation cancellation.
  361. if (slot.is_connected())
  362. {
  363. p.p->cancellation_key_ =
  364. &slot.template emplace<reactor_op_cancellation>(
  365. &reactor_, &impl.reactor_data_,
  366. impl.descriptor_, reactor::read_op);
  367. }
  368. BOOST_ASIO_HANDLER_CREATION((reactor_.context(), *p.p, "descriptor",
  369. &impl, impl.descriptor_, "async_read_some"));
  370. start_op(impl, reactor::read_op, p.p, is_continuation, true,
  371. buffer_sequence_adapter<boost::asio::mutable_buffer,
  372. MutableBufferSequence>::all_empty(buffers), true, &io_ex, 0);
  373. p.v = p.p = 0;
  374. }
  375. // Wait until data can be read without blocking.
  376. template <typename Handler, typename IoExecutor>
  377. void async_read_some(implementation_type& impl,
  378. const null_buffers&, Handler& handler, const IoExecutor& io_ex)
  379. {
  380. bool is_continuation =
  381. boost_asio_handler_cont_helpers::is_continuation(handler);
  382. associated_cancellation_slot_t<Handler> slot
  383. = boost::asio::get_associated_cancellation_slot(handler);
  384. // Allocate and construct an operation to wrap the handler.
  385. typedef reactive_null_buffers_op<Handler, IoExecutor> op;
  386. typename op::ptr p = { boost::asio::detail::addressof(handler),
  387. op::ptr::allocate(handler), 0 };
  388. p.p = new (p.v) op(success_ec_, handler, io_ex);
  389. // Optionally register for per-operation cancellation.
  390. if (slot.is_connected())
  391. {
  392. p.p->cancellation_key_ =
  393. &slot.template emplace<reactor_op_cancellation>(
  394. &reactor_, &impl.reactor_data_,
  395. impl.descriptor_, reactor::read_op);
  396. }
  397. BOOST_ASIO_HANDLER_CREATION((reactor_.context(), *p.p, "descriptor",
  398. &impl, impl.descriptor_, "async_read_some(null_buffers)"));
  399. start_op(impl, reactor::read_op, p.p,
  400. is_continuation, false, false, false, &io_ex, 0);
  401. p.v = p.p = 0;
  402. }
  403. private:
  404. // Start the asynchronous operation.
  405. BOOST_ASIO_DECL void do_start_op(implementation_type& impl,
  406. int op_type, reactor_op* op, bool is_continuation,
  407. bool allow_speculative, bool noop, bool needs_non_blocking,
  408. void (*on_immediate)(operation* op, bool, const void*),
  409. const void* immediate_arg);
  410. // Start the asynchronous operation for handlers that are specialised for
  411. // immediate completion.
  412. template <typename Op>
  413. void start_op(implementation_type& impl, int op_type, Op* op,
  414. bool is_continuation, bool allow_speculative, bool noop,
  415. bool needs_non_blocking, const void* io_ex, ...)
  416. {
  417. return do_start_op(impl, op_type, op, is_continuation, allow_speculative,
  418. noop, needs_non_blocking, &Op::do_immediate, io_ex);
  419. }
  420. // Start the asynchronous operation for handlers that are not specialised for
  421. // immediate completion.
  422. template <typename Op>
  423. void start_op(implementation_type& impl, int op_type,
  424. Op* op, bool is_continuation, bool allow_speculative,
  425. bool noop, bool needs_non_blocking, const void*,
  426. enable_if_t<
  427. is_same<
  428. typename associated_immediate_executor<
  429. typename Op::handler_type,
  430. typename Op::io_executor_type
  431. >::asio_associated_immediate_executor_is_unspecialised,
  432. void
  433. >::value
  434. >*)
  435. {
  436. return do_start_op(impl, op_type, op, is_continuation,
  437. allow_speculative, noop, needs_non_blocking,
  438. &reactor::call_post_immediate_completion, &reactor_);
  439. }
  440. // Helper class used to implement per-operation cancellation
  441. class reactor_op_cancellation
  442. {
  443. public:
  444. reactor_op_cancellation(reactor* r,
  445. reactor::per_descriptor_data* p, int d, int o)
  446. : reactor_(r),
  447. reactor_data_(p),
  448. descriptor_(d),
  449. op_type_(o)
  450. {
  451. }
  452. void operator()(cancellation_type_t type)
  453. {
  454. if (!!(type &
  455. (cancellation_type::terminal
  456. | cancellation_type::partial
  457. | cancellation_type::total)))
  458. {
  459. reactor_->cancel_ops_by_key(descriptor_,
  460. *reactor_data_, op_type_, this);
  461. }
  462. }
  463. private:
  464. reactor* reactor_;
  465. reactor::per_descriptor_data* reactor_data_;
  466. int descriptor_;
  467. int op_type_;
  468. };
  469. // The selector that performs event demultiplexing for the service.
  470. reactor& reactor_;
  471. // Cached success value to avoid accessing category singleton.
  472. const boost::system::error_code success_ec_;
  473. };
  474. } // namespace detail
  475. } // namespace asio
  476. } // namespace boost
  477. #include <boost/asio/detail/pop_options.hpp>
  478. #if defined(BOOST_ASIO_HEADER_ONLY)
  479. # include <boost/asio/detail/impl/reactive_descriptor_service.ipp>
  480. #endif // defined(BOOST_ASIO_HEADER_ONLY)
  481. #endif // !defined(BOOST_ASIO_WINDOWS)
  482. // && !defined(BOOST_ASIO_WINDOWS_RUNTIME)
  483. // && !defined(__CYGWIN__)
  484. // && !defined(BOOST_ASIO_HAS_IO_URING_AS_DEFAULT)
  485. #endif // BOOST_ASIO_DETAIL_REACTIVE_DESCRIPTOR_SERVICE_HPP