connect.hpp 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180
  1. //
  2. // connect.hpp
  3. // ~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2023 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. #ifndef ASIO_CONNECT_HPP
  11. #define ASIO_CONNECT_HPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include "asio/detail/config.hpp"
  16. #include "asio/async_result.hpp"
  17. #include "asio/basic_socket.hpp"
  18. #include "asio/detail/type_traits.hpp"
  19. #include "asio/error.hpp"
  20. #include "asio/detail/push_options.hpp"
  21. namespace asio {
  22. namespace detail
  23. {
  24. struct default_connect_condition;
  25. template <typename, typename> class initiate_async_range_connect;
  26. template <typename, typename> class initiate_async_iterator_connect;
  27. char (&has_iterator_helper(...))[2];
  28. template <typename T>
  29. char has_iterator_helper(T*, typename T::iterator* = 0);
  30. template <typename T>
  31. struct has_iterator_typedef
  32. {
  33. enum { value = (sizeof((has_iterator_helper)((T*)(0))) == 1) };
  34. };
  35. } // namespace detail
  36. /// Type trait used to determine whether a type is an endpoint sequence that can
  37. /// be used with with @c connect and @c async_connect.
  38. template <typename T>
  39. struct is_endpoint_sequence
  40. {
  41. #if defined(GENERATING_DOCUMENTATION)
  42. /// The value member is true if the type may be used as an endpoint sequence.
  43. static const bool value;
  44. #else
  45. enum
  46. {
  47. value = detail::has_iterator_typedef<T>::value
  48. };
  49. #endif
  50. };
  51. /**
  52. * @defgroup connect asio::connect
  53. *
  54. * @brief The @c connect function is a composed operation that establishes a
  55. * socket connection by trying each endpoint in a sequence.
  56. */
  57. /*@{*/
  58. /// Establishes a socket connection by trying each endpoint in a sequence.
  59. /**
  60. * This function attempts to connect a socket to one of a sequence of
  61. * endpoints. It does this by repeated calls to the socket's @c connect member
  62. * function, once for each endpoint in the sequence, until a connection is
  63. * successfully established.
  64. *
  65. * @param s The socket to be connected. If the socket is already open, it will
  66. * be closed.
  67. *
  68. * @param endpoints A sequence of endpoints.
  69. *
  70. * @returns The successfully connected endpoint.
  71. *
  72. * @throws asio::system_error Thrown on failure. If the sequence is
  73. * empty, the associated @c error_code is asio::error::not_found.
  74. * Otherwise, contains the error from the last connection attempt.
  75. *
  76. * @par Example
  77. * @code tcp::resolver r(my_context);
  78. * tcp::resolver::query q("host", "service");
  79. * tcp::socket s(my_context);
  80. * asio::connect(s, r.resolve(q)); @endcode
  81. */
  82. template <typename Protocol, typename Executor, typename EndpointSequence>
  83. typename Protocol::endpoint connect(basic_socket<Protocol, Executor>& s,
  84. const EndpointSequence& endpoints,
  85. constraint_t<is_endpoint_sequence<EndpointSequence>::value> = 0);
  86. /// Establishes a socket connection by trying each endpoint in a sequence.
  87. /**
  88. * This function attempts to connect a socket to one of a sequence of
  89. * endpoints. It does this by repeated calls to the socket's @c connect member
  90. * function, once for each endpoint in the sequence, until a connection is
  91. * successfully established.
  92. *
  93. * @param s The socket to be connected. If the socket is already open, it will
  94. * be closed.
  95. *
  96. * @param endpoints A sequence of endpoints.
  97. *
  98. * @param ec Set to indicate what error occurred, if any. If the sequence is
  99. * empty, set to asio::error::not_found. Otherwise, contains the error
  100. * from the last connection attempt.
  101. *
  102. * @returns On success, the successfully connected endpoint. Otherwise, a
  103. * default-constructed endpoint.
  104. *
  105. * @par Example
  106. * @code tcp::resolver r(my_context);
  107. * tcp::resolver::query q("host", "service");
  108. * tcp::socket s(my_context);
  109. * asio::error_code ec;
  110. * asio::connect(s, r.resolve(q), ec);
  111. * if (ec)
  112. * {
  113. * // An error occurred.
  114. * } @endcode
  115. */
  116. template <typename Protocol, typename Executor, typename EndpointSequence>
  117. typename Protocol::endpoint connect(basic_socket<Protocol, Executor>& s,
  118. const EndpointSequence& endpoints, asio::error_code& ec,
  119. constraint_t<is_endpoint_sequence<EndpointSequence>::value> = 0);
  120. #if !defined(ASIO_NO_DEPRECATED)
  121. /// (Deprecated: Use range overload.) Establishes a socket connection by trying
  122. /// each endpoint in a sequence.
  123. /**
  124. * This function attempts to connect a socket to one of a sequence of
  125. * endpoints. It does this by repeated calls to the socket's @c connect member
  126. * function, once for each endpoint in the sequence, until a connection is
  127. * successfully established.
  128. *
  129. * @param s The socket to be connected. If the socket is already open, it will
  130. * be closed.
  131. *
  132. * @param begin An iterator pointing to the start of a sequence of endpoints.
  133. *
  134. * @returns On success, an iterator denoting the successfully connected
  135. * endpoint. Otherwise, the end iterator.
  136. *
  137. * @throws asio::system_error Thrown on failure. If the sequence is
  138. * empty, the associated @c error_code is asio::error::not_found.
  139. * Otherwise, contains the error from the last connection attempt.
  140. *
  141. * @note This overload assumes that a default constructed object of type @c
  142. * Iterator represents the end of the sequence. This is a valid assumption for
  143. * iterator types such as @c asio::ip::tcp::resolver::iterator.
  144. */
  145. template <typename Protocol, typename Executor, typename Iterator>
  146. Iterator connect(basic_socket<Protocol, Executor>& s, Iterator begin,
  147. constraint_t<!is_endpoint_sequence<Iterator>::value> = 0);
  148. /// (Deprecated: Use range overload.) Establishes a socket connection by trying
  149. /// each endpoint in a sequence.
  150. /**
  151. * This function attempts to connect a socket to one of a sequence of
  152. * endpoints. It does this by repeated calls to the socket's @c connect member
  153. * function, once for each endpoint in the sequence, until a connection is
  154. * successfully established.
  155. *
  156. * @param s The socket to be connected. If the socket is already open, it will
  157. * be closed.
  158. *
  159. * @param begin An iterator pointing to the start of a sequence of endpoints.
  160. *
  161. * @param ec Set to indicate what error occurred, if any. If the sequence is
  162. * empty, set to asio::error::not_found. Otherwise, contains the error
  163. * from the last connection attempt.
  164. *
  165. * @returns On success, an iterator denoting the successfully connected
  166. * endpoint. Otherwise, the end iterator.
  167. *
  168. * @note This overload assumes that a default constructed object of type @c
  169. * Iterator represents the end of the sequence. This is a valid assumption for
  170. * iterator types such as @c asio::ip::tcp::resolver::iterator.
  171. */
  172. template <typename Protocol, typename Executor, typename Iterator>
  173. Iterator connect(basic_socket<Protocol, Executor>& s,
  174. Iterator begin, asio::error_code& ec,
  175. constraint_t<!is_endpoint_sequence<Iterator>::value> = 0);
  176. #endif // !defined(ASIO_NO_DEPRECATED)
  177. /// Establishes a socket connection by trying each endpoint in a sequence.
  178. /**
  179. * This function attempts to connect a socket to one of a sequence of
  180. * endpoints. It does this by repeated calls to the socket's @c connect member
  181. * function, once for each endpoint in the sequence, until a connection is
  182. * successfully established.
  183. *
  184. * @param s The socket to be connected. If the socket is already open, it will
  185. * be closed.
  186. *
  187. * @param begin An iterator pointing to the start of a sequence of endpoints.
  188. *
  189. * @param end An iterator pointing to the end of a sequence of endpoints.
  190. *
  191. * @returns An iterator denoting the successfully connected endpoint.
  192. *
  193. * @throws asio::system_error Thrown on failure. If the sequence is
  194. * empty, the associated @c error_code is asio::error::not_found.
  195. * Otherwise, contains the error from the last connection attempt.
  196. *
  197. * @par Example
  198. * @code tcp::resolver r(my_context);
  199. * tcp::resolver::query q("host", "service");
  200. * tcp::resolver::results_type e = r.resolve(q);
  201. * tcp::socket s(my_context);
  202. * asio::connect(s, e.begin(), e.end()); @endcode
  203. */
  204. template <typename Protocol, typename Executor, typename Iterator>
  205. Iterator connect(basic_socket<Protocol, Executor>& s,
  206. Iterator begin, Iterator end);
  207. /// Establishes a socket connection by trying each endpoint in a sequence.
  208. /**
  209. * This function attempts to connect a socket to one of a sequence of
  210. * endpoints. It does this by repeated calls to the socket's @c connect member
  211. * function, once for each endpoint in the sequence, until a connection is
  212. * successfully established.
  213. *
  214. * @param s The socket to be connected. If the socket is already open, it will
  215. * be closed.
  216. *
  217. * @param begin An iterator pointing to the start of a sequence of endpoints.
  218. *
  219. * @param end An iterator pointing to the end of a sequence of endpoints.
  220. *
  221. * @param ec Set to indicate what error occurred, if any. If the sequence is
  222. * empty, set to asio::error::not_found. Otherwise, contains the error
  223. * from the last connection attempt.
  224. *
  225. * @returns On success, an iterator denoting the successfully connected
  226. * endpoint. Otherwise, the end iterator.
  227. *
  228. * @par Example
  229. * @code tcp::resolver r(my_context);
  230. * tcp::resolver::query q("host", "service");
  231. * tcp::resolver::results_type e = r.resolve(q);
  232. * tcp::socket s(my_context);
  233. * asio::error_code ec;
  234. * asio::connect(s, e.begin(), e.end(), ec);
  235. * if (ec)
  236. * {
  237. * // An error occurred.
  238. * } @endcode
  239. */
  240. template <typename Protocol, typename Executor, typename Iterator>
  241. Iterator connect(basic_socket<Protocol, Executor>& s,
  242. Iterator begin, Iterator end, asio::error_code& ec);
  243. /// Establishes a socket connection by trying each endpoint in a sequence.
  244. /**
  245. * This function attempts to connect a socket to one of a sequence of
  246. * endpoints. It does this by repeated calls to the socket's @c connect member
  247. * function, once for each endpoint in the sequence, until a connection is
  248. * successfully established.
  249. *
  250. * @param s The socket to be connected. If the socket is already open, it will
  251. * be closed.
  252. *
  253. * @param endpoints A sequence of endpoints.
  254. *
  255. * @param connect_condition A function object that is called prior to each
  256. * connection attempt. The signature of the function object must be:
  257. * @code bool connect_condition(
  258. * const asio::error_code& ec,
  259. * const typename Protocol::endpoint& next); @endcode
  260. * The @c ec parameter contains the result from the most recent connect
  261. * operation. Before the first connection attempt, @c ec is always set to
  262. * indicate success. The @c next parameter is the next endpoint to be tried.
  263. * The function object should return true if the next endpoint should be tried,
  264. * and false if it should be skipped.
  265. *
  266. * @returns The successfully connected endpoint.
  267. *
  268. * @throws asio::system_error Thrown on failure. If the sequence is
  269. * empty, the associated @c error_code is asio::error::not_found.
  270. * Otherwise, contains the error from the last connection attempt.
  271. *
  272. * @par Example
  273. * The following connect condition function object can be used to output
  274. * information about the individual connection attempts:
  275. * @code struct my_connect_condition
  276. * {
  277. * bool operator()(
  278. * const asio::error_code& ec,
  279. * const::tcp::endpoint& next)
  280. * {
  281. * if (ec) std::cout << "Error: " << ec.message() << std::endl;
  282. * std::cout << "Trying: " << next << std::endl;
  283. * return true;
  284. * }
  285. * }; @endcode
  286. * It would be used with the asio::connect function as follows:
  287. * @code tcp::resolver r(my_context);
  288. * tcp::resolver::query q("host", "service");
  289. * tcp::socket s(my_context);
  290. * tcp::endpoint e = asio::connect(s,
  291. * r.resolve(q), my_connect_condition());
  292. * std::cout << "Connected to: " << e << std::endl; @endcode
  293. */
  294. template <typename Protocol, typename Executor,
  295. typename EndpointSequence, typename ConnectCondition>
  296. typename Protocol::endpoint connect(basic_socket<Protocol, Executor>& s,
  297. const EndpointSequence& endpoints, ConnectCondition connect_condition,
  298. constraint_t<is_endpoint_sequence<EndpointSequence>::value> = 0);
  299. /// Establishes a socket connection by trying each endpoint in a sequence.
  300. /**
  301. * This function attempts to connect a socket to one of a sequence of
  302. * endpoints. It does this by repeated calls to the socket's @c connect member
  303. * function, once for each endpoint in the sequence, until a connection is
  304. * successfully established.
  305. *
  306. * @param s The socket to be connected. If the socket is already open, it will
  307. * be closed.
  308. *
  309. * @param endpoints A sequence of endpoints.
  310. *
  311. * @param connect_condition A function object that is called prior to each
  312. * connection attempt. The signature of the function object must be:
  313. * @code bool connect_condition(
  314. * const asio::error_code& ec,
  315. * const typename Protocol::endpoint& next); @endcode
  316. * The @c ec parameter contains the result from the most recent connect
  317. * operation. Before the first connection attempt, @c ec is always set to
  318. * indicate success. The @c next parameter is the next endpoint to be tried.
  319. * The function object should return true if the next endpoint should be tried,
  320. * and false if it should be skipped.
  321. *
  322. * @param ec Set to indicate what error occurred, if any. If the sequence is
  323. * empty, set to asio::error::not_found. Otherwise, contains the error
  324. * from the last connection attempt.
  325. *
  326. * @returns On success, the successfully connected endpoint. Otherwise, a
  327. * default-constructed endpoint.
  328. *
  329. * @par Example
  330. * The following connect condition function object can be used to output
  331. * information about the individual connection attempts:
  332. * @code struct my_connect_condition
  333. * {
  334. * bool operator()(
  335. * const asio::error_code& ec,
  336. * const::tcp::endpoint& next)
  337. * {
  338. * if (ec) std::cout << "Error: " << ec.message() << std::endl;
  339. * std::cout << "Trying: " << next << std::endl;
  340. * return true;
  341. * }
  342. * }; @endcode
  343. * It would be used with the asio::connect function as follows:
  344. * @code tcp::resolver r(my_context);
  345. * tcp::resolver::query q("host", "service");
  346. * tcp::socket s(my_context);
  347. * asio::error_code ec;
  348. * tcp::endpoint e = asio::connect(s,
  349. * r.resolve(q), my_connect_condition(), ec);
  350. * if (ec)
  351. * {
  352. * // An error occurred.
  353. * }
  354. * else
  355. * {
  356. * std::cout << "Connected to: " << e << std::endl;
  357. * } @endcode
  358. */
  359. template <typename Protocol, typename Executor,
  360. typename EndpointSequence, typename ConnectCondition>
  361. typename Protocol::endpoint connect(basic_socket<Protocol, Executor>& s,
  362. const EndpointSequence& endpoints, ConnectCondition connect_condition,
  363. asio::error_code& ec,
  364. constraint_t<is_endpoint_sequence<EndpointSequence>::value> = 0);
  365. #if !defined(ASIO_NO_DEPRECATED)
  366. /// (Deprecated: Use range overload.) Establishes a socket connection by trying
  367. /// each endpoint in a sequence.
  368. /**
  369. * This function attempts to connect a socket to one of a sequence of
  370. * endpoints. It does this by repeated calls to the socket's @c connect member
  371. * function, once for each endpoint in the sequence, until a connection is
  372. * successfully established.
  373. *
  374. * @param s The socket to be connected. If the socket is already open, it will
  375. * be closed.
  376. *
  377. * @param begin An iterator pointing to the start of a sequence of endpoints.
  378. *
  379. * @param connect_condition A function object that is called prior to each
  380. * connection attempt. The signature of the function object must be:
  381. * @code bool connect_condition(
  382. * const asio::error_code& ec,
  383. * const typename Protocol::endpoint& next); @endcode
  384. * The @c ec parameter contains the result from the most recent connect
  385. * operation. Before the first connection attempt, @c ec is always set to
  386. * indicate success. The @c next parameter is the next endpoint to be tried.
  387. * The function object should return true if the next endpoint should be tried,
  388. * and false if it should be skipped.
  389. *
  390. * @returns On success, an iterator denoting the successfully connected
  391. * endpoint. Otherwise, the end iterator.
  392. *
  393. * @throws asio::system_error Thrown on failure. If the sequence is
  394. * empty, the associated @c error_code is asio::error::not_found.
  395. * Otherwise, contains the error from the last connection attempt.
  396. *
  397. * @note This overload assumes that a default constructed object of type @c
  398. * Iterator represents the end of the sequence. This is a valid assumption for
  399. * iterator types such as @c asio::ip::tcp::resolver::iterator.
  400. */
  401. template <typename Protocol, typename Executor,
  402. typename Iterator, typename ConnectCondition>
  403. Iterator connect(basic_socket<Protocol, Executor>& s,
  404. Iterator begin, ConnectCondition connect_condition,
  405. constraint_t<!is_endpoint_sequence<Iterator>::value> = 0);
  406. /// (Deprecated: Use range overload.) Establishes a socket connection by trying
  407. /// each endpoint in a sequence.
  408. /**
  409. * This function attempts to connect a socket to one of a sequence of
  410. * endpoints. It does this by repeated calls to the socket's @c connect member
  411. * function, once for each endpoint in the sequence, until a connection is
  412. * successfully established.
  413. *
  414. * @param s The socket to be connected. If the socket is already open, it will
  415. * be closed.
  416. *
  417. * @param begin An iterator pointing to the start of a sequence of endpoints.
  418. *
  419. * @param connect_condition A function object that is called prior to each
  420. * connection attempt. The signature of the function object must be:
  421. * @code bool connect_condition(
  422. * const asio::error_code& ec,
  423. * const typename Protocol::endpoint& next); @endcode
  424. * The @c ec parameter contains the result from the most recent connect
  425. * operation. Before the first connection attempt, @c ec is always set to
  426. * indicate success. The @c next parameter is the next endpoint to be tried.
  427. * The function object should return true if the next endpoint should be tried,
  428. * and false if it should be skipped.
  429. *
  430. * @param ec Set to indicate what error occurred, if any. If the sequence is
  431. * empty, set to asio::error::not_found. Otherwise, contains the error
  432. * from the last connection attempt.
  433. *
  434. * @returns On success, an iterator denoting the successfully connected
  435. * endpoint. Otherwise, the end iterator.
  436. *
  437. * @note This overload assumes that a default constructed object of type @c
  438. * Iterator represents the end of the sequence. This is a valid assumption for
  439. * iterator types such as @c asio::ip::tcp::resolver::iterator.
  440. */
  441. template <typename Protocol, typename Executor,
  442. typename Iterator, typename ConnectCondition>
  443. Iterator connect(basic_socket<Protocol, Executor>& s, Iterator begin,
  444. ConnectCondition connect_condition, asio::error_code& ec,
  445. constraint_t<!is_endpoint_sequence<Iterator>::value> = 0);
  446. #endif // !defined(ASIO_NO_DEPRECATED)
  447. /// Establishes a socket connection by trying each endpoint in a sequence.
  448. /**
  449. * This function attempts to connect a socket to one of a sequence of
  450. * endpoints. It does this by repeated calls to the socket's @c connect member
  451. * function, once for each endpoint in the sequence, until a connection is
  452. * successfully established.
  453. *
  454. * @param s The socket to be connected. If the socket is already open, it will
  455. * be closed.
  456. *
  457. * @param begin An iterator pointing to the start of a sequence of endpoints.
  458. *
  459. * @param end An iterator pointing to the end of a sequence of endpoints.
  460. *
  461. * @param connect_condition A function object that is called prior to each
  462. * connection attempt. The signature of the function object must be:
  463. * @code bool connect_condition(
  464. * const asio::error_code& ec,
  465. * const typename Protocol::endpoint& next); @endcode
  466. * The @c ec parameter contains the result from the most recent connect
  467. * operation. Before the first connection attempt, @c ec is always set to
  468. * indicate success. The @c next parameter is the next endpoint to be tried.
  469. * The function object should return true if the next endpoint should be tried,
  470. * and false if it should be skipped.
  471. *
  472. * @returns An iterator denoting the successfully connected endpoint.
  473. *
  474. * @throws asio::system_error Thrown on failure. If the sequence is
  475. * empty, the associated @c error_code is asio::error::not_found.
  476. * Otherwise, contains the error from the last connection attempt.
  477. *
  478. * @par Example
  479. * The following connect condition function object can be used to output
  480. * information about the individual connection attempts:
  481. * @code struct my_connect_condition
  482. * {
  483. * bool operator()(
  484. * const asio::error_code& ec,
  485. * const::tcp::endpoint& next)
  486. * {
  487. * if (ec) std::cout << "Error: " << ec.message() << std::endl;
  488. * std::cout << "Trying: " << next << std::endl;
  489. * return true;
  490. * }
  491. * }; @endcode
  492. * It would be used with the asio::connect function as follows:
  493. * @code tcp::resolver r(my_context);
  494. * tcp::resolver::query q("host", "service");
  495. * tcp::resolver::results_type e = r.resolve(q);
  496. * tcp::socket s(my_context);
  497. * tcp::resolver::results_type::iterator i = asio::connect(
  498. * s, e.begin(), e.end(), my_connect_condition());
  499. * std::cout << "Connected to: " << i->endpoint() << std::endl; @endcode
  500. */
  501. template <typename Protocol, typename Executor,
  502. typename Iterator, typename ConnectCondition>
  503. Iterator connect(basic_socket<Protocol, Executor>& s, Iterator begin,
  504. Iterator end, ConnectCondition connect_condition);
  505. /// Establishes a socket connection by trying each endpoint in a sequence.
  506. /**
  507. * This function attempts to connect a socket to one of a sequence of
  508. * endpoints. It does this by repeated calls to the socket's @c connect member
  509. * function, once for each endpoint in the sequence, until a connection is
  510. * successfully established.
  511. *
  512. * @param s The socket to be connected. If the socket is already open, it will
  513. * be closed.
  514. *
  515. * @param begin An iterator pointing to the start of a sequence of endpoints.
  516. *
  517. * @param end An iterator pointing to the end of a sequence of endpoints.
  518. *
  519. * @param connect_condition A function object that is called prior to each
  520. * connection attempt. The signature of the function object must be:
  521. * @code bool connect_condition(
  522. * const asio::error_code& ec,
  523. * const typename Protocol::endpoint& next); @endcode
  524. * The @c ec parameter contains the result from the most recent connect
  525. * operation. Before the first connection attempt, @c ec is always set to
  526. * indicate success. The @c next parameter is the next endpoint to be tried.
  527. * The function object should return true if the next endpoint should be tried,
  528. * and false if it should be skipped.
  529. *
  530. * @param ec Set to indicate what error occurred, if any. If the sequence is
  531. * empty, set to asio::error::not_found. Otherwise, contains the error
  532. * from the last connection attempt.
  533. *
  534. * @returns On success, an iterator denoting the successfully connected
  535. * endpoint. Otherwise, the end iterator.
  536. *
  537. * @par Example
  538. * The following connect condition function object can be used to output
  539. * information about the individual connection attempts:
  540. * @code struct my_connect_condition
  541. * {
  542. * bool operator()(
  543. * const asio::error_code& ec,
  544. * const::tcp::endpoint& next)
  545. * {
  546. * if (ec) std::cout << "Error: " << ec.message() << std::endl;
  547. * std::cout << "Trying: " << next << std::endl;
  548. * return true;
  549. * }
  550. * }; @endcode
  551. * It would be used with the asio::connect function as follows:
  552. * @code tcp::resolver r(my_context);
  553. * tcp::resolver::query q("host", "service");
  554. * tcp::resolver::results_type e = r.resolve(q);
  555. * tcp::socket s(my_context);
  556. * asio::error_code ec;
  557. * tcp::resolver::results_type::iterator i = asio::connect(
  558. * s, e.begin(), e.end(), my_connect_condition());
  559. * if (ec)
  560. * {
  561. * // An error occurred.
  562. * }
  563. * else
  564. * {
  565. * std::cout << "Connected to: " << i->endpoint() << std::endl;
  566. * } @endcode
  567. */
  568. template <typename Protocol, typename Executor,
  569. typename Iterator, typename ConnectCondition>
  570. Iterator connect(basic_socket<Protocol, Executor>& s,
  571. Iterator begin, Iterator end, ConnectCondition connect_condition,
  572. asio::error_code& ec);
  573. /*@}*/
  574. /**
  575. * @defgroup async_connect asio::async_connect
  576. *
  577. * @brief The @c async_connect function is a composed asynchronous operation
  578. * that establishes a socket connection by trying each endpoint in a sequence.
  579. */
  580. /*@{*/
  581. /// Asynchronously establishes a socket connection by trying each endpoint in a
  582. /// sequence.
  583. /**
  584. * This function attempts to connect a socket to one of a sequence of
  585. * endpoints. It does this by repeated calls to the socket's @c async_connect
  586. * member function, once for each endpoint in the sequence, until a connection
  587. * is successfully established. It is an initiating function for an @ref
  588. * asynchronous_operation, and always returns immediately.
  589. *
  590. * @param s The socket to be connected. If the socket is already open, it will
  591. * be closed.
  592. *
  593. * @param endpoints A sequence of endpoints.
  594. *
  595. * @param token The @ref completion_token that will be used to produce a
  596. * completion handler, which will be called when the connect completes.
  597. * Potential completion tokens include @ref use_future, @ref use_awaitable,
  598. * @ref yield_context, or a function object with the correct completion
  599. * signature. The function signature of the completion handler must be:
  600. * @code void handler(
  601. * // Result of operation. if the sequence is empty, set to
  602. * // asio::error::not_found. Otherwise, contains the
  603. * // error from the last connection attempt.
  604. * const asio::error_code& error,
  605. *
  606. * // On success, the successfully connected endpoint.
  607. * // Otherwise, a default-constructed endpoint.
  608. * const typename Protocol::endpoint& endpoint
  609. * ); @endcode
  610. * Regardless of whether the asynchronous operation completes immediately or
  611. * not, the completion handler will not be invoked from within this function.
  612. * On immediate completion, invocation of the handler will be performed in a
  613. * manner equivalent to using asio::post().
  614. *
  615. * @par Completion Signature
  616. * @code void(asio::error_code, typename Protocol::endpoint) @endcode
  617. *
  618. * @par Example
  619. * @code tcp::resolver r(my_context);
  620. * tcp::resolver::query q("host", "service");
  621. * tcp::socket s(my_context);
  622. *
  623. * // ...
  624. *
  625. * r.async_resolve(q, resolve_handler);
  626. *
  627. * // ...
  628. *
  629. * void resolve_handler(
  630. * const asio::error_code& ec,
  631. * tcp::resolver::results_type results)
  632. * {
  633. * if (!ec)
  634. * {
  635. * asio::async_connect(s, results, connect_handler);
  636. * }
  637. * }
  638. *
  639. * // ...
  640. *
  641. * void connect_handler(
  642. * const asio::error_code& ec,
  643. * const tcp::endpoint& endpoint)
  644. * {
  645. * // ...
  646. * } @endcode
  647. *
  648. * @par Per-Operation Cancellation
  649. * This asynchronous operation supports cancellation for the following
  650. * asio::cancellation_type values:
  651. *
  652. * @li @c cancellation_type::terminal
  653. *
  654. * @li @c cancellation_type::partial
  655. *
  656. * if they are also supported by the socket's @c async_connect operation.
  657. */
  658. template <typename Protocol, typename Executor, typename EndpointSequence,
  659. ASIO_COMPLETION_TOKEN_FOR(void (asio::error_code,
  660. typename Protocol::endpoint)) RangeConnectToken
  661. = default_completion_token_t<Executor>>
  662. auto async_connect(basic_socket<Protocol, Executor>& s,
  663. const EndpointSequence& endpoints,
  664. RangeConnectToken&& token = default_completion_token_t<Executor>(),
  665. constraint_t<is_endpoint_sequence<EndpointSequence>::value> = 0)
  666. -> decltype(
  667. async_initiate<RangeConnectToken,
  668. void (asio::error_code, typename Protocol::endpoint)>(
  669. declval<detail::initiate_async_range_connect<Protocol, Executor>>(),
  670. token, endpoints, declval<detail::default_connect_condition>()));
  671. #if !defined(ASIO_NO_DEPRECATED)
  672. /// (Deprecated: Use range overload.) Asynchronously establishes a socket
  673. /// connection by trying each endpoint in a sequence.
  674. /**
  675. * This function attempts to connect a socket to one of a sequence of
  676. * endpoints. It does this by repeated calls to the socket's @c async_connect
  677. * member function, once for each endpoint in the sequence, until a connection
  678. * is successfully established. It is an initiating function for an @ref
  679. * asynchronous_operation, and always returns immediately.
  680. *
  681. * @param s The socket to be connected. If the socket is already open, it will
  682. * be closed.
  683. *
  684. * @param begin An iterator pointing to the start of a sequence of endpoints.
  685. *
  686. * @param token The @ref completion_token that will be used to produce a
  687. * completion handler, which will be called when the connect completes.
  688. * Potential completion tokens include @ref use_future, @ref use_awaitable,
  689. * @ref yield_context, or a function object with the correct completion
  690. * signature. The function signature of the completion handler must be:
  691. * @code void handler(
  692. * // Result of operation. if the sequence is empty, set to
  693. * // asio::error::not_found. Otherwise, contains the
  694. * // error from the last connection attempt.
  695. * const asio::error_code& error,
  696. *
  697. * // On success, an iterator denoting the successfully
  698. * // connected endpoint. Otherwise, the end iterator.
  699. * Iterator iterator
  700. * ); @endcode
  701. * Regardless of whether the asynchronous operation completes immediately or
  702. * not, the completion handler will not be invoked from within this function.
  703. * On immediate completion, invocation of the handler will be performed in a
  704. * manner equivalent to using asio::post().
  705. *
  706. * @par Completion Signature
  707. * @code void(asio::error_code, Iterator) @endcode
  708. *
  709. * @note This overload assumes that a default constructed object of type @c
  710. * Iterator represents the end of the sequence. This is a valid assumption for
  711. * iterator types such as @c asio::ip::tcp::resolver::iterator.
  712. *
  713. * @par Per-Operation Cancellation
  714. * This asynchronous operation supports cancellation for the following
  715. * asio::cancellation_type values:
  716. *
  717. * @li @c cancellation_type::terminal
  718. *
  719. * @li @c cancellation_type::partial
  720. *
  721. * if they are also supported by the socket's @c async_connect operation.
  722. */
  723. template <typename Protocol, typename Executor, typename Iterator,
  724. ASIO_COMPLETION_TOKEN_FOR(void (asio::error_code,
  725. Iterator)) IteratorConnectToken = default_completion_token_t<Executor>>
  726. auto async_connect(basic_socket<Protocol, Executor>& s, Iterator begin,
  727. IteratorConnectToken&& token = default_completion_token_t<Executor>(),
  728. constraint_t<!is_endpoint_sequence<Iterator>::value> = 0)
  729. -> decltype(
  730. async_initiate<IteratorConnectToken,
  731. void (asio::error_code, Iterator)>(
  732. declval<detail::initiate_async_iterator_connect<Protocol, Executor>>(),
  733. token, begin, Iterator(),
  734. declval<detail::default_connect_condition>()));
  735. #endif // !defined(ASIO_NO_DEPRECATED)
  736. /// Asynchronously establishes a socket connection by trying each endpoint in a
  737. /// sequence.
  738. /**
  739. * This function attempts to connect a socket to one of a sequence of
  740. * endpoints. It does this by repeated calls to the socket's @c async_connect
  741. * member function, once for each endpoint in the sequence, until a connection
  742. * is successfully established. It is an initiating function for an @ref
  743. * asynchronous_operation, and always returns immediately.
  744. *
  745. * @param s The socket to be connected. If the socket is already open, it will
  746. * be closed.
  747. *
  748. * @param begin An iterator pointing to the start of a sequence of endpoints.
  749. *
  750. * @param end An iterator pointing to the end of a sequence of endpoints.
  751. *
  752. * @param token The @ref completion_token that will be used to produce a
  753. * completion handler, which will be called when the connect completes.
  754. * Potential completion tokens include @ref use_future, @ref use_awaitable,
  755. * @ref yield_context, or a function object with the correct completion
  756. * signature. The function signature of the completion handler must be:
  757. * @code void handler(
  758. * // Result of operation. if the sequence is empty, set to
  759. * // asio::error::not_found. Otherwise, contains the
  760. * // error from the last connection attempt.
  761. * const asio::error_code& error,
  762. *
  763. * // On success, an iterator denoting the successfully
  764. * // connected endpoint. Otherwise, the end iterator.
  765. * Iterator iterator
  766. * ); @endcode
  767. * Regardless of whether the asynchronous operation completes immediately or
  768. * not, the completion handler will not be invoked from within this function.
  769. * On immediate completion, invocation of the handler will be performed in a
  770. * manner equivalent to using asio::post().
  771. *
  772. * @par Completion Signature
  773. * @code void(asio::error_code, Iterator) @endcode
  774. *
  775. * @par Example
  776. * @code std::vector<tcp::endpoint> endpoints = ...;
  777. * tcp::socket s(my_context);
  778. * asio::async_connect(s,
  779. * endpoints.begin(), endpoints.end(),
  780. * connect_handler);
  781. *
  782. * // ...
  783. *
  784. * void connect_handler(
  785. * const asio::error_code& ec,
  786. * std::vector<tcp::endpoint>::iterator i)
  787. * {
  788. * // ...
  789. * } @endcode
  790. *
  791. * @par Per-Operation Cancellation
  792. * This asynchronous operation supports cancellation for the following
  793. * asio::cancellation_type values:
  794. *
  795. * @li @c cancellation_type::terminal
  796. *
  797. * @li @c cancellation_type::partial
  798. *
  799. * if they are also supported by the socket's @c async_connect operation.
  800. */
  801. template <typename Protocol, typename Executor, typename Iterator,
  802. ASIO_COMPLETION_TOKEN_FOR(void (asio::error_code,
  803. Iterator)) IteratorConnectToken = default_completion_token_t<Executor>>
  804. auto async_connect(
  805. basic_socket<Protocol, Executor>& s, Iterator begin, Iterator end,
  806. IteratorConnectToken&& token = default_completion_token_t<Executor>())
  807. -> decltype(
  808. async_initiate<IteratorConnectToken,
  809. void (asio::error_code, Iterator)>(
  810. declval<detail::initiate_async_iterator_connect<Protocol, Executor>>(),
  811. token, begin, end, declval<detail::default_connect_condition>()));
  812. /// Asynchronously establishes a socket connection by trying each endpoint in a
  813. /// sequence.
  814. /**
  815. * This function attempts to connect a socket to one of a sequence of
  816. * endpoints. It does this by repeated calls to the socket's @c async_connect
  817. * member function, once for each endpoint in the sequence, until a connection
  818. * is successfully established. It is an initiating function for an @ref
  819. * asynchronous_operation, and always returns immediately.
  820. *
  821. * @param s The socket to be connected. If the socket is already open, it will
  822. * be closed.
  823. *
  824. * @param endpoints A sequence of endpoints.
  825. *
  826. * @param connect_condition A function object that is called prior to each
  827. * connection attempt. The signature of the function object must be:
  828. * @code bool connect_condition(
  829. * const asio::error_code& ec,
  830. * const typename Protocol::endpoint& next); @endcode
  831. * The @c ec parameter contains the result from the most recent connect
  832. * operation. Before the first connection attempt, @c ec is always set to
  833. * indicate success. The @c next parameter is the next endpoint to be tried.
  834. * The function object should return true if the next endpoint should be tried,
  835. * and false if it should be skipped.
  836. *
  837. * @param token The @ref completion_token that will be used to produce a
  838. * completion handler, which will be called when the connect completes.
  839. * Potential completion tokens include @ref use_future, @ref use_awaitable,
  840. * @ref yield_context, or a function object with the correct completion
  841. * signature. The function signature of the completion handler must be:
  842. * @code void handler(
  843. * // Result of operation. if the sequence is empty, set to
  844. * // asio::error::not_found. Otherwise, contains the
  845. * // error from the last connection attempt.
  846. * const asio::error_code& error,
  847. *
  848. * // On success, an iterator denoting the successfully
  849. * // connected endpoint. Otherwise, the end iterator.
  850. * Iterator iterator
  851. * ); @endcode
  852. * Regardless of whether the asynchronous operation completes immediately or
  853. * not, the completion handler will not be invoked from within this function.
  854. * On immediate completion, invocation of the handler will be performed in a
  855. * manner equivalent to using asio::post().
  856. *
  857. * @par Completion Signature
  858. * @code void(asio::error_code, typename Protocol::endpoint) @endcode
  859. *
  860. * @par Example
  861. * The following connect condition function object can be used to output
  862. * information about the individual connection attempts:
  863. * @code struct my_connect_condition
  864. * {
  865. * bool operator()(
  866. * const asio::error_code& ec,
  867. * const::tcp::endpoint& next)
  868. * {
  869. * if (ec) std::cout << "Error: " << ec.message() << std::endl;
  870. * std::cout << "Trying: " << next << std::endl;
  871. * return true;
  872. * }
  873. * }; @endcode
  874. * It would be used with the asio::connect function as follows:
  875. * @code tcp::resolver r(my_context);
  876. * tcp::resolver::query q("host", "service");
  877. * tcp::socket s(my_context);
  878. *
  879. * // ...
  880. *
  881. * r.async_resolve(q, resolve_handler);
  882. *
  883. * // ...
  884. *
  885. * void resolve_handler(
  886. * const asio::error_code& ec,
  887. * tcp::resolver::results_type results)
  888. * {
  889. * if (!ec)
  890. * {
  891. * asio::async_connect(s, results,
  892. * my_connect_condition(),
  893. * connect_handler);
  894. * }
  895. * }
  896. *
  897. * // ...
  898. *
  899. * void connect_handler(
  900. * const asio::error_code& ec,
  901. * const tcp::endpoint& endpoint)
  902. * {
  903. * if (ec)
  904. * {
  905. * // An error occurred.
  906. * }
  907. * else
  908. * {
  909. * std::cout << "Connected to: " << endpoint << std::endl;
  910. * }
  911. * } @endcode
  912. *
  913. * @par Per-Operation Cancellation
  914. * This asynchronous operation supports cancellation for the following
  915. * asio::cancellation_type values:
  916. *
  917. * @li @c cancellation_type::terminal
  918. *
  919. * @li @c cancellation_type::partial
  920. *
  921. * if they are also supported by the socket's @c async_connect operation.
  922. */
  923. template <typename Protocol, typename Executor,
  924. typename EndpointSequence, typename ConnectCondition,
  925. ASIO_COMPLETION_TOKEN_FOR(void (asio::error_code,
  926. typename Protocol::endpoint)) RangeConnectToken
  927. = default_completion_token_t<Executor>>
  928. auto async_connect(basic_socket<Protocol, Executor>& s,
  929. const EndpointSequence& endpoints, ConnectCondition connect_condition,
  930. RangeConnectToken&& token = default_completion_token_t<Executor>(),
  931. constraint_t<is_endpoint_sequence<EndpointSequence>::value> = 0)
  932. -> decltype(
  933. async_initiate<RangeConnectToken,
  934. void (asio::error_code, typename Protocol::endpoint)>(
  935. declval<detail::initiate_async_range_connect<Protocol, Executor>>(),
  936. token, endpoints, connect_condition));
  937. #if !defined(ASIO_NO_DEPRECATED)
  938. /// (Deprecated: Use range overload.) Asynchronously establishes a socket
  939. /// connection by trying each endpoint in a sequence.
  940. /**
  941. * This function attempts to connect a socket to one of a sequence of
  942. * endpoints. It does this by repeated calls to the socket's @c async_connect
  943. * member function, once for each endpoint in the sequence, until a connection
  944. * is successfully established. It is an initiating function for an @ref
  945. * asynchronous_operation, and always returns immediately.
  946. *
  947. * @param s The socket to be connected. If the socket is already open, it will
  948. * be closed.
  949. *
  950. * @param begin An iterator pointing to the start of a sequence of endpoints.
  951. *
  952. * @param connect_condition A function object that is called prior to each
  953. * connection attempt. The signature of the function object must be:
  954. * @code bool connect_condition(
  955. * const asio::error_code& ec,
  956. * const typename Protocol::endpoint& next); @endcode
  957. * The @c ec parameter contains the result from the most recent connect
  958. * operation. Before the first connection attempt, @c ec is always set to
  959. * indicate success. The @c next parameter is the next endpoint to be tried.
  960. * The function object should return true if the next endpoint should be tried,
  961. * and false if it should be skipped.
  962. *
  963. * @param token The @ref completion_token that will be used to produce a
  964. * completion handler, which will be called when the connect completes.
  965. * Potential completion tokens include @ref use_future, @ref use_awaitable,
  966. * @ref yield_context, or a function object with the correct completion
  967. * signature. The function signature of the completion handler must be:
  968. * @code void handler(
  969. * // Result of operation. if the sequence is empty, set to
  970. * // asio::error::not_found. Otherwise, contains the
  971. * // error from the last connection attempt.
  972. * const asio::error_code& error,
  973. *
  974. * // On success, an iterator denoting the successfully
  975. * // connected endpoint. Otherwise, the end iterator.
  976. * Iterator iterator
  977. * ); @endcode
  978. * Regardless of whether the asynchronous operation completes immediately or
  979. * not, the completion handler will not be invoked from within this function.
  980. * On immediate completion, invocation of the handler will be performed in a
  981. * manner equivalent to using asio::post().
  982. *
  983. * @par Completion Signature
  984. * @code void(asio::error_code, Iterator) @endcode
  985. *
  986. * @note This overload assumes that a default constructed object of type @c
  987. * Iterator represents the end of the sequence. This is a valid assumption for
  988. * iterator types such as @c asio::ip::tcp::resolver::iterator.
  989. *
  990. * @par Per-Operation Cancellation
  991. * This asynchronous operation supports cancellation for the following
  992. * asio::cancellation_type values:
  993. *
  994. * @li @c cancellation_type::terminal
  995. *
  996. * @li @c cancellation_type::partial
  997. *
  998. * if they are also supported by the socket's @c async_connect operation.
  999. */
  1000. template <typename Protocol, typename Executor,
  1001. typename Iterator, typename ConnectCondition,
  1002. ASIO_COMPLETION_TOKEN_FOR(void (asio::error_code,
  1003. Iterator)) IteratorConnectToken = default_completion_token_t<Executor>>
  1004. auto async_connect(basic_socket<Protocol, Executor>& s,
  1005. Iterator begin, ConnectCondition connect_condition,
  1006. IteratorConnectToken&& token = default_completion_token_t<Executor>(),
  1007. constraint_t<!is_endpoint_sequence<Iterator>::value> = 0)
  1008. -> decltype(
  1009. async_initiate<IteratorConnectToken,
  1010. void (asio::error_code, Iterator)>(
  1011. declval<detail::initiate_async_iterator_connect<Protocol, Executor>>(),
  1012. token, begin, Iterator(), connect_condition));
  1013. #endif // !defined(ASIO_NO_DEPRECATED)
  1014. /// Asynchronously establishes a socket connection by trying each endpoint in a
  1015. /// sequence.
  1016. /**
  1017. * This function attempts to connect a socket to one of a sequence of
  1018. * endpoints. It does this by repeated calls to the socket's @c async_connect
  1019. * member function, once for each endpoint in the sequence, until a connection
  1020. * is successfully established. It is an initiating function for an @ref
  1021. * asynchronous_operation, and always returns immediately.
  1022. *
  1023. * @param s The socket to be connected. If the socket is already open, it will
  1024. * be closed.
  1025. *
  1026. * @param begin An iterator pointing to the start of a sequence of endpoints.
  1027. *
  1028. * @param end An iterator pointing to the end of a sequence of endpoints.
  1029. *
  1030. * @param connect_condition A function object that is called prior to each
  1031. * connection attempt. The signature of the function object must be:
  1032. * @code bool connect_condition(
  1033. * const asio::error_code& ec,
  1034. * const typename Protocol::endpoint& next); @endcode
  1035. * The @c ec parameter contains the result from the most recent connect
  1036. * operation. Before the first connection attempt, @c ec is always set to
  1037. * indicate success. The @c next parameter is the next endpoint to be tried.
  1038. * The function object should return true if the next endpoint should be tried,
  1039. * and false if it should be skipped.
  1040. *
  1041. * @param token The @ref completion_token that will be used to produce a
  1042. * completion handler, which will be called when the connect completes.
  1043. * Potential completion tokens include @ref use_future, @ref use_awaitable,
  1044. * @ref yield_context, or a function object with the correct completion
  1045. * signature. The function signature of the completion handler must be:
  1046. * @code void handler(
  1047. * // Result of operation. if the sequence is empty, set to
  1048. * // asio::error::not_found. Otherwise, contains the
  1049. * // error from the last connection attempt.
  1050. * const asio::error_code& error,
  1051. *
  1052. * // On success, an iterator denoting the successfully
  1053. * // connected endpoint. Otherwise, the end iterator.
  1054. * Iterator iterator
  1055. * ); @endcode
  1056. * Regardless of whether the asynchronous operation completes immediately or
  1057. * not, the completion handler will not be invoked from within this function.
  1058. * On immediate completion, invocation of the handler will be performed in a
  1059. * manner equivalent to using asio::post().
  1060. *
  1061. * @par Completion Signature
  1062. * @code void(asio::error_code, Iterator) @endcode
  1063. *
  1064. * @par Example
  1065. * The following connect condition function object can be used to output
  1066. * information about the individual connection attempts:
  1067. * @code struct my_connect_condition
  1068. * {
  1069. * bool operator()(
  1070. * const asio::error_code& ec,
  1071. * const::tcp::endpoint& next)
  1072. * {
  1073. * if (ec) std::cout << "Error: " << ec.message() << std::endl;
  1074. * std::cout << "Trying: " << next << std::endl;
  1075. * return true;
  1076. * }
  1077. * }; @endcode
  1078. * It would be used with the asio::connect function as follows:
  1079. * @code tcp::resolver r(my_context);
  1080. * tcp::resolver::query q("host", "service");
  1081. * tcp::socket s(my_context);
  1082. *
  1083. * // ...
  1084. *
  1085. * r.async_resolve(q, resolve_handler);
  1086. *
  1087. * // ...
  1088. *
  1089. * void resolve_handler(
  1090. * const asio::error_code& ec,
  1091. * tcp::resolver::iterator i)
  1092. * {
  1093. * if (!ec)
  1094. * {
  1095. * tcp::resolver::iterator end;
  1096. * asio::async_connect(s, i, end,
  1097. * my_connect_condition(),
  1098. * connect_handler);
  1099. * }
  1100. * }
  1101. *
  1102. * // ...
  1103. *
  1104. * void connect_handler(
  1105. * const asio::error_code& ec,
  1106. * tcp::resolver::iterator i)
  1107. * {
  1108. * if (ec)
  1109. * {
  1110. * // An error occurred.
  1111. * }
  1112. * else
  1113. * {
  1114. * std::cout << "Connected to: " << i->endpoint() << std::endl;
  1115. * }
  1116. * } @endcode
  1117. *
  1118. * @par Per-Operation Cancellation
  1119. * This asynchronous operation supports cancellation for the following
  1120. * asio::cancellation_type values:
  1121. *
  1122. * @li @c cancellation_type::terminal
  1123. *
  1124. * @li @c cancellation_type::partial
  1125. *
  1126. * if they are also supported by the socket's @c async_connect operation.
  1127. */
  1128. template <typename Protocol, typename Executor,
  1129. typename Iterator, typename ConnectCondition,
  1130. ASIO_COMPLETION_TOKEN_FOR(void (asio::error_code,
  1131. Iterator)) IteratorConnectToken = default_completion_token_t<Executor>>
  1132. auto async_connect(basic_socket<Protocol, Executor>& s,
  1133. Iterator begin, Iterator end, ConnectCondition connect_condition,
  1134. IteratorConnectToken&& token = default_completion_token_t<Executor>())
  1135. -> decltype(
  1136. async_initiate<IteratorConnectToken,
  1137. void (asio::error_code, Iterator)>(
  1138. declval<detail::initiate_async_iterator_connect<Protocol, Executor>>(),
  1139. token, begin, end, connect_condition));
  1140. /*@}*/
  1141. } // namespace asio
  1142. #include "asio/detail/pop_options.hpp"
  1143. #include "asio/impl/connect.hpp"
  1144. #endif