win_iocp_socket_accept_op.hpp 10 KB

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