Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:28:42

0001 //
0002 // detail/reactive_socket_connect_op.hpp
0003 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
0004 //
0005 // Copyright (c) 2003-2023 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_REACTIVE_SOCKET_CONNECT_OP_HPP
0012 #define BOOST_ASIO_DETAIL_REACTIVE_SOCKET_CONNECT_OP_HPP
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 #include <boost/asio/detail/bind_handler.hpp>
0020 #include <boost/asio/detail/fenced_block.hpp>
0021 #include <boost/asio/detail/handler_alloc_helpers.hpp>
0022 #include <boost/asio/detail/handler_work.hpp>
0023 #include <boost/asio/detail/memory.hpp>
0024 #include <boost/asio/detail/reactor_op.hpp>
0025 #include <boost/asio/detail/socket_ops.hpp>
0026 
0027 #include <boost/asio/detail/push_options.hpp>
0028 
0029 namespace boost {
0030 namespace asio {
0031 namespace detail {
0032 
0033 class reactive_socket_connect_op_base : public reactor_op
0034 {
0035 public:
0036   reactive_socket_connect_op_base(const boost::system::error_code& success_ec,
0037       socket_type socket, func_type complete_func)
0038     : reactor_op(success_ec,
0039         &reactive_socket_connect_op_base::do_perform, complete_func),
0040       socket_(socket)
0041   {
0042   }
0043 
0044   static status do_perform(reactor_op* base)
0045   {
0046     BOOST_ASIO_ASSUME(base != 0);
0047     reactive_socket_connect_op_base* o(
0048         static_cast<reactive_socket_connect_op_base*>(base));
0049 
0050     status result = socket_ops::non_blocking_connect(
0051         o->socket_, o->ec_) ? done : not_done;
0052 
0053     BOOST_ASIO_HANDLER_REACTOR_OPERATION((*o, "non_blocking_connect", o->ec_));
0054 
0055     return result;
0056   }
0057 
0058 private:
0059   socket_type socket_;
0060 };
0061 
0062 template <typename Handler, typename IoExecutor>
0063 class reactive_socket_connect_op : public reactive_socket_connect_op_base
0064 {
0065 public:
0066   typedef Handler handler_type;
0067   typedef IoExecutor io_executor_type;
0068 
0069   BOOST_ASIO_DEFINE_HANDLER_PTR(reactive_socket_connect_op);
0070 
0071   reactive_socket_connect_op(const boost::system::error_code& success_ec,
0072       socket_type socket, Handler& handler, const IoExecutor& io_ex)
0073     : reactive_socket_connect_op_base(success_ec, socket,
0074         &reactive_socket_connect_op::do_complete),
0075       handler_(static_cast<Handler&&>(handler)),
0076       work_(handler_, io_ex)
0077   {
0078   }
0079 
0080   static void do_complete(void* owner, operation* base,
0081       const boost::system::error_code& /*ec*/,
0082       std::size_t /*bytes_transferred*/)
0083   {
0084     // Take ownership of the handler object.
0085     BOOST_ASIO_ASSUME(base != 0);
0086     reactive_socket_connect_op* o
0087       (static_cast<reactive_socket_connect_op*>(base));
0088     ptr p = { boost::asio::detail::addressof(o->handler_), o, o };
0089 
0090     BOOST_ASIO_HANDLER_COMPLETION((*o));
0091 
0092     // Take ownership of the operation's outstanding work.
0093     handler_work<Handler, IoExecutor> w(
0094         static_cast<handler_work<Handler, IoExecutor>&&>(
0095           o->work_));
0096 
0097     BOOST_ASIO_ERROR_LOCATION(o->ec_);
0098 
0099     // Make a copy of the handler so that the memory can be deallocated before
0100     // the upcall is made. Even if we're not about to make an upcall, a
0101     // sub-object of the handler may be the true owner of the memory associated
0102     // with the handler. Consequently, a local copy of the handler is required
0103     // to ensure that any owning sub-object remains valid until after we have
0104     // deallocated the memory here.
0105     detail::binder1<Handler, boost::system::error_code>
0106       handler(o->handler_, o->ec_);
0107     p.h = boost::asio::detail::addressof(handler.handler_);
0108     p.reset();
0109 
0110     // Make the upcall if required.
0111     if (owner)
0112     {
0113       fenced_block b(fenced_block::half);
0114       BOOST_ASIO_HANDLER_INVOCATION_BEGIN((handler.arg1_));
0115       w.complete(handler, handler.handler_);
0116       BOOST_ASIO_HANDLER_INVOCATION_END;
0117     }
0118   }
0119 
0120   static void do_immediate(operation* base, bool, const void* io_ex)
0121   {
0122     // Take ownership of the handler object.
0123     BOOST_ASIO_ASSUME(base != 0);
0124     reactive_socket_connect_op* o
0125       (static_cast<reactive_socket_connect_op*>(base));
0126     ptr p = { boost::asio::detail::addressof(o->handler_), o, o };
0127 
0128     BOOST_ASIO_HANDLER_COMPLETION((*o));
0129 
0130     // Take ownership of the operation's outstanding work.
0131     immediate_handler_work<Handler, IoExecutor> w(
0132         static_cast<handler_work<Handler, IoExecutor>&&>(
0133           o->work_));
0134 
0135     BOOST_ASIO_ERROR_LOCATION(o->ec_);
0136 
0137     // Make a copy of the handler so that the memory can be deallocated before
0138     // the upcall is made. Even if we're not about to make an upcall, a
0139     // sub-object of the handler may be the true owner of the memory associated
0140     // with the handler. Consequently, a local copy of the handler is required
0141     // to ensure that any owning sub-object remains valid until after we have
0142     // deallocated the memory here.
0143     detail::binder1<Handler, boost::system::error_code>
0144       handler(o->handler_, o->ec_);
0145     p.h = boost::asio::detail::addressof(handler.handler_);
0146     p.reset();
0147 
0148     BOOST_ASIO_HANDLER_INVOCATION_BEGIN((handler.arg1_));
0149     w.complete(handler, handler.handler_, io_ex);
0150     BOOST_ASIO_HANDLER_INVOCATION_END;
0151   }
0152 
0153 private:
0154   Handler handler_;
0155   handler_work<Handler, IoExecutor> work_;
0156 };
0157 
0158 } // namespace detail
0159 } // namespace asio
0160 } // namespace boost
0161 
0162 #include <boost/asio/detail/pop_options.hpp>
0163 
0164 #endif // BOOST_ASIO_DETAIL_REACTIVE_SOCKET_CONNECT_OP_HPP