reactive_socket_accept_op.hpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. //
  2. // detail/reactive_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_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/buffer_sequence_adapter.hpp>
  18. #include <boost/asio/detail/fenced_block.hpp>
  19. #include <boost/asio/detail/memory.hpp>
  20. #include <boost/asio/detail/reactor_op.hpp>
  21. #include <boost/asio/detail/socket_holder.hpp>
  22. #include <boost/asio/detail/socket_ops.hpp>
  23. #include <boost/asio/detail/push_options.hpp>
  24. namespace boost {
  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(socket_type socket,
  32. socket_ops::state_type state, Socket& peer, const Protocol& protocol,
  33. typename Protocol::endpoint* peer_endpoint, func_type complete_func)
  34. : reactor_op(&reactive_socket_accept_op_base::do_perform, complete_func),
  35. socket_(socket),
  36. state_(state),
  37. peer_(peer),
  38. protocol_(protocol),
  39. peer_endpoint_(peer_endpoint),
  40. addrlen_(peer_endpoint ? peer_endpoint->capacity() : 0)
  41. {
  42. }
  43. static status do_perform(reactor_op* base)
  44. {
  45. reactive_socket_accept_op_base* o(
  46. static_cast<reactive_socket_accept_op_base*>(base));
  47. socket_type new_socket = invalid_socket;
  48. status result = socket_ops::non_blocking_accept(o->socket_,
  49. o->state_, o->peer_endpoint_ ? o->peer_endpoint_->data() : 0,
  50. o->peer_endpoint_ ? &o->addrlen_ : 0, o->ec_, new_socket)
  51. ? done : not_done;
  52. o->new_socket_.reset(new_socket);
  53. BOOST_ASIO_HANDLER_REACTOR_OPERATION((*o, "non_blocking_accept", o->ec_));
  54. return result;
  55. }
  56. void do_assign()
  57. {
  58. if (new_socket_.get() != invalid_socket)
  59. {
  60. if (peer_endpoint_)
  61. peer_endpoint_->resize(addrlen_);
  62. peer_.assign(protocol_, new_socket_.get(), ec_);
  63. if (!ec_)
  64. new_socket_.release();
  65. }
  66. }
  67. private:
  68. socket_type socket_;
  69. socket_ops::state_type state_;
  70. socket_holder new_socket_;
  71. Socket& peer_;
  72. Protocol protocol_;
  73. typename Protocol::endpoint* peer_endpoint_;
  74. std::size_t addrlen_;
  75. };
  76. template <typename Socket, typename Protocol,
  77. typename Handler, typename IoExecutor>
  78. class reactive_socket_accept_op :
  79. public reactive_socket_accept_op_base<Socket, Protocol>
  80. {
  81. public:
  82. BOOST_ASIO_DEFINE_HANDLER_PTR(reactive_socket_accept_op);
  83. reactive_socket_accept_op(socket_type socket,
  84. socket_ops::state_type state, Socket& peer, const Protocol& protocol,
  85. typename Protocol::endpoint* peer_endpoint, Handler& handler,
  86. const IoExecutor& io_ex)
  87. : reactive_socket_accept_op_base<Socket, Protocol>(socket, state, peer,
  88. protocol, peer_endpoint, &reactive_socket_accept_op::do_complete),
  89. handler_(BOOST_ASIO_MOVE_CAST(Handler)(handler)),
  90. io_executor_(io_ex)
  91. {
  92. handler_work<Handler, IoExecutor>::start(handler_, io_executor_);
  93. }
  94. static void do_complete(void* owner, operation* base,
  95. const boost::system::error_code& /*ec*/,
  96. std::size_t /*bytes_transferred*/)
  97. {
  98. // Take ownership of the handler object.
  99. reactive_socket_accept_op* o(static_cast<reactive_socket_accept_op*>(base));
  100. ptr p = { boost::asio::detail::addressof(o->handler_), o, o };
  101. handler_work<Handler, IoExecutor> w(o->handler_, o->io_executor_);
  102. // On success, assign new connection to peer socket object.
  103. if (owner)
  104. o->do_assign();
  105. BOOST_ASIO_HANDLER_COMPLETION((*o));
  106. // Make a copy of the handler so that the memory can be deallocated before
  107. // the upcall is made. Even if we're not about to make an upcall, a
  108. // sub-object of the handler may be the true owner of the memory associated
  109. // with the handler. Consequently, a local copy of the handler is required
  110. // to ensure that any owning sub-object remains valid until after we have
  111. // deallocated the memory here.
  112. detail::binder1<Handler, boost::system::error_code>
  113. handler(o->handler_, o->ec_);
  114. p.h = boost::asio::detail::addressof(handler.handler_);
  115. p.reset();
  116. // Make the upcall if required.
  117. if (owner)
  118. {
  119. fenced_block b(fenced_block::half);
  120. BOOST_ASIO_HANDLER_INVOCATION_BEGIN((handler.arg1_));
  121. w.complete(handler, handler.handler_);
  122. BOOST_ASIO_HANDLER_INVOCATION_END;
  123. }
  124. }
  125. private:
  126. Handler handler_;
  127. IoExecutor io_executor_;
  128. };
  129. #if defined(BOOST_ASIO_HAS_MOVE)
  130. template <typename Protocol, typename PeerIoExecutor,
  131. typename Handler, typename IoExecutor>
  132. class reactive_socket_move_accept_op :
  133. private Protocol::socket::template rebind_executor<PeerIoExecutor>::other,
  134. public reactive_socket_accept_op_base<
  135. typename Protocol::socket::template rebind_executor<PeerIoExecutor>::other,
  136. Protocol>
  137. {
  138. public:
  139. BOOST_ASIO_DEFINE_HANDLER_PTR(reactive_socket_move_accept_op);
  140. reactive_socket_move_accept_op(const PeerIoExecutor& peer_io_ex,
  141. socket_type socket, socket_ops::state_type state,
  142. const Protocol& protocol, typename Protocol::endpoint* peer_endpoint,
  143. Handler& handler, const IoExecutor& io_ex)
  144. : peer_socket_type(peer_io_ex),
  145. reactive_socket_accept_op_base<peer_socket_type, Protocol>(
  146. socket, state, *this, protocol, peer_endpoint,
  147. &reactive_socket_move_accept_op::do_complete),
  148. handler_(BOOST_ASIO_MOVE_CAST(Handler)(handler)),
  149. io_executor_(io_ex)
  150. {
  151. handler_work<Handler, IoExecutor>::start(handler_, io_executor_);
  152. }
  153. static void do_complete(void* owner, operation* base,
  154. const boost::system::error_code& /*ec*/,
  155. std::size_t /*bytes_transferred*/)
  156. {
  157. // Take ownership of the handler object.
  158. reactive_socket_move_accept_op* o(
  159. static_cast<reactive_socket_move_accept_op*>(base));
  160. ptr p = { boost::asio::detail::addressof(o->handler_), o, o };
  161. handler_work<Handler, IoExecutor> w(o->handler_, o->io_executor_);
  162. // On success, assign new connection to peer socket object.
  163. if (owner)
  164. o->do_assign();
  165. BOOST_ASIO_HANDLER_COMPLETION((*o));
  166. // Make a copy of the handler so that the memory can be deallocated before
  167. // the upcall is made. Even if we're not about to make an upcall, a
  168. // sub-object of the handler may be the true owner of the memory associated
  169. // with the handler. Consequently, a local copy of the handler is required
  170. // to ensure that any owning sub-object remains valid until after we have
  171. // deallocated the memory here.
  172. detail::move_binder2<Handler,
  173. boost::system::error_code, peer_socket_type>
  174. handler(0, BOOST_ASIO_MOVE_CAST(Handler)(o->handler_), o->ec_,
  175. BOOST_ASIO_MOVE_CAST(peer_socket_type)(*o));
  176. p.h = boost::asio::detail::addressof(handler.handler_);
  177. p.reset();
  178. // Make the upcall if required.
  179. if (owner)
  180. {
  181. fenced_block b(fenced_block::half);
  182. BOOST_ASIO_HANDLER_INVOCATION_BEGIN((handler.arg1_, "..."));
  183. w.complete(handler, handler.handler_);
  184. BOOST_ASIO_HANDLER_INVOCATION_END;
  185. }
  186. }
  187. private:
  188. typedef typename Protocol::socket::template
  189. rebind_executor<PeerIoExecutor>::other peer_socket_type;
  190. Handler handler_;
  191. IoExecutor io_executor_;
  192. };
  193. #endif // defined(BOOST_ASIO_HAS_MOVE)
  194. } // namespace detail
  195. } // namespace asio
  196. } // namespace boost
  197. #include <boost/asio/detail/pop_options.hpp>
  198. #endif // BOOST_ASIO_DETAIL_REACTIVE_SOCKET_ACCEPT_OP_HPP