reactive_socket_accept_op.hpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. //
  2. // detail/reactive_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_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_invoke_helpers.hpp"
  20. #include "asio/detail/handler_work.hpp"
  21. #include "asio/detail/memory.hpp"
  22. #include "asio/detail/reactor_op.hpp"
  23. #include "asio/detail/socket_holder.hpp"
  24. #include "asio/detail/socket_ops.hpp"
  25. #include "asio/detail/push_options.hpp"
  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 asio::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. 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. ASIO_DEFINE_HANDLER_PTR(reactive_socket_accept_op);
  86. reactive_socket_accept_op(const asio::error_code& success_ec,
  87. socket_type socket, socket_ops::state_type state, Socket& peer,
  88. const Protocol& protocol, typename Protocol::endpoint* peer_endpoint,
  89. Handler& handler, const IoExecutor& io_ex)
  90. : reactive_socket_accept_op_base<Socket, Protocol>(
  91. success_ec, socket, state, peer, protocol, peer_endpoint,
  92. &reactive_socket_accept_op::do_complete),
  93. handler_(ASIO_MOVE_CAST(Handler)(handler)),
  94. work_(handler_, io_ex)
  95. {
  96. }
  97. static void do_complete(void* owner, operation* base,
  98. const asio::error_code& /*ec*/,
  99. std::size_t /*bytes_transferred*/)
  100. {
  101. // Take ownership of the handler object.
  102. reactive_socket_accept_op* o(static_cast<reactive_socket_accept_op*>(base));
  103. ptr p = { asio::detail::addressof(o->handler_), o, o };
  104. // On success, assign new connection to peer socket object.
  105. if (owner)
  106. o->do_assign();
  107. ASIO_HANDLER_COMPLETION((*o));
  108. // Take ownership of the operation's outstanding work.
  109. handler_work<Handler, IoExecutor> w(
  110. ASIO_MOVE_CAST2(handler_work<Handler, IoExecutor>)(
  111. o->work_));
  112. // Make a copy of the handler so that the memory can be deallocated before
  113. // the upcall is made. Even if we're not about to make an upcall, a
  114. // sub-object of the handler may be the true owner of the memory associated
  115. // with the handler. Consequently, a local copy of the handler is required
  116. // to ensure that any owning sub-object remains valid until after we have
  117. // deallocated the memory here.
  118. detail::binder1<Handler, asio::error_code>
  119. handler(o->handler_, o->ec_);
  120. p.h = asio::detail::addressof(handler.handler_);
  121. p.reset();
  122. // Make the upcall if required.
  123. if (owner)
  124. {
  125. fenced_block b(fenced_block::half);
  126. ASIO_HANDLER_INVOCATION_BEGIN((handler.arg1_));
  127. w.complete(handler, handler.handler_);
  128. ASIO_HANDLER_INVOCATION_END;
  129. }
  130. }
  131. private:
  132. Handler handler_;
  133. handler_work<Handler, IoExecutor> work_;
  134. };
  135. #if defined(ASIO_HAS_MOVE)
  136. template <typename Protocol, typename PeerIoExecutor,
  137. typename Handler, typename IoExecutor>
  138. class reactive_socket_move_accept_op :
  139. private Protocol::socket::template rebind_executor<PeerIoExecutor>::other,
  140. public reactive_socket_accept_op_base<
  141. typename Protocol::socket::template rebind_executor<PeerIoExecutor>::other,
  142. Protocol>
  143. {
  144. public:
  145. ASIO_DEFINE_HANDLER_PTR(reactive_socket_move_accept_op);
  146. reactive_socket_move_accept_op(const asio::error_code& success_ec,
  147. const PeerIoExecutor& peer_io_ex, socket_type socket,
  148. socket_ops::state_type state, const Protocol& protocol,
  149. typename Protocol::endpoint* peer_endpoint, Handler& handler,
  150. const IoExecutor& io_ex)
  151. : peer_socket_type(peer_io_ex),
  152. reactive_socket_accept_op_base<peer_socket_type, Protocol>(
  153. success_ec, socket, state, *this, protocol, peer_endpoint,
  154. &reactive_socket_move_accept_op::do_complete),
  155. handler_(ASIO_MOVE_CAST(Handler)(handler)),
  156. work_(handler_, io_ex)
  157. {
  158. }
  159. static void do_complete(void* owner, operation* base,
  160. const asio::error_code& /*ec*/,
  161. std::size_t /*bytes_transferred*/)
  162. {
  163. // Take ownership of the handler object.
  164. reactive_socket_move_accept_op* o(
  165. static_cast<reactive_socket_move_accept_op*>(base));
  166. ptr p = { asio::detail::addressof(o->handler_), o, o };
  167. // On success, assign new connection to peer socket object.
  168. if (owner)
  169. o->do_assign();
  170. ASIO_HANDLER_COMPLETION((*o));
  171. // Take ownership of the operation's outstanding work.
  172. handler_work<Handler, IoExecutor> w(
  173. ASIO_MOVE_CAST2(handler_work<Handler, IoExecutor>)(
  174. o->work_));
  175. // Make a copy of the handler so that the memory can be deallocated before
  176. // the upcall is made. Even if we're not about to make an upcall, a
  177. // sub-object of the handler may be the true owner of the memory associated
  178. // with the handler. Consequently, a local copy of the handler is required
  179. // to ensure that any owning sub-object remains valid until after we have
  180. // deallocated the memory here.
  181. detail::move_binder2<Handler,
  182. asio::error_code, peer_socket_type>
  183. handler(0, ASIO_MOVE_CAST(Handler)(o->handler_), o->ec_,
  184. ASIO_MOVE_CAST(peer_socket_type)(*o));
  185. p.h = asio::detail::addressof(handler.handler_);
  186. p.reset();
  187. // Make the upcall if required.
  188. if (owner)
  189. {
  190. fenced_block b(fenced_block::half);
  191. ASIO_HANDLER_INVOCATION_BEGIN((handler.arg1_, "..."));
  192. w.complete(handler, handler.handler_);
  193. ASIO_HANDLER_INVOCATION_END;
  194. }
  195. }
  196. private:
  197. typedef typename Protocol::socket::template
  198. rebind_executor<PeerIoExecutor>::other peer_socket_type;
  199. Handler handler_;
  200. handler_work<Handler, IoExecutor> work_;
  201. };
  202. #endif // defined(ASIO_HAS_MOVE)
  203. } // namespace detail
  204. } // namespace asio
  205. #include "asio/detail/pop_options.hpp"
  206. #endif // ASIO_DETAIL_REACTIVE_SOCKET_ACCEPT_OP_HPP