Back to home page

EIC code displayed by LXR

 
 

    


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

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