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