reactive_socket_service.hpp 21 KB

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