Back to home page

EIC code displayed by LXR

 
 

    


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

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