reactive_descriptor_service.hpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  1. //
  2. // detail/reactive_descriptor_service.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_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, &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, false, false, &io_ex, 0);
  217. p.v = p.p = 0;
  218. }
  219. // Write some data to the descriptor.
  220. template <typename ConstBufferSequence>
  221. size_t write_some(implementation_type& impl,
  222. const ConstBufferSequence& buffers, boost::system::error_code& ec)
  223. {
  224. typedef buffer_sequence_adapter<boost::asio::const_buffer,
  225. ConstBufferSequence> bufs_type;
  226. size_t n;
  227. if (bufs_type::is_single_buffer)
  228. {
  229. n = descriptor_ops::sync_write1(impl.descriptor_,
  230. impl.state_, bufs_type::first(buffers).data(),
  231. bufs_type::first(buffers).size(), ec);
  232. }
  233. else
  234. {
  235. bufs_type bufs(buffers);
  236. n = descriptor_ops::sync_write(impl.descriptor_, impl.state_,
  237. bufs.buffers(), bufs.count(), bufs.all_empty(), ec);
  238. }
  239. BOOST_ASIO_ERROR_LOCATION(ec);
  240. return n;
  241. }
  242. // Wait until data can be written without blocking.
  243. size_t write_some(implementation_type& impl,
  244. const null_buffers&, boost::system::error_code& ec)
  245. {
  246. // Wait for descriptor to become ready.
  247. descriptor_ops::poll_write(impl.descriptor_, impl.state_, ec);
  248. BOOST_ASIO_ERROR_LOCATION(ec);
  249. return 0;
  250. }
  251. // Start an asynchronous write. The data being sent must be valid for the
  252. // lifetime of the asynchronous operation.
  253. template <typename ConstBufferSequence, typename Handler, typename IoExecutor>
  254. void async_write_some(implementation_type& impl,
  255. const ConstBufferSequence& buffers, Handler& handler,
  256. const IoExecutor& io_ex)
  257. {
  258. bool is_continuation =
  259. boost_asio_handler_cont_helpers::is_continuation(handler);
  260. associated_cancellation_slot_t<Handler> slot
  261. = boost::asio::get_associated_cancellation_slot(handler);
  262. // Allocate and construct an operation to wrap the handler.
  263. typedef descriptor_write_op<ConstBufferSequence, Handler, IoExecutor> op;
  264. typename op::ptr p = { boost::asio::detail::addressof(handler),
  265. op::ptr::allocate(handler), 0 };
  266. p.p = new (p.v) op(success_ec_, impl.descriptor_, buffers, handler, io_ex);
  267. // Optionally register for per-operation cancellation.
  268. if (slot.is_connected())
  269. {
  270. p.p->cancellation_key_ =
  271. &slot.template emplace<reactor_op_cancellation>(
  272. &reactor_, &impl.reactor_data_,
  273. impl.descriptor_, reactor::write_op);
  274. }
  275. BOOST_ASIO_HANDLER_CREATION((reactor_.context(), *p.p, "descriptor",
  276. &impl, impl.descriptor_, "async_write_some"));
  277. start_op(impl, reactor::write_op, p.p, is_continuation, true,
  278. buffer_sequence_adapter<boost::asio::const_buffer,
  279. ConstBufferSequence>::all_empty(buffers), &io_ex, 0);
  280. p.v = p.p = 0;
  281. }
  282. // Start an asynchronous wait until data can be written without blocking.
  283. template <typename Handler, typename IoExecutor>
  284. void async_write_some(implementation_type& impl,
  285. const null_buffers&, Handler& handler, const IoExecutor& io_ex)
  286. {
  287. bool is_continuation =
  288. boost_asio_handler_cont_helpers::is_continuation(handler);
  289. associated_cancellation_slot_t<Handler> slot
  290. = boost::asio::get_associated_cancellation_slot(handler);
  291. // Allocate and construct an operation to wrap the handler.
  292. typedef reactive_null_buffers_op<Handler, IoExecutor> op;
  293. typename op::ptr p = { boost::asio::detail::addressof(handler),
  294. op::ptr::allocate(handler), 0 };
  295. p.p = new (p.v) op(success_ec_, handler, io_ex);
  296. // Optionally register for per-operation cancellation.
  297. if (slot.is_connected())
  298. {
  299. p.p->cancellation_key_ =
  300. &slot.template emplace<reactor_op_cancellation>(
  301. &reactor_, &impl.reactor_data_,
  302. impl.descriptor_, reactor::write_op);
  303. }
  304. BOOST_ASIO_HANDLER_CREATION((reactor_.context(), *p.p, "descriptor",
  305. &impl, impl.descriptor_, "async_write_some(null_buffers)"));
  306. start_op(impl, reactor::write_op, p.p,
  307. is_continuation, false, false, &io_ex, 0);
  308. p.v = p.p = 0;
  309. }
  310. // Read some data from the stream. Returns the number of bytes read.
  311. template <typename MutableBufferSequence>
  312. size_t read_some(implementation_type& impl,
  313. const MutableBufferSequence& buffers, boost::system::error_code& ec)
  314. {
  315. typedef buffer_sequence_adapter<boost::asio::mutable_buffer,
  316. MutableBufferSequence> bufs_type;
  317. size_t n;
  318. if (bufs_type::is_single_buffer)
  319. {
  320. n = descriptor_ops::sync_read1(impl.descriptor_,
  321. impl.state_, bufs_type::first(buffers).data(),
  322. bufs_type::first(buffers).size(), ec);
  323. }
  324. else
  325. {
  326. bufs_type bufs(buffers);
  327. n = descriptor_ops::sync_read(impl.descriptor_, impl.state_,
  328. bufs.buffers(), bufs.count(), bufs.all_empty(), ec);
  329. }
  330. BOOST_ASIO_ERROR_LOCATION(ec);
  331. return n;
  332. }
  333. // Wait until data can be read without blocking.
  334. size_t read_some(implementation_type& impl,
  335. const null_buffers&, boost::system::error_code& ec)
  336. {
  337. // Wait for descriptor to become ready.
  338. descriptor_ops::poll_read(impl.descriptor_, impl.state_, ec);
  339. BOOST_ASIO_ERROR_LOCATION(ec);
  340. return 0;
  341. }
  342. // Start an asynchronous read. The buffer for the data being read must be
  343. // valid for the lifetime of the asynchronous operation.
  344. template <typename MutableBufferSequence,
  345. typename Handler, typename IoExecutor>
  346. void async_read_some(implementation_type& impl,
  347. const MutableBufferSequence& buffers,
  348. Handler& handler, const IoExecutor& io_ex)
  349. {
  350. bool is_continuation =
  351. boost_asio_handler_cont_helpers::is_continuation(handler);
  352. associated_cancellation_slot_t<Handler> slot
  353. = boost::asio::get_associated_cancellation_slot(handler);
  354. // Allocate and construct an operation to wrap the handler.
  355. typedef descriptor_read_op<MutableBufferSequence, Handler, IoExecutor> op;
  356. typename op::ptr p = { boost::asio::detail::addressof(handler),
  357. op::ptr::allocate(handler), 0 };
  358. p.p = new (p.v) op(success_ec_, impl.descriptor_, buffers, handler, io_ex);
  359. // Optionally register for per-operation cancellation.
  360. if (slot.is_connected())
  361. {
  362. p.p->cancellation_key_ =
  363. &slot.template emplace<reactor_op_cancellation>(
  364. &reactor_, &impl.reactor_data_,
  365. impl.descriptor_, reactor::read_op);
  366. }
  367. BOOST_ASIO_HANDLER_CREATION((reactor_.context(), *p.p, "descriptor",
  368. &impl, impl.descriptor_, "async_read_some"));
  369. start_op(impl, reactor::read_op, p.p, is_continuation, true,
  370. buffer_sequence_adapter<boost::asio::mutable_buffer,
  371. MutableBufferSequence>::all_empty(buffers), &io_ex, 0);
  372. p.v = p.p = 0;
  373. }
  374. // Wait until data can be read without blocking.
  375. template <typename Handler, typename IoExecutor>
  376. void async_read_some(implementation_type& impl,
  377. const null_buffers&, Handler& handler, const IoExecutor& io_ex)
  378. {
  379. bool is_continuation =
  380. boost_asio_handler_cont_helpers::is_continuation(handler);
  381. associated_cancellation_slot_t<Handler> slot
  382. = boost::asio::get_associated_cancellation_slot(handler);
  383. // Allocate and construct an operation to wrap the handler.
  384. typedef reactive_null_buffers_op<Handler, IoExecutor> op;
  385. typename op::ptr p = { boost::asio::detail::addressof(handler),
  386. op::ptr::allocate(handler), 0 };
  387. p.p = new (p.v) op(success_ec_, handler, io_ex);
  388. // Optionally register for per-operation cancellation.
  389. if (slot.is_connected())
  390. {
  391. p.p->cancellation_key_ =
  392. &slot.template emplace<reactor_op_cancellation>(
  393. &reactor_, &impl.reactor_data_,
  394. impl.descriptor_, reactor::read_op);
  395. }
  396. BOOST_ASIO_HANDLER_CREATION((reactor_.context(), *p.p, "descriptor",
  397. &impl, impl.descriptor_, "async_read_some(null_buffers)"));
  398. start_op(impl, reactor::read_op, p.p,
  399. is_continuation, false, false, &io_ex, 0);
  400. p.v = p.p = 0;
  401. }
  402. private:
  403. // Start the asynchronous operation.
  404. BOOST_ASIO_DECL void do_start_op(implementation_type& impl, int op_type,
  405. reactor_op* op, bool is_continuation, bool is_non_blocking, bool noop,
  406. void (*on_immediate)(operation* op, bool, const void*),
  407. const void* immediate_arg);
  408. // Start the asynchronous operation for handlers that are specialised for
  409. // immediate completion.
  410. template <typename Op>
  411. void start_op(implementation_type& impl, int op_type, Op* op,
  412. bool is_continuation, bool is_non_blocking, bool noop,
  413. const void* io_ex, ...)
  414. {
  415. return do_start_op(impl, op_type, op, is_continuation,
  416. is_non_blocking, noop, &Op::do_immediate, io_ex);
  417. }
  418. // Start the asynchronous operation for handlers that are not specialised for
  419. // immediate completion.
  420. template <typename Op>
  421. void start_op(implementation_type& impl, int op_type, Op* op,
  422. bool is_continuation, bool is_non_blocking, bool noop, const void*,
  423. enable_if_t<
  424. is_same<
  425. typename associated_immediate_executor<
  426. typename Op::handler_type,
  427. typename Op::io_executor_type
  428. >::asio_associated_immediate_executor_is_unspecialised,
  429. void
  430. >::value
  431. >*)
  432. {
  433. return do_start_op(impl, op_type, op, is_continuation, is_non_blocking,
  434. noop, &reactor::call_post_immediate_completion, &reactor_);
  435. }
  436. // Helper class used to implement per-operation cancellation
  437. class reactor_op_cancellation
  438. {
  439. public:
  440. reactor_op_cancellation(reactor* r,
  441. reactor::per_descriptor_data* p, int d, int o)
  442. : reactor_(r),
  443. reactor_data_(p),
  444. descriptor_(d),
  445. op_type_(o)
  446. {
  447. }
  448. void operator()(cancellation_type_t type)
  449. {
  450. if (!!(type &
  451. (cancellation_type::terminal
  452. | cancellation_type::partial
  453. | cancellation_type::total)))
  454. {
  455. reactor_->cancel_ops_by_key(descriptor_,
  456. *reactor_data_, op_type_, this);
  457. }
  458. }
  459. private:
  460. reactor* reactor_;
  461. reactor::per_descriptor_data* reactor_data_;
  462. int descriptor_;
  463. int op_type_;
  464. };
  465. // The selector that performs event demultiplexing for the service.
  466. reactor& reactor_;
  467. // Cached success value to avoid accessing category singleton.
  468. const boost::system::error_code success_ec_;
  469. };
  470. } // namespace detail
  471. } // namespace asio
  472. } // namespace boost
  473. #include <boost/asio/detail/pop_options.hpp>
  474. #if defined(BOOST_ASIO_HEADER_ONLY)
  475. # include <boost/asio/detail/impl/reactive_descriptor_service.ipp>
  476. #endif // defined(BOOST_ASIO_HEADER_ONLY)
  477. #endif // !defined(BOOST_ASIO_WINDOWS)
  478. // && !defined(BOOST_ASIO_WINDOWS_RUNTIME)
  479. // && !defined(__CYGWIN__)
  480. // && !defined(BOOST_ASIO_HAS_IO_URING_AS_DEFAULT)
  481. #endif // BOOST_ASIO_DETAIL_REACTIVE_DESCRIPTOR_SERVICE_HPP