win_iocp_socket_accept_op.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. //
  2. // detail/win_iocp_socket_accept_op.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_ACCEPT_OP_HPP
  11. #define BOOST_ASIO_DETAIL_WIN_IOCP_SOCKET_ACCEPT_OP_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 <boost/asio/detail/bind_handler.hpp>
  18. #include <boost/asio/detail/fenced_block.hpp>
  19. #include <boost/asio/detail/handler_alloc_helpers.hpp>
  20. #include <boost/asio/detail/handler_work.hpp>
  21. #include <boost/asio/detail/memory.hpp>
  22. #include <boost/asio/detail/operation.hpp>
  23. #include <boost/asio/detail/socket_ops.hpp>
  24. #include <boost/asio/detail/win_iocp_socket_service_base.hpp>
  25. #include <boost/asio/error.hpp>
  26. #include <boost/asio/detail/push_options.hpp>
  27. namespace boost {
  28. namespace asio {
  29. namespace detail {
  30. template <typename Socket, typename Protocol,
  31. typename Handler, typename IoExecutor>
  32. class win_iocp_socket_accept_op : public operation
  33. {
  34. public:
  35. BOOST_ASIO_DEFINE_HANDLER_PTR(win_iocp_socket_accept_op);
  36. win_iocp_socket_accept_op(win_iocp_socket_service_base& socket_service,
  37. socket_type socket, Socket& peer, const Protocol& protocol,
  38. typename Protocol::endpoint* peer_endpoint,
  39. bool enable_connection_aborted, Handler& handler, const IoExecutor& io_ex)
  40. : operation(&win_iocp_socket_accept_op::do_complete),
  41. socket_service_(socket_service),
  42. socket_(socket),
  43. peer_(peer),
  44. protocol_(protocol),
  45. peer_endpoint_(peer_endpoint),
  46. enable_connection_aborted_(enable_connection_aborted),
  47. proxy_op_(0),
  48. cancel_requested_(0),
  49. handler_(static_cast<Handler&&>(handler)),
  50. work_(handler_, io_ex)
  51. {
  52. }
  53. socket_holder& new_socket()
  54. {
  55. return new_socket_;
  56. }
  57. void* output_buffer()
  58. {
  59. return output_buffer_;
  60. }
  61. DWORD address_length()
  62. {
  63. return sizeof(sockaddr_storage_type) + 16;
  64. }
  65. void enable_cancellation(long* cancel_requested, operation* proxy_op)
  66. {
  67. cancel_requested_ = cancel_requested;
  68. proxy_op_ = proxy_op;
  69. }
  70. static void do_complete(void* owner, operation* base,
  71. const boost::system::error_code& result_ec,
  72. std::size_t /*bytes_transferred*/)
  73. {
  74. boost::system::error_code ec(result_ec);
  75. // Take ownership of the operation object.
  76. BOOST_ASIO_ASSUME(base != 0);
  77. win_iocp_socket_accept_op* o(static_cast<win_iocp_socket_accept_op*>(base));
  78. ptr p = { boost::asio::detail::addressof(o->handler_), o, o };
  79. if (owner)
  80. {
  81. typename Protocol::endpoint peer_endpoint;
  82. std::size_t addr_len = peer_endpoint.capacity();
  83. socket_ops::complete_iocp_accept(o->socket_,
  84. o->output_buffer(), o->address_length(),
  85. peer_endpoint.data(), &addr_len,
  86. o->new_socket_.get(), ec);
  87. // Restart the accept operation if we got the connection_aborted error
  88. // and the enable_connection_aborted socket option is not set.
  89. if (ec == boost::asio::error::connection_aborted
  90. && !o->enable_connection_aborted_)
  91. {
  92. o->reset();
  93. if (o->proxy_op_)
  94. o->proxy_op_->reset();
  95. o->socket_service_.restart_accept_op(o->socket_,
  96. o->new_socket_, o->protocol_.family(),
  97. o->protocol_.type(), o->protocol_.protocol(),
  98. o->output_buffer(), o->address_length(),
  99. o->cancel_requested_, o->proxy_op_ ? o->proxy_op_ : o);
  100. p.v = p.p = 0;
  101. return;
  102. }
  103. // If the socket was successfully accepted, transfer ownership of the
  104. // socket to the peer object.
  105. if (!ec)
  106. {
  107. o->peer_.assign(o->protocol_,
  108. typename Socket::native_handle_type(
  109. o->new_socket_.get(), peer_endpoint), ec);
  110. if (!ec)
  111. o->new_socket_.release();
  112. }
  113. // Pass endpoint back to caller.
  114. if (o->peer_endpoint_)
  115. *o->peer_endpoint_ = peer_endpoint;
  116. }
  117. BOOST_ASIO_HANDLER_COMPLETION((*o));
  118. // Take ownership of the operation's outstanding work.
  119. handler_work<Handler, IoExecutor> w(
  120. static_cast<handler_work<Handler, IoExecutor>&&>(
  121. o->work_));
  122. BOOST_ASIO_ERROR_LOCATION(ec);
  123. // Make a copy of the handler so that the memory can be deallocated before
  124. // the upcall is made. Even if we're not about to make an upcall, a
  125. // sub-object of the handler may be the true owner of the memory associated
  126. // with the handler. Consequently, a local copy of the handler is required
  127. // to ensure that any owning sub-object remains valid until after we have
  128. // deallocated the memory here.
  129. detail::binder1<Handler, boost::system::error_code>
  130. handler(o->handler_, ec);
  131. p.h = boost::asio::detail::addressof(handler.handler_);
  132. p.reset();
  133. // Make the upcall if required.
  134. if (owner)
  135. {
  136. fenced_block b(fenced_block::half);
  137. BOOST_ASIO_HANDLER_INVOCATION_BEGIN((handler.arg1_));
  138. w.complete(handler, handler.handler_);
  139. BOOST_ASIO_HANDLER_INVOCATION_END;
  140. }
  141. }
  142. private:
  143. win_iocp_socket_service_base& socket_service_;
  144. socket_type socket_;
  145. socket_holder new_socket_;
  146. Socket& peer_;
  147. Protocol protocol_;
  148. typename Protocol::endpoint* peer_endpoint_;
  149. unsigned char output_buffer_[(sizeof(sockaddr_storage_type) + 16) * 2];
  150. bool enable_connection_aborted_;
  151. operation* proxy_op_;
  152. long* cancel_requested_;
  153. Handler handler_;
  154. handler_work<Handler, IoExecutor> work_;
  155. };
  156. template <typename Protocol, typename PeerIoExecutor,
  157. typename Handler, typename IoExecutor>
  158. class win_iocp_socket_move_accept_op : public operation
  159. {
  160. public:
  161. BOOST_ASIO_DEFINE_HANDLER_PTR(win_iocp_socket_move_accept_op);
  162. win_iocp_socket_move_accept_op(
  163. win_iocp_socket_service_base& socket_service, socket_type socket,
  164. const Protocol& protocol, const PeerIoExecutor& peer_io_ex,
  165. typename Protocol::endpoint* peer_endpoint,
  166. bool enable_connection_aborted, Handler& handler, const IoExecutor& io_ex)
  167. : operation(&win_iocp_socket_move_accept_op::do_complete),
  168. socket_service_(socket_service),
  169. socket_(socket),
  170. peer_(peer_io_ex),
  171. protocol_(protocol),
  172. peer_endpoint_(peer_endpoint),
  173. enable_connection_aborted_(enable_connection_aborted),
  174. cancel_requested_(0),
  175. proxy_op_(0),
  176. handler_(static_cast<Handler&&>(handler)),
  177. work_(handler_, io_ex)
  178. {
  179. }
  180. socket_holder& new_socket()
  181. {
  182. return new_socket_;
  183. }
  184. void* output_buffer()
  185. {
  186. return output_buffer_;
  187. }
  188. DWORD address_length()
  189. {
  190. return sizeof(sockaddr_storage_type) + 16;
  191. }
  192. void enable_cancellation(long* cancel_requested, operation* proxy_op)
  193. {
  194. cancel_requested_ = cancel_requested;
  195. proxy_op_ = proxy_op;
  196. }
  197. static void do_complete(void* owner, operation* base,
  198. const boost::system::error_code& result_ec,
  199. std::size_t /*bytes_transferred*/)
  200. {
  201. boost::system::error_code ec(result_ec);
  202. // Take ownership of the operation object.
  203. BOOST_ASIO_ASSUME(base != 0);
  204. win_iocp_socket_move_accept_op* o(
  205. static_cast<win_iocp_socket_move_accept_op*>(base));
  206. ptr p = { boost::asio::detail::addressof(o->handler_), o, o };
  207. if (owner)
  208. {
  209. typename Protocol::endpoint peer_endpoint;
  210. std::size_t addr_len = peer_endpoint.capacity();
  211. socket_ops::complete_iocp_accept(o->socket_,
  212. o->output_buffer(), o->address_length(),
  213. peer_endpoint.data(), &addr_len,
  214. o->new_socket_.get(), ec);
  215. // Restart the accept operation if we got the connection_aborted error
  216. // and the enable_connection_aborted socket option is not set.
  217. if (ec == boost::asio::error::connection_aborted
  218. && !o->enable_connection_aborted_)
  219. {
  220. o->reset();
  221. if (o->proxy_op_)
  222. o->proxy_op_->reset();
  223. o->socket_service_.restart_accept_op(o->socket_,
  224. o->new_socket_, o->protocol_.family(),
  225. o->protocol_.type(), o->protocol_.protocol(),
  226. o->output_buffer(), o->address_length(),
  227. o->cancel_requested_, o->proxy_op_ ? o->proxy_op_ : o);
  228. p.v = p.p = 0;
  229. return;
  230. }
  231. // If the socket was successfully accepted, transfer ownership of the
  232. // socket to the peer object.
  233. if (!ec)
  234. {
  235. o->peer_.assign(o->protocol_,
  236. typename Protocol::socket::native_handle_type(
  237. o->new_socket_.get(), peer_endpoint), ec);
  238. if (!ec)
  239. o->new_socket_.release();
  240. }
  241. // Pass endpoint back to caller.
  242. if (o->peer_endpoint_)
  243. *o->peer_endpoint_ = peer_endpoint;
  244. }
  245. BOOST_ASIO_HANDLER_COMPLETION((*o));
  246. // Take ownership of the operation's outstanding work.
  247. handler_work<Handler, IoExecutor> w(
  248. static_cast<handler_work<Handler, IoExecutor>&&>(
  249. o->work_));
  250. BOOST_ASIO_ERROR_LOCATION(ec);
  251. // Make a copy of the handler so that the memory can be deallocated before
  252. // the upcall is made. Even if we're not about to make an upcall, a
  253. // sub-object of the handler may be the true owner of the memory associated
  254. // with the handler. Consequently, a local copy of the handler is required
  255. // to ensure that any owning sub-object remains valid until after we have
  256. // deallocated the memory here.
  257. detail::move_binder2<Handler,
  258. boost::system::error_code, peer_socket_type>
  259. handler(0, static_cast<Handler&&>(o->handler_), ec,
  260. static_cast<peer_socket_type&&>(o->peer_));
  261. p.h = boost::asio::detail::addressof(handler.handler_);
  262. p.reset();
  263. // Make the upcall if required.
  264. if (owner)
  265. {
  266. fenced_block b(fenced_block::half);
  267. BOOST_ASIO_HANDLER_INVOCATION_BEGIN((handler.arg1_, "..."));
  268. w.complete(handler, handler.handler_);
  269. BOOST_ASIO_HANDLER_INVOCATION_END;
  270. }
  271. }
  272. private:
  273. typedef typename Protocol::socket::template
  274. rebind_executor<PeerIoExecutor>::other peer_socket_type;
  275. win_iocp_socket_service_base& socket_service_;
  276. socket_type socket_;
  277. socket_holder new_socket_;
  278. peer_socket_type peer_;
  279. Protocol protocol_;
  280. typename Protocol::endpoint* peer_endpoint_;
  281. unsigned char output_buffer_[(sizeof(sockaddr_storage_type) + 16) * 2];
  282. bool enable_connection_aborted_;
  283. long* cancel_requested_;
  284. operation* proxy_op_;
  285. Handler handler_;
  286. handler_work<Handler, IoExecutor> work_;
  287. };
  288. } // namespace detail
  289. } // namespace asio
  290. } // namespace boost
  291. #include <boost/asio/detail/pop_options.hpp>
  292. #endif // defined(BOOST_ASIO_HAS_IOCP)
  293. #endif // BOOST_ASIO_DETAIL_WIN_IOCP_SOCKET_ACCEPT_OP_HPP