win_iocp_socket_service.hpp 22 KB

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