reactive_socket_accept_op.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. //
  2. // detail/reactive_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_REACTIVE_SOCKET_ACCEPT_OP_HPP
  11. #define BOOST_ASIO_DETAIL_REACTIVE_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. #include <boost/asio/detail/bind_handler.hpp>
  17. #include <boost/asio/detail/fenced_block.hpp>
  18. #include <boost/asio/detail/handler_alloc_helpers.hpp>
  19. #include <boost/asio/detail/handler_work.hpp>
  20. #include <boost/asio/detail/memory.hpp>
  21. #include <boost/asio/detail/reactor_op.hpp>
  22. #include <boost/asio/detail/socket_holder.hpp>
  23. #include <boost/asio/detail/socket_ops.hpp>
  24. #include <boost/asio/detail/push_options.hpp>
  25. namespace boost {
  26. namespace asio {
  27. namespace detail {
  28. template <typename Socket, typename Protocol>
  29. class reactive_socket_accept_op_base : public reactor_op
  30. {
  31. public:
  32. reactive_socket_accept_op_base(const boost::system::error_code& success_ec,
  33. socket_type socket, socket_ops::state_type state, Socket& peer,
  34. const Protocol& protocol, typename Protocol::endpoint* peer_endpoint,
  35. func_type complete_func)
  36. : reactor_op(success_ec,
  37. &reactive_socket_accept_op_base::do_perform, complete_func),
  38. socket_(socket),
  39. state_(state),
  40. peer_(peer),
  41. protocol_(protocol),
  42. peer_endpoint_(peer_endpoint),
  43. addrlen_(peer_endpoint ? peer_endpoint->capacity() : 0)
  44. {
  45. }
  46. static status do_perform(reactor_op* base)
  47. {
  48. BOOST_ASIO_ASSUME(base != 0);
  49. reactive_socket_accept_op_base* o(
  50. static_cast<reactive_socket_accept_op_base*>(base));
  51. socket_type new_socket = invalid_socket;
  52. status result = socket_ops::non_blocking_accept(o->socket_,
  53. o->state_, o->peer_endpoint_ ? o->peer_endpoint_->data() : 0,
  54. o->peer_endpoint_ ? &o->addrlen_ : 0, o->ec_, new_socket)
  55. ? done : not_done;
  56. o->new_socket_.reset(new_socket);
  57. BOOST_ASIO_HANDLER_REACTOR_OPERATION((*o, "non_blocking_accept", o->ec_));
  58. return result;
  59. }
  60. void do_assign()
  61. {
  62. if (new_socket_.get() != invalid_socket)
  63. {
  64. if (peer_endpoint_)
  65. peer_endpoint_->resize(addrlen_);
  66. peer_.assign(protocol_, new_socket_.get(), ec_);
  67. if (!ec_)
  68. new_socket_.release();
  69. }
  70. }
  71. private:
  72. socket_type socket_;
  73. socket_ops::state_type state_;
  74. socket_holder new_socket_;
  75. Socket& peer_;
  76. Protocol protocol_;
  77. typename Protocol::endpoint* peer_endpoint_;
  78. std::size_t addrlen_;
  79. };
  80. template <typename Socket, typename Protocol,
  81. typename Handler, typename IoExecutor>
  82. class reactive_socket_accept_op :
  83. public reactive_socket_accept_op_base<Socket, Protocol>
  84. {
  85. public:
  86. typedef Handler handler_type;
  87. typedef IoExecutor io_executor_type;
  88. BOOST_ASIO_DEFINE_HANDLER_PTR(reactive_socket_accept_op);
  89. reactive_socket_accept_op(const boost::system::error_code& success_ec,
  90. socket_type socket, socket_ops::state_type state, Socket& peer,
  91. const Protocol& protocol, typename Protocol::endpoint* peer_endpoint,
  92. Handler& handler, const IoExecutor& io_ex)
  93. : reactive_socket_accept_op_base<Socket, Protocol>(
  94. success_ec, socket, state, peer, protocol, peer_endpoint,
  95. &reactive_socket_accept_op::do_complete),
  96. handler_(static_cast<Handler&&>(handler)),
  97. work_(handler_, io_ex)
  98. {
  99. }
  100. static void do_complete(void* owner, operation* base,
  101. const boost::system::error_code& /*ec*/,
  102. std::size_t /*bytes_transferred*/)
  103. {
  104. // Take ownership of the handler object.
  105. BOOST_ASIO_ASSUME(base != 0);
  106. reactive_socket_accept_op* o(static_cast<reactive_socket_accept_op*>(base));
  107. ptr p = { boost::asio::detail::addressof(o->handler_), o, o };
  108. // On success, assign new connection to peer socket object.
  109. if (owner)
  110. o->do_assign();
  111. BOOST_ASIO_HANDLER_COMPLETION((*o));
  112. // Take ownership of the operation's outstanding work.
  113. handler_work<Handler, IoExecutor> w(
  114. static_cast<handler_work<Handler, IoExecutor>&&>(
  115. o->work_));
  116. BOOST_ASIO_ERROR_LOCATION(o->ec_);
  117. // Make a copy of the handler so that the memory can be deallocated before
  118. // the upcall is made. Even if we're not about to make an upcall, a
  119. // sub-object of the handler may be the true owner of the memory associated
  120. // with the handler. Consequently, a local copy of the handler is required
  121. // to ensure that any owning sub-object remains valid until after we have
  122. // deallocated the memory here.
  123. detail::binder1<Handler, boost::system::error_code>
  124. handler(o->handler_, o->ec_);
  125. p.h = boost::asio::detail::addressof(handler.handler_);
  126. p.reset();
  127. // Make the upcall if required.
  128. if (owner)
  129. {
  130. fenced_block b(fenced_block::half);
  131. BOOST_ASIO_HANDLER_INVOCATION_BEGIN((handler.arg1_));
  132. w.complete(handler, handler.handler_);
  133. BOOST_ASIO_HANDLER_INVOCATION_END;
  134. }
  135. }
  136. static void do_immediate(operation* base, bool, const void* io_ex)
  137. {
  138. // Take ownership of the handler object.
  139. BOOST_ASIO_ASSUME(base != 0);
  140. reactive_socket_accept_op* o(static_cast<reactive_socket_accept_op*>(base));
  141. ptr p = { boost::asio::detail::addressof(o->handler_), o, o };
  142. // On success, assign new connection to peer socket object.
  143. o->do_assign();
  144. BOOST_ASIO_HANDLER_COMPLETION((*o));
  145. // Take ownership of the operation's outstanding work.
  146. immediate_handler_work<Handler, IoExecutor> w(
  147. static_cast<handler_work<Handler, IoExecutor>&&>(
  148. o->work_));
  149. BOOST_ASIO_ERROR_LOCATION(o->ec_);
  150. // Make a copy of the handler so that the memory can be deallocated before
  151. // the upcall is made. Even if we're not about to make an upcall, a
  152. // sub-object of the handler may be the true owner of the memory associated
  153. // with the handler. Consequently, a local copy of the handler is required
  154. // to ensure that any owning sub-object remains valid until after we have
  155. // deallocated the memory here.
  156. detail::binder1<Handler, boost::system::error_code>
  157. handler(o->handler_, o->ec_);
  158. p.h = boost::asio::detail::addressof(handler.handler_);
  159. p.reset();
  160. BOOST_ASIO_HANDLER_INVOCATION_BEGIN((handler.arg1_));
  161. w.complete(handler, handler.handler_, io_ex);
  162. BOOST_ASIO_HANDLER_INVOCATION_END;
  163. }
  164. private:
  165. Handler handler_;
  166. handler_work<Handler, IoExecutor> work_;
  167. };
  168. template <typename Protocol, typename PeerIoExecutor,
  169. typename Handler, typename IoExecutor>
  170. class reactive_socket_move_accept_op :
  171. private Protocol::socket::template rebind_executor<PeerIoExecutor>::other,
  172. public reactive_socket_accept_op_base<
  173. typename Protocol::socket::template rebind_executor<PeerIoExecutor>::other,
  174. Protocol>
  175. {
  176. public:
  177. typedef Handler handler_type;
  178. typedef IoExecutor io_executor_type;
  179. BOOST_ASIO_DEFINE_HANDLER_PTR(reactive_socket_move_accept_op);
  180. reactive_socket_move_accept_op(const boost::system::error_code& success_ec,
  181. const PeerIoExecutor& peer_io_ex, socket_type socket,
  182. socket_ops::state_type state, const Protocol& protocol,
  183. typename Protocol::endpoint* peer_endpoint, Handler& handler,
  184. const IoExecutor& io_ex)
  185. : peer_socket_type(peer_io_ex),
  186. reactive_socket_accept_op_base<peer_socket_type, Protocol>(
  187. success_ec, socket, state, *this, protocol, peer_endpoint,
  188. &reactive_socket_move_accept_op::do_complete),
  189. handler_(static_cast<Handler&&>(handler)),
  190. work_(handler_, io_ex)
  191. {
  192. }
  193. static void do_complete(void* owner, operation* base,
  194. const boost::system::error_code& /*ec*/,
  195. std::size_t /*bytes_transferred*/)
  196. {
  197. // Take ownership of the handler object.
  198. BOOST_ASIO_ASSUME(base != 0);
  199. reactive_socket_move_accept_op* o(
  200. static_cast<reactive_socket_move_accept_op*>(base));
  201. ptr p = { boost::asio::detail::addressof(o->handler_), o, o };
  202. // On success, assign new connection to peer socket object.
  203. if (owner)
  204. o->do_assign();
  205. BOOST_ASIO_HANDLER_COMPLETION((*o));
  206. // Take ownership of the operation's outstanding work.
  207. handler_work<Handler, IoExecutor> w(
  208. static_cast<handler_work<Handler, IoExecutor>&&>(
  209. o->work_));
  210. BOOST_ASIO_ERROR_LOCATION(o->ec_);
  211. // Make a copy of the handler so that the memory can be deallocated before
  212. // the upcall is made. Even if we're not about to make an upcall, a
  213. // sub-object of the handler may be the true owner of the memory associated
  214. // with the handler. Consequently, a local copy of the handler is required
  215. // to ensure that any owning sub-object remains valid until after we have
  216. // deallocated the memory here.
  217. detail::move_binder2<Handler,
  218. boost::system::error_code, peer_socket_type>
  219. handler(0, static_cast<Handler&&>(o->handler_), o->ec_,
  220. static_cast<peer_socket_type&&>(*o));
  221. p.h = boost::asio::detail::addressof(handler.handler_);
  222. p.reset();
  223. // Make the upcall if required.
  224. if (owner)
  225. {
  226. fenced_block b(fenced_block::half);
  227. BOOST_ASIO_HANDLER_INVOCATION_BEGIN((handler.arg1_, "..."));
  228. w.complete(handler, handler.handler_);
  229. BOOST_ASIO_HANDLER_INVOCATION_END;
  230. }
  231. }
  232. static void do_immediate(operation* base, bool, const void* io_ex)
  233. {
  234. // Take ownership of the handler object.
  235. BOOST_ASIO_ASSUME(base != 0);
  236. reactive_socket_move_accept_op* o(
  237. static_cast<reactive_socket_move_accept_op*>(base));
  238. ptr p = { boost::asio::detail::addressof(o->handler_), o, o };
  239. // On success, assign new connection to peer socket object.
  240. o->do_assign();
  241. BOOST_ASIO_HANDLER_COMPLETION((*o));
  242. // Take ownership of the operation's outstanding work.
  243. immediate_handler_work<Handler, IoExecutor> w(
  244. static_cast<handler_work<Handler, IoExecutor>&&>(
  245. o->work_));
  246. BOOST_ASIO_ERROR_LOCATION(o->ec_);
  247. // Make a copy of the handler so that the memory can be deallocated before
  248. // the upcall is made. Even if we're not about to make an upcall, a
  249. // sub-object of the handler may be the true owner of the memory associated
  250. // with the handler. Consequently, a local copy of the handler is required
  251. // to ensure that any owning sub-object remains valid until after we have
  252. // deallocated the memory here.
  253. detail::move_binder2<Handler,
  254. boost::system::error_code, peer_socket_type>
  255. handler(0, static_cast<Handler&&>(o->handler_), o->ec_,
  256. static_cast<peer_socket_type&&>(*o));
  257. p.h = boost::asio::detail::addressof(handler.handler_);
  258. p.reset();
  259. BOOST_ASIO_HANDLER_INVOCATION_BEGIN((handler.arg1_, "..."));
  260. w.complete(handler, handler.handler_, io_ex);
  261. BOOST_ASIO_HANDLER_INVOCATION_END;
  262. }
  263. private:
  264. typedef typename Protocol::socket::template
  265. rebind_executor<PeerIoExecutor>::other peer_socket_type;
  266. Handler handler_;
  267. handler_work<Handler, IoExecutor> work_;
  268. };
  269. } // namespace detail
  270. } // namespace asio
  271. } // namespace boost
  272. #include <boost/asio/detail/pop_options.hpp>
  273. #endif // BOOST_ASIO_DETAIL_REACTIVE_SOCKET_ACCEPT_OP_HPP