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