1//
2// detail/reactive_socket_accept_op.hpp
3// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4//
5// Copyright (c) 2003-2015 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
11#ifndef BOOST_ASIO_DETAIL_REACTIVE_SOCKET_ACCEPT_OP_HPP
12#define BOOST_ASIO_DETAIL_REACTIVE_SOCKET_ACCEPT_OP_HPP
13
14#if defined(_MSC_VER) && (_MSC_VER >= 1200)
15# pragma once
16#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
17
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>
26
27#include <boost/asio/detail/push_options.hpp>
28
29namespace boost {
30namespace asio {
31namespace detail {
32
33template <typename Socket, typename Protocol>
34class reactive_socket_accept_op_base : public reactor_op
35{
36public:
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),
41 socket_(socket),
42 state_(state),
43 peer_(peer),
44 protocol_(protocol),
45 peer_endpoint_(peer_endpoint)
46 {
47 }
48
49 static bool do_perform(reactor_op* base)
50 {
51 reactive_socket_accept_op_base* o(
52 static_cast<reactive_socket_accept_op_base*>(base));
53
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(s: o->socket_,
57 state: o->state_, addr: o->peer_endpoint_ ? o->peer_endpoint_->data() : 0,
58 addrlen: o->peer_endpoint_ ? &addrlen : 0, ec&: o->ec_, new_socket);
59
60 // On success, assign new connection to peer socket object.
61 if (new_socket != invalid_socket)
62 {
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();
68 }
69
70 return result;
71 }
72
73private:
74 socket_type socket_;
75 socket_ops::state_type state_;
76 Socket& peer_;
77 Protocol protocol_;
78 typename Protocol::endpoint* peer_endpoint_;
79};
80
81template <typename Socket, typename Protocol, typename Handler>
82class reactive_socket_accept_op :
83 public reactive_socket_accept_op_base<Socket, Protocol>
84{
85public:
86 BOOST_ASIO_DEFINE_HANDLER_PTR(reactive_socket_accept_op);
87
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))
94 {
95 }
96
97 static void do_complete(io_service_impl* owner, operation* base,
98 const boost::system::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 = { boost::asio::detail::addressof(o->handler_), o, o };
104
105 BOOST_ASIO_HANDLER_COMPLETION((o));
106
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_);
116 p.reset();
117
118 // Make the upcall if required.
119 if (owner)
120 {
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;
125 }
126 }
127
128private:
129 Handler handler_;
130};
131
132} // namespace detail
133} // namespace asio
134} // namespace boost
135
136#include <boost/asio/detail/pop_options.hpp>
137
138#endif // BOOST_ASIO_DETAIL_REACTIVE_SOCKET_ACCEPT_OP_HPP
139

source code of boost/boost/asio/detail/reactive_socket_accept_op.hpp