Back to home page

EIC code displayed by LXR

 
 

    


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

0001 //
0002 // detail/reactive_socket_recv_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_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     else
0070     {
0071       bufs_type bufs(o->buffers_);
0072       result = socket_ops::non_blocking_recv(o->socket_,
0073           bufs.buffers(), bufs.count(), o->flags_,
0074           (o->state_ & socket_ops::stream_oriented) != 0,
0075           o->ec_, o->bytes_transferred_) ? done : not_done;
0076     }
0077 
0078     if (result == done)
0079       if ((o->state_ & socket_ops::stream_oriented) != 0)
0080         if (o->bytes_transferred_ == 0)
0081           result = done_and_exhausted;
0082 
0083     BOOST_ASIO_HANDLER_REACTOR_OPERATION((*o, "non_blocking_recv",
0084           o->ec_, o->bytes_transferred_));
0085 
0086     return result;
0087   }
0088 
0089 private:
0090   socket_type socket_;
0091   socket_ops::state_type state_;
0092   MutableBufferSequence buffers_;
0093   socket_base::message_flags flags_;
0094 };
0095 
0096 template <typename MutableBufferSequence, typename Handler, typename IoExecutor>
0097 class reactive_socket_recv_op :
0098   public reactive_socket_recv_op_base<MutableBufferSequence>
0099 {
0100 public:
0101   typedef Handler handler_type;
0102   typedef IoExecutor io_executor_type;
0103 
0104   BOOST_ASIO_DEFINE_HANDLER_PTR(reactive_socket_recv_op);
0105 
0106   reactive_socket_recv_op(const boost::system::error_code& success_ec,
0107       socket_type socket, socket_ops::state_type state,
0108       const MutableBufferSequence& buffers, socket_base::message_flags flags,
0109       Handler& handler, const IoExecutor& io_ex)
0110     : reactive_socket_recv_op_base<MutableBufferSequence>(success_ec, socket,
0111         state, buffers, flags, &reactive_socket_recv_op::do_complete),
0112       handler_(static_cast<Handler&&>(handler)),
0113       work_(handler_, io_ex)
0114   {
0115   }
0116 
0117   static void do_complete(void* owner, operation* base,
0118       const boost::system::error_code& /*ec*/,
0119       std::size_t /*bytes_transferred*/)
0120   {
0121     // Take ownership of the handler object.
0122     BOOST_ASIO_ASSUME(base != 0);
0123     reactive_socket_recv_op* o(static_cast<reactive_socket_recv_op*>(base));
0124     ptr p = { boost::asio::detail::addressof(o->handler_), o, o };
0125 
0126     BOOST_ASIO_HANDLER_COMPLETION((*o));
0127 
0128     // Take ownership of the operation's outstanding work.
0129     handler_work<Handler, IoExecutor> w(
0130         static_cast<handler_work<Handler, IoExecutor>&&>(
0131           o->work_));
0132 
0133     BOOST_ASIO_ERROR_LOCATION(o->ec_);
0134 
0135     // Make a copy of the handler so that the memory can be deallocated before
0136     // the upcall is made. Even if we're not about to make an upcall, a
0137     // sub-object of the handler may be the true owner of the memory associated
0138     // with the handler. Consequently, a local copy of the handler is required
0139     // to ensure that any owning sub-object remains valid until after we have
0140     // deallocated the memory here.
0141     detail::binder2<Handler, boost::system::error_code, std::size_t>
0142       handler(o->handler_, o->ec_, o->bytes_transferred_);
0143     p.h = boost::asio::detail::addressof(handler.handler_);
0144     p.reset();
0145 
0146     // Make the upcall if required.
0147     if (owner)
0148     {
0149       fenced_block b(fenced_block::half);
0150       BOOST_ASIO_HANDLER_INVOCATION_BEGIN((handler.arg1_, handler.arg2_));
0151       w.complete(handler, handler.handler_);
0152       BOOST_ASIO_HANDLER_INVOCATION_END;
0153     }
0154   }
0155 
0156   static void do_immediate(operation* base, bool, const void* io_ex)
0157   {
0158     // Take ownership of the handler object.
0159     BOOST_ASIO_ASSUME(base != 0);
0160     reactive_socket_recv_op* o(static_cast<reactive_socket_recv_op*>(base));
0161     ptr p = { boost::asio::detail::addressof(o->handler_), o, o };
0162 
0163     BOOST_ASIO_HANDLER_COMPLETION((*o));
0164 
0165     // Take ownership of the operation's outstanding work.
0166     immediate_handler_work<Handler, IoExecutor> w(
0167         static_cast<handler_work<Handler, IoExecutor>&&>(
0168           o->work_));
0169 
0170     BOOST_ASIO_ERROR_LOCATION(o->ec_);
0171 
0172     // Make a copy of the handler so that the memory can be deallocated before
0173     // the upcall is made. Even if we're not about to make an upcall, a
0174     // sub-object of the handler may be the true owner of the memory associated
0175     // with the handler. Consequently, a local copy of the handler is required
0176     // to ensure that any owning sub-object remains valid until after we have
0177     // deallocated the memory here.
0178     detail::binder2<Handler, boost::system::error_code, std::size_t>
0179       handler(o->handler_, o->ec_, o->bytes_transferred_);
0180     p.h = boost::asio::detail::addressof(handler.handler_);
0181     p.reset();
0182 
0183     BOOST_ASIO_HANDLER_INVOCATION_BEGIN((handler.arg1_, handler.arg2_));
0184     w.complete(handler, handler.handler_, io_ex);
0185     BOOST_ASIO_HANDLER_INVOCATION_END;
0186   }
0187 
0188 private:
0189   Handler handler_;
0190   handler_work<Handler, IoExecutor> work_;
0191 };
0192 
0193 } // namespace detail
0194 } // namespace asio
0195 } // namespace boost
0196 
0197 #include <boost/asio/detail/pop_options.hpp>
0198 
0199 #endif // BOOST_ASIO_DETAIL_REACTIVE_SOCKET_RECV_OP_HPP