File indexing completed on 2025-01-18 09:28:33
0001
0002
0003
0004
0005
0006
0007
0008
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
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
0123
0124
0125
0126
0127
0128
0129
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, int op_type,
0239 reactor_op* op, bool is_continuation, bool is_non_blocking, bool noop,
0240 void (*on_immediate)(operation* op, bool, const void*),
0241 const void* immediate_arg)
0242 {
0243 if (!noop)
0244 {
0245 if ((impl.state_ & socket_ops::non_blocking)
0246 || socket_ops::set_internal_non_blocking(
0247 impl.socket_, impl.state_, true, op->ec_))
0248 {
0249 reactor_.start_op(op_type, impl.socket_, impl.reactor_data_, op,
0250 is_continuation, is_non_blocking, on_immediate, immediate_arg);
0251 return;
0252 }
0253 }
0254
0255 on_immediate(op, is_continuation, immediate_arg);
0256 }
0257
0258 void reactive_socket_service_base::do_start_accept_op(
0259 reactive_socket_service_base::base_implementation_type& impl,
0260 reactor_op* op, bool is_continuation, bool peer_is_open,
0261 void (*on_immediate)(operation* op, bool, const void*),
0262 const void* immediate_arg)
0263 {
0264 if (!peer_is_open)
0265 {
0266 do_start_op(impl, reactor::read_op, op, is_continuation,
0267 true, false, on_immediate, immediate_arg);
0268 }
0269 else
0270 {
0271 op->ec_ = boost::asio::error::already_open;
0272 on_immediate(op, is_continuation, immediate_arg);
0273 }
0274 }
0275
0276 void reactive_socket_service_base::do_start_connect_op(
0277 reactive_socket_service_base::base_implementation_type& impl,
0278 reactor_op* op, bool is_continuation, const void* addr, size_t addrlen,
0279 void (*on_immediate)(operation* op, bool, const void*),
0280 const void* immediate_arg)
0281 {
0282 if ((impl.state_ & socket_ops::non_blocking)
0283 || socket_ops::set_internal_non_blocking(
0284 impl.socket_, impl.state_, true, op->ec_))
0285 {
0286 if (socket_ops::connect(impl.socket_, addr, addrlen, op->ec_) != 0)
0287 {
0288 if (op->ec_ == boost::asio::error::in_progress
0289 || op->ec_ == boost::asio::error::would_block)
0290 {
0291 op->ec_ = boost::system::error_code();
0292 reactor_.start_op(reactor::connect_op, impl.socket_, impl.reactor_data_,
0293 op, is_continuation, false, on_immediate, immediate_arg);
0294 return;
0295 }
0296 }
0297 }
0298
0299 on_immediate(op, is_continuation, immediate_arg);
0300 }
0301
0302 }
0303 }
0304 }
0305
0306 #include <boost/asio/detail/pop_options.hpp>
0307
0308 #endif
0309
0310
0311
0312 #endif