io_uring_socket_service.hpp 21 KB

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