reactive_descriptor_service.hpp 18 KB

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