io_uring_socket_service_base.hpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665
  1. //
  2. // detail/io_uring_socket_service_base.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_BASE_HPP
  11. #define BOOST_ASIO_DETAIL_IO_URING_SOCKET_SERVICE_BASE_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/associated_cancellation_slot.hpp>
  18. #include <boost/asio/buffer.hpp>
  19. #include <boost/asio/cancellation_type.hpp>
  20. #include <boost/asio/error.hpp>
  21. #include <boost/asio/execution_context.hpp>
  22. #include <boost/asio/socket_base.hpp>
  23. #include <boost/asio/detail/buffer_sequence_adapter.hpp>
  24. #include <boost/asio/detail/memory.hpp>
  25. #include <boost/asio/detail/io_uring_null_buffers_op.hpp>
  26. #include <boost/asio/detail/io_uring_service.hpp>
  27. #include <boost/asio/detail/io_uring_socket_recv_op.hpp>
  28. #include <boost/asio/detail/io_uring_socket_recvmsg_op.hpp>
  29. #include <boost/asio/detail/io_uring_socket_send_op.hpp>
  30. #include <boost/asio/detail/io_uring_wait_op.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. class io_uring_socket_service_base
  39. {
  40. public:
  41. // The native type of a socket.
  42. typedef socket_type native_handle_type;
  43. // The implementation type of the socket.
  44. struct base_implementation_type
  45. {
  46. // The native socket representation.
  47. socket_type socket_;
  48. // The current state of the socket.
  49. socket_ops::state_type state_;
  50. // Per I/O object data used by the io_uring_service.
  51. io_uring_service::per_io_object_data io_object_data_;
  52. };
  53. // Constructor.
  54. BOOST_ASIO_DECL io_uring_socket_service_base(execution_context& context);
  55. // Destroy all user-defined handler objects owned by the service.
  56. BOOST_ASIO_DECL void base_shutdown();
  57. // Construct a new socket implementation.
  58. BOOST_ASIO_DECL void construct(base_implementation_type& impl);
  59. // Move-construct a new socket implementation.
  60. BOOST_ASIO_DECL void base_move_construct(base_implementation_type& impl,
  61. base_implementation_type& other_impl) noexcept;
  62. // Move-assign from another socket implementation.
  63. BOOST_ASIO_DECL void base_move_assign(base_implementation_type& impl,
  64. io_uring_socket_service_base& other_service,
  65. base_implementation_type& other_impl);
  66. // Destroy a socket implementation.
  67. BOOST_ASIO_DECL void destroy(base_implementation_type& impl);
  68. // Determine whether the socket is open.
  69. bool is_open(const base_implementation_type& impl) const
  70. {
  71. return impl.socket_ != invalid_socket;
  72. }
  73. // Destroy a socket implementation.
  74. BOOST_ASIO_DECL boost::system::error_code close(
  75. base_implementation_type& impl, boost::system::error_code& ec);
  76. // Release ownership of the socket.
  77. BOOST_ASIO_DECL socket_type release(
  78. base_implementation_type& impl, boost::system::error_code& ec);
  79. // Get the native socket representation.
  80. native_handle_type native_handle(base_implementation_type& impl)
  81. {
  82. return impl.socket_;
  83. }
  84. // Cancel all operations associated with the socket.
  85. BOOST_ASIO_DECL boost::system::error_code cancel(
  86. base_implementation_type& impl, boost::system::error_code& ec);
  87. // Determine whether the socket is at the out-of-band data mark.
  88. bool at_mark(const base_implementation_type& impl,
  89. boost::system::error_code& ec) const
  90. {
  91. return socket_ops::sockatmark(impl.socket_, ec);
  92. }
  93. // Determine the number of bytes available for reading.
  94. std::size_t available(const base_implementation_type& impl,
  95. boost::system::error_code& ec) const
  96. {
  97. return socket_ops::available(impl.socket_, ec);
  98. }
  99. // Place the socket into the state where it will listen for new connections.
  100. boost::system::error_code listen(base_implementation_type& impl,
  101. int backlog, boost::system::error_code& ec)
  102. {
  103. socket_ops::listen(impl.socket_, backlog, ec);
  104. return ec;
  105. }
  106. // Perform an IO control command on the socket.
  107. template <typename IO_Control_Command>
  108. boost::system::error_code io_control(base_implementation_type& impl,
  109. IO_Control_Command& command, boost::system::error_code& ec)
  110. {
  111. socket_ops::ioctl(impl.socket_, impl.state_, command.name(),
  112. static_cast<ioctl_arg_type*>(command.data()), ec);
  113. return ec;
  114. }
  115. // Gets the non-blocking mode of the socket.
  116. bool non_blocking(const base_implementation_type& impl) const
  117. {
  118. return (impl.state_ & socket_ops::user_set_non_blocking) != 0;
  119. }
  120. // Sets the non-blocking mode of the socket.
  121. boost::system::error_code non_blocking(base_implementation_type& impl,
  122. bool mode, boost::system::error_code& ec)
  123. {
  124. socket_ops::set_user_non_blocking(impl.socket_, impl.state_, mode, ec);
  125. return ec;
  126. }
  127. // Gets the non-blocking mode of the native socket implementation.
  128. bool native_non_blocking(const base_implementation_type& impl) const
  129. {
  130. return (impl.state_ & socket_ops::internal_non_blocking) != 0;
  131. }
  132. // Sets the non-blocking mode of the native socket implementation.
  133. boost::system::error_code native_non_blocking(base_implementation_type& impl,
  134. bool mode, boost::system::error_code& ec)
  135. {
  136. socket_ops::set_internal_non_blocking(impl.socket_, impl.state_, mode, ec);
  137. return ec;
  138. }
  139. // Wait for the socket to become ready to read, ready to write, or to have
  140. // pending error conditions.
  141. boost::system::error_code wait(base_implementation_type& impl,
  142. socket_base::wait_type w, boost::system::error_code& ec)
  143. {
  144. switch (w)
  145. {
  146. case socket_base::wait_read:
  147. socket_ops::poll_read(impl.socket_, impl.state_, -1, ec);
  148. break;
  149. case socket_base::wait_write:
  150. socket_ops::poll_write(impl.socket_, impl.state_, -1, ec);
  151. break;
  152. case socket_base::wait_error:
  153. socket_ops::poll_error(impl.socket_, impl.state_, -1, ec);
  154. break;
  155. default:
  156. ec = boost::asio::error::invalid_argument;
  157. break;
  158. }
  159. return ec;
  160. }
  161. // Asynchronously wait for the socket to become ready to read, ready to
  162. // write, or to have pending error conditions.
  163. template <typename Handler, typename IoExecutor>
  164. void async_wait(base_implementation_type& impl,
  165. socket_base::wait_type w, Handler& handler, const IoExecutor& io_ex)
  166. {
  167. bool is_continuation =
  168. boost_asio_handler_cont_helpers::is_continuation(handler);
  169. associated_cancellation_slot_t<Handler> slot
  170. = boost::asio::get_associated_cancellation_slot(handler);
  171. int op_type;
  172. int poll_flags;
  173. switch (w)
  174. {
  175. case socket_base::wait_read:
  176. op_type = io_uring_service::read_op;
  177. poll_flags = POLLIN;
  178. break;
  179. case socket_base::wait_write:
  180. op_type = io_uring_service::write_op;
  181. poll_flags = POLLOUT;
  182. break;
  183. case socket_base::wait_error:
  184. op_type = io_uring_service::except_op;
  185. poll_flags = POLLPRI | POLLERR | POLLHUP;
  186. break;
  187. default:
  188. op_type = -1;
  189. poll_flags = -1;
  190. return;
  191. }
  192. // Allocate and construct an operation to wrap the handler.
  193. typedef io_uring_wait_op<Handler, IoExecutor> op;
  194. typename op::ptr p = { boost::asio::detail::addressof(handler),
  195. op::ptr::allocate(handler), 0 };
  196. p.p = new (p.v) op(success_ec_, impl.socket_,
  197. poll_flags, handler, io_ex);
  198. BOOST_ASIO_HANDLER_CREATION((io_uring_service_.context(), *p.p,
  199. "socket", &impl, impl.socket_, "async_wait"));
  200. // Optionally register for per-operation cancellation.
  201. if (slot.is_connected())
  202. {
  203. p.p->cancellation_key_ =
  204. &slot.template emplace<io_uring_op_cancellation>(
  205. &io_uring_service_, &impl.io_object_data_, op_type);
  206. }
  207. start_op(impl, op_type, p.p, is_continuation, op_type == -1);
  208. p.v = p.p = 0;
  209. }
  210. // Send the given data to the peer.
  211. template <typename ConstBufferSequence>
  212. size_t send(base_implementation_type& impl,
  213. const ConstBufferSequence& buffers,
  214. socket_base::message_flags flags, boost::system::error_code& ec)
  215. {
  216. typedef buffer_sequence_adapter<boost::asio::const_buffer,
  217. ConstBufferSequence> bufs_type;
  218. if (bufs_type::is_single_buffer)
  219. {
  220. return socket_ops::sync_send1(impl.socket_,
  221. impl.state_, bufs_type::first(buffers).data(),
  222. bufs_type::first(buffers).size(), flags, ec);
  223. }
  224. else
  225. {
  226. bufs_type bufs(buffers);
  227. return socket_ops::sync_send(impl.socket_, impl.state_,
  228. bufs.buffers(), bufs.count(), flags, bufs.all_empty(), ec);
  229. }
  230. }
  231. // Wait until data can be sent without blocking.
  232. size_t send(base_implementation_type& impl, const null_buffers&,
  233. socket_base::message_flags, boost::system::error_code& ec)
  234. {
  235. // Wait for socket to become ready.
  236. socket_ops::poll_write(impl.socket_, impl.state_, -1, ec);
  237. return 0;
  238. }
  239. // Start an asynchronous send. The data being sent must be valid for the
  240. // lifetime of the asynchronous operation.
  241. template <typename ConstBufferSequence, typename Handler, typename IoExecutor>
  242. void async_send(base_implementation_type& impl,
  243. const ConstBufferSequence& buffers, socket_base::message_flags flags,
  244. Handler& handler, const IoExecutor& io_ex)
  245. {
  246. bool is_continuation =
  247. boost_asio_handler_cont_helpers::is_continuation(handler);
  248. associated_cancellation_slot_t<Handler> slot
  249. = boost::asio::get_associated_cancellation_slot(handler);
  250. // Allocate and construct an operation to wrap the handler.
  251. typedef io_uring_socket_send_op<
  252. ConstBufferSequence, Handler, IoExecutor> op;
  253. typename op::ptr p = { boost::asio::detail::addressof(handler),
  254. op::ptr::allocate(handler), 0 };
  255. p.p = new (p.v) op(success_ec_, impl.socket_,
  256. impl.state_, buffers, flags, handler, io_ex);
  257. // Optionally register for per-operation cancellation.
  258. if (slot.is_connected())
  259. {
  260. p.p->cancellation_key_ =
  261. &slot.template emplace<io_uring_op_cancellation>(&io_uring_service_,
  262. &impl.io_object_data_, io_uring_service::write_op);
  263. }
  264. BOOST_ASIO_HANDLER_CREATION((io_uring_service_.context(), *p.p,
  265. "socket", &impl, impl.socket_, "async_send"));
  266. start_op(impl, io_uring_service::write_op, p.p, is_continuation,
  267. ((impl.state_ & socket_ops::stream_oriented)
  268. && buffer_sequence_adapter<boost::asio::const_buffer,
  269. ConstBufferSequence>::all_empty(buffers)));
  270. p.v = p.p = 0;
  271. }
  272. // Start an asynchronous wait until data can be sent without blocking.
  273. template <typename Handler, typename IoExecutor>
  274. void async_send(base_implementation_type& impl, const null_buffers&,
  275. socket_base::message_flags, Handler& handler, const IoExecutor& io_ex)
  276. {
  277. bool is_continuation =
  278. boost_asio_handler_cont_helpers::is_continuation(handler);
  279. associated_cancellation_slot_t<Handler> slot
  280. = boost::asio::get_associated_cancellation_slot(handler);
  281. // Allocate and construct an operation to wrap the handler.
  282. typedef io_uring_null_buffers_op<Handler, IoExecutor> op;
  283. typename op::ptr p = { boost::asio::detail::addressof(handler),
  284. op::ptr::allocate(handler), 0 };
  285. p.p = new (p.v) op(success_ec_, impl.socket_, POLLOUT, handler, io_ex);
  286. // Optionally register for per-operation cancellation.
  287. if (slot.is_connected())
  288. {
  289. p.p->cancellation_key_ =
  290. &slot.template emplace<io_uring_op_cancellation>(&io_uring_service_,
  291. &impl.io_object_data_, io_uring_service::write_op);
  292. }
  293. BOOST_ASIO_HANDLER_CREATION((io_uring_service_.context(), *p.p,
  294. "socket", &impl, impl.socket_, "async_send(null_buffers)"));
  295. start_op(impl, io_uring_service::write_op, p.p, is_continuation, false);
  296. p.v = p.p = 0;
  297. }
  298. // Receive some data from the peer. Returns the number of bytes received.
  299. template <typename MutableBufferSequence>
  300. size_t receive(base_implementation_type& impl,
  301. const MutableBufferSequence& buffers,
  302. socket_base::message_flags flags, boost::system::error_code& ec)
  303. {
  304. typedef buffer_sequence_adapter<boost::asio::mutable_buffer,
  305. MutableBufferSequence> bufs_type;
  306. if (bufs_type::is_single_buffer)
  307. {
  308. return socket_ops::sync_recv1(impl.socket_,
  309. impl.state_, bufs_type::first(buffers).data(),
  310. bufs_type::first(buffers).size(), flags, ec);
  311. }
  312. else
  313. {
  314. bufs_type bufs(buffers);
  315. return socket_ops::sync_recv(impl.socket_, impl.state_,
  316. bufs.buffers(), bufs.count(), flags, bufs.all_empty(), ec);
  317. }
  318. }
  319. // Wait until data can be received without blocking.
  320. size_t receive(base_implementation_type& impl, const null_buffers&,
  321. socket_base::message_flags, boost::system::error_code& ec)
  322. {
  323. // Wait for socket to become ready.
  324. socket_ops::poll_read(impl.socket_, impl.state_, -1, ec);
  325. return 0;
  326. }
  327. // Start an asynchronous receive. The buffer for the data being received
  328. // must be valid for the lifetime of the asynchronous operation.
  329. template <typename MutableBufferSequence,
  330. typename Handler, typename IoExecutor>
  331. void async_receive(base_implementation_type& impl,
  332. const MutableBufferSequence& buffers, socket_base::message_flags flags,
  333. Handler& handler, const IoExecutor& io_ex)
  334. {
  335. bool is_continuation =
  336. boost_asio_handler_cont_helpers::is_continuation(handler);
  337. int op_type = (flags & socket_base::message_out_of_band)
  338. ? io_uring_service::except_op : io_uring_service::read_op;
  339. associated_cancellation_slot_t<Handler> slot
  340. = boost::asio::get_associated_cancellation_slot(handler);
  341. // Allocate and construct an operation to wrap the handler.
  342. typedef io_uring_socket_recv_op<
  343. MutableBufferSequence, Handler, IoExecutor> op;
  344. typename op::ptr p = { boost::asio::detail::addressof(handler),
  345. op::ptr::allocate(handler), 0 };
  346. p.p = new (p.v) op(success_ec_, impl.socket_,
  347. impl.state_, buffers, flags, handler, io_ex);
  348. // Optionally register for per-operation cancellation.
  349. if (slot.is_connected())
  350. {
  351. p.p->cancellation_key_ =
  352. &slot.template emplace<io_uring_op_cancellation>(
  353. &io_uring_service_, &impl.io_object_data_, op_type);
  354. }
  355. BOOST_ASIO_HANDLER_CREATION((io_uring_service_.context(), *p.p,
  356. "socket", &impl, impl.socket_, "async_receive"));
  357. start_op(impl, op_type, p.p, is_continuation,
  358. ((impl.state_ & socket_ops::stream_oriented)
  359. && buffer_sequence_adapter<boost::asio::mutable_buffer,
  360. MutableBufferSequence>::all_empty(buffers)));
  361. p.v = p.p = 0;
  362. }
  363. // Wait until data can be received without blocking.
  364. template <typename Handler, typename IoExecutor>
  365. void async_receive(base_implementation_type& impl,
  366. const null_buffers&, socket_base::message_flags flags,
  367. Handler& handler, const IoExecutor& io_ex)
  368. {
  369. bool is_continuation =
  370. boost_asio_handler_cont_helpers::is_continuation(handler);
  371. int op_type;
  372. int poll_flags;
  373. if ((flags & socket_base::message_out_of_band) != 0)
  374. {
  375. op_type = io_uring_service::except_op;
  376. poll_flags = POLLPRI;
  377. }
  378. else
  379. {
  380. op_type = io_uring_service::read_op;
  381. poll_flags = POLLIN;
  382. }
  383. associated_cancellation_slot_t<Handler> slot
  384. = boost::asio::get_associated_cancellation_slot(handler);
  385. // Allocate and construct an operation to wrap the handler.
  386. typedef io_uring_null_buffers_op<Handler, IoExecutor> op;
  387. typename op::ptr p = { boost::asio::detail::addressof(handler),
  388. op::ptr::allocate(handler), 0 };
  389. p.p = new (p.v) op(success_ec_, impl.socket_, poll_flags, handler, io_ex);
  390. // Optionally register for per-operation cancellation.
  391. if (slot.is_connected())
  392. {
  393. p.p->cancellation_key_ =
  394. &slot.template emplace<io_uring_op_cancellation>(
  395. &io_uring_service_, &impl.io_object_data_, op_type);
  396. }
  397. BOOST_ASIO_HANDLER_CREATION((io_uring_service_.context(), *p.p,
  398. "socket", &impl, impl.socket_, "async_receive(null_buffers)"));
  399. start_op(impl, op_type, p.p, is_continuation, false);
  400. p.v = p.p = 0;
  401. }
  402. // Receive some data with associated flags. Returns the number of bytes
  403. // received.
  404. template <typename MutableBufferSequence>
  405. size_t receive_with_flags(base_implementation_type& impl,
  406. const MutableBufferSequence& buffers,
  407. socket_base::message_flags in_flags,
  408. socket_base::message_flags& out_flags, boost::system::error_code& ec)
  409. {
  410. buffer_sequence_adapter<boost::asio::mutable_buffer,
  411. MutableBufferSequence> bufs(buffers);
  412. return socket_ops::sync_recvmsg(impl.socket_, impl.state_,
  413. bufs.buffers(), bufs.count(), in_flags, out_flags, ec);
  414. }
  415. // Wait until data can be received without blocking.
  416. size_t receive_with_flags(base_implementation_type& impl,
  417. const null_buffers&, socket_base::message_flags,
  418. socket_base::message_flags& out_flags, boost::system::error_code& ec)
  419. {
  420. // Wait for socket to become ready.
  421. socket_ops::poll_read(impl.socket_, impl.state_, -1, ec);
  422. // Clear out_flags, since we cannot give it any other sensible value when
  423. // performing a null_buffers operation.
  424. out_flags = 0;
  425. return 0;
  426. }
  427. // Start an asynchronous receive. The buffer for the data being received
  428. // must be valid for the lifetime of the asynchronous operation.
  429. template <typename MutableBufferSequence,
  430. typename Handler, typename IoExecutor>
  431. void async_receive_with_flags(base_implementation_type& impl,
  432. const MutableBufferSequence& buffers, socket_base::message_flags in_flags,
  433. socket_base::message_flags& out_flags, Handler& handler,
  434. const IoExecutor& io_ex)
  435. {
  436. bool is_continuation =
  437. boost_asio_handler_cont_helpers::is_continuation(handler);
  438. int op_type = (in_flags & socket_base::message_out_of_band)
  439. ? io_uring_service::except_op : io_uring_service::read_op;
  440. associated_cancellation_slot_t<Handler> slot
  441. = boost::asio::get_associated_cancellation_slot(handler);
  442. // Allocate and construct an operation to wrap the handler.
  443. typedef io_uring_socket_recvmsg_op<
  444. MutableBufferSequence, Handler, IoExecutor> op;
  445. typename op::ptr p = { boost::asio::detail::addressof(handler),
  446. op::ptr::allocate(handler), 0 };
  447. p.p = new (p.v) op(success_ec_, impl.socket_, impl.state_,
  448. buffers, in_flags, out_flags, handler, io_ex);
  449. // Optionally register for per-operation cancellation.
  450. if (slot.is_connected())
  451. {
  452. p.p->cancellation_key_ =
  453. &slot.template emplace<io_uring_op_cancellation>(
  454. &io_uring_service_, &impl.io_object_data_, op_type);
  455. }
  456. BOOST_ASIO_HANDLER_CREATION((io_uring_service_.context(), *p.p,
  457. "socket", &impl, impl.socket_, "async_receive_with_flags"));
  458. start_op(impl, op_type, p.p, is_continuation, false);
  459. p.v = p.p = 0;
  460. }
  461. // Wait until data can be received without blocking.
  462. template <typename Handler, typename IoExecutor>
  463. void async_receive_with_flags(base_implementation_type& impl,
  464. const null_buffers&, socket_base::message_flags in_flags,
  465. socket_base::message_flags& out_flags, Handler& handler,
  466. const IoExecutor& io_ex)
  467. {
  468. bool is_continuation =
  469. boost_asio_handler_cont_helpers::is_continuation(handler);
  470. int op_type;
  471. int poll_flags;
  472. if ((in_flags & socket_base::message_out_of_band) != 0)
  473. {
  474. op_type = io_uring_service::except_op;
  475. poll_flags = POLLPRI;
  476. }
  477. else
  478. {
  479. op_type = io_uring_service::read_op;
  480. poll_flags = POLLIN;
  481. }
  482. associated_cancellation_slot_t<Handler> slot
  483. = boost::asio::get_associated_cancellation_slot(handler);
  484. // Allocate and construct an operation to wrap the handler.
  485. typedef io_uring_null_buffers_op<Handler, IoExecutor> op;
  486. typename op::ptr p = { boost::asio::detail::addressof(handler),
  487. op::ptr::allocate(handler), 0 };
  488. p.p = new (p.v) op(success_ec_, impl.socket_, poll_flags, handler, io_ex);
  489. // Optionally register for per-operation cancellation.
  490. if (slot.is_connected())
  491. {
  492. p.p->cancellation_key_ =
  493. &slot.template emplace<io_uring_op_cancellation>(
  494. &io_uring_service_, &impl.io_object_data_, op_type);
  495. }
  496. BOOST_ASIO_HANDLER_CREATION((io_uring_service_.context(), *p.p, "socket",
  497. &impl, impl.socket_, "async_receive_with_flags(null_buffers)"));
  498. // Clear out_flags, since we cannot give it any other sensible value when
  499. // performing a null_buffers operation.
  500. out_flags = 0;
  501. start_op(impl, op_type, p.p, is_continuation, false);
  502. p.v = p.p = 0;
  503. }
  504. protected:
  505. // Open a new socket implementation.
  506. BOOST_ASIO_DECL boost::system::error_code do_open(
  507. base_implementation_type& impl, int af,
  508. int type, int protocol, boost::system::error_code& ec);
  509. // Assign a native socket to a socket implementation.
  510. BOOST_ASIO_DECL boost::system::error_code do_assign(
  511. base_implementation_type& impl, int type,
  512. const native_handle_type& native_socket, boost::system::error_code& ec);
  513. // Start the asynchronous read or write operation.
  514. BOOST_ASIO_DECL void start_op(base_implementation_type& impl, int op_type,
  515. io_uring_operation* op, bool is_continuation, bool noop);
  516. // Start the asynchronous accept operation.
  517. BOOST_ASIO_DECL void start_accept_op(base_implementation_type& impl,
  518. io_uring_operation* op, bool is_continuation, bool peer_is_open);
  519. // Helper class used to implement per-operation cancellation
  520. class io_uring_op_cancellation
  521. {
  522. public:
  523. io_uring_op_cancellation(io_uring_service* s,
  524. io_uring_service::per_io_object_data* p, int o)
  525. : io_uring_service_(s),
  526. io_object_data_(p),
  527. op_type_(o)
  528. {
  529. }
  530. void operator()(cancellation_type_t type)
  531. {
  532. if (!!(type &
  533. (cancellation_type::terminal
  534. | cancellation_type::partial
  535. | cancellation_type::total)))
  536. {
  537. io_uring_service_->cancel_ops_by_key(*io_object_data_, op_type_, this);
  538. }
  539. }
  540. private:
  541. io_uring_service* io_uring_service_;
  542. io_uring_service::per_io_object_data* io_object_data_;
  543. int op_type_;
  544. };
  545. // The io_uring_service that performs event demultiplexing for the service.
  546. io_uring_service& io_uring_service_;
  547. // Cached success value to avoid accessing category singleton.
  548. const boost::system::error_code success_ec_;
  549. };
  550. } // namespace detail
  551. } // namespace asio
  552. } // namespace boost
  553. #include <boost/asio/detail/pop_options.hpp>
  554. #if defined(BOOST_ASIO_HEADER_ONLY)
  555. # include <boost/asio/detail/impl/io_uring_socket_service_base.ipp>
  556. #endif // defined(BOOST_ASIO_HEADER_ONLY)
  557. #endif // defined(BOOST_ASIO_HAS_IO_URING)
  558. #endif // BOOST_ASIO_DETAIL_IO_URING_SOCKET_SERVICE_BASE_HPP