io_uring_socket_sendto_op.hpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. //
  2. // detail/io_uring_socket_sendto_op.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2024 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  6. //
  7. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  8. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. //
  10. #ifndef BOOST_ASIO_DETAIL_IO_URING_SOCKET_SENDTO_OP_HPP
  11. #define BOOST_ASIO_DETAIL_IO_URING_SOCKET_SENDTO_OP_HPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include <boost/asio/detail/config.hpp>
  16. #if defined(BOOST_ASIO_HAS_IO_URING)
  17. #include <boost/asio/detail/bind_handler.hpp>
  18. #include <boost/asio/detail/buffer_sequence_adapter.hpp>
  19. #include <boost/asio/detail/socket_ops.hpp>
  20. #include <boost/asio/detail/fenced_block.hpp>
  21. #include <boost/asio/detail/handler_work.hpp>
  22. #include <boost/asio/detail/io_uring_operation.hpp>
  23. #include <boost/asio/detail/memory.hpp>
  24. #include <boost/asio/detail/push_options.hpp>
  25. namespace boost {
  26. namespace asio {
  27. namespace detail {
  28. template <typename ConstBufferSequence, typename Endpoint>
  29. class io_uring_socket_sendto_op_base : public io_uring_operation
  30. {
  31. public:
  32. io_uring_socket_sendto_op_base(const boost::system::error_code& success_ec,
  33. socket_type socket, socket_ops::state_type state,
  34. const ConstBufferSequence& buffers, const Endpoint& endpoint,
  35. socket_base::message_flags flags, func_type complete_func)
  36. : io_uring_operation(success_ec,
  37. &io_uring_socket_sendto_op_base::do_prepare,
  38. &io_uring_socket_sendto_op_base::do_perform, complete_func),
  39. socket_(socket),
  40. state_(state),
  41. buffers_(buffers),
  42. destination_(endpoint),
  43. flags_(flags),
  44. bufs_(buffers),
  45. msghdr_()
  46. {
  47. msghdr_.msg_iov = bufs_.buffers();
  48. msghdr_.msg_iovlen = static_cast<int>(bufs_.count());
  49. msghdr_.msg_name = static_cast<sockaddr*>(
  50. static_cast<void*>(destination_.data()));
  51. msghdr_.msg_namelen = destination_.size();
  52. }
  53. static void do_prepare(io_uring_operation* base, ::io_uring_sqe* sqe)
  54. {
  55. BOOST_ASIO_ASSUME(base != 0);
  56. io_uring_socket_sendto_op_base* o(
  57. static_cast<io_uring_socket_sendto_op_base*>(base));
  58. if ((o->state_ & socket_ops::internal_non_blocking) != 0)
  59. {
  60. ::io_uring_prep_poll_add(sqe, o->socket_, POLLOUT);
  61. }
  62. else
  63. {
  64. ::io_uring_prep_sendmsg(sqe, o->socket_, &o->msghdr_, o->flags_);
  65. }
  66. }
  67. static bool do_perform(io_uring_operation* base, bool after_completion)
  68. {
  69. BOOST_ASIO_ASSUME(base != 0);
  70. io_uring_socket_sendto_op_base* o(
  71. static_cast<io_uring_socket_sendto_op_base*>(base));
  72. if ((o->state_ & socket_ops::internal_non_blocking) != 0)
  73. {
  74. if (o->bufs_.is_single_buffer)
  75. {
  76. return socket_ops::non_blocking_sendto1(o->socket_,
  77. o->bufs_.first(o->buffers_).data(),
  78. o->bufs_.first(o->buffers_).size(), o->flags_,
  79. o->destination_.data(), o->destination_.size(),
  80. o->ec_, o->bytes_transferred_);
  81. }
  82. else
  83. {
  84. return socket_ops::non_blocking_sendto(o->socket_,
  85. o->bufs_.buffers(), o->bufs_.count(), o->flags_,
  86. o->destination_.data(), o->destination_.size(),
  87. o->ec_, o->bytes_transferred_);
  88. }
  89. }
  90. if (o->ec_ && o->ec_ == boost::asio::error::would_block)
  91. {
  92. o->state_ |= socket_ops::internal_non_blocking;
  93. return false;
  94. }
  95. return after_completion;
  96. }
  97. private:
  98. socket_type socket_;
  99. socket_ops::state_type state_;
  100. ConstBufferSequence buffers_;
  101. Endpoint destination_;
  102. socket_base::message_flags flags_;
  103. buffer_sequence_adapter<boost::asio::const_buffer, ConstBufferSequence> bufs_;
  104. msghdr msghdr_;
  105. };
  106. template <typename ConstBufferSequence, typename Endpoint,
  107. typename Handler, typename IoExecutor>
  108. class io_uring_socket_sendto_op
  109. : public io_uring_socket_sendto_op_base<ConstBufferSequence, Endpoint>
  110. {
  111. public:
  112. BOOST_ASIO_DEFINE_HANDLER_PTR(io_uring_socket_sendto_op);
  113. io_uring_socket_sendto_op(const boost::system::error_code& success_ec,
  114. int socket, socket_ops::state_type state,
  115. const ConstBufferSequence& buffers, const Endpoint& endpoint,
  116. socket_base::message_flags flags,
  117. Handler& handler, const IoExecutor& io_ex)
  118. : io_uring_socket_sendto_op_base<ConstBufferSequence, Endpoint>(
  119. success_ec, socket, state, buffers, endpoint, flags,
  120. &io_uring_socket_sendto_op::do_complete),
  121. handler_(static_cast<Handler&&>(handler)),
  122. work_(handler_, io_ex)
  123. {
  124. }
  125. static void do_complete(void* owner, operation* base,
  126. const boost::system::error_code& /*ec*/,
  127. std::size_t /*bytes_transferred*/)
  128. {
  129. // Take ownership of the handler object.
  130. BOOST_ASIO_ASSUME(base != 0);
  131. io_uring_socket_sendto_op* o
  132. (static_cast<io_uring_socket_sendto_op*>(base));
  133. ptr p = { boost::asio::detail::addressof(o->handler_), o, o };
  134. BOOST_ASIO_HANDLER_COMPLETION((*o));
  135. // Take ownership of the operation's outstanding work.
  136. handler_work<Handler, IoExecutor> w(
  137. static_cast<handler_work<Handler, IoExecutor>&&>(
  138. o->work_));
  139. BOOST_ASIO_ERROR_LOCATION(o->ec_);
  140. // Make a copy of the handler so that the memory can be deallocated before
  141. // the upcall is made. Even if we're not about to make an upcall, a
  142. // sub-object of the handler may be the true owner of the memory associated
  143. // with the handler. Consequently, a local copy of the handler is required
  144. // to ensure that any owning sub-object remains valid until after we have
  145. // deallocated the memory here.
  146. detail::binder2<Handler, boost::system::error_code, std::size_t>
  147. handler(o->handler_, o->ec_, o->bytes_transferred_);
  148. p.h = boost::asio::detail::addressof(handler.handler_);
  149. p.reset();
  150. // Make the upcall if required.
  151. if (owner)
  152. {
  153. fenced_block b(fenced_block::half);
  154. BOOST_ASIO_HANDLER_INVOCATION_BEGIN((handler.arg1_, handler.arg2_));
  155. w.complete(handler, handler.handler_);
  156. BOOST_ASIO_HANDLER_INVOCATION_END;
  157. }
  158. }
  159. private:
  160. Handler handler_;
  161. handler_work<Handler, IoExecutor> work_;
  162. };
  163. } // namespace detail
  164. } // namespace asio
  165. } // namespace boost
  166. #include <boost/asio/detail/pop_options.hpp>
  167. #endif // defined(BOOST_ASIO_HAS_IO_URING)
  168. #endif // BOOST_ASIO_DETAIL_IO_URING_SOCKET_SENDTO_OP_HPP