reactive_socket_accept_op.hpp 11 KB

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