2 // detail/impl/reactive_descriptor_service.ipp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2016 Christopher M. Kohlhoff (chris at kohlhoff dot com)
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)
11 #ifndef BOOST_ASIO_DETAIL_IMPL_REACTIVE_DESCRIPTOR_SERVICE_IPP
12 #define BOOST_ASIO_DETAIL_IMPL_REACTIVE_DESCRIPTOR_SERVICE_IPP
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
18 #include <boost/asio/detail/config.hpp>
20 #if !defined(BOOST_ASIO_WINDOWS) \
21 && !defined(BOOST_ASIO_WINDOWS_RUNTIME) \
22 && !defined(__CYGWIN__)
24 #include <boost/asio/error.hpp>
25 #include <boost/asio/detail/reactive_descriptor_service.hpp>
27 #include <boost/asio/detail/push_options.hpp>
33 reactive_descriptor_service::reactive_descriptor_service(
34 boost::asio::io_service& io_service)
35 : reactor_(boost::asio::use_service<reactor>(io_service))
40 void reactive_descriptor_service::shutdown_service()
44 void reactive_descriptor_service::construct(
45 reactive_descriptor_service::implementation_type& impl)
47 impl.descriptor_ = -1;
51 void reactive_descriptor_service::move_construct(
52 reactive_descriptor_service::implementation_type& impl,
53 reactive_descriptor_service::implementation_type& other_impl)
55 impl.descriptor_ = other_impl.descriptor_;
56 other_impl.descriptor_ = -1;
58 impl.state_ = other_impl.state_;
59 other_impl.state_ = 0;
61 reactor_.move_descriptor(impl.descriptor_,
62 impl.reactor_data_, other_impl.reactor_data_);
65 void reactive_descriptor_service::move_assign(
66 reactive_descriptor_service::implementation_type& impl,
67 reactive_descriptor_service& other_service,
68 reactive_descriptor_service::implementation_type& other_impl)
72 impl.descriptor_ = other_impl.descriptor_;
73 other_impl.descriptor_ = -1;
75 impl.state_ = other_impl.state_;
76 other_impl.state_ = 0;
78 other_service.reactor_.move_descriptor(impl.descriptor_,
79 impl.reactor_data_, other_impl.reactor_data_);
82 void reactive_descriptor_service::destroy(
83 reactive_descriptor_service::implementation_type& impl)
87 BOOST_ASIO_HANDLER_OPERATION(("descriptor", &impl, "close"));
89 reactor_.deregister_descriptor(impl.descriptor_, impl.reactor_data_,
90 (impl.state_ & descriptor_ops::possible_dup) == 0);
93 boost::system::error_code ignored_ec;
94 descriptor_ops::close(impl.descriptor_, impl.state_, ignored_ec);
97 boost::system::error_code reactive_descriptor_service::assign(
98 reactive_descriptor_service::implementation_type& impl,
99 const native_handle_type& native_descriptor, boost::system::error_code& ec)
103 ec = boost::asio::error::already_open;
107 if (int err = reactor_.register_descriptor(
108 native_descriptor, impl.reactor_data_))
110 ec = boost::system::error_code(err,
111 boost::asio::error::get_system_category());
115 impl.descriptor_ = native_descriptor;
116 impl.state_ = descriptor_ops::possible_dup;
117 ec = boost::system::error_code();
121 boost::system::error_code reactive_descriptor_service::close(
122 reactive_descriptor_service::implementation_type& impl,
123 boost::system::error_code& ec)
127 BOOST_ASIO_HANDLER_OPERATION(("descriptor", &impl, "close"));
129 reactor_.deregister_descriptor(impl.descriptor_, impl.reactor_data_,
130 (impl.state_ & descriptor_ops::possible_dup) == 0);
133 descriptor_ops::close(impl.descriptor_, impl.state_, ec);
135 // The descriptor is closed by the OS even if close() returns an error.
137 // (Actually, POSIX says the state of the descriptor is unspecified. On
138 // Linux the descriptor is apparently closed anyway; e.g. see
139 // http://lkml.org/lkml/2005/9/10/129
140 // We'll just have to assume that other OSes follow the same behaviour.)
146 reactive_descriptor_service::native_handle_type
147 reactive_descriptor_service::release(
148 reactive_descriptor_service::implementation_type& impl)
150 native_handle_type descriptor = impl.descriptor_;
154 BOOST_ASIO_HANDLER_OPERATION(("descriptor", &impl, "release"));
156 reactor_.deregister_descriptor(impl.descriptor_, impl.reactor_data_, false);
163 boost::system::error_code reactive_descriptor_service::cancel(
164 reactive_descriptor_service::implementation_type& impl,
165 boost::system::error_code& ec)
169 ec = boost::asio::error::bad_descriptor;
173 BOOST_ASIO_HANDLER_OPERATION(("descriptor", &impl, "cancel"));
175 reactor_.cancel_ops(impl.descriptor_, impl.reactor_data_);
176 ec = boost::system::error_code();
180 void reactive_descriptor_service::start_op(
181 reactive_descriptor_service::implementation_type& impl,
182 int op_type, reactor_op* op, bool is_continuation,
183 bool is_non_blocking, bool noop)
187 if ((impl.state_ & descriptor_ops::non_blocking) ||
188 descriptor_ops::set_internal_non_blocking(
189 impl.descriptor_, impl.state_, true, op->ec_))
191 reactor_.start_op(op_type, impl.descriptor_,
192 impl.reactor_data_, op, is_continuation, is_non_blocking);
197 reactor_.post_immediate_completion(op, is_continuation);
200 } // namespace detail
204 #include <boost/asio/detail/pop_options.hpp>
206 #endif // !defined(BOOST_ASIO_WINDOWS)
207 // && !defined(BOOST_ASIO_WINDOWS_RUNTIME)
208 // && !defined(__CYGWIN__)
210 #endif // BOOST_ASIO_DETAIL_IMPL_REACTIVE_DESCRIPTOR_SERVICE_IPP