reactive_descriptor_service.ipp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. //
  2. // detail/impl/reactive_descriptor_service.ipp
  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_IMPL_REACTIVE_DESCRIPTOR_SERVICE_IPP
  11. #define ASIO_DETAIL_IMPL_REACTIVE_DESCRIPTOR_SERVICE_IPP
  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/error.hpp"
  21. #include "asio/detail/reactive_descriptor_service.hpp"
  22. #include "asio/detail/push_options.hpp"
  23. namespace asio {
  24. namespace detail {
  25. reactive_descriptor_service::reactive_descriptor_service(
  26. execution_context& context)
  27. : execution_context_service_base<reactive_descriptor_service>(context),
  28. reactor_(asio::use_service<reactor>(context))
  29. {
  30. reactor_.init_task();
  31. }
  32. void reactive_descriptor_service::shutdown()
  33. {
  34. }
  35. void reactive_descriptor_service::construct(
  36. reactive_descriptor_service::implementation_type& impl)
  37. {
  38. impl.descriptor_ = -1;
  39. impl.state_ = 0;
  40. impl.reactor_data_ = reactor::per_descriptor_data();
  41. }
  42. void reactive_descriptor_service::move_construct(
  43. reactive_descriptor_service::implementation_type& impl,
  44. reactive_descriptor_service::implementation_type& other_impl)
  45. noexcept
  46. {
  47. impl.descriptor_ = other_impl.descriptor_;
  48. other_impl.descriptor_ = -1;
  49. impl.state_ = other_impl.state_;
  50. other_impl.state_ = 0;
  51. reactor_.move_descriptor(impl.descriptor_,
  52. impl.reactor_data_, other_impl.reactor_data_);
  53. }
  54. void reactive_descriptor_service::move_assign(
  55. reactive_descriptor_service::implementation_type& impl,
  56. reactive_descriptor_service& other_service,
  57. reactive_descriptor_service::implementation_type& other_impl)
  58. {
  59. destroy(impl);
  60. impl.descriptor_ = other_impl.descriptor_;
  61. other_impl.descriptor_ = -1;
  62. impl.state_ = other_impl.state_;
  63. other_impl.state_ = 0;
  64. other_service.reactor_.move_descriptor(impl.descriptor_,
  65. impl.reactor_data_, other_impl.reactor_data_);
  66. }
  67. void reactive_descriptor_service::destroy(
  68. reactive_descriptor_service::implementation_type& impl)
  69. {
  70. if (is_open(impl))
  71. {
  72. ASIO_HANDLER_OPERATION((reactor_.context(),
  73. "descriptor", &impl, impl.descriptor_, "close"));
  74. reactor_.deregister_descriptor(impl.descriptor_, impl.reactor_data_,
  75. (impl.state_ & descriptor_ops::possible_dup) == 0);
  76. asio::error_code ignored_ec;
  77. descriptor_ops::close(impl.descriptor_, impl.state_, ignored_ec);
  78. reactor_.cleanup_descriptor_data(impl.reactor_data_);
  79. }
  80. }
  81. asio::error_code reactive_descriptor_service::assign(
  82. reactive_descriptor_service::implementation_type& impl,
  83. const native_handle_type& native_descriptor, asio::error_code& ec)
  84. {
  85. if (is_open(impl))
  86. {
  87. ec = asio::error::already_open;
  88. ASIO_ERROR_LOCATION(ec);
  89. return ec;
  90. }
  91. if (int err = reactor_.register_descriptor(
  92. native_descriptor, impl.reactor_data_))
  93. {
  94. ec = asio::error_code(err,
  95. asio::error::get_system_category());
  96. ASIO_ERROR_LOCATION(ec);
  97. return ec;
  98. }
  99. impl.descriptor_ = native_descriptor;
  100. impl.state_ = descriptor_ops::possible_dup;
  101. ec = asio::error_code();
  102. return ec;
  103. }
  104. asio::error_code reactive_descriptor_service::close(
  105. reactive_descriptor_service::implementation_type& impl,
  106. asio::error_code& ec)
  107. {
  108. if (is_open(impl))
  109. {
  110. ASIO_HANDLER_OPERATION((reactor_.context(),
  111. "descriptor", &impl, impl.descriptor_, "close"));
  112. reactor_.deregister_descriptor(impl.descriptor_, impl.reactor_data_,
  113. (impl.state_ & descriptor_ops::possible_dup) == 0);
  114. descriptor_ops::close(impl.descriptor_, impl.state_, ec);
  115. reactor_.cleanup_descriptor_data(impl.reactor_data_);
  116. }
  117. else
  118. {
  119. ec = asio::error_code();
  120. }
  121. // The descriptor is closed by the OS even if close() returns an error.
  122. //
  123. // (Actually, POSIX says the state of the descriptor is unspecified. On
  124. // Linux the descriptor is apparently closed anyway; e.g. see
  125. // http://lkml.org/lkml/2005/9/10/129
  126. // We'll just have to assume that other OSes follow the same behaviour.)
  127. construct(impl);
  128. ASIO_ERROR_LOCATION(ec);
  129. return ec;
  130. }
  131. reactive_descriptor_service::native_handle_type
  132. reactive_descriptor_service::release(
  133. reactive_descriptor_service::implementation_type& impl)
  134. {
  135. native_handle_type descriptor = impl.descriptor_;
  136. if (is_open(impl))
  137. {
  138. ASIO_HANDLER_OPERATION((reactor_.context(),
  139. "descriptor", &impl, impl.descriptor_, "release"));
  140. reactor_.deregister_descriptor(impl.descriptor_, impl.reactor_data_, false);
  141. reactor_.cleanup_descriptor_data(impl.reactor_data_);
  142. construct(impl);
  143. }
  144. return descriptor;
  145. }
  146. asio::error_code reactive_descriptor_service::cancel(
  147. reactive_descriptor_service::implementation_type& impl,
  148. asio::error_code& ec)
  149. {
  150. if (!is_open(impl))
  151. {
  152. ec = asio::error::bad_descriptor;
  153. ASIO_ERROR_LOCATION(ec);
  154. return ec;
  155. }
  156. ASIO_HANDLER_OPERATION((reactor_.context(),
  157. "descriptor", &impl, impl.descriptor_, "cancel"));
  158. reactor_.cancel_ops(impl.descriptor_, impl.reactor_data_);
  159. ec = asio::error_code();
  160. return ec;
  161. }
  162. void reactive_descriptor_service::do_start_op(implementation_type& impl,
  163. int op_type, reactor_op* op, bool is_continuation, bool is_non_blocking,
  164. bool noop, void (*on_immediate)(operation* op, bool, const void*),
  165. const void* immediate_arg)
  166. {
  167. if (!noop)
  168. {
  169. if ((impl.state_ & descriptor_ops::non_blocking) ||
  170. descriptor_ops::set_internal_non_blocking(
  171. impl.descriptor_, impl.state_, true, op->ec_))
  172. {
  173. reactor_.start_op(op_type, impl.descriptor_, impl.reactor_data_, op,
  174. is_continuation, is_non_blocking, on_immediate, immediate_arg);
  175. return;
  176. }
  177. }
  178. on_immediate(op, is_continuation, immediate_arg);
  179. }
  180. } // namespace detail
  181. } // namespace asio
  182. #include "asio/detail/pop_options.hpp"
  183. #endif // !defined(ASIO_WINDOWS)
  184. // && !defined(ASIO_WINDOWS_RUNTIME)
  185. // && !defined(__CYGWIN__)
  186. // && !defined(ASIO_HAS_IO_URING_AS_DEFAULT)
  187. #endif // ASIO_DETAIL_IMPL_REACTIVE_DESCRIPTOR_SERVICE_IPP