reactive_descriptor_service.ipp 6.2 KB

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