reactive_socket_accept_op.hpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. //
  2. // detail/reactive_socket_accept_op.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2013 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/addressof.hpp>
  17. #include <boost/asio/detail/bind_handler.hpp>
  18. #include <boost/asio/detail/buffer_sequence_adapter.hpp>
  19. #include <boost/asio/detail/fenced_block.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. {
  41. }
  42. static bool do_perform(reactor_op* base)
  43. {
  44. reactive_socket_accept_op_base* o(
  45. static_cast<reactive_socket_accept_op_base*>(base));
  46. std::size_t addrlen = o->peer_endpoint_ ? o->peer_endpoint_->capacity() : 0;
  47. socket_type new_socket = invalid_socket;
  48. bool result = socket_ops::non_blocking_accept(o->socket_,
  49. o->state_, o->peer_endpoint_ ? o->peer_endpoint_->data() : 0,
  50. o->peer_endpoint_ ? &addrlen : 0, o->ec_, new_socket);
  51. // On success, assign new connection to peer socket object.
  52. if (new_socket != invalid_socket)
  53. {
  54. socket_holder new_socket_holder(new_socket);
  55. if (o->peer_endpoint_)
  56. o->peer_endpoint_->resize(addrlen);
  57. if (!o->peer_.assign(o->protocol_, new_socket, o->ec_))
  58. new_socket_holder.release();
  59. }
  60. return result;
  61. }
  62. private:
  63. socket_type socket_;
  64. socket_ops::state_type state_;
  65. Socket& peer_;
  66. Protocol protocol_;
  67. typename Protocol::endpoint* peer_endpoint_;
  68. };
  69. template <typename Socket, typename Protocol, typename Handler>
  70. class reactive_socket_accept_op :
  71. public reactive_socket_accept_op_base<Socket, Protocol>
  72. {
  73. public:
  74. BOOST_ASIO_DEFINE_HANDLER_PTR(reactive_socket_accept_op);
  75. reactive_socket_accept_op(socket_type socket,
  76. socket_ops::state_type state, Socket& peer, const Protocol& protocol,
  77. typename Protocol::endpoint* peer_endpoint, Handler& handler)
  78. : reactive_socket_accept_op_base<Socket, Protocol>(socket, state, peer,
  79. protocol, peer_endpoint, &reactive_socket_accept_op::do_complete),
  80. handler_(BOOST_ASIO_MOVE_CAST(Handler)(handler))
  81. {
  82. }
  83. static void do_complete(io_service_impl* owner, operation* base,
  84. const boost::system::error_code& /*ec*/,
  85. std::size_t /*bytes_transferred*/)
  86. {
  87. // Take ownership of the handler object.
  88. reactive_socket_accept_op* o(static_cast<reactive_socket_accept_op*>(base));
  89. ptr p = { boost::asio::detail::addressof(o->handler_), o, o };
  90. BOOST_ASIO_HANDLER_COMPLETION((o));
  91. // Make a copy of the handler so that the memory can be deallocated before
  92. // the upcall is made. Even if we're not about to make an upcall, a
  93. // sub-object of the handler may be the true owner of the memory associated
  94. // with the handler. Consequently, a local copy of the handler is required
  95. // to ensure that any owning sub-object remains valid until after we have
  96. // deallocated the memory here.
  97. detail::binder1<Handler, boost::system::error_code>
  98. handler(o->handler_, o->ec_);
  99. p.h = boost::asio::detail::addressof(handler.handler_);
  100. p.reset();
  101. // Make the upcall if required.
  102. if (owner)
  103. {
  104. fenced_block b(fenced_block::half);
  105. BOOST_ASIO_HANDLER_INVOCATION_BEGIN((handler.arg1_));
  106. boost_asio_handler_invoke_helpers::invoke(handler, handler.handler_);
  107. BOOST_ASIO_HANDLER_INVOCATION_END;
  108. }
  109. }
  110. private:
  111. Handler handler_;
  112. };
  113. } // namespace detail
  114. } // namespace asio
  115. } // namespace boost
  116. #include <boost/asio/detail/pop_options.hpp>
  117. #endif // BOOST_ASIO_DETAIL_REACTIVE_SOCKET_ACCEPT_OP_HPP