io_uring_socket_accept_op.hpp 8.8 KB

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