io_uring_socket_service_base.hpp 22 KB

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