]>
Commit | Line | Data |
---|---|---|
1 | // | |
2 | // impl/connect.hpp | |
3 | // ~~~~~~~~~~~~~~~~ | |
4 | // | |
5 | // Copyright (c) 2003-2016 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_IMPL_CONNECT_HPP | |
12 | #define BOOST_ASIO_IMPL_CONNECT_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/bind_handler.hpp> | |
19 | #include <boost/asio/detail/consuming_buffers.hpp> | |
20 | #include <boost/asio/detail/handler_alloc_helpers.hpp> | |
21 | #include <boost/asio/detail/handler_cont_helpers.hpp> | |
22 | #include <boost/asio/detail/handler_invoke_helpers.hpp> | |
23 | #include <boost/asio/detail/handler_type_requirements.hpp> | |
24 | #include <boost/asio/detail/throw_error.hpp> | |
25 | #include <boost/asio/error.hpp> | |
26 | ||
27 | #include <boost/asio/detail/push_options.hpp> | |
28 | ||
29 | namespace boost { | |
30 | namespace asio { | |
31 | ||
32 | namespace detail | |
33 | { | |
34 | struct default_connect_condition | |
35 | { | |
36 | template <typename Iterator> | |
37 | Iterator operator()(const boost::system::error_code&, Iterator next) | |
38 | { | |
39 | return next; | |
40 | } | |
41 | }; | |
42 | } | |
43 | ||
44 | template <typename Protocol, typename SocketService, typename Iterator> | |
45 | Iterator connect(basic_socket<Protocol, SocketService>& s, Iterator begin) | |
46 | { | |
47 | boost::system::error_code ec; | |
48 | Iterator result = connect(s, begin, ec); | |
49 | boost::asio::detail::throw_error(ec, "connect"); | |
50 | return result; | |
51 | } | |
52 | ||
53 | template <typename Protocol, typename SocketService, typename Iterator> | |
54 | inline Iterator connect(basic_socket<Protocol, SocketService>& s, | |
55 | Iterator begin, boost::system::error_code& ec) | |
56 | { | |
57 | return connect(s, begin, Iterator(), detail::default_connect_condition(), ec); | |
58 | } | |
59 | ||
60 | template <typename Protocol, typename SocketService, typename Iterator> | |
61 | Iterator connect(basic_socket<Protocol, SocketService>& s, | |
62 | Iterator begin, Iterator end) | |
63 | { | |
64 | boost::system::error_code ec; | |
65 | Iterator result = connect(s, begin, end, ec); | |
66 | boost::asio::detail::throw_error(ec, "connect"); | |
67 | return result; | |
68 | } | |
69 | ||
70 | template <typename Protocol, typename SocketService, typename Iterator> | |
71 | inline Iterator connect(basic_socket<Protocol, SocketService>& s, | |
72 | Iterator begin, Iterator end, boost::system::error_code& ec) | |
73 | { | |
74 | return connect(s, begin, end, detail::default_connect_condition(), ec); | |
75 | } | |
76 | ||
77 | template <typename Protocol, typename SocketService, | |
78 | typename Iterator, typename ConnectCondition> | |
79 | Iterator connect(basic_socket<Protocol, SocketService>& s, | |
80 | Iterator begin, ConnectCondition connect_condition) | |
81 | { | |
82 | boost::system::error_code ec; | |
83 | Iterator result = connect(s, begin, connect_condition, ec); | |
84 | boost::asio::detail::throw_error(ec, "connect"); | |
85 | return result; | |
86 | } | |
87 | ||
88 | template <typename Protocol, typename SocketService, | |
89 | typename Iterator, typename ConnectCondition> | |
90 | inline Iterator connect(basic_socket<Protocol, SocketService>& s, | |
91 | Iterator begin, ConnectCondition connect_condition, | |
92 | boost::system::error_code& ec) | |
93 | { | |
94 | return connect(s, begin, Iterator(), connect_condition, ec); | |
95 | } | |
96 | ||
97 | template <typename Protocol, typename SocketService, | |
98 | typename Iterator, typename ConnectCondition> | |
99 | Iterator connect(basic_socket<Protocol, SocketService>& s, | |
100 | Iterator begin, Iterator end, ConnectCondition connect_condition) | |
101 | { | |
102 | boost::system::error_code ec; | |
103 | Iterator result = connect(s, begin, end, connect_condition, ec); | |
104 | boost::asio::detail::throw_error(ec, "connect"); | |
105 | return result; | |
106 | } | |
107 | ||
108 | template <typename Protocol, typename SocketService, | |
109 | typename Iterator, typename ConnectCondition> | |
110 | Iterator connect(basic_socket<Protocol, SocketService>& s, | |
111 | Iterator begin, Iterator end, ConnectCondition connect_condition, | |
112 | boost::system::error_code& ec) | |
113 | { | |
114 | ec = boost::system::error_code(); | |
115 | ||
116 | for (Iterator iter = begin; iter != end; ++iter) | |
117 | { | |
118 | iter = connect_condition(ec, iter); | |
119 | if (iter != end) | |
120 | { | |
121 | s.close(ec); | |
122 | s.connect(*iter, ec); | |
123 | if (!ec) | |
124 | return iter; | |
125 | } | |
126 | else | |
127 | break; | |
128 | } | |
129 | ||
130 | if (!ec) | |
131 | ec = boost::asio::error::not_found; | |
132 | ||
133 | return end; | |
134 | } | |
135 | ||
136 | namespace detail | |
137 | { | |
138 | // Enable the empty base class optimisation for the connect condition. | |
139 | template <typename ConnectCondition> | |
140 | class base_from_connect_condition | |
141 | { | |
142 | protected: | |
143 | explicit base_from_connect_condition( | |
144 | const ConnectCondition& connect_condition) | |
145 | : connect_condition_(connect_condition) | |
146 | { | |
147 | } | |
148 | ||
149 | template <typename Iterator> | |
150 | void check_condition(const boost::system::error_code& ec, | |
151 | Iterator& iter, Iterator& end) | |
152 | { | |
153 | if (iter != end) | |
154 | iter = connect_condition_(ec, static_cast<const Iterator&>(iter)); | |
155 | } | |
156 | ||
157 | private: | |
158 | ConnectCondition connect_condition_; | |
159 | }; | |
160 | ||
161 | // The default_connect_condition implementation is essentially a no-op. This | |
162 | // template specialisation lets us eliminate all costs associated with it. | |
163 | template <> | |
164 | class base_from_connect_condition<default_connect_condition> | |
165 | { | |
166 | protected: | |
167 | explicit base_from_connect_condition(const default_connect_condition&) | |
168 | { | |
169 | } | |
170 | ||
171 | template <typename Iterator> | |
172 | void check_condition(const boost::system::error_code&, Iterator&, Iterator&) | |
173 | { | |
174 | } | |
175 | }; | |
176 | ||
177 | template <typename Protocol, typename SocketService, typename Iterator, | |
178 | typename ConnectCondition, typename ComposedConnectHandler> | |
179 | class connect_op : base_from_connect_condition<ConnectCondition> | |
180 | { | |
181 | public: | |
182 | connect_op(basic_socket<Protocol, SocketService>& sock, | |
183 | const Iterator& begin, const Iterator& end, | |
184 | const ConnectCondition& connect_condition, | |
185 | ComposedConnectHandler& handler) | |
186 | : base_from_connect_condition<ConnectCondition>(connect_condition), | |
187 | socket_(sock), | |
188 | iter_(begin), | |
189 | end_(end), | |
190 | start_(0), | |
191 | handler_(BOOST_ASIO_MOVE_CAST(ComposedConnectHandler)(handler)) | |
192 | { | |
193 | } | |
194 | ||
195 | #if defined(BOOST_ASIO_HAS_MOVE) | |
196 | connect_op(const connect_op& other) | |
197 | : base_from_connect_condition<ConnectCondition>(other), | |
198 | socket_(other.socket_), | |
199 | iter_(other.iter_), | |
200 | end_(other.end_), | |
201 | start_(other.start_), | |
202 | handler_(other.handler_) | |
203 | { | |
204 | } | |
205 | ||
206 | connect_op(connect_op&& other) | |
207 | : base_from_connect_condition<ConnectCondition>(other), | |
208 | socket_(other.socket_), | |
209 | iter_(other.iter_), | |
210 | end_(other.end_), | |
211 | start_(other.start_), | |
212 | handler_(BOOST_ASIO_MOVE_CAST(ComposedConnectHandler)(other.handler_)) | |
213 | { | |
214 | } | |
215 | #endif // defined(BOOST_ASIO_HAS_MOVE) | |
216 | ||
217 | void operator()(boost::system::error_code ec, int start = 0) | |
218 | { | |
219 | switch (start_ = start) | |
220 | { | |
221 | case 1: | |
222 | for (;;) | |
223 | { | |
224 | this->check_condition(ec, iter_, end_); | |
225 | ||
226 | if (iter_ != end_) | |
227 | { | |
228 | socket_.close(ec); | |
229 | socket_.async_connect(*iter_, | |
230 | BOOST_ASIO_MOVE_CAST(connect_op)(*this)); | |
231 | return; | |
232 | } | |
233 | ||
234 | if (start) | |
235 | { | |
236 | ec = boost::asio::error::not_found; | |
237 | socket_.get_io_service().post(detail::bind_handler(*this, ec)); | |
238 | return; | |
239 | } | |
240 | ||
241 | default: | |
242 | ||
243 | if (iter_ == end_) | |
244 | break; | |
245 | ||
246 | if (!socket_.is_open()) | |
247 | { | |
248 | ec = boost::asio::error::operation_aborted; | |
249 | break; | |
250 | } | |
251 | ||
252 | if (!ec) | |
253 | break; | |
254 | ||
255 | ++iter_; | |
256 | } | |
257 | ||
258 | handler_(static_cast<const boost::system::error_code&>(ec), | |
259 | static_cast<const Iterator&>(iter_)); | |
260 | } | |
261 | } | |
262 | ||
263 | //private: | |
264 | basic_socket<Protocol, SocketService>& socket_; | |
265 | Iterator iter_; | |
266 | Iterator end_; | |
267 | int start_; | |
268 | ComposedConnectHandler handler_; | |
269 | }; | |
270 | ||
271 | template <typename Protocol, typename SocketService, typename Iterator, | |
272 | typename ConnectCondition, typename ComposedConnectHandler> | |
273 | inline void* asio_handler_allocate(std::size_t size, | |
274 | connect_op<Protocol, SocketService, Iterator, | |
275 | ConnectCondition, ComposedConnectHandler>* this_handler) | |
276 | { | |
277 | return boost_asio_handler_alloc_helpers::allocate( | |
278 | size, this_handler->handler_); | |
279 | } | |
280 | ||
281 | template <typename Protocol, typename SocketService, typename Iterator, | |
282 | typename ConnectCondition, typename ComposedConnectHandler> | |
283 | inline void asio_handler_deallocate(void* pointer, std::size_t size, | |
284 | connect_op<Protocol, SocketService, Iterator, | |
285 | ConnectCondition, ComposedConnectHandler>* this_handler) | |
286 | { | |
287 | boost_asio_handler_alloc_helpers::deallocate( | |
288 | pointer, size, this_handler->handler_); | |
289 | } | |
290 | ||
291 | template <typename Protocol, typename SocketService, typename Iterator, | |
292 | typename ConnectCondition, typename ComposedConnectHandler> | |
293 | inline bool asio_handler_is_continuation( | |
294 | connect_op<Protocol, SocketService, Iterator, | |
295 | ConnectCondition, ComposedConnectHandler>* this_handler) | |
296 | { | |
297 | return boost_asio_handler_cont_helpers::is_continuation( | |
298 | this_handler->handler_); | |
299 | } | |
300 | ||
301 | template <typename Function, typename Protocol, | |
302 | typename SocketService, typename Iterator, | |
303 | typename ConnectCondition, typename ComposedConnectHandler> | |
304 | inline void asio_handler_invoke(Function& function, | |
305 | connect_op<Protocol, SocketService, Iterator, | |
306 | ConnectCondition, ComposedConnectHandler>* this_handler) | |
307 | { | |
308 | boost_asio_handler_invoke_helpers::invoke( | |
309 | function, this_handler->handler_); | |
310 | } | |
311 | ||
312 | template <typename Function, typename Protocol, | |
313 | typename SocketService, typename Iterator, | |
314 | typename ConnectCondition, typename ComposedConnectHandler> | |
315 | inline void asio_handler_invoke(const Function& function, | |
316 | connect_op<Protocol, SocketService, Iterator, | |
317 | ConnectCondition, ComposedConnectHandler>* this_handler) | |
318 | { | |
319 | boost_asio_handler_invoke_helpers::invoke( | |
320 | function, this_handler->handler_); | |
321 | } | |
322 | } // namespace detail | |
323 | ||
324 | template <typename Protocol, typename SocketService, | |
325 | typename Iterator, typename ComposedConnectHandler> | |
326 | inline BOOST_ASIO_INITFN_RESULT_TYPE(ComposedConnectHandler, | |
327 | void (boost::system::error_code, Iterator)) | |
328 | async_connect(basic_socket<Protocol, SocketService>& s, | |
329 | Iterator begin, BOOST_ASIO_MOVE_ARG(ComposedConnectHandler) handler) | |
330 | { | |
331 | // If you get an error on the following line it means that your handler does | |
332 | // not meet the documented type requirements for a ComposedConnectHandler. | |
333 | BOOST_ASIO_COMPOSED_CONNECT_HANDLER_CHECK( | |
334 | ComposedConnectHandler, handler, Iterator) type_check; | |
335 | ||
336 | detail::async_result_init<ComposedConnectHandler, | |
337 | void (boost::system::error_code, Iterator)> init( | |
338 | BOOST_ASIO_MOVE_CAST(ComposedConnectHandler)(handler)); | |
339 | ||
340 | detail::connect_op<Protocol, SocketService, Iterator, | |
341 | detail::default_connect_condition, BOOST_ASIO_HANDLER_TYPE( | |
342 | ComposedConnectHandler, void (boost::system::error_code, Iterator))>(s, | |
343 | begin, Iterator(), detail::default_connect_condition(), init.handler)( | |
344 | boost::system::error_code(), 1); | |
345 | ||
346 | return init.result.get(); | |
347 | } | |
348 | ||
349 | template <typename Protocol, typename SocketService, | |
350 | typename Iterator, typename ComposedConnectHandler> | |
351 | inline BOOST_ASIO_INITFN_RESULT_TYPE(ComposedConnectHandler, | |
352 | void (boost::system::error_code, Iterator)) | |
353 | async_connect(basic_socket<Protocol, SocketService>& s, | |
354 | Iterator begin, Iterator end, | |
355 | BOOST_ASIO_MOVE_ARG(ComposedConnectHandler) handler) | |
356 | { | |
357 | // If you get an error on the following line it means that your handler does | |
358 | // not meet the documented type requirements for a ComposedConnectHandler. | |
359 | BOOST_ASIO_COMPOSED_CONNECT_HANDLER_CHECK( | |
360 | ComposedConnectHandler, handler, Iterator) type_check; | |
361 | ||
362 | detail::async_result_init<ComposedConnectHandler, | |
363 | void (boost::system::error_code, Iterator)> init( | |
364 | BOOST_ASIO_MOVE_CAST(ComposedConnectHandler)(handler)); | |
365 | ||
366 | detail::connect_op<Protocol, SocketService, Iterator, | |
367 | detail::default_connect_condition, BOOST_ASIO_HANDLER_TYPE( | |
368 | ComposedConnectHandler, void (boost::system::error_code, Iterator))>(s, | |
369 | begin, end, detail::default_connect_condition(), init.handler)( | |
370 | boost::system::error_code(), 1); | |
371 | ||
372 | return init.result.get(); | |
373 | } | |
374 | ||
375 | template <typename Protocol, typename SocketService, typename Iterator, | |
376 | typename ConnectCondition, typename ComposedConnectHandler> | |
377 | inline BOOST_ASIO_INITFN_RESULT_TYPE(ComposedConnectHandler, | |
378 | void (boost::system::error_code, Iterator)) | |
379 | async_connect(basic_socket<Protocol, SocketService>& s, | |
380 | Iterator begin, ConnectCondition connect_condition, | |
381 | BOOST_ASIO_MOVE_ARG(ComposedConnectHandler) handler) | |
382 | { | |
383 | // If you get an error on the following line it means that your handler does | |
384 | // not meet the documented type requirements for a ComposedConnectHandler. | |
385 | BOOST_ASIO_COMPOSED_CONNECT_HANDLER_CHECK( | |
386 | ComposedConnectHandler, handler, Iterator) type_check; | |
387 | ||
388 | detail::async_result_init<ComposedConnectHandler, | |
389 | void (boost::system::error_code, Iterator)> init( | |
390 | BOOST_ASIO_MOVE_CAST(ComposedConnectHandler)(handler)); | |
391 | ||
392 | detail::connect_op<Protocol, SocketService, Iterator, | |
393 | ConnectCondition, BOOST_ASIO_HANDLER_TYPE( | |
394 | ComposedConnectHandler, void (boost::system::error_code, Iterator))>(s, | |
395 | begin, Iterator(), connect_condition, init.handler)( | |
396 | boost::system::error_code(), 1); | |
397 | ||
398 | return init.result.get(); | |
399 | } | |
400 | ||
401 | template <typename Protocol, typename SocketService, typename Iterator, | |
402 | typename ConnectCondition, typename ComposedConnectHandler> | |
403 | inline BOOST_ASIO_INITFN_RESULT_TYPE(ComposedConnectHandler, | |
404 | void (boost::system::error_code, Iterator)) | |
405 | async_connect(basic_socket<Protocol, SocketService>& s, | |
406 | Iterator begin, Iterator end, ConnectCondition connect_condition, | |
407 | BOOST_ASIO_MOVE_ARG(ComposedConnectHandler) handler) | |
408 | { | |
409 | // If you get an error on the following line it means that your handler does | |
410 | // not meet the documented type requirements for a ComposedConnectHandler. | |
411 | BOOST_ASIO_COMPOSED_CONNECT_HANDLER_CHECK( | |
412 | ComposedConnectHandler, handler, Iterator) type_check; | |
413 | ||
414 | detail::async_result_init<ComposedConnectHandler, | |
415 | void (boost::system::error_code, Iterator)> init( | |
416 | BOOST_ASIO_MOVE_CAST(ComposedConnectHandler)(handler)); | |
417 | ||
418 | detail::connect_op<Protocol, SocketService, Iterator, | |
419 | ConnectCondition, BOOST_ASIO_HANDLER_TYPE( | |
420 | ComposedConnectHandler, void (boost::system::error_code, Iterator))>(s, | |
421 | begin, end, connect_condition, init.handler)( | |
422 | boost::system::error_code(), 1); | |
423 | ||
424 | return init.result.get(); | |
425 | } | |
426 | ||
427 | } // namespace asio | |
428 | } // namespace boost | |
429 | ||
430 | #include <boost/asio/detail/pop_options.hpp> | |
431 | ||
432 | #endif // BOOST_ASIO_IMPL_CONNECT_HPP |