win_iocp_socket_service.hpp 23 KB

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