io_uring_socket_accept_op.hpp 8.9 KB

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