reactive_socket_service.hpp 21 KB

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