Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-07-01 08:08:40

0001 //
0002 // detail/reactive_socket_service_base.ipp
0003 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
0004 //
0005 // Copyright (c) 2003-2024 Christopher M. Kohlhoff (chris at kohlhoff dot com)
0006 //
0007 // Distributed under the Boost Software License, Version 1.0. (See accompanying
0008 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0009 //
0010 
0011 #ifndef BOOST_ASIO_DETAIL_IMPL_REACTIVE_SOCKET_SERVICE_BASE_IPP
0012 #define BOOST_ASIO_DETAIL_IMPL_REACTIVE_SOCKET_SERVICE_BASE_IPP
0013 
0014 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
0015 # pragma once
0016 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
0017 
0018 #include <boost/asio/detail/config.hpp>
0019 
0020 #if !defined(BOOST_ASIO_HAS_IOCP) \
0021   && !defined(BOOST_ASIO_WINDOWS_RUNTIME) \
0022   && !defined(BOOST_ASIO_HAS_IO_URING_AS_DEFAULT)
0023 
0024 #include <boost/asio/detail/reactive_socket_service_base.hpp>
0025 
0026 #include <boost/asio/detail/push_options.hpp>
0027 
0028 namespace boost {
0029 namespace asio {
0030 namespace detail {
0031 
0032 reactive_socket_service_base::reactive_socket_service_base(
0033     execution_context& context)
0034   : reactor_(use_service<reactor>(context))
0035 {
0036   reactor_.init_task();
0037 }
0038 
0039 void reactive_socket_service_base::base_shutdown()
0040 {
0041 }
0042 
0043 void reactive_socket_service_base::construct(
0044     reactive_socket_service_base::base_implementation_type& impl)
0045 {
0046   impl.socket_ = invalid_socket;
0047   impl.state_ = 0;
0048   impl.reactor_data_ = reactor::per_descriptor_data();
0049 }
0050 
0051 void reactive_socket_service_base::base_move_construct(
0052     reactive_socket_service_base::base_implementation_type& impl,
0053     reactive_socket_service_base::base_implementation_type& other_impl)
0054   noexcept
0055 {
0056   impl.socket_ = other_impl.socket_;
0057   other_impl.socket_ = invalid_socket;
0058 
0059   impl.state_ = other_impl.state_;
0060   other_impl.state_ = 0;
0061 
0062   reactor_.move_descriptor(impl.socket_,
0063       impl.reactor_data_, other_impl.reactor_data_);
0064 }
0065 
0066 void reactive_socket_service_base::base_move_assign(
0067     reactive_socket_service_base::base_implementation_type& impl,
0068     reactive_socket_service_base& other_service,
0069     reactive_socket_service_base::base_implementation_type& other_impl)
0070 {
0071   destroy(impl);
0072 
0073   impl.socket_ = other_impl.socket_;
0074   other_impl.socket_ = invalid_socket;
0075 
0076   impl.state_ = other_impl.state_;
0077   other_impl.state_ = 0;
0078 
0079   other_service.reactor_.move_descriptor(impl.socket_,
0080       impl.reactor_data_, other_impl.reactor_data_);
0081 }
0082 
0083 void reactive_socket_service_base::destroy(
0084     reactive_socket_service_base::base_implementation_type& impl)
0085 {
0086   if (impl.socket_ != invalid_socket)
0087   {
0088     BOOST_ASIO_HANDLER_OPERATION((reactor_.context(),
0089           "socket", &impl, impl.socket_, "close"));
0090 
0091     reactor_.deregister_descriptor(impl.socket_, impl.reactor_data_,
0092         (impl.state_ & socket_ops::possible_dup) == 0);
0093 
0094     boost::system::error_code ignored_ec;
0095     socket_ops::close(impl.socket_, impl.state_, true, ignored_ec);
0096 
0097     reactor_.cleanup_descriptor_data(impl.reactor_data_);
0098   }
0099 }
0100 
0101 boost::system::error_code reactive_socket_service_base::close(
0102     reactive_socket_service_base::base_implementation_type& impl,
0103     boost::system::error_code& ec)
0104 {
0105   if (is_open(impl))
0106   {
0107     BOOST_ASIO_HANDLER_OPERATION((reactor_.context(),
0108           "socket", &impl, impl.socket_, "close"));
0109 
0110     reactor_.deregister_descriptor(impl.socket_, impl.reactor_data_,
0111         (impl.state_ & socket_ops::possible_dup) == 0);
0112 
0113     socket_ops::close(impl.socket_, impl.state_, false, ec);
0114 
0115     reactor_.cleanup_descriptor_data(impl.reactor_data_);
0116   }
0117   else
0118   {
0119     ec = boost::system::error_code();
0120   }
0121 
0122   // The descriptor is closed by the OS even if close() returns an error.
0123   //
0124   // (Actually, POSIX says the state of the descriptor is unspecified. On
0125   // Linux the descriptor is apparently closed anyway; e.g. see
0126   //   http://lkml.org/lkml/2005/9/10/129
0127   // We'll just have to assume that other OSes follow the same behaviour. The
0128   // known exception is when Windows's closesocket() function fails with
0129   // WSAEWOULDBLOCK, but this case is handled inside socket_ops::close().
0130   construct(impl);
0131 
0132   return ec;
0133 }
0134 
0135 socket_type reactive_socket_service_base::release(
0136     reactive_socket_service_base::base_implementation_type& impl,
0137     boost::system::error_code& ec)
0138 {
0139   if (!is_open(impl))
0140   {
0141     ec = boost::asio::error::bad_descriptor;
0142     return invalid_socket;
0143   }
0144 
0145   BOOST_ASIO_HANDLER_OPERATION((reactor_.context(),
0146         "socket", &impl, impl.socket_, "release"));
0147 
0148   reactor_.deregister_descriptor(impl.socket_, impl.reactor_data_, false);
0149   reactor_.cleanup_descriptor_data(impl.reactor_data_);
0150   socket_type sock = impl.socket_;
0151   construct(impl);
0152   ec = boost::system::error_code();
0153   return sock;
0154 }
0155 
0156 boost::system::error_code reactive_socket_service_base::cancel(
0157     reactive_socket_service_base::base_implementation_type& impl,
0158     boost::system::error_code& ec)
0159 {
0160   if (!is_open(impl))
0161   {
0162     ec = boost::asio::error::bad_descriptor;
0163     return ec;
0164   }
0165 
0166   BOOST_ASIO_HANDLER_OPERATION((reactor_.context(),
0167         "socket", &impl, impl.socket_, "cancel"));
0168 
0169   reactor_.cancel_ops(impl.socket_, impl.reactor_data_);
0170   ec = boost::system::error_code();
0171   return ec;
0172 }
0173 
0174 boost::system::error_code reactive_socket_service_base::do_open(
0175     reactive_socket_service_base::base_implementation_type& impl,
0176     int af, int type, int protocol, boost::system::error_code& ec)
0177 {
0178   if (is_open(impl))
0179   {
0180     ec = boost::asio::error::already_open;
0181     return ec;
0182   }
0183 
0184   socket_holder sock(socket_ops::socket(af, type, protocol, ec));
0185   if (sock.get() == invalid_socket)
0186     return ec;
0187 
0188   if (int err = reactor_.register_descriptor(sock.get(), impl.reactor_data_))
0189   {
0190     ec = boost::system::error_code(err,
0191         boost::asio::error::get_system_category());
0192     return ec;
0193   }
0194 
0195   impl.socket_ = sock.release();
0196   switch (type)
0197   {
0198   case SOCK_STREAM: impl.state_ = socket_ops::stream_oriented; break;
0199   case SOCK_DGRAM: impl.state_ = socket_ops::datagram_oriented; break;
0200   default: impl.state_ = 0; break;
0201   }
0202   ec = boost::system::error_code();
0203   return ec;
0204 }
0205 
0206 boost::system::error_code reactive_socket_service_base::do_assign(
0207     reactive_socket_service_base::base_implementation_type& impl, int type,
0208     const reactive_socket_service_base::native_handle_type& native_socket,
0209     boost::system::error_code& ec)
0210 {
0211   if (is_open(impl))
0212   {
0213     ec = boost::asio::error::already_open;
0214     return ec;
0215   }
0216 
0217   if (int err = reactor_.register_descriptor(
0218         native_socket, impl.reactor_data_))
0219   {
0220     ec = boost::system::error_code(err,
0221         boost::asio::error::get_system_category());
0222     return ec;
0223   }
0224 
0225   impl.socket_ = native_socket;
0226   switch (type)
0227   {
0228   case SOCK_STREAM: impl.state_ = socket_ops::stream_oriented; break;
0229   case SOCK_DGRAM: impl.state_ = socket_ops::datagram_oriented; break;
0230   default: impl.state_ = 0; break;
0231   }
0232   impl.state_ |= socket_ops::possible_dup;
0233   ec = boost::system::error_code();
0234   return ec;
0235 }
0236 
0237 void reactive_socket_service_base::do_start_op(
0238     reactive_socket_service_base::base_implementation_type& impl,
0239     int op_type, reactor_op* op, bool is_continuation,
0240     bool allow_speculative, bool noop, bool needs_non_blocking,
0241     void (*on_immediate)(operation* op, bool, const void*),
0242     const void* immediate_arg)
0243 {
0244   if (!noop)
0245   {
0246     if ((impl.state_ & socket_ops::non_blocking)
0247         || !needs_non_blocking
0248         || socket_ops::set_internal_non_blocking(
0249           impl.socket_, impl.state_, true, op->ec_))
0250     {
0251       reactor_.start_op(op_type, impl.socket_, impl.reactor_data_, op,
0252           is_continuation, allow_speculative, on_immediate, immediate_arg);
0253       return;
0254     }
0255   }
0256 
0257   on_immediate(op, is_continuation, immediate_arg);
0258 }
0259 
0260 void reactive_socket_service_base::do_start_accept_op(
0261     reactive_socket_service_base::base_implementation_type& impl,
0262     reactor_op* op, bool is_continuation, bool peer_is_open,
0263     void (*on_immediate)(operation* op, bool, const void*),
0264     const void* immediate_arg)
0265 {
0266   if (!peer_is_open)
0267   {
0268     do_start_op(impl, reactor::read_op, op, is_continuation,
0269         true, false, true, on_immediate, immediate_arg);
0270   }
0271   else
0272   {
0273     op->ec_ = boost::asio::error::already_open;
0274     on_immediate(op, is_continuation, immediate_arg);
0275   }
0276 }
0277 
0278 void reactive_socket_service_base::do_start_connect_op(
0279     reactive_socket_service_base::base_implementation_type& impl,
0280     reactor_op* op, bool is_continuation, const void* addr, size_t addrlen,
0281     void (*on_immediate)(operation* op, bool, const void*),
0282     const void* immediate_arg)
0283 {
0284   if ((impl.state_ & socket_ops::non_blocking)
0285       || socket_ops::set_internal_non_blocking(
0286         impl.socket_, impl.state_, true, op->ec_))
0287   {
0288     if (socket_ops::connect(impl.socket_, addr, addrlen, op->ec_) != 0)
0289     {
0290       if (op->ec_ == boost::asio::error::in_progress
0291           || op->ec_ == boost::asio::error::would_block)
0292       {
0293         op->ec_ = boost::system::error_code();
0294         reactor_.start_op(reactor::connect_op, impl.socket_, impl.reactor_data_,
0295             op, is_continuation, false, on_immediate, immediate_arg);
0296         return;
0297       }
0298     }
0299   }
0300 
0301   on_immediate(op, is_continuation, immediate_arg);
0302 }
0303 
0304 } // namespace detail
0305 } // namespace asio
0306 } // namespace boost
0307 
0308 #include <boost/asio/detail/pop_options.hpp>
0309 
0310 #endif // !defined(BOOST_ASIO_HAS_IOCP)
0311        //   && !defined(BOOST_ASIO_WINDOWS_RUNTIME)
0312        //   && !defined(BOOST_ASIO_HAS_IO_URING_AS_DEFAULT)
0313 
0314 #endif // BOOST_ASIO_DETAIL_IMPL_REACTIVE_SOCKET_SERVICE_BASE_IPP