connect.hpp 48 KB

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