2 // detail/reactive_socket_accept_op.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2016 Christopher M. Kohlhoff (chris at kohlhoff dot com)
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)
11 #ifndef BOOST_ASIO_DETAIL_REACTIVE_SOCKET_ACCEPT_OP_HPP
12 #define BOOST_ASIO_DETAIL_REACTIVE_SOCKET_ACCEPT_OP_HPP
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
18 #include <boost/asio/detail/config.hpp>
19 #include <boost/asio/detail/addressof.hpp>
20 #include <boost/asio/detail/bind_handler.hpp>
21 #include <boost/asio/detail/buffer_sequence_adapter.hpp>
22 #include <boost/asio/detail/fenced_block.hpp>
23 #include <boost/asio/detail/reactor_op.hpp>
24 #include <boost/asio/detail/socket_holder.hpp>
25 #include <boost/asio/detail/socket_ops.hpp>
27 #include <boost/asio/detail/push_options.hpp>
33 template <typename Socket, typename Protocol>
34 class reactive_socket_accept_op_base : public reactor_op
37 reactive_socket_accept_op_base(socket_type socket,
38 socket_ops::state_type state, Socket& peer, const Protocol& protocol,
39 typename Protocol::endpoint* peer_endpoint, func_type complete_func)
40 : reactor_op(&reactive_socket_accept_op_base::do_perform, complete_func),
45 peer_endpoint_(peer_endpoint)
49 static bool do_perform(reactor_op* base)
51 reactive_socket_accept_op_base* o(
52 static_cast<reactive_socket_accept_op_base*>(base));
54 std::size_t addrlen = o->peer_endpoint_ ? o->peer_endpoint_->capacity() : 0;
55 socket_type new_socket = invalid_socket;
56 bool result = socket_ops::non_blocking_accept(o->socket_,
57 o->state_, o->peer_endpoint_ ? o->peer_endpoint_->data() : 0,
58 o->peer_endpoint_ ? &addrlen : 0, o->ec_, new_socket);
60 // On success, assign new connection to peer socket object.
61 if (new_socket != invalid_socket)
63 socket_holder new_socket_holder(new_socket);
64 if (o->peer_endpoint_)
65 o->peer_endpoint_->resize(addrlen);
66 if (!o->peer_.assign(o->protocol_, new_socket, o->ec_))
67 new_socket_holder.release();
75 socket_ops::state_type state_;
78 typename Protocol::endpoint* peer_endpoint_;
81 template <typename Socket, typename Protocol, typename Handler>
82 class reactive_socket_accept_op :
83 public reactive_socket_accept_op_base<Socket, Protocol>
86 BOOST_ASIO_DEFINE_HANDLER_PTR(reactive_socket_accept_op);
88 reactive_socket_accept_op(socket_type socket,
89 socket_ops::state_type state, Socket& peer, const Protocol& protocol,
90 typename Protocol::endpoint* peer_endpoint, Handler& handler)
91 : reactive_socket_accept_op_base<Socket, Protocol>(socket, state, peer,
92 protocol, peer_endpoint, &reactive_socket_accept_op::do_complete),
93 handler_(BOOST_ASIO_MOVE_CAST(Handler)(handler))
97 static void do_complete(io_service_impl* owner, operation* base,
98 const boost::system::error_code& /*ec*/,
99 std::size_t /*bytes_transferred*/)
101 // Take ownership of the handler object.
102 reactive_socket_accept_op* o(static_cast<reactive_socket_accept_op*>(base));
103 ptr p = { boost::asio::detail::addressof(o->handler_), o, o };
105 BOOST_ASIO_HANDLER_COMPLETION((o));
107 // Make a copy of the handler so that the memory can be deallocated before
108 // the upcall is made. Even if we're not about to make an upcall, a
109 // sub-object of the handler may be the true owner of the memory associated
110 // with the handler. Consequently, a local copy of the handler is required
111 // to ensure that any owning sub-object remains valid until after we have
112 // deallocated the memory here.
113 detail::binder1<Handler, boost::system::error_code>
114 handler(o->handler_, o->ec_);
115 p.h = boost::asio::detail::addressof(handler.handler_);
118 // Make the upcall if required.
121 fenced_block b(fenced_block::half);
122 BOOST_ASIO_HANDLER_INVOCATION_BEGIN((handler.arg1_));
123 boost_asio_handler_invoke_helpers::invoke(handler, handler.handler_);
124 BOOST_ASIO_HANDLER_INVOCATION_END;
132 } // namespace detail
136 #include <boost/asio/detail/pop_options.hpp>
138 #endif // BOOST_ASIO_DETAIL_REACTIVE_SOCKET_ACCEPT_OP_HPP