io_uring_socket_recvfrom_op.hpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. //
  2. // detail/io_uring_socket_recvfrom_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_RECVFROM_OP_HPP
  11. #define BOOST_ASIO_DETAIL_IO_URING_SOCKET_RECVFROM_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 MutableBufferSequence, typename Endpoint>
  29. class io_uring_socket_recvfrom_op_base : public io_uring_operation
  30. {
  31. public:
  32. io_uring_socket_recvfrom_op_base(const boost::system::error_code& success_ec,
  33. socket_type socket, socket_ops::state_type state,
  34. const MutableBufferSequence& buffers, Endpoint& endpoint,
  35. socket_base::message_flags flags, func_type complete_func)
  36. : io_uring_operation(success_ec,
  37. &io_uring_socket_recvfrom_op_base::do_prepare,
  38. &io_uring_socket_recvfrom_op_base::do_perform, complete_func),
  39. socket_(socket),
  40. state_(state),
  41. buffers_(buffers),
  42. sender_endpoint_(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*>(sender_endpoint_.data()));
  51. msghdr_.msg_namelen = sender_endpoint_.capacity();
  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_recvfrom_op_base* o(
  57. static_cast<io_uring_socket_recvfrom_op_base*>(base));
  58. if ((o->state_ & socket_ops::internal_non_blocking) != 0)
  59. {
  60. bool except_op = (o->flags_ & socket_base::message_out_of_band) != 0;
  61. ::io_uring_prep_poll_add(sqe, o->socket_, except_op ? POLLPRI : POLLIN);
  62. }
  63. else
  64. {
  65. ::io_uring_prep_recvmsg(sqe, o->socket_, &o->msghdr_, o->flags_);
  66. }
  67. }
  68. static bool do_perform(io_uring_operation* base, bool after_completion)
  69. {
  70. BOOST_ASIO_ASSUME(base != 0);
  71. io_uring_socket_recvfrom_op_base* o(
  72. static_cast<io_uring_socket_recvfrom_op_base*>(base));
  73. if ((o->state_ & socket_ops::internal_non_blocking) != 0)
  74. {
  75. bool except_op = (o->flags_ & socket_base::message_out_of_band) != 0;
  76. if (after_completion || !except_op)
  77. {
  78. std::size_t addr_len = o->sender_endpoint_.capacity();
  79. bool result;
  80. if (o->bufs_.is_single_buffer)
  81. {
  82. result = socket_ops::non_blocking_recvfrom1(o->socket_,
  83. o->bufs_.first(o->buffers_).data(),
  84. o->bufs_.first(o->buffers_).size(), o->flags_,
  85. o->sender_endpoint_.data(), &addr_len,
  86. o->ec_, o->bytes_transferred_);
  87. }
  88. else
  89. {
  90. result = socket_ops::non_blocking_recvfrom(o->socket_,
  91. o->bufs_.buffers(), o->bufs_.count(), o->flags_,
  92. o->sender_endpoint_.data(), &addr_len,
  93. o->ec_, o->bytes_transferred_);
  94. }
  95. if (result && !o->ec_)
  96. o->sender_endpoint_.resize(addr_len);
  97. }
  98. }
  99. else if (after_completion && !o->ec_)
  100. o->sender_endpoint_.resize(o->msghdr_.msg_namelen);
  101. if (o->ec_ && o->ec_ == boost::asio::error::would_block)
  102. {
  103. o->state_ |= socket_ops::internal_non_blocking;
  104. return false;
  105. }
  106. return after_completion;
  107. }
  108. private:
  109. socket_type socket_;
  110. socket_ops::state_type state_;
  111. MutableBufferSequence buffers_;
  112. Endpoint& sender_endpoint_;
  113. socket_base::message_flags flags_;
  114. buffer_sequence_adapter<boost::asio::mutable_buffer,
  115. MutableBufferSequence> bufs_;
  116. msghdr msghdr_;
  117. };
  118. template <typename MutableBufferSequence, typename Endpoint,
  119. typename Handler, typename IoExecutor>
  120. class io_uring_socket_recvfrom_op
  121. : public io_uring_socket_recvfrom_op_base<MutableBufferSequence, Endpoint>
  122. {
  123. public:
  124. BOOST_ASIO_DEFINE_HANDLER_PTR(io_uring_socket_recvfrom_op);
  125. io_uring_socket_recvfrom_op(const boost::system::error_code& success_ec,
  126. int socket, socket_ops::state_type state,
  127. const MutableBufferSequence& buffers, Endpoint& endpoint,
  128. socket_base::message_flags flags,
  129. Handler& handler, const IoExecutor& io_ex)
  130. : io_uring_socket_recvfrom_op_base<MutableBufferSequence, Endpoint>(
  131. success_ec, socket, state, buffers, endpoint, flags,
  132. &io_uring_socket_recvfrom_op::do_complete),
  133. handler_(static_cast<Handler&&>(handler)),
  134. work_(handler_, io_ex)
  135. {
  136. }
  137. static void do_complete(void* owner, operation* base,
  138. const boost::system::error_code& /*ec*/,
  139. std::size_t /*bytes_transferred*/)
  140. {
  141. // Take ownership of the handler object.
  142. BOOST_ASIO_ASSUME(base != 0);
  143. io_uring_socket_recvfrom_op* o
  144. (static_cast<io_uring_socket_recvfrom_op*>(base));
  145. ptr p = { boost::asio::detail::addressof(o->handler_), o, o };
  146. BOOST_ASIO_HANDLER_COMPLETION((*o));
  147. // Take ownership of the operation's outstanding work.
  148. handler_work<Handler, IoExecutor> w(
  149. static_cast<handler_work<Handler, IoExecutor>&&>(
  150. o->work_));
  151. BOOST_ASIO_ERROR_LOCATION(o->ec_);
  152. // Make a copy of the handler so that the memory can be deallocated before
  153. // the upcall is made. Even if we're not about to make an upcall, a
  154. // sub-object of the handler may be the true owner of the memory associated
  155. // with the handler. Consequently, a local copy of the handler is required
  156. // to ensure that any owning sub-object remains valid until after we have
  157. // deallocated the memory here.
  158. detail::binder2<Handler, boost::system::error_code, std::size_t>
  159. handler(o->handler_, o->ec_, o->bytes_transferred_);
  160. p.h = boost::asio::detail::addressof(handler.handler_);
  161. p.reset();
  162. // Make the upcall if required.
  163. if (owner)
  164. {
  165. fenced_block b(fenced_block::half);
  166. BOOST_ASIO_HANDLER_INVOCATION_BEGIN((handler.arg1_, handler.arg2_));
  167. w.complete(handler, handler.handler_);
  168. BOOST_ASIO_HANDLER_INVOCATION_END;
  169. }
  170. }
  171. private:
  172. Handler handler_;
  173. handler_work<Handler, IoExecutor> work_;
  174. };
  175. } // namespace detail
  176. } // namespace asio
  177. } // namespace boost
  178. #include <boost/asio/detail/pop_options.hpp>
  179. #endif // defined(BOOST_ASIO_HAS_IO_URING)
  180. #endif // BOOST_ASIO_DETAIL_IO_URING_SOCKET_RECVFROM_OP_HPP