win_iocp_socket_accept_op.hpp 9.4 KB

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