io_uring_socket_service.hpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629
  1. //
  2. // detail/io_uring_socket_service.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_SERVICE_HPP
  11. #define ASIO_DETAIL_IO_URING_SOCKET_SERVICE_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/buffer.hpp"
  18. #include "asio/error.hpp"
  19. #include "asio/execution_context.hpp"
  20. #include "asio/socket_base.hpp"
  21. #include "asio/detail/buffer_sequence_adapter.hpp"
  22. #include "asio/detail/memory.hpp"
  23. #include "asio/detail/noncopyable.hpp"
  24. #include "asio/detail/io_uring_null_buffers_op.hpp"
  25. #include "asio/detail/io_uring_service.hpp"
  26. #include "asio/detail/io_uring_socket_accept_op.hpp"
  27. #include "asio/detail/io_uring_socket_connect_op.hpp"
  28. #include "asio/detail/io_uring_socket_recvfrom_op.hpp"
  29. #include "asio/detail/io_uring_socket_sendto_op.hpp"
  30. #include "asio/detail/io_uring_socket_service_base.hpp"
  31. #include "asio/detail/socket_holder.hpp"
  32. #include "asio/detail/socket_ops.hpp"
  33. #include "asio/detail/socket_types.hpp"
  34. #include "asio/detail/push_options.hpp"
  35. namespace asio {
  36. namespace detail {
  37. template <typename Protocol>
  38. class io_uring_socket_service :
  39. public execution_context_service_base<io_uring_socket_service<Protocol>>,
  40. public io_uring_socket_service_base
  41. {
  42. public:
  43. // The protocol type.
  44. typedef Protocol protocol_type;
  45. // The endpoint type.
  46. typedef typename Protocol::endpoint endpoint_type;
  47. // The native type of a socket.
  48. typedef socket_type native_handle_type;
  49. // The implementation type of the socket.
  50. struct implementation_type :
  51. io_uring_socket_service_base::base_implementation_type
  52. {
  53. // Default constructor.
  54. implementation_type()
  55. : protocol_(endpoint_type().protocol())
  56. {
  57. }
  58. // The protocol associated with the socket.
  59. protocol_type protocol_;
  60. };
  61. // Constructor.
  62. io_uring_socket_service(execution_context& context)
  63. : execution_context_service_base<
  64. io_uring_socket_service<Protocol>>(context),
  65. io_uring_socket_service_base(context)
  66. {
  67. }
  68. // Destroy all user-defined handler objects owned by the service.
  69. void shutdown()
  70. {
  71. this->base_shutdown();
  72. }
  73. // Move-construct a new socket implementation.
  74. void move_construct(implementation_type& impl,
  75. implementation_type& other_impl) noexcept
  76. {
  77. this->base_move_construct(impl, other_impl);
  78. impl.protocol_ = other_impl.protocol_;
  79. other_impl.protocol_ = endpoint_type().protocol();
  80. }
  81. // Move-assign from another socket implementation.
  82. void move_assign(implementation_type& impl,
  83. io_uring_socket_service_base& other_service,
  84. implementation_type& other_impl)
  85. {
  86. this->base_move_assign(impl, other_service, other_impl);
  87. impl.protocol_ = other_impl.protocol_;
  88. other_impl.protocol_ = endpoint_type().protocol();
  89. }
  90. // Move-construct a new socket implementation from another protocol type.
  91. template <typename Protocol1>
  92. void converting_move_construct(implementation_type& impl,
  93. io_uring_socket_service<Protocol1>&,
  94. typename io_uring_socket_service<
  95. Protocol1>::implementation_type& other_impl)
  96. {
  97. this->base_move_construct(impl, other_impl);
  98. impl.protocol_ = protocol_type(other_impl.protocol_);
  99. other_impl.protocol_ = typename Protocol1::endpoint().protocol();
  100. }
  101. // Open a new socket implementation.
  102. asio::error_code open(implementation_type& impl,
  103. const protocol_type& protocol, asio::error_code& ec)
  104. {
  105. if (!do_open(impl, protocol.family(),
  106. protocol.type(), protocol.protocol(), ec))
  107. impl.protocol_ = protocol;
  108. ASIO_ERROR_LOCATION(ec);
  109. return ec;
  110. }
  111. // Assign a native socket to a socket implementation.
  112. asio::error_code assign(implementation_type& impl,
  113. const protocol_type& protocol, const native_handle_type& native_socket,
  114. asio::error_code& ec)
  115. {
  116. if (!do_assign(impl, protocol.type(), native_socket, ec))
  117. impl.protocol_ = protocol;
  118. ASIO_ERROR_LOCATION(ec);
  119. return ec;
  120. }
  121. // Get the native socket representation.
  122. native_handle_type native_handle(implementation_type& impl)
  123. {
  124. return impl.socket_;
  125. }
  126. // Bind the socket to the specified local endpoint.
  127. asio::error_code bind(implementation_type& impl,
  128. const endpoint_type& endpoint, asio::error_code& ec)
  129. {
  130. socket_ops::bind(impl.socket_, endpoint.data(), endpoint.size(), ec);
  131. ASIO_ERROR_LOCATION(ec);
  132. return ec;
  133. }
  134. // Set a socket option.
  135. template <typename Option>
  136. asio::error_code set_option(implementation_type& impl,
  137. const Option& option, asio::error_code& ec)
  138. {
  139. socket_ops::setsockopt(impl.socket_, impl.state_,
  140. option.level(impl.protocol_), option.name(impl.protocol_),
  141. option.data(impl.protocol_), option.size(impl.protocol_), ec);
  142. ASIO_ERROR_LOCATION(ec);
  143. return ec;
  144. }
  145. // Set a socket option.
  146. template <typename Option>
  147. asio::error_code get_option(const implementation_type& impl,
  148. Option& option, asio::error_code& ec) const
  149. {
  150. std::size_t size = option.size(impl.protocol_);
  151. socket_ops::getsockopt(impl.socket_, impl.state_,
  152. option.level(impl.protocol_), option.name(impl.protocol_),
  153. option.data(impl.protocol_), &size, ec);
  154. if (!ec)
  155. option.resize(impl.protocol_, size);
  156. ASIO_ERROR_LOCATION(ec);
  157. return ec;
  158. }
  159. // Get the local endpoint.
  160. endpoint_type local_endpoint(const implementation_type& impl,
  161. asio::error_code& ec) const
  162. {
  163. endpoint_type endpoint;
  164. std::size_t addr_len = endpoint.capacity();
  165. if (socket_ops::getsockname(impl.socket_, endpoint.data(), &addr_len, ec))
  166. {
  167. ASIO_ERROR_LOCATION(ec);
  168. return endpoint_type();
  169. }
  170. endpoint.resize(addr_len);
  171. return endpoint;
  172. }
  173. // Get the remote endpoint.
  174. endpoint_type remote_endpoint(const implementation_type& impl,
  175. asio::error_code& ec) const
  176. {
  177. endpoint_type endpoint;
  178. std::size_t addr_len = endpoint.capacity();
  179. if (socket_ops::getpeername(impl.socket_,
  180. endpoint.data(), &addr_len, false, ec))
  181. {
  182. ASIO_ERROR_LOCATION(ec);
  183. return endpoint_type();
  184. }
  185. endpoint.resize(addr_len);
  186. return endpoint;
  187. }
  188. // Disable sends or receives on the socket.
  189. asio::error_code shutdown(base_implementation_type& impl,
  190. socket_base::shutdown_type what, asio::error_code& ec)
  191. {
  192. socket_ops::shutdown(impl.socket_, what, ec);
  193. ASIO_ERROR_LOCATION(ec);
  194. return ec;
  195. }
  196. // Send a datagram to the specified endpoint. Returns the number of bytes
  197. // sent.
  198. template <typename ConstBufferSequence>
  199. size_t send_to(implementation_type& impl, const ConstBufferSequence& buffers,
  200. const endpoint_type& destination, socket_base::message_flags flags,
  201. asio::error_code& ec)
  202. {
  203. typedef buffer_sequence_adapter<asio::const_buffer,
  204. ConstBufferSequence> bufs_type;
  205. size_t n;
  206. if (bufs_type::is_single_buffer)
  207. {
  208. n = socket_ops::sync_sendto1(impl.socket_, impl.state_,
  209. bufs_type::first(buffers).data(),
  210. bufs_type::first(buffers).size(), flags,
  211. destination.data(), destination.size(), ec);
  212. }
  213. else
  214. {
  215. bufs_type bufs(buffers);
  216. n = socket_ops::sync_sendto(impl.socket_, impl.state_,
  217. bufs.buffers(), bufs.count(), flags,
  218. destination.data(), destination.size(), ec);
  219. }
  220. ASIO_ERROR_LOCATION(ec);
  221. return n;
  222. }
  223. // Wait until data can be sent without blocking.
  224. size_t send_to(implementation_type& impl, const null_buffers&,
  225. const endpoint_type&, socket_base::message_flags,
  226. asio::error_code& ec)
  227. {
  228. // Wait for socket to become ready.
  229. socket_ops::poll_write(impl.socket_, impl.state_, -1, ec);
  230. ASIO_ERROR_LOCATION(ec);
  231. return 0;
  232. }
  233. // Start an asynchronous send. The data being sent must be valid for the
  234. // lifetime of the asynchronous operation.
  235. template <typename ConstBufferSequence, typename Handler, typename IoExecutor>
  236. void async_send_to(implementation_type& impl,
  237. const ConstBufferSequence& buffers,
  238. const endpoint_type& destination, socket_base::message_flags flags,
  239. Handler& handler, const IoExecutor& io_ex)
  240. {
  241. bool is_continuation =
  242. asio_handler_cont_helpers::is_continuation(handler);
  243. associated_cancellation_slot_t<Handler> slot
  244. = asio::get_associated_cancellation_slot(handler);
  245. // Allocate and construct an operation to wrap the handler.
  246. typedef io_uring_socket_sendto_op<ConstBufferSequence,
  247. endpoint_type, Handler, IoExecutor> op;
  248. typename op::ptr p = { asio::detail::addressof(handler),
  249. op::ptr::allocate(handler), 0 };
  250. p.p = new (p.v) op(success_ec_, impl.socket_, impl.state_,
  251. buffers, destination, flags, handler, io_ex);
  252. // Optionally register for per-operation cancellation.
  253. if (slot.is_connected())
  254. {
  255. p.p->cancellation_key_ =
  256. &slot.template emplace<io_uring_op_cancellation>(&io_uring_service_,
  257. &impl.io_object_data_, io_uring_service::write_op);
  258. }
  259. ASIO_HANDLER_CREATION((io_uring_service_.context(), *p.p,
  260. "socket", &impl, impl.socket_, "async_send_to"));
  261. start_op(impl, io_uring_service::write_op, p.p, is_continuation, false);
  262. p.v = p.p = 0;
  263. }
  264. // Start an asynchronous wait until data can be sent without blocking.
  265. template <typename Handler, typename IoExecutor>
  266. void async_send_to(implementation_type& impl, const null_buffers&,
  267. const endpoint_type&, socket_base::message_flags,
  268. Handler& handler, const IoExecutor& io_ex)
  269. {
  270. bool is_continuation =
  271. asio_handler_cont_helpers::is_continuation(handler);
  272. associated_cancellation_slot_t<Handler> slot
  273. = asio::get_associated_cancellation_slot(handler);
  274. // Allocate and construct an operation to wrap the handler.
  275. typedef io_uring_null_buffers_op<Handler, IoExecutor> op;
  276. typename op::ptr p = { asio::detail::addressof(handler),
  277. op::ptr::allocate(handler), 0 };
  278. p.p = new (p.v) op(success_ec_, impl.socket_, POLLOUT, handler, io_ex);
  279. // Optionally register for per-operation cancellation.
  280. if (slot.is_connected())
  281. {
  282. p.p->cancellation_key_ =
  283. &slot.template emplace<io_uring_op_cancellation>(&io_uring_service_,
  284. &impl.io_object_data_, io_uring_service::write_op);
  285. }
  286. ASIO_HANDLER_CREATION((io_uring_service_.context(), *p.p, "socket",
  287. &impl, impl.socket_, "async_send_to(null_buffers)"));
  288. start_op(impl, io_uring_service::write_op, p.p, is_continuation, false);
  289. p.v = p.p = 0;
  290. }
  291. // Receive a datagram with the endpoint of the sender. Returns the number of
  292. // bytes received.
  293. template <typename MutableBufferSequence>
  294. size_t receive_from(implementation_type& impl,
  295. const MutableBufferSequence& buffers,
  296. endpoint_type& sender_endpoint, socket_base::message_flags flags,
  297. asio::error_code& ec)
  298. {
  299. typedef buffer_sequence_adapter<asio::mutable_buffer,
  300. MutableBufferSequence> bufs_type;
  301. std::size_t addr_len = sender_endpoint.capacity();
  302. std::size_t n;
  303. if (bufs_type::is_single_buffer)
  304. {
  305. n = socket_ops::sync_recvfrom1(impl.socket_, impl.state_,
  306. bufs_type::first(buffers).data(), bufs_type::first(buffers).size(),
  307. flags, sender_endpoint.data(), &addr_len, ec);
  308. }
  309. else
  310. {
  311. bufs_type bufs(buffers);
  312. n = socket_ops::sync_recvfrom(impl.socket_, impl.state_, bufs.buffers(),
  313. bufs.count(), flags, sender_endpoint.data(), &addr_len, ec);
  314. }
  315. if (!ec)
  316. sender_endpoint.resize(addr_len);
  317. ASIO_ERROR_LOCATION(ec);
  318. return n;
  319. }
  320. // Wait until data can be received without blocking.
  321. size_t receive_from(implementation_type& impl, const null_buffers&,
  322. endpoint_type& sender_endpoint, socket_base::message_flags,
  323. asio::error_code& ec)
  324. {
  325. // Wait for socket to become ready.
  326. socket_ops::poll_read(impl.socket_, impl.state_, -1, ec);
  327. // Reset endpoint since it can be given no sensible value at this time.
  328. sender_endpoint = endpoint_type();
  329. ASIO_ERROR_LOCATION(ec);
  330. return 0;
  331. }
  332. // Start an asynchronous receive. The buffer for the data being received and
  333. // the sender_endpoint object must both be valid for the lifetime of the
  334. // asynchronous operation.
  335. template <typename MutableBufferSequence,
  336. typename Handler, typename IoExecutor>
  337. void async_receive_from(implementation_type& impl,
  338. const MutableBufferSequence& buffers, endpoint_type& sender_endpoint,
  339. socket_base::message_flags flags, Handler& handler,
  340. const IoExecutor& io_ex)
  341. {
  342. bool is_continuation =
  343. asio_handler_cont_helpers::is_continuation(handler);
  344. int op_type = (flags & socket_base::message_out_of_band)
  345. ? io_uring_service::except_op : io_uring_service::read_op;
  346. associated_cancellation_slot_t<Handler> slot
  347. = asio::get_associated_cancellation_slot(handler);
  348. // Allocate and construct an operation to wrap the handler.
  349. typedef io_uring_socket_recvfrom_op<MutableBufferSequence,
  350. endpoint_type, Handler, IoExecutor> op;
  351. typename op::ptr p = { asio::detail::addressof(handler),
  352. op::ptr::allocate(handler), 0 };
  353. p.p = new (p.v) op(success_ec_, impl.socket_, impl.state_,
  354. buffers, sender_endpoint, flags, handler, io_ex);
  355. // Optionally register for per-operation cancellation.
  356. if (slot.is_connected())
  357. {
  358. p.p->cancellation_key_ =
  359. &slot.template emplace<io_uring_op_cancellation>(
  360. &io_uring_service_, &impl.io_object_data_, op_type);
  361. }
  362. ASIO_HANDLER_CREATION((io_uring_service_.context(), *p.p,
  363. "socket", &impl, impl.socket_, "async_receive_from"));
  364. start_op(impl, op_type, p.p, is_continuation, false);
  365. p.v = p.p = 0;
  366. }
  367. // Wait until data can be received without blocking.
  368. template <typename Handler, typename IoExecutor>
  369. void async_receive_from(implementation_type& impl, const null_buffers&,
  370. endpoint_type& sender_endpoint, socket_base::message_flags flags,
  371. Handler& handler, const IoExecutor& io_ex)
  372. {
  373. bool is_continuation =
  374. asio_handler_cont_helpers::is_continuation(handler);
  375. int op_type;
  376. int poll_flags;
  377. if ((flags & socket_base::message_out_of_band) != 0)
  378. {
  379. op_type = io_uring_service::except_op;
  380. poll_flags = POLLPRI;
  381. }
  382. else
  383. {
  384. op_type = io_uring_service::read_op;
  385. poll_flags = POLLIN;
  386. }
  387. associated_cancellation_slot_t<Handler> slot
  388. = asio::get_associated_cancellation_slot(handler);
  389. // Allocate and construct an operation to wrap the handler.
  390. typedef io_uring_null_buffers_op<Handler, IoExecutor> op;
  391. typename op::ptr p = { asio::detail::addressof(handler),
  392. op::ptr::allocate(handler), 0 };
  393. p.p = new (p.v) op(success_ec_, impl.socket_, poll_flags, handler, io_ex);
  394. // Optionally register for per-operation cancellation.
  395. if (slot.is_connected())
  396. {
  397. p.p->cancellation_key_ =
  398. &slot.template emplace<io_uring_op_cancellation>(
  399. &io_uring_service_, &impl.io_object_data_, op_type);
  400. }
  401. ASIO_HANDLER_CREATION((io_uring_service_.context(), *p.p, "socket",
  402. &impl, impl.socket_, "async_receive_from(null_buffers)"));
  403. // Reset endpoint since it can be given no sensible value at this time.
  404. sender_endpoint = endpoint_type();
  405. start_op(impl, op_type, p.p, is_continuation, false);
  406. p.v = p.p = 0;
  407. }
  408. // Accept a new connection.
  409. template <typename Socket>
  410. asio::error_code accept(implementation_type& impl,
  411. Socket& peer, endpoint_type* peer_endpoint, asio::error_code& ec)
  412. {
  413. // We cannot accept a socket that is already open.
  414. if (peer.is_open())
  415. {
  416. ec = asio::error::already_open;
  417. ASIO_ERROR_LOCATION(ec);
  418. return ec;
  419. }
  420. std::size_t addr_len = peer_endpoint ? peer_endpoint->capacity() : 0;
  421. socket_holder new_socket(socket_ops::sync_accept(impl.socket_,
  422. impl.state_, peer_endpoint ? peer_endpoint->data() : 0,
  423. peer_endpoint ? &addr_len : 0, ec));
  424. // On success, assign new connection to peer socket object.
  425. if (new_socket.get() != invalid_socket)
  426. {
  427. if (peer_endpoint)
  428. peer_endpoint->resize(addr_len);
  429. peer.assign(impl.protocol_, new_socket.get(), ec);
  430. if (!ec)
  431. new_socket.release();
  432. }
  433. ASIO_ERROR_LOCATION(ec);
  434. return ec;
  435. }
  436. // Start an asynchronous accept. The peer and peer_endpoint objects must be
  437. // valid until the accept's handler is invoked.
  438. template <typename Socket, typename Handler, typename IoExecutor>
  439. void async_accept(implementation_type& impl, Socket& peer,
  440. endpoint_type* peer_endpoint, Handler& handler, const IoExecutor& io_ex)
  441. {
  442. bool is_continuation =
  443. asio_handler_cont_helpers::is_continuation(handler);
  444. associated_cancellation_slot_t<Handler> slot
  445. = asio::get_associated_cancellation_slot(handler);
  446. // Allocate and construct an operation to wrap the handler.
  447. typedef io_uring_socket_accept_op<Socket, Protocol, Handler, IoExecutor> op;
  448. typename op::ptr p = { asio::detail::addressof(handler),
  449. op::ptr::allocate(handler), 0 };
  450. p.p = new (p.v) op(success_ec_, impl.socket_, impl.state_,
  451. peer, impl.protocol_, peer_endpoint, handler, io_ex);
  452. // Optionally register for per-operation cancellation.
  453. if (slot.is_connected() && !peer.is_open())
  454. {
  455. p.p->cancellation_key_ =
  456. &slot.template emplace<io_uring_op_cancellation>(&io_uring_service_,
  457. &impl.io_object_data_, io_uring_service::read_op);
  458. }
  459. ASIO_HANDLER_CREATION((io_uring_service_.context(), *p.p,
  460. "socket", &impl, impl.socket_, "async_accept"));
  461. start_accept_op(impl, p.p, is_continuation, peer.is_open());
  462. p.v = p.p = 0;
  463. }
  464. // Start an asynchronous accept. The peer_endpoint object must be valid until
  465. // the accept's handler is invoked.
  466. template <typename PeerIoExecutor, typename Handler, typename IoExecutor>
  467. void async_move_accept(implementation_type& impl,
  468. const PeerIoExecutor& peer_io_ex, endpoint_type* peer_endpoint,
  469. Handler& handler, const IoExecutor& io_ex)
  470. {
  471. bool is_continuation =
  472. asio_handler_cont_helpers::is_continuation(handler);
  473. associated_cancellation_slot_t<Handler> slot
  474. = asio::get_associated_cancellation_slot(handler);
  475. // Allocate and construct an operation to wrap the handler.
  476. typedef io_uring_socket_move_accept_op<Protocol,
  477. PeerIoExecutor, Handler, IoExecutor> op;
  478. typename op::ptr p = { asio::detail::addressof(handler),
  479. op::ptr::allocate(handler), 0 };
  480. p.p = new (p.v) op(success_ec_, peer_io_ex, impl.socket_,
  481. impl.state_, impl.protocol_, peer_endpoint, handler, io_ex);
  482. // Optionally register for per-operation cancellation.
  483. if (slot.is_connected())
  484. {
  485. p.p->cancellation_key_ =
  486. &slot.template emplace<io_uring_op_cancellation>(&io_uring_service_,
  487. &impl.io_object_data_, io_uring_service::read_op);
  488. }
  489. ASIO_HANDLER_CREATION((io_uring_service_.context(), *p.p,
  490. "socket", &impl, impl.socket_, "async_accept"));
  491. start_accept_op(impl, p.p, is_continuation, false);
  492. p.v = p.p = 0;
  493. }
  494. // Connect the socket to the specified endpoint.
  495. asio::error_code connect(implementation_type& impl,
  496. const endpoint_type& peer_endpoint, asio::error_code& ec)
  497. {
  498. socket_ops::sync_connect(impl.socket_,
  499. peer_endpoint.data(), peer_endpoint.size(), ec);
  500. return ec;
  501. }
  502. // Start an asynchronous connect.
  503. template <typename Handler, typename IoExecutor>
  504. void async_connect(implementation_type& impl,
  505. const endpoint_type& peer_endpoint,
  506. Handler& handler, const IoExecutor& io_ex)
  507. {
  508. bool is_continuation =
  509. asio_handler_cont_helpers::is_continuation(handler);
  510. associated_cancellation_slot_t<Handler> slot
  511. = asio::get_associated_cancellation_slot(handler);
  512. // Allocate and construct an operation to wrap the handler.
  513. typedef io_uring_socket_connect_op<Protocol, Handler, IoExecutor> op;
  514. typename op::ptr p = { asio::detail::addressof(handler),
  515. op::ptr::allocate(handler), 0 };
  516. p.p = new (p.v) op(success_ec_, impl.socket_,
  517. peer_endpoint, handler, io_ex);
  518. // Optionally register for per-operation cancellation.
  519. if (slot.is_connected())
  520. {
  521. p.p->cancellation_key_ =
  522. &slot.template emplace<io_uring_op_cancellation>(&io_uring_service_,
  523. &impl.io_object_data_, io_uring_service::write_op);
  524. }
  525. ASIO_HANDLER_CREATION((io_uring_service_.context(), *p.p,
  526. "socket", &impl, impl.socket_, "async_connect"));
  527. start_op(impl, io_uring_service::write_op, p.p, is_continuation, false);
  528. p.v = p.p = 0;
  529. }
  530. };
  531. } // namespace detail
  532. } // namespace asio
  533. #include "asio/detail/pop_options.hpp"
  534. #endif // defined(ASIO_HAS_IO_URING)
  535. #endif // ASIO_DETAIL_IO_URING_SOCKET_SERVICE_HPP