win_iocp_socket_accept_op.hpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. //
  2. // detail/win_iocp_socket_accept_op.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2022 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_invoke_helpers.hpp"
  21. #include "asio/detail/handler_work.hpp"
  22. #include "asio/detail/memory.hpp"
  23. #include "asio/detail/operation.hpp"
  24. #include "asio/detail/socket_ops.hpp"
  25. #include "asio/detail/win_iocp_socket_service_base.hpp"
  26. #include "asio/error.hpp"
  27. #include "asio/detail/push_options.hpp"
  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. 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_(ASIO_MOVE_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 asio::error_code& result_ec,
  72. std::size_t /*bytes_transferred*/)
  73. {
  74. asio::error_code ec(result_ec);
  75. // Take ownership of the operation object.
  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. ASIO_MOVE_CAST2(handler_work<Handler, IoExecutor>)(
  120. o->work_));
  121. // Make a copy of the handler so that the memory can be deallocated before
  122. // the upcall is made. Even if we're not about to make an upcall, a
  123. // sub-object of the handler may be the true owner of the memory associated
  124. // with the handler. Consequently, a local copy of the handler is required
  125. // to ensure that any owning sub-object remains valid until after we have
  126. // deallocated the memory here.
  127. detail::binder1<Handler, asio::error_code>
  128. handler(o->handler_, ec);
  129. p.h = asio::detail::addressof(handler.handler_);
  130. p.reset();
  131. // Make the upcall if required.
  132. if (owner)
  133. {
  134. fenced_block b(fenced_block::half);
  135. ASIO_HANDLER_INVOCATION_BEGIN((handler.arg1_));
  136. w.complete(handler, handler.handler_);
  137. ASIO_HANDLER_INVOCATION_END;
  138. }
  139. }
  140. private:
  141. win_iocp_socket_service_base& socket_service_;
  142. socket_type socket_;
  143. socket_holder new_socket_;
  144. Socket& peer_;
  145. Protocol protocol_;
  146. typename Protocol::endpoint* peer_endpoint_;
  147. unsigned char output_buffer_[(sizeof(sockaddr_storage_type) + 16) * 2];
  148. bool enable_connection_aborted_;
  149. operation* proxy_op_;
  150. long* cancel_requested_;
  151. Handler handler_;
  152. handler_work<Handler, IoExecutor> work_;
  153. };
  154. #if defined(ASIO_HAS_MOVE)
  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_(ASIO_MOVE_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. win_iocp_socket_move_accept_op* o(
  203. static_cast<win_iocp_socket_move_accept_op*>(base));
  204. ptr p = { asio::detail::addressof(o->handler_), o, o };
  205. if (owner)
  206. {
  207. typename Protocol::endpoint peer_endpoint;
  208. std::size_t addr_len = peer_endpoint.capacity();
  209. socket_ops::complete_iocp_accept(o->socket_,
  210. o->output_buffer(), o->address_length(),
  211. peer_endpoint.data(), &addr_len,
  212. o->new_socket_.get(), ec);
  213. // Restart the accept operation if we got the connection_aborted error
  214. // and the enable_connection_aborted socket option is not set.
  215. if (ec == asio::error::connection_aborted
  216. && !o->enable_connection_aborted_)
  217. {
  218. o->reset();
  219. if (o->proxy_op_)
  220. o->proxy_op_->reset();
  221. o->socket_service_.restart_accept_op(o->socket_,
  222. o->new_socket_, o->protocol_.family(),
  223. o->protocol_.type(), o->protocol_.protocol(),
  224. o->output_buffer(), o->address_length(),
  225. o->cancel_requested_, o->proxy_op_ ? o->proxy_op_ : o);
  226. p.v = p.p = 0;
  227. return;
  228. }
  229. // If the socket was successfully accepted, transfer ownership of the
  230. // socket to the peer object.
  231. if (!ec)
  232. {
  233. o->peer_.assign(o->protocol_,
  234. typename Protocol::socket::native_handle_type(
  235. o->new_socket_.get(), peer_endpoint), ec);
  236. if (!ec)
  237. o->new_socket_.release();
  238. }
  239. // Pass endpoint back to caller.
  240. if (o->peer_endpoint_)
  241. *o->peer_endpoint_ = peer_endpoint;
  242. }
  243. ASIO_HANDLER_COMPLETION((*o));
  244. // Take ownership of the operation's outstanding work.
  245. handler_work<Handler, IoExecutor> w(
  246. ASIO_MOVE_CAST2(handler_work<Handler, IoExecutor>)(
  247. o->work_));
  248. // Make a copy of the handler so that the memory can be deallocated before
  249. // the upcall is made. Even if we're not about to make an upcall, a
  250. // sub-object of the handler may be the true owner of the memory associated
  251. // with the handler. Consequently, a local copy of the handler is required
  252. // to ensure that any owning sub-object remains valid until after we have
  253. // deallocated the memory here.
  254. detail::move_binder2<Handler,
  255. asio::error_code, peer_socket_type>
  256. handler(0, ASIO_MOVE_CAST(Handler)(o->handler_), ec,
  257. ASIO_MOVE_CAST(peer_socket_type)(o->peer_));
  258. p.h = asio::detail::addressof(handler.handler_);
  259. p.reset();
  260. // Make the upcall if required.
  261. if (owner)
  262. {
  263. fenced_block b(fenced_block::half);
  264. ASIO_HANDLER_INVOCATION_BEGIN((handler.arg1_, "..."));
  265. w.complete(handler, handler.handler_);
  266. ASIO_HANDLER_INVOCATION_END;
  267. }
  268. }
  269. private:
  270. typedef typename Protocol::socket::template
  271. rebind_executor<PeerIoExecutor>::other peer_socket_type;
  272. win_iocp_socket_service_base& socket_service_;
  273. socket_type socket_;
  274. socket_holder new_socket_;
  275. peer_socket_type peer_;
  276. Protocol protocol_;
  277. typename Protocol::endpoint* peer_endpoint_;
  278. unsigned char output_buffer_[(sizeof(sockaddr_storage_type) + 16) * 2];
  279. bool enable_connection_aborted_;
  280. long* cancel_requested_;
  281. operation* proxy_op_;
  282. Handler handler_;
  283. handler_work<Handler, IoExecutor> work_;
  284. };
  285. #endif // defined(ASIO_HAS_MOVE)
  286. } // namespace detail
  287. } // namespace asio
  288. #include "asio/detail/pop_options.hpp"
  289. #endif // defined(ASIO_HAS_IOCP)
  290. #endif // ASIO_DETAIL_WIN_IOCP_SOCKET_ACCEPT_OP_HPP