5 // Copyright (c) 2003-2022 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_CONNECT_HPP
12 #define BOOST_ASIO_CONNECT_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/async_result.hpp>
20 #include <boost/asio/basic_socket.hpp>
21 #include <boost/asio/detail/type_traits.hpp>
22 #include <boost/asio/error.hpp>
24 #include <boost/asio/detail/push_options.hpp>
31 char (&has_iterator_helper(...))[2];
34 char has_iterator_helper(T*, typename T::iterator* = 0);
37 struct has_iterator_typedef
39 enum { value = (sizeof((has_iterator_helper)((T*)(0))) == 1) };
43 /// Type trait used to determine whether a type is an endpoint sequence that can
44 /// be used with with @c connect and @c async_connect.
46 struct is_endpoint_sequence
48 #if defined(GENERATING_DOCUMENTATION)
49 /// The value member is true if the type may be used as an endpoint sequence.
50 static const bool value;
54 value = detail::has_iterator_typedef<T>::value
60 * @defgroup connect boost::asio::connect
62 * @brief The @c connect function is a composed operation that establishes a
63 * socket connection by trying each endpoint in a sequence.
67 /// Establishes a socket connection by trying each endpoint in a sequence.
69 * This function attempts to connect a socket to one of a sequence of
70 * endpoints. It does this by repeated calls to the socket's @c connect member
71 * function, once for each endpoint in the sequence, until a connection is
72 * successfully established.
74 * @param s The socket to be connected. If the socket is already open, it will
77 * @param endpoints A sequence of endpoints.
79 * @returns The successfully connected endpoint.
81 * @throws boost::system::system_error Thrown on failure. If the sequence is
82 * empty, the associated @c error_code is boost::asio::error::not_found.
83 * Otherwise, contains the error from the last connection attempt.
86 * @code tcp::resolver r(my_context);
87 * tcp::resolver::query q("host", "service");
88 * tcp::socket s(my_context);
89 * boost::asio::connect(s, r.resolve(q)); @endcode
91 template <typename Protocol, typename Executor, typename EndpointSequence>
92 typename Protocol::endpoint connect(basic_socket<Protocol, Executor>& s,
93 const EndpointSequence& endpoints,
94 typename constraint<is_endpoint_sequence<
95 EndpointSequence>::value>::type = 0);
97 /// Establishes a socket connection by trying each endpoint in a sequence.
99 * This function attempts to connect a socket to one of a sequence of
100 * endpoints. It does this by repeated calls to the socket's @c connect member
101 * function, once for each endpoint in the sequence, until a connection is
102 * successfully established.
104 * @param s The socket to be connected. If the socket is already open, it will
107 * @param endpoints A sequence of endpoints.
109 * @param ec Set to indicate what error occurred, if any. If the sequence is
110 * empty, set to boost::asio::error::not_found. Otherwise, contains the error
111 * from the last connection attempt.
113 * @returns On success, the successfully connected endpoint. Otherwise, a
114 * default-constructed endpoint.
117 * @code tcp::resolver r(my_context);
118 * tcp::resolver::query q("host", "service");
119 * tcp::socket s(my_context);
120 * boost::system::error_code ec;
121 * boost::asio::connect(s, r.resolve(q), ec);
124 * // An error occurred.
127 template <typename Protocol, typename Executor, typename EndpointSequence>
128 typename Protocol::endpoint connect(basic_socket<Protocol, Executor>& s,
129 const EndpointSequence& endpoints, boost::system::error_code& ec,
130 typename constraint<is_endpoint_sequence<
131 EndpointSequence>::value>::type = 0);
133 #if !defined(BOOST_ASIO_NO_DEPRECATED)
134 /// (Deprecated: Use range overload.) Establishes a socket connection by trying
135 /// each endpoint in a sequence.
137 * This function attempts to connect a socket to one of a sequence of
138 * endpoints. It does this by repeated calls to the socket's @c connect member
139 * function, once for each endpoint in the sequence, until a connection is
140 * successfully established.
142 * @param s The socket to be connected. If the socket is already open, it will
145 * @param begin An iterator pointing to the start of a sequence of endpoints.
147 * @returns On success, an iterator denoting the successfully connected
148 * endpoint. Otherwise, the end iterator.
150 * @throws boost::system::system_error Thrown on failure. If the sequence is
151 * empty, the associated @c error_code is boost::asio::error::not_found.
152 * Otherwise, contains the error from the last connection attempt.
154 * @note This overload assumes that a default constructed object of type @c
155 * Iterator represents the end of the sequence. This is a valid assumption for
156 * iterator types such as @c boost::asio::ip::tcp::resolver::iterator.
158 template <typename Protocol, typename Executor, typename Iterator>
159 Iterator connect(basic_socket<Protocol, Executor>& s, Iterator begin,
160 typename constraint<!is_endpoint_sequence<Iterator>::value>::type = 0);
162 /// (Deprecated: Use range overload.) Establishes a socket connection by trying
163 /// each endpoint in a sequence.
165 * This function attempts to connect a socket to one of a sequence of
166 * endpoints. It does this by repeated calls to the socket's @c connect member
167 * function, once for each endpoint in the sequence, until a connection is
168 * successfully established.
170 * @param s The socket to be connected. If the socket is already open, it will
173 * @param begin An iterator pointing to the start of a sequence of endpoints.
175 * @param ec Set to indicate what error occurred, if any. If the sequence is
176 * empty, set to boost::asio::error::not_found. Otherwise, contains the error
177 * from the last connection attempt.
179 * @returns On success, an iterator denoting the successfully connected
180 * endpoint. Otherwise, the end iterator.
182 * @note This overload assumes that a default constructed object of type @c
183 * Iterator represents the end of the sequence. This is a valid assumption for
184 * iterator types such as @c boost::asio::ip::tcp::resolver::iterator.
186 template <typename Protocol, typename Executor, typename Iterator>
187 Iterator connect(basic_socket<Protocol, Executor>& s,
188 Iterator begin, boost::system::error_code& ec,
189 typename constraint<!is_endpoint_sequence<Iterator>::value>::type = 0);
190 #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
192 /// Establishes a socket connection by trying each endpoint in a sequence.
194 * This function attempts to connect a socket to one of a sequence of
195 * endpoints. It does this by repeated calls to the socket's @c connect member
196 * function, once for each endpoint in the sequence, until a connection is
197 * successfully established.
199 * @param s The socket to be connected. If the socket is already open, it will
202 * @param begin An iterator pointing to the start of a sequence of endpoints.
204 * @param end An iterator pointing to the end of a sequence of endpoints.
206 * @returns An iterator denoting the successfully connected endpoint.
208 * @throws boost::system::system_error Thrown on failure. If the sequence is
209 * empty, the associated @c error_code is boost::asio::error::not_found.
210 * Otherwise, contains the error from the last connection attempt.
213 * @code tcp::resolver r(my_context);
214 * tcp::resolver::query q("host", "service");
215 * tcp::resolver::results_type e = r.resolve(q);
216 * tcp::socket s(my_context);
217 * boost::asio::connect(s, e.begin(), e.end()); @endcode
219 template <typename Protocol, typename Executor, typename Iterator>
220 Iterator connect(basic_socket<Protocol, Executor>& s,
221 Iterator begin, Iterator end);
223 /// Establishes a socket connection by trying each endpoint in a sequence.
225 * This function attempts to connect a socket to one of a sequence of
226 * endpoints. It does this by repeated calls to the socket's @c connect member
227 * function, once for each endpoint in the sequence, until a connection is
228 * successfully established.
230 * @param s The socket to be connected. If the socket is already open, it will
233 * @param begin An iterator pointing to the start of a sequence of endpoints.
235 * @param end An iterator pointing to the end of a sequence of endpoints.
237 * @param ec Set to indicate what error occurred, if any. If the sequence is
238 * empty, set to boost::asio::error::not_found. Otherwise, contains the error
239 * from the last connection attempt.
241 * @returns On success, an iterator denoting the successfully connected
242 * endpoint. Otherwise, the end iterator.
245 * @code tcp::resolver r(my_context);
246 * tcp::resolver::query q("host", "service");
247 * tcp::resolver::results_type e = r.resolve(q);
248 * tcp::socket s(my_context);
249 * boost::system::error_code ec;
250 * boost::asio::connect(s, e.begin(), e.end(), ec);
253 * // An error occurred.
256 template <typename Protocol, typename Executor, typename Iterator>
257 Iterator connect(basic_socket<Protocol, Executor>& s,
258 Iterator begin, Iterator end, boost::system::error_code& ec);
260 /// Establishes a socket connection by trying each endpoint in a sequence.
262 * This function attempts to connect a socket to one of a sequence of
263 * endpoints. It does this by repeated calls to the socket's @c connect member
264 * function, once for each endpoint in the sequence, until a connection is
265 * successfully established.
267 * @param s The socket to be connected. If the socket is already open, it will
270 * @param endpoints A sequence of endpoints.
272 * @param connect_condition A function object that is called prior to each
273 * connection attempt. The signature of the function object must be:
274 * @code bool connect_condition(
275 * const boost::system::error_code& ec,
276 * const typename Protocol::endpoint& next); @endcode
277 * The @c ec parameter contains the result from the most recent connect
278 * operation. Before the first connection attempt, @c ec is always set to
279 * indicate success. The @c next parameter is the next endpoint to be tried.
280 * The function object should return true if the next endpoint should be tried,
281 * and false if it should be skipped.
283 * @returns The successfully connected endpoint.
285 * @throws boost::system::system_error Thrown on failure. If the sequence is
286 * empty, the associated @c error_code is boost::asio::error::not_found.
287 * Otherwise, contains the error from the last connection attempt.
290 * The following connect condition function object can be used to output
291 * information about the individual connection attempts:
292 * @code struct my_connect_condition
295 * const boost::system::error_code& ec,
296 * const::tcp::endpoint& next)
298 * if (ec) std::cout << "Error: " << ec.message() << std::endl;
299 * std::cout << "Trying: " << next << std::endl;
303 * It would be used with the boost::asio::connect function as follows:
304 * @code tcp::resolver r(my_context);
305 * tcp::resolver::query q("host", "service");
306 * tcp::socket s(my_context);
307 * tcp::endpoint e = boost::asio::connect(s,
308 * r.resolve(q), my_connect_condition());
309 * std::cout << "Connected to: " << e << std::endl; @endcode
311 template <typename Protocol, typename Executor,
312 typename EndpointSequence, typename ConnectCondition>
313 typename Protocol::endpoint connect(basic_socket<Protocol, Executor>& s,
314 const EndpointSequence& endpoints, ConnectCondition connect_condition,
315 typename constraint<is_endpoint_sequence<
316 EndpointSequence>::value>::type = 0);
318 /// Establishes a socket connection by trying each endpoint in a sequence.
320 * This function attempts to connect a socket to one of a sequence of
321 * endpoints. It does this by repeated calls to the socket's @c connect member
322 * function, once for each endpoint in the sequence, until a connection is
323 * successfully established.
325 * @param s The socket to be connected. If the socket is already open, it will
328 * @param endpoints A sequence of endpoints.
330 * @param connect_condition A function object that is called prior to each
331 * connection attempt. The signature of the function object must be:
332 * @code bool connect_condition(
333 * const boost::system::error_code& ec,
334 * const typename Protocol::endpoint& next); @endcode
335 * The @c ec parameter contains the result from the most recent connect
336 * operation. Before the first connection attempt, @c ec is always set to
337 * indicate success. The @c next parameter is the next endpoint to be tried.
338 * The function object should return true if the next endpoint should be tried,
339 * and false if it should be skipped.
341 * @param ec Set to indicate what error occurred, if any. If the sequence is
342 * empty, set to boost::asio::error::not_found. Otherwise, contains the error
343 * from the last connection attempt.
345 * @returns On success, the successfully connected endpoint. Otherwise, a
346 * default-constructed endpoint.
349 * The following connect condition function object can be used to output
350 * information about the individual connection attempts:
351 * @code struct my_connect_condition
354 * const boost::system::error_code& ec,
355 * const::tcp::endpoint& next)
357 * if (ec) std::cout << "Error: " << ec.message() << std::endl;
358 * std::cout << "Trying: " << next << std::endl;
362 * It would be used with the boost::asio::connect function as follows:
363 * @code tcp::resolver r(my_context);
364 * tcp::resolver::query q("host", "service");
365 * tcp::socket s(my_context);
366 * boost::system::error_code ec;
367 * tcp::endpoint e = boost::asio::connect(s,
368 * r.resolve(q), my_connect_condition(), ec);
371 * // An error occurred.
375 * std::cout << "Connected to: " << e << std::endl;
378 template <typename Protocol, typename Executor,
379 typename EndpointSequence, typename ConnectCondition>
380 typename Protocol::endpoint connect(basic_socket<Protocol, Executor>& s,
381 const EndpointSequence& endpoints, ConnectCondition connect_condition,
382 boost::system::error_code& ec,
383 typename constraint<is_endpoint_sequence<
384 EndpointSequence>::value>::type = 0);
386 #if !defined(BOOST_ASIO_NO_DEPRECATED)
387 /// (Deprecated: Use range overload.) Establishes a socket connection by trying
388 /// each endpoint in a sequence.
390 * This function attempts to connect a socket to one of a sequence of
391 * endpoints. It does this by repeated calls to the socket's @c connect member
392 * function, once for each endpoint in the sequence, until a connection is
393 * successfully established.
395 * @param s The socket to be connected. If the socket is already open, it will
398 * @param begin An iterator pointing to the start of a sequence of endpoints.
400 * @param connect_condition A function object that is called prior to each
401 * connection attempt. The signature of the function object must be:
402 * @code bool connect_condition(
403 * const boost::system::error_code& ec,
404 * const typename Protocol::endpoint& next); @endcode
405 * The @c ec parameter contains the result from the most recent connect
406 * operation. Before the first connection attempt, @c ec is always set to
407 * indicate success. The @c next parameter is the next endpoint to be tried.
408 * The function object should return true if the next endpoint should be tried,
409 * and false if it should be skipped.
411 * @returns On success, an iterator denoting the successfully connected
412 * endpoint. Otherwise, the end iterator.
414 * @throws boost::system::system_error Thrown on failure. If the sequence is
415 * empty, the associated @c error_code is boost::asio::error::not_found.
416 * Otherwise, contains the error from the last connection attempt.
418 * @note This overload assumes that a default constructed object of type @c
419 * Iterator represents the end of the sequence. This is a valid assumption for
420 * iterator types such as @c boost::asio::ip::tcp::resolver::iterator.
422 template <typename Protocol, typename Executor,
423 typename Iterator, typename ConnectCondition>
424 Iterator connect(basic_socket<Protocol, Executor>& s,
425 Iterator begin, ConnectCondition connect_condition,
426 typename constraint<!is_endpoint_sequence<Iterator>::value>::type = 0);
428 /// (Deprecated: Use range overload.) Establishes a socket connection by trying
429 /// each endpoint in a sequence.
431 * This function attempts to connect a socket to one of a sequence of
432 * endpoints. It does this by repeated calls to the socket's @c connect member
433 * function, once for each endpoint in the sequence, until a connection is
434 * successfully established.
436 * @param s The socket to be connected. If the socket is already open, it will
439 * @param begin An iterator pointing to the start of a sequence of endpoints.
441 * @param connect_condition A function object that is called prior to each
442 * connection attempt. The signature of the function object must be:
443 * @code bool connect_condition(
444 * const boost::system::error_code& ec,
445 * const typename Protocol::endpoint& next); @endcode
446 * The @c ec parameter contains the result from the most recent connect
447 * operation. Before the first connection attempt, @c ec is always set to
448 * indicate success. The @c next parameter is the next endpoint to be tried.
449 * The function object should return true if the next endpoint should be tried,
450 * and false if it should be skipped.
452 * @param ec Set to indicate what error occurred, if any. If the sequence is
453 * empty, set to boost::asio::error::not_found. Otherwise, contains the error
454 * from the last connection attempt.
456 * @returns On success, an iterator denoting the successfully connected
457 * endpoint. Otherwise, the end iterator.
459 * @note This overload assumes that a default constructed object of type @c
460 * Iterator represents the end of the sequence. This is a valid assumption for
461 * iterator types such as @c boost::asio::ip::tcp::resolver::iterator.
463 template <typename Protocol, typename Executor,
464 typename Iterator, typename ConnectCondition>
465 Iterator connect(basic_socket<Protocol, Executor>& s, Iterator begin,
466 ConnectCondition connect_condition, boost::system::error_code& ec,
467 typename constraint<!is_endpoint_sequence<Iterator>::value>::type = 0);
468 #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
470 /// Establishes a socket connection by trying each endpoint in a sequence.
472 * This function attempts to connect a socket to one of a sequence of
473 * endpoints. It does this by repeated calls to the socket's @c connect member
474 * function, once for each endpoint in the sequence, until a connection is
475 * successfully established.
477 * @param s The socket to be connected. If the socket is already open, it will
480 * @param begin An iterator pointing to the start of a sequence of endpoints.
482 * @param end An iterator pointing to the end of a sequence of endpoints.
484 * @param connect_condition A function object that is called prior to each
485 * connection attempt. The signature of the function object must be:
486 * @code bool connect_condition(
487 * const boost::system::error_code& ec,
488 * const typename Protocol::endpoint& next); @endcode
489 * The @c ec parameter contains the result from the most recent connect
490 * operation. Before the first connection attempt, @c ec is always set to
491 * indicate success. The @c next parameter is the next endpoint to be tried.
492 * The function object should return true if the next endpoint should be tried,
493 * and false if it should be skipped.
495 * @returns An iterator denoting the successfully connected endpoint.
497 * @throws boost::system::system_error Thrown on failure. If the sequence is
498 * empty, the associated @c error_code is boost::asio::error::not_found.
499 * Otherwise, contains the error from the last connection attempt.
502 * The following connect condition function object can be used to output
503 * information about the individual connection attempts:
504 * @code struct my_connect_condition
507 * const boost::system::error_code& ec,
508 * const::tcp::endpoint& next)
510 * if (ec) std::cout << "Error: " << ec.message() << std::endl;
511 * std::cout << "Trying: " << next << std::endl;
515 * It would be used with the boost::asio::connect function as follows:
516 * @code tcp::resolver r(my_context);
517 * tcp::resolver::query q("host", "service");
518 * tcp::resolver::results_type e = r.resolve(q);
519 * tcp::socket s(my_context);
520 * tcp::resolver::results_type::iterator i = boost::asio::connect(
521 * s, e.begin(), e.end(), my_connect_condition());
522 * std::cout << "Connected to: " << i->endpoint() << std::endl; @endcode
524 template <typename Protocol, typename Executor,
525 typename Iterator, typename ConnectCondition>
526 Iterator connect(basic_socket<Protocol, Executor>& s, Iterator begin,
527 Iterator end, ConnectCondition connect_condition);
529 /// Establishes a socket connection by trying each endpoint in a sequence.
531 * This function attempts to connect a socket to one of a sequence of
532 * endpoints. It does this by repeated calls to the socket's @c connect member
533 * function, once for each endpoint in the sequence, until a connection is
534 * successfully established.
536 * @param s The socket to be connected. If the socket is already open, it will
539 * @param begin An iterator pointing to the start of a sequence of endpoints.
541 * @param end An iterator pointing to the end of a sequence of endpoints.
543 * @param connect_condition A function object that is called prior to each
544 * connection attempt. The signature of the function object must be:
545 * @code bool connect_condition(
546 * const boost::system::error_code& ec,
547 * const typename Protocol::endpoint& next); @endcode
548 * The @c ec parameter contains the result from the most recent connect
549 * operation. Before the first connection attempt, @c ec is always set to
550 * indicate success. The @c next parameter is the next endpoint to be tried.
551 * The function object should return true if the next endpoint should be tried,
552 * and false if it should be skipped.
554 * @param ec Set to indicate what error occurred, if any. If the sequence is
555 * empty, set to boost::asio::error::not_found. Otherwise, contains the error
556 * from the last connection attempt.
558 * @returns On success, an iterator denoting the successfully connected
559 * endpoint. Otherwise, the end iterator.
562 * The following connect condition function object can be used to output
563 * information about the individual connection attempts:
564 * @code struct my_connect_condition
567 * const boost::system::error_code& ec,
568 * const::tcp::endpoint& next)
570 * if (ec) std::cout << "Error: " << ec.message() << std::endl;
571 * std::cout << "Trying: " << next << std::endl;
575 * It would be used with the boost::asio::connect function as follows:
576 * @code tcp::resolver r(my_context);
577 * tcp::resolver::query q("host", "service");
578 * tcp::resolver::results_type e = r.resolve(q);
579 * tcp::socket s(my_context);
580 * boost::system::error_code ec;
581 * tcp::resolver::results_type::iterator i = boost::asio::connect(
582 * s, e.begin(), e.end(), my_connect_condition());
585 * // An error occurred.
589 * std::cout << "Connected to: " << i->endpoint() << std::endl;
592 template <typename Protocol, typename Executor,
593 typename Iterator, typename ConnectCondition>
594 Iterator connect(basic_socket<Protocol, Executor>& s,
595 Iterator begin, Iterator end, ConnectCondition connect_condition,
596 boost::system::error_code& ec);
601 * @defgroup async_connect boost::asio::async_connect
603 * @brief The @c async_connect function is a composed asynchronous operation
604 * that establishes a socket connection by trying each endpoint in a sequence.
608 /// Asynchronously establishes a socket connection by trying each endpoint in a
611 * This function attempts to connect a socket to one of a sequence of
612 * endpoints. It does this by repeated calls to the socket's @c async_connect
613 * member function, once for each endpoint in the sequence, until a connection
614 * is successfully established. It is an initiating function for an @ref
615 * asynchronous_operation, and always returns immediately.
617 * @param s The socket to be connected. If the socket is already open, it will
620 * @param endpoints A sequence of endpoints.
622 * @param token The @ref completion_token that will be used to produce a
623 * completion handler, which will be called when the connect completes.
624 * Potential completion tokens include @ref use_future, @ref use_awaitable,
625 * @ref yield_context, or a function object with the correct completion
626 * signature. The function signature of the completion handler must be:
627 * @code void handler(
628 * // Result of operation. if the sequence is empty, set to
629 * // boost::asio::error::not_found. Otherwise, contains the
630 * // error from the last connection attempt.
631 * const boost::system::error_code& error,
633 * // On success, the successfully connected endpoint.
634 * // Otherwise, a default-constructed endpoint.
635 * const typename Protocol::endpoint& endpoint
637 * Regardless of whether the asynchronous operation completes immediately or
638 * not, the completion handler will not be invoked from within this function.
639 * On immediate completion, invocation of the handler will be performed in a
640 * manner equivalent to using boost::asio::post().
642 * @par Completion Signature
643 * @code void(boost::system::error_code, typename Protocol::endpoint) @endcode
646 * @code tcp::resolver r(my_context);
647 * tcp::resolver::query q("host", "service");
648 * tcp::socket s(my_context);
652 * r.async_resolve(q, resolve_handler);
656 * void resolve_handler(
657 * const boost::system::error_code& ec,
658 * tcp::resolver::results_type results)
662 * boost::asio::async_connect(s, results, connect_handler);
668 * void connect_handler(
669 * const boost::system::error_code& ec,
670 * const tcp::endpoint& endpoint)
675 * @par Per-Operation Cancellation
676 * This asynchronous operation supports cancellation for the following
677 * boost::asio::cancellation_type values:
679 * @li @c cancellation_type::terminal
681 * @li @c cancellation_type::partial
683 * if they are also supported by the socket's @c async_connect operation.
685 template <typename Protocol, typename Executor, typename EndpointSequence,
686 BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code,
687 typename Protocol::endpoint)) RangeConnectToken
688 BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(Executor)>
689 BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(RangeConnectToken,
690 void (boost::system::error_code, typename Protocol::endpoint))
691 async_connect(basic_socket<Protocol, Executor>& s,
692 const EndpointSequence& endpoints,
693 BOOST_ASIO_MOVE_ARG(RangeConnectToken) token
694 BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(Executor),
695 typename constraint<is_endpoint_sequence<
696 EndpointSequence>::value>::type = 0);
698 #if !defined(BOOST_ASIO_NO_DEPRECATED)
699 /// (Deprecated: Use range overload.) Asynchronously establishes a socket
700 /// connection by trying each endpoint in a sequence.
702 * This function attempts to connect a socket to one of a sequence of
703 * endpoints. It does this by repeated calls to the socket's @c async_connect
704 * member function, once for each endpoint in the sequence, until a connection
705 * is successfully established. It is an initiating function for an @ref
706 * asynchronous_operation, and always returns immediately.
708 * @param s The socket to be connected. If the socket is already open, it will
711 * @param begin An iterator pointing to the start of a sequence of endpoints.
713 * @param token The @ref completion_token that will be used to produce a
714 * completion handler, which will be called when the connect completes.
715 * Potential completion tokens include @ref use_future, @ref use_awaitable,
716 * @ref yield_context, or a function object with the correct completion
717 * signature. The function signature of the completion handler must be:
718 * @code void handler(
719 * // Result of operation. if the sequence is empty, set to
720 * // boost::asio::error::not_found. Otherwise, contains the
721 * // error from the last connection attempt.
722 * const boost::system::error_code& error,
724 * // On success, an iterator denoting the successfully
725 * // connected endpoint. Otherwise, the end iterator.
728 * Regardless of whether the asynchronous operation completes immediately or
729 * not, the completion handler will not be invoked from within this function.
730 * On immediate completion, invocation of the handler will be performed in a
731 * manner equivalent to using boost::asio::post().
733 * @par Completion Signature
734 * @code void(boost::system::error_code, Iterator) @endcode
736 * @note This overload assumes that a default constructed object of type @c
737 * Iterator represents the end of the sequence. This is a valid assumption for
738 * iterator types such as @c boost::asio::ip::tcp::resolver::iterator.
740 * @par Per-Operation Cancellation
741 * This asynchronous operation supports cancellation for the following
742 * boost::asio::cancellation_type values:
744 * @li @c cancellation_type::terminal
746 * @li @c cancellation_type::partial
748 * if they are also supported by the socket's @c async_connect operation.
750 template <typename Protocol, typename Executor, typename Iterator,
751 BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code,
752 Iterator)) IteratorConnectToken
753 BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(Executor)>
754 BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(IteratorConnectToken,
755 void (boost::system::error_code, Iterator))
756 async_connect(basic_socket<Protocol, Executor>& s, Iterator begin,
757 BOOST_ASIO_MOVE_ARG(IteratorConnectToken) token
758 BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(Executor),
759 typename constraint<!is_endpoint_sequence<Iterator>::value>::type = 0);
760 #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
762 /// Asynchronously establishes a socket connection by trying each endpoint in a
765 * This function attempts to connect a socket to one of a sequence of
766 * endpoints. It does this by repeated calls to the socket's @c async_connect
767 * member function, once for each endpoint in the sequence, until a connection
768 * is successfully established. It is an initiating function for an @ref
769 * asynchronous_operation, and always returns immediately.
771 * @param s The socket to be connected. If the socket is already open, it will
774 * @param begin An iterator pointing to the start of a sequence of endpoints.
776 * @param end An iterator pointing to the end of a sequence of endpoints.
778 * @param token The @ref completion_token that will be used to produce a
779 * completion handler, which will be called when the connect completes.
780 * Potential completion tokens include @ref use_future, @ref use_awaitable,
781 * @ref yield_context, or a function object with the correct completion
782 * signature. The function signature of the completion handler must be:
783 * @code void handler(
784 * // Result of operation. if the sequence is empty, set to
785 * // boost::asio::error::not_found. Otherwise, contains the
786 * // error from the last connection attempt.
787 * const boost::system::error_code& error,
789 * // On success, an iterator denoting the successfully
790 * // connected endpoint. Otherwise, the end iterator.
793 * Regardless of whether the asynchronous operation completes immediately or
794 * not, the completion handler will not be invoked from within this function.
795 * On immediate completion, invocation of the handler will be performed in a
796 * manner equivalent to using boost::asio::post().
798 * @par Completion Signature
799 * @code void(boost::system::error_code, Iterator) @endcode
802 * @code std::vector<tcp::endpoint> endpoints = ...;
803 * tcp::socket s(my_context);
804 * boost::asio::async_connect(s,
805 * endpoints.begin(), endpoints.end(),
810 * void connect_handler(
811 * const boost::system::error_code& ec,
812 * std::vector<tcp::endpoint>::iterator i)
817 * @par Per-Operation Cancellation
818 * This asynchronous operation supports cancellation for the following
819 * boost::asio::cancellation_type values:
821 * @li @c cancellation_type::terminal
823 * @li @c cancellation_type::partial
825 * if they are also supported by the socket's @c async_connect operation.
827 template <typename Protocol, typename Executor, typename Iterator,
828 BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code,
829 Iterator)) IteratorConnectToken
830 BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(Executor)>
831 BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(IteratorConnectToken,
832 void (boost::system::error_code, Iterator))
833 async_connect(basic_socket<Protocol, Executor>& s, Iterator begin, Iterator end,
834 BOOST_ASIO_MOVE_ARG(IteratorConnectToken) token
835 BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(Executor));
837 /// Asynchronously establishes a socket connection by trying each endpoint in a
840 * This function attempts to connect a socket to one of a sequence of
841 * endpoints. It does this by repeated calls to the socket's @c async_connect
842 * member function, once for each endpoint in the sequence, until a connection
843 * is successfully established. It is an initiating function for an @ref
844 * asynchronous_operation, and always returns immediately.
846 * @param s The socket to be connected. If the socket is already open, it will
849 * @param endpoints A sequence of endpoints.
851 * @param connect_condition A function object that is called prior to each
852 * connection attempt. The signature of the function object must be:
853 * @code bool connect_condition(
854 * const boost::system::error_code& ec,
855 * const typename Protocol::endpoint& next); @endcode
856 * The @c ec parameter contains the result from the most recent connect
857 * operation. Before the first connection attempt, @c ec is always set to
858 * indicate success. The @c next parameter is the next endpoint to be tried.
859 * The function object should return true if the next endpoint should be tried,
860 * and false if it should be skipped.
862 * @param token The @ref completion_token that will be used to produce a
863 * completion handler, which will be called when the connect completes.
864 * Potential completion tokens include @ref use_future, @ref use_awaitable,
865 * @ref yield_context, or a function object with the correct completion
866 * signature. The function signature of the completion handler must be:
867 * @code void handler(
868 * // Result of operation. if the sequence is empty, set to
869 * // boost::asio::error::not_found. Otherwise, contains the
870 * // error from the last connection attempt.
871 * const boost::system::error_code& error,
873 * // On success, an iterator denoting the successfully
874 * // connected endpoint. Otherwise, the end iterator.
877 * Regardless of whether the asynchronous operation completes immediately or
878 * not, the completion handler will not be invoked from within this function.
879 * On immediate completion, invocation of the handler will be performed in a
880 * manner equivalent to using boost::asio::post().
882 * @par Completion Signature
883 * @code void(boost::system::error_code, typename Protocol::endpoint) @endcode
886 * The following connect condition function object can be used to output
887 * information about the individual connection attempts:
888 * @code struct my_connect_condition
891 * const boost::system::error_code& ec,
892 * const::tcp::endpoint& next)
894 * if (ec) std::cout << "Error: " << ec.message() << std::endl;
895 * std::cout << "Trying: " << next << std::endl;
899 * It would be used with the boost::asio::connect function as follows:
900 * @code tcp::resolver r(my_context);
901 * tcp::resolver::query q("host", "service");
902 * tcp::socket s(my_context);
906 * r.async_resolve(q, resolve_handler);
910 * void resolve_handler(
911 * const boost::system::error_code& ec,
912 * tcp::resolver::results_type results)
916 * boost::asio::async_connect(s, results,
917 * my_connect_condition(),
924 * void connect_handler(
925 * const boost::system::error_code& ec,
926 * const tcp::endpoint& endpoint)
930 * // An error occurred.
934 * std::cout << "Connected to: " << endpoint << std::endl;
938 * @par Per-Operation Cancellation
939 * This asynchronous operation supports cancellation for the following
940 * boost::asio::cancellation_type values:
942 * @li @c cancellation_type::terminal
944 * @li @c cancellation_type::partial
946 * if they are also supported by the socket's @c async_connect operation.
948 template <typename Protocol, typename Executor,
949 typename EndpointSequence, typename ConnectCondition,
950 BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code,
951 typename Protocol::endpoint)) RangeConnectToken
952 BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(Executor)>
953 BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(RangeConnectToken,
954 void (boost::system::error_code, typename Protocol::endpoint))
955 async_connect(basic_socket<Protocol, Executor>& s,
956 const EndpointSequence& endpoints, ConnectCondition connect_condition,
957 BOOST_ASIO_MOVE_ARG(RangeConnectToken) token
958 BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(Executor),
959 typename constraint<is_endpoint_sequence<
960 EndpointSequence>::value>::type = 0);
962 #if !defined(BOOST_ASIO_NO_DEPRECATED)
963 /// (Deprecated: Use range overload.) Asynchronously establishes a socket
964 /// connection by trying each endpoint in a sequence.
966 * This function attempts to connect a socket to one of a sequence of
967 * endpoints. It does this by repeated calls to the socket's @c async_connect
968 * member function, once for each endpoint in the sequence, until a connection
969 * is successfully established. It is an initiating function for an @ref
970 * asynchronous_operation, and always returns immediately.
972 * @param s The socket to be connected. If the socket is already open, it will
975 * @param begin An iterator pointing to the start of a sequence of endpoints.
977 * @param connect_condition A function object that is called prior to each
978 * connection attempt. The signature of the function object must be:
979 * @code bool connect_condition(
980 * const boost::system::error_code& ec,
981 * const typename Protocol::endpoint& next); @endcode
982 * The @c ec parameter contains the result from the most recent connect
983 * operation. Before the first connection attempt, @c ec is always set to
984 * indicate success. The @c next parameter is the next endpoint to be tried.
985 * The function object should return true if the next endpoint should be tried,
986 * and false if it should be skipped.
988 * @param token The @ref completion_token that will be used to produce a
989 * completion handler, which will be called when the connect completes.
990 * Potential completion tokens include @ref use_future, @ref use_awaitable,
991 * @ref yield_context, or a function object with the correct completion
992 * signature. The function signature of the completion handler must be:
993 * @code void handler(
994 * // Result of operation. if the sequence is empty, set to
995 * // boost::asio::error::not_found. Otherwise, contains the
996 * // error from the last connection attempt.
997 * const boost::system::error_code& error,
999 * // On success, an iterator denoting the successfully
1000 * // connected endpoint. Otherwise, the end iterator.
1003 * Regardless of whether the asynchronous operation completes immediately or
1004 * not, the completion handler will not be invoked from within this function.
1005 * On immediate completion, invocation of the handler will be performed in a
1006 * manner equivalent to using boost::asio::post().
1008 * @par Completion Signature
1009 * @code void(boost::system::error_code, Iterator) @endcode
1011 * @note This overload assumes that a default constructed object of type @c
1012 * Iterator represents the end of the sequence. This is a valid assumption for
1013 * iterator types such as @c boost::asio::ip::tcp::resolver::iterator.
1015 * @par Per-Operation Cancellation
1016 * This asynchronous operation supports cancellation for the following
1017 * boost::asio::cancellation_type values:
1019 * @li @c cancellation_type::terminal
1021 * @li @c cancellation_type::partial
1023 * if they are also supported by the socket's @c async_connect operation.
1025 template <typename Protocol, typename Executor,
1026 typename Iterator, typename ConnectCondition,
1027 BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code,
1028 Iterator)) IteratorConnectToken
1029 BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(Executor)>
1030 BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(IteratorConnectToken,
1031 void (boost::system::error_code, Iterator))
1032 async_connect(basic_socket<Protocol, Executor>& s, Iterator begin,
1033 ConnectCondition connect_condition,
1034 BOOST_ASIO_MOVE_ARG(IteratorConnectToken) token
1035 BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(Executor),
1036 typename constraint<!is_endpoint_sequence<Iterator>::value>::type = 0);
1037 #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
1039 /// Asynchronously establishes a socket connection by trying each endpoint in a
1042 * This function attempts to connect a socket to one of a sequence of
1043 * endpoints. It does this by repeated calls to the socket's @c async_connect
1044 * member function, once for each endpoint in the sequence, until a connection
1045 * is successfully established. It is an initiating function for an @ref
1046 * asynchronous_operation, and always returns immediately.
1048 * @param s The socket to be connected. If the socket is already open, it will
1051 * @param begin An iterator pointing to the start of a sequence of endpoints.
1053 * @param end An iterator pointing to the end of a sequence of endpoints.
1055 * @param connect_condition A function object that is called prior to each
1056 * connection attempt. The signature of the function object must be:
1057 * @code bool connect_condition(
1058 * const boost::system::error_code& ec,
1059 * const typename Protocol::endpoint& next); @endcode
1060 * The @c ec parameter contains the result from the most recent connect
1061 * operation. Before the first connection attempt, @c ec is always set to
1062 * indicate success. The @c next parameter is the next endpoint to be tried.
1063 * The function object should return true if the next endpoint should be tried,
1064 * and false if it should be skipped.
1066 * @param token The @ref completion_token that will be used to produce a
1067 * completion handler, which will be called when the connect completes.
1068 * Potential completion tokens include @ref use_future, @ref use_awaitable,
1069 * @ref yield_context, or a function object with the correct completion
1070 * signature. The function signature of the completion handler must be:
1071 * @code void handler(
1072 * // Result of operation. if the sequence is empty, set to
1073 * // boost::asio::error::not_found. Otherwise, contains the
1074 * // error from the last connection attempt.
1075 * const boost::system::error_code& error,
1077 * // On success, an iterator denoting the successfully
1078 * // connected endpoint. Otherwise, the end iterator.
1081 * Regardless of whether the asynchronous operation completes immediately or
1082 * not, the completion handler will not be invoked from within this function.
1083 * On immediate completion, invocation of the handler will be performed in a
1084 * manner equivalent to using boost::asio::post().
1086 * @par Completion Signature
1087 * @code void(boost::system::error_code, Iterator) @endcode
1090 * The following connect condition function object can be used to output
1091 * information about the individual connection attempts:
1092 * @code struct my_connect_condition
1095 * const boost::system::error_code& ec,
1096 * const::tcp::endpoint& next)
1098 * if (ec) std::cout << "Error: " << ec.message() << std::endl;
1099 * std::cout << "Trying: " << next << std::endl;
1103 * It would be used with the boost::asio::connect function as follows:
1104 * @code tcp::resolver r(my_context);
1105 * tcp::resolver::query q("host", "service");
1106 * tcp::socket s(my_context);
1110 * r.async_resolve(q, resolve_handler);
1114 * void resolve_handler(
1115 * const boost::system::error_code& ec,
1116 * tcp::resolver::iterator i)
1120 * tcp::resolver::iterator end;
1121 * boost::asio::async_connect(s, i, end,
1122 * my_connect_condition(),
1129 * void connect_handler(
1130 * const boost::system::error_code& ec,
1131 * tcp::resolver::iterator i)
1135 * // An error occurred.
1139 * std::cout << "Connected to: " << i->endpoint() << std::endl;
1143 * @par Per-Operation Cancellation
1144 * This asynchronous operation supports cancellation for the following
1145 * boost::asio::cancellation_type values:
1147 * @li @c cancellation_type::terminal
1149 * @li @c cancellation_type::partial
1151 * if they are also supported by the socket's @c async_connect operation.
1153 template <typename Protocol, typename Executor,
1154 typename Iterator, typename ConnectCondition,
1155 BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code,
1156 Iterator)) IteratorConnectToken
1157 BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(Executor)>
1158 BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(IteratorConnectToken,
1159 void (boost::system::error_code, Iterator))
1160 async_connect(basic_socket<Protocol, Executor>& s, Iterator begin,
1161 Iterator end, ConnectCondition connect_condition,
1162 BOOST_ASIO_MOVE_ARG(IteratorConnectToken) token
1163 BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(Executor));
1168 } // namespace boost
1170 #include <boost/asio/detail/pop_options.hpp>
1172 #include <boost/asio/impl/connect.hpp>