connect.hpp 45 KB

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