win_iocp_socket_accept_op.hpp 10 KB

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