reactive_descriptor_service.ipp 6.2 KB

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