Back to home page

EIC code displayed by LXR

 
 

    


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

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