Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-08-22 08:37:39

0001 //
0002 // detail/reactive_socket_recv_op.hpp
0003 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
0004 //
0005 // Copyright (c) 2003-2025 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_RECV_OP_HPP
0012 #define BOOST_ASIO_DETAIL_REACTIVE_SOCKET_RECV_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/buffer_sequence_adapter.hpp>
0021 #include <boost/asio/detail/fenced_block.hpp>
0022 #include <boost/asio/detail/handler_alloc_helpers.hpp>
0023 #include <boost/asio/detail/handler_work.hpp>
0024 #include <boost/asio/detail/memory.hpp>
0025 #include <boost/asio/detail/reactor_op.hpp>
0026 #include <boost/asio/detail/socket_ops.hpp>
0027 
0028 #include <boost/asio/detail/push_options.hpp>
0029 
0030 namespace boost {
0031 namespace asio {
0032 namespace detail {
0033 
0034 template <typename MutableBufferSequence>
0035 class reactive_socket_recv_op_base : public reactor_op
0036 {
0037 public:
0038   reactive_socket_recv_op_base(const boost::system::error_code& success_ec,
0039       socket_type socket, socket_ops::state_type state,
0040       const MutableBufferSequence& buffers,
0041       socket_base::message_flags flags, func_type complete_func)
0042     : reactor_op(success_ec,
0043         &reactive_socket_recv_op_base::do_perform, complete_func),
0044       socket_(socket),
0045       state_(state),
0046       buffers_(buffers),
0047       flags_(flags)
0048   {
0049   }
0050 
0051   static status do_perform(reactor_op* base)
0052   {
0053     BOOST_ASIO_ASSUME(base != 0);
0054     reactive_socket_recv_op_base* o(
0055         static_cast<reactive_socket_recv_op_base*>(base));
0056 
0057     typedef buffer_sequence_adapter<boost::asio::mutable_buffer,
0058         MutableBufferSequence> bufs_type;
0059 
0060     status result;
0061     if (bufs_type::is_single_buffer)
0062     {
0063       result = socket_ops::non_blocking_recv1(o->socket_,
0064           bufs_type::first(o->buffers_).data(),
0065           bufs_type::first(o->buffers_).size(), o->flags_,
0066           (o->state_ & socket_ops::stream_oriented) != 0,
0067           o->ec_, o->bytes_transferred_) ? done : not_done;
0068 
0069 #if defined(BOOST_ASIO_HAS_EPOLL)
0070       if (result == done)
0071         if ((o->state_ & socket_ops::stream_oriented) != 0)
0072           if (o->bytes_transferred_ <
0073               (((o->state_ & socket_ops::reset_edge_on_partial_read) != 0)
0074                 ? bufs_type::first(o->buffers_).size() : 1))
0075             result = done_and_exhausted;
0076 #endif // defined(BOOST_ASIO_HAS_EPOLL)
0077     }
0078     else
0079     {
0080       bufs_type bufs(o->buffers_);
0081       result = socket_ops::non_blocking_recv(o->socket_,
0082           bufs.buffers(), bufs.count(), o->flags_,
0083           (o->state_ & socket_ops::stream_oriented) != 0,
0084           o->ec_, o->bytes_transferred_) ? done : not_done;
0085 
0086 #if defined(BOOST_ASIO_HAS_EPOLL)
0087       if (result == done)
0088         if ((o->state_ & socket_ops::stream_oriented) != 0)
0089           if (o->bytes_transferred_ <
0090               (((o->state_ & socket_ops::reset_edge_on_partial_read) != 0)
0091                 ? bufs.total_size() : 1))
0092             result = done_and_exhausted;
0093 #endif // defined(BOOST_ASIO_HAS_EPOLL)
0094     }
0095 
0096 #if !defined(BOOST_ASIO_HAS_EPOLL)
0097     if (result == done)
0098       if ((o->state_ & socket_ops::stream_oriented) != 0)
0099         if (o->bytes_transferred_ == 0)
0100           result = done_and_exhausted;
0101 #endif // !defined(BOOST_ASIO_HAS_EPOLL)
0102 
0103     BOOST_ASIO_HANDLER_REACTOR_OPERATION((*o, "non_blocking_recv",
0104           o->ec_, o->bytes_transferred_));
0105 
0106     return result;
0107   }
0108 
0109 private:
0110   socket_type socket_;
0111   socket_ops::state_type state_;
0112   MutableBufferSequence buffers_;
0113   socket_base::message_flags flags_;
0114 };
0115 
0116 template <typename MutableBufferSequence, typename Handler, typename IoExecutor>
0117 class reactive_socket_recv_op :
0118   public reactive_socket_recv_op_base<MutableBufferSequence>
0119 {
0120 public:
0121   typedef Handler handler_type;
0122   typedef IoExecutor io_executor_type;
0123 
0124   BOOST_ASIO_DEFINE_HANDLER_PTR(reactive_socket_recv_op);
0125 
0126   reactive_socket_recv_op(const boost::system::error_code& success_ec,
0127       socket_type socket, socket_ops::state_type state,
0128       const MutableBufferSequence& buffers, socket_base::message_flags flags,
0129       Handler& handler, const IoExecutor& io_ex)
0130     : reactive_socket_recv_op_base<MutableBufferSequence>(success_ec, socket,
0131         state, buffers, flags, &reactive_socket_recv_op::do_complete),
0132       handler_(static_cast<Handler&&>(handler)),
0133       work_(handler_, io_ex)
0134   {
0135   }
0136 
0137   static void do_complete(void* owner, operation* base,
0138       const boost::system::error_code& /*ec*/,
0139       std::size_t /*bytes_transferred*/)
0140   {
0141     // Take ownership of the handler object.
0142     BOOST_ASIO_ASSUME(base != 0);
0143     reactive_socket_recv_op* o(static_cast<reactive_socket_recv_op*>(base));
0144     ptr p = { boost::asio::detail::addressof(o->handler_), o, o };
0145 
0146     BOOST_ASIO_HANDLER_COMPLETION((*o));
0147 
0148     // Take ownership of the operation's outstanding work.
0149     handler_work<Handler, IoExecutor> w(
0150         static_cast<handler_work<Handler, IoExecutor>&&>(
0151           o->work_));
0152 
0153     BOOST_ASIO_ERROR_LOCATION(o->ec_);
0154 
0155     // Make a copy of the handler so that the memory can be deallocated before
0156     // the upcall is made. Even if we're not about to make an upcall, a
0157     // sub-object of the handler may be the true owner of the memory associated
0158     // with the handler. Consequently, a local copy of the handler is required
0159     // to ensure that any owning sub-object remains valid until after we have
0160     // deallocated the memory here.
0161     detail::binder2<Handler, boost::system::error_code, std::size_t>
0162       handler(o->handler_, o->ec_, o->bytes_transferred_);
0163     p.h = boost::asio::detail::addressof(handler.handler_);
0164     p.reset();
0165 
0166     // Make the upcall if required.
0167     if (owner)
0168     {
0169       fenced_block b(fenced_block::half);
0170       BOOST_ASIO_HANDLER_INVOCATION_BEGIN((handler.arg1_, handler.arg2_));
0171       w.complete(handler, handler.handler_);
0172       BOOST_ASIO_HANDLER_INVOCATION_END;
0173     }
0174   }
0175 
0176   static void do_immediate(operation* base, bool, const void* io_ex)
0177   {
0178     // Take ownership of the handler object.
0179     BOOST_ASIO_ASSUME(base != 0);
0180     reactive_socket_recv_op* o(static_cast<reactive_socket_recv_op*>(base));
0181     ptr p = { boost::asio::detail::addressof(o->handler_), o, o };
0182 
0183     BOOST_ASIO_HANDLER_COMPLETION((*o));
0184 
0185     // Take ownership of the operation's outstanding work.
0186     immediate_handler_work<Handler, IoExecutor> w(
0187         static_cast<handler_work<Handler, IoExecutor>&&>(
0188           o->work_));
0189 
0190     BOOST_ASIO_ERROR_LOCATION(o->ec_);
0191 
0192     // Make a copy of the handler so that the memory can be deallocated before
0193     // the upcall is made. Even if we're not about to make an upcall, a
0194     // sub-object of the handler may be the true owner of the memory associated
0195     // with the handler. Consequently, a local copy of the handler is required
0196     // to ensure that any owning sub-object remains valid until after we have
0197     // deallocated the memory here.
0198     detail::binder2<Handler, boost::system::error_code, std::size_t>
0199       handler(o->handler_, o->ec_, o->bytes_transferred_);
0200     p.h = boost::asio::detail::addressof(handler.handler_);
0201     p.reset();
0202 
0203     BOOST_ASIO_HANDLER_INVOCATION_BEGIN((handler.arg1_, handler.arg2_));
0204     w.complete(handler, handler.handler_, io_ex);
0205     BOOST_ASIO_HANDLER_INVOCATION_END;
0206   }
0207 
0208 private:
0209   Handler handler_;
0210   handler_work<Handler, IoExecutor> work_;
0211 };
0212 
0213 } // namespace detail
0214 } // namespace asio
0215 } // namespace boost
0216 
0217 #include <boost/asio/detail/pop_options.hpp>
0218 
0219 #endif // BOOST_ASIO_DETAIL_REACTIVE_SOCKET_RECV_OP_HPP