Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-12-16 09:43:07

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