reactive_socket_accept_op.hpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. //
  2. // detail/reactive_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_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, typename Handler>
  77. class reactive_socket_accept_op :
  78. public reactive_socket_accept_op_base<Socket, Protocol>
  79. {
  80. public:
  81. BOOST_ASIO_DEFINE_HANDLER_PTR(reactive_socket_accept_op);
  82. reactive_socket_accept_op(socket_type socket,
  83. socket_ops::state_type state, Socket& peer, const Protocol& protocol,
  84. typename Protocol::endpoint* peer_endpoint, Handler& handler)
  85. : reactive_socket_accept_op_base<Socket, Protocol>(socket, state, peer,
  86. protocol, peer_endpoint, &reactive_socket_accept_op::do_complete),
  87. handler_(BOOST_ASIO_MOVE_CAST(Handler)(handler))
  88. {
  89. handler_work<Handler>::start(handler_);
  90. }
  91. static void do_complete(void* owner, operation* base,
  92. const boost::system::error_code& /*ec*/,
  93. std::size_t /*bytes_transferred*/)
  94. {
  95. // Take ownership of the handler object.
  96. reactive_socket_accept_op* o(static_cast<reactive_socket_accept_op*>(base));
  97. ptr p = { boost::asio::detail::addressof(o->handler_), o, o };
  98. handler_work<Handler> w(o->handler_);
  99. // On success, assign new connection to peer socket object.
  100. if (owner)
  101. o->do_assign();
  102. BOOST_ASIO_HANDLER_COMPLETION((*o));
  103. // Make a copy of the handler so that the memory can be deallocated before
  104. // the upcall is made. Even if we're not about to make an upcall, a
  105. // sub-object of the handler may be the true owner of the memory associated
  106. // with the handler. Consequently, a local copy of the handler is required
  107. // to ensure that any owning sub-object remains valid until after we have
  108. // deallocated the memory here.
  109. detail::binder1<Handler, boost::system::error_code>
  110. handler(o->handler_, o->ec_);
  111. p.h = boost::asio::detail::addressof(handler.handler_);
  112. p.reset();
  113. // Make the upcall if required.
  114. if (owner)
  115. {
  116. fenced_block b(fenced_block::half);
  117. BOOST_ASIO_HANDLER_INVOCATION_BEGIN((handler.arg1_));
  118. w.complete(handler, handler.handler_);
  119. BOOST_ASIO_HANDLER_INVOCATION_END;
  120. }
  121. }
  122. private:
  123. Handler handler_;
  124. };
  125. #if defined(BOOST_ASIO_HAS_MOVE)
  126. template <typename Protocol, typename Handler>
  127. class reactive_socket_move_accept_op :
  128. private Protocol::socket,
  129. public reactive_socket_accept_op_base<typename Protocol::socket, Protocol>
  130. {
  131. public:
  132. BOOST_ASIO_DEFINE_HANDLER_PTR(reactive_socket_move_accept_op);
  133. reactive_socket_move_accept_op(io_context& ioc, socket_type socket,
  134. socket_ops::state_type state, const Protocol& protocol,
  135. typename Protocol::endpoint* peer_endpoint, Handler& handler)
  136. : Protocol::socket(ioc),
  137. reactive_socket_accept_op_base<typename Protocol::socket, Protocol>(
  138. socket, state, *this, protocol, peer_endpoint,
  139. &reactive_socket_move_accept_op::do_complete),
  140. handler_(BOOST_ASIO_MOVE_CAST(Handler)(handler))
  141. {
  142. handler_work<Handler>::start(handler_);
  143. }
  144. static void do_complete(void* owner, operation* base,
  145. const boost::system::error_code& /*ec*/,
  146. std::size_t /*bytes_transferred*/)
  147. {
  148. // Take ownership of the handler object.
  149. reactive_socket_move_accept_op* o(
  150. static_cast<reactive_socket_move_accept_op*>(base));
  151. ptr p = { boost::asio::detail::addressof(o->handler_), o, o };
  152. handler_work<Handler> w(o->handler_);
  153. // On success, assign new connection to peer socket object.
  154. if (owner)
  155. o->do_assign();
  156. BOOST_ASIO_HANDLER_COMPLETION((*o));
  157. // Make a copy of the handler so that the memory can be deallocated before
  158. // the upcall is made. Even if we're not about to make an upcall, a
  159. // sub-object of the handler may be the true owner of the memory associated
  160. // with the handler. Consequently, a local copy of the handler is required
  161. // to ensure that any owning sub-object remains valid until after we have
  162. // deallocated the memory here.
  163. detail::move_binder2<Handler,
  164. boost::system::error_code, typename Protocol::socket>
  165. handler(0, BOOST_ASIO_MOVE_CAST(Handler)(o->handler_), o->ec_,
  166. BOOST_ASIO_MOVE_CAST(typename Protocol::socket)(*o));
  167. p.h = boost::asio::detail::addressof(handler.handler_);
  168. p.reset();
  169. // Make the upcall if required.
  170. if (owner)
  171. {
  172. fenced_block b(fenced_block::half);
  173. BOOST_ASIO_HANDLER_INVOCATION_BEGIN((handler.arg1_, "..."));
  174. w.complete(handler, handler.handler_);
  175. BOOST_ASIO_HANDLER_INVOCATION_END;
  176. }
  177. }
  178. private:
  179. Handler handler_;
  180. };
  181. #endif // defined(BOOST_ASIO_HAS_MOVE)
  182. } // namespace detail
  183. } // namespace asio
  184. } // namespace boost
  185. #include <boost/asio/detail/pop_options.hpp>
  186. #endif // BOOST_ASIO_DETAIL_REACTIVE_SOCKET_ACCEPT_OP_HPP