io_uring_socket_accept_op.hpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. //
  2. // detail/io_uring_socket_accept_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_ACCEPT_OP_HPP
  11. #define BOOST_ASIO_DETAIL_IO_URING_SOCKET_ACCEPT_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/fenced_block.hpp>
  19. #include <boost/asio/detail/handler_alloc_helpers.hpp>
  20. #include <boost/asio/detail/handler_work.hpp>
  21. #include <boost/asio/detail/io_uring_operation.hpp>
  22. #include <boost/asio/detail/memory.hpp>
  23. #include <boost/asio/detail/socket_holder.hpp>
  24. #include <boost/asio/detail/socket_ops.hpp>
  25. #include <boost/asio/detail/push_options.hpp>
  26. namespace boost {
  27. namespace asio {
  28. namespace detail {
  29. template <typename Socket, typename Protocol>
  30. class io_uring_socket_accept_op_base : public io_uring_operation
  31. {
  32. public:
  33. io_uring_socket_accept_op_base(const boost::system::error_code& success_ec,
  34. socket_type socket, socket_ops::state_type state, Socket& peer,
  35. const Protocol& protocol, typename Protocol::endpoint* peer_endpoint,
  36. func_type complete_func)
  37. : io_uring_operation(success_ec,
  38. &io_uring_socket_accept_op_base::do_prepare,
  39. &io_uring_socket_accept_op_base::do_perform, complete_func),
  40. socket_(socket),
  41. state_(state),
  42. peer_(peer),
  43. protocol_(protocol),
  44. peer_endpoint_(peer_endpoint),
  45. addrlen_(peer_endpoint ? peer_endpoint->capacity() : 0)
  46. {
  47. }
  48. static void do_prepare(io_uring_operation* base, ::io_uring_sqe* sqe)
  49. {
  50. BOOST_ASIO_ASSUME(base != 0);
  51. io_uring_socket_accept_op_base* o(
  52. static_cast<io_uring_socket_accept_op_base*>(base));
  53. if ((o->state_ & socket_ops::internal_non_blocking) != 0)
  54. {
  55. ::io_uring_prep_poll_add(sqe, o->socket_, POLLIN);
  56. }
  57. else
  58. {
  59. ::io_uring_prep_accept(sqe, o->socket_,
  60. o->peer_endpoint_ ? o->peer_endpoint_->data() : 0,
  61. o->peer_endpoint_ ? &o->addrlen_ : 0, 0);
  62. }
  63. }
  64. static bool do_perform(io_uring_operation* base, bool after_completion)
  65. {
  66. BOOST_ASIO_ASSUME(base != 0);
  67. io_uring_socket_accept_op_base* o(
  68. static_cast<io_uring_socket_accept_op_base*>(base));
  69. if ((o->state_ & socket_ops::internal_non_blocking) != 0)
  70. {
  71. socket_type new_socket = invalid_socket;
  72. std::size_t addrlen = static_cast<std::size_t>(o->addrlen_);
  73. bool result = socket_ops::non_blocking_accept(o->socket_,
  74. o->state_, o->peer_endpoint_ ? o->peer_endpoint_->data() : 0,
  75. o->peer_endpoint_ ? &addrlen : 0, o->ec_, new_socket);
  76. o->new_socket_.reset(new_socket);
  77. o->addrlen_ = static_cast<socklen_t>(addrlen);
  78. return result;
  79. }
  80. if (o->ec_ && o->ec_ == boost::asio::error::would_block)
  81. {
  82. o->state_ |= socket_ops::internal_non_blocking;
  83. return false;
  84. }
  85. if (after_completion && !o->ec_)
  86. o->new_socket_.reset(static_cast<int>(o->bytes_transferred_));
  87. return after_completion;
  88. }
  89. void do_assign()
  90. {
  91. if (new_socket_.get() != invalid_socket)
  92. {
  93. if (peer_endpoint_)
  94. peer_endpoint_->resize(addrlen_);
  95. peer_.assign(protocol_, new_socket_.get(), ec_);
  96. if (!ec_)
  97. new_socket_.release();
  98. }
  99. }
  100. private:
  101. socket_type socket_;
  102. socket_ops::state_type state_;
  103. socket_holder new_socket_;
  104. Socket& peer_;
  105. Protocol protocol_;
  106. typename Protocol::endpoint* peer_endpoint_;
  107. socklen_t addrlen_;
  108. };
  109. template <typename Socket, typename Protocol,
  110. typename Handler, typename IoExecutor>
  111. class io_uring_socket_accept_op :
  112. public io_uring_socket_accept_op_base<Socket, Protocol>
  113. {
  114. public:
  115. BOOST_ASIO_DEFINE_HANDLER_PTR(io_uring_socket_accept_op);
  116. io_uring_socket_accept_op(const boost::system::error_code& success_ec,
  117. socket_type socket, socket_ops::state_type state, Socket& peer,
  118. const Protocol& protocol, typename Protocol::endpoint* peer_endpoint,
  119. Handler& handler, const IoExecutor& io_ex)
  120. : io_uring_socket_accept_op_base<Socket, Protocol>(
  121. success_ec, socket, state, peer, protocol, peer_endpoint,
  122. &io_uring_socket_accept_op::do_complete),
  123. handler_(static_cast<Handler&&>(handler)),
  124. work_(handler_, io_ex)
  125. {
  126. }
  127. static void do_complete(void* owner, operation* base,
  128. const boost::system::error_code& /*ec*/,
  129. std::size_t /*bytes_transferred*/)
  130. {
  131. // Take ownership of the handler object.
  132. BOOST_ASIO_ASSUME(base != 0);
  133. io_uring_socket_accept_op* o(static_cast<io_uring_socket_accept_op*>(base));
  134. ptr p = { boost::asio::detail::addressof(o->handler_), o, o };
  135. // On success, assign new connection to peer socket object.
  136. if (owner)
  137. o->do_assign();
  138. BOOST_ASIO_HANDLER_COMPLETION((*o));
  139. // Take ownership of the operation's outstanding work.
  140. handler_work<Handler, IoExecutor> w(
  141. static_cast<handler_work<Handler, IoExecutor>&&>(
  142. o->work_));
  143. BOOST_ASIO_ERROR_LOCATION(o->ec_);
  144. // Make a copy of the handler so that the memory can be deallocated before
  145. // the upcall is made. Even if we're not about to make an upcall, a
  146. // sub-object of the handler may be the true owner of the memory associated
  147. // with the handler. Consequently, a local copy of the handler is required
  148. // to ensure that any owning sub-object remains valid until after we have
  149. // deallocated the memory here.
  150. detail::binder1<Handler, boost::system::error_code>
  151. handler(o->handler_, o->ec_);
  152. p.h = boost::asio::detail::addressof(handler.handler_);
  153. p.reset();
  154. // Make the upcall if required.
  155. if (owner)
  156. {
  157. fenced_block b(fenced_block::half);
  158. BOOST_ASIO_HANDLER_INVOCATION_BEGIN((handler.arg1_));
  159. w.complete(handler, handler.handler_);
  160. BOOST_ASIO_HANDLER_INVOCATION_END;
  161. }
  162. }
  163. private:
  164. Handler handler_;
  165. handler_work<Handler, IoExecutor> work_;
  166. };
  167. template <typename Protocol, typename PeerIoExecutor,
  168. typename Handler, typename IoExecutor>
  169. class io_uring_socket_move_accept_op :
  170. private Protocol::socket::template rebind_executor<PeerIoExecutor>::other,
  171. public io_uring_socket_accept_op_base<
  172. typename Protocol::socket::template rebind_executor<PeerIoExecutor>::other,
  173. Protocol>
  174. {
  175. public:
  176. BOOST_ASIO_DEFINE_HANDLER_PTR(io_uring_socket_move_accept_op);
  177. io_uring_socket_move_accept_op(const boost::system::error_code& success_ec,
  178. const PeerIoExecutor& peer_io_ex, socket_type socket,
  179. socket_ops::state_type state, const Protocol& protocol,
  180. typename Protocol::endpoint* peer_endpoint, Handler& handler,
  181. const IoExecutor& io_ex)
  182. : peer_socket_type(peer_io_ex),
  183. io_uring_socket_accept_op_base<peer_socket_type, Protocol>(
  184. success_ec, socket, state, *this, protocol, peer_endpoint,
  185. &io_uring_socket_move_accept_op::do_complete),
  186. handler_(static_cast<Handler&&>(handler)),
  187. work_(handler_, io_ex)
  188. {
  189. }
  190. static void do_complete(void* owner, operation* base,
  191. const boost::system::error_code& /*ec*/,
  192. std::size_t /*bytes_transferred*/)
  193. {
  194. // Take ownership of the handler object.
  195. BOOST_ASIO_ASSUME(base != 0);
  196. io_uring_socket_move_accept_op* o(
  197. static_cast<io_uring_socket_move_accept_op*>(base));
  198. ptr p = { boost::asio::detail::addressof(o->handler_), o, o };
  199. // On success, assign new connection to peer socket object.
  200. if (owner)
  201. o->do_assign();
  202. BOOST_ASIO_HANDLER_COMPLETION((*o));
  203. // Take ownership of the operation's outstanding work.
  204. handler_work<Handler, IoExecutor> w(
  205. static_cast<handler_work<Handler, IoExecutor>&&>(
  206. o->work_));
  207. BOOST_ASIO_ERROR_LOCATION(o->ec_);
  208. // Make a copy of the handler so that the memory can be deallocated before
  209. // the upcall is made. Even if we're not about to make an upcall, a
  210. // sub-object of the handler may be the true owner of the memory associated
  211. // with the handler. Consequently, a local copy of the handler is required
  212. // to ensure that any owning sub-object remains valid until after we have
  213. // deallocated the memory here.
  214. detail::move_binder2<Handler,
  215. boost::system::error_code, peer_socket_type>
  216. handler(0, static_cast<Handler&&>(o->handler_), o->ec_,
  217. static_cast<peer_socket_type&&>(*o));
  218. p.h = boost::asio::detail::addressof(handler.handler_);
  219. p.reset();
  220. // Make the upcall if required.
  221. if (owner)
  222. {
  223. fenced_block b(fenced_block::half);
  224. BOOST_ASIO_HANDLER_INVOCATION_BEGIN((handler.arg1_, "..."));
  225. w.complete(handler, handler.handler_);
  226. BOOST_ASIO_HANDLER_INVOCATION_END;
  227. }
  228. }
  229. private:
  230. typedef typename Protocol::socket::template
  231. rebind_executor<PeerIoExecutor>::other peer_socket_type;
  232. Handler handler_;
  233. handler_work<Handler, IoExecutor> work_;
  234. };
  235. } // namespace detail
  236. } // namespace asio
  237. } // namespace boost
  238. #include <boost/asio/detail/pop_options.hpp>
  239. #endif // defined(BOOST_ASIO_HAS_IO_URING)
  240. #endif // BOOST_ASIO_DETAIL_IO_URING_SOCKET_ACCEPT_OP_HPP