connect.hpp 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182
  1. //
  2. // connect.hpp
  3. // ~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2024 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. constraint_t<is_endpoint_sequence<EndpointSequence>::value> = 0);
  87. /// Establishes a socket connection by trying each endpoint in a sequence.
  88. /**
  89. * This function attempts to connect a socket to one of a sequence of
  90. * endpoints. It does this by repeated calls to the socket's @c connect member
  91. * function, once for each endpoint in the sequence, until a connection is
  92. * successfully established.
  93. *
  94. * @param s The socket to be connected. If the socket is already open, it will
  95. * be closed.
  96. *
  97. * @param endpoints A sequence of endpoints.
  98. *
  99. * @param ec Set to indicate what error occurred, if any. If the sequence is
  100. * empty, set to boost::asio::error::not_found. Otherwise, contains the error
  101. * from the last connection attempt.
  102. *
  103. * @returns On success, the successfully connected endpoint. Otherwise, a
  104. * default-constructed endpoint.
  105. *
  106. * @par Example
  107. * @code tcp::resolver r(my_context);
  108. * tcp::resolver::query q("host", "service");
  109. * tcp::socket s(my_context);
  110. * boost::system::error_code ec;
  111. * boost::asio::connect(s, r.resolve(q), ec);
  112. * if (ec)
  113. * {
  114. * // An error occurred.
  115. * } @endcode
  116. */
  117. template <typename Protocol, typename Executor, typename EndpointSequence>
  118. typename Protocol::endpoint connect(basic_socket<Protocol, Executor>& s,
  119. const EndpointSequence& endpoints, boost::system::error_code& ec,
  120. constraint_t<is_endpoint_sequence<EndpointSequence>::value> = 0);
  121. #if !defined(BOOST_ASIO_NO_DEPRECATED)
  122. /// (Deprecated: Use range overload.) Establishes a socket connection by trying
  123. /// each endpoint in a sequence.
  124. /**
  125. * This function attempts to connect a socket to one of a sequence of
  126. * endpoints. It does this by repeated calls to the socket's @c connect member
  127. * function, once for each endpoint in the sequence, until a connection is
  128. * successfully established.
  129. *
  130. * @param s The socket to be connected. If the socket is already open, it will
  131. * be closed.
  132. *
  133. * @param begin An iterator pointing to the start of a sequence of endpoints.
  134. *
  135. * @returns On success, an iterator denoting the successfully connected
  136. * endpoint. Otherwise, the end iterator.
  137. *
  138. * @throws boost::system::system_error Thrown on failure. If the sequence is
  139. * empty, the associated @c error_code is boost::asio::error::not_found.
  140. * Otherwise, contains the error from the last connection attempt.
  141. *
  142. * @note This overload assumes that a default constructed object of type @c
  143. * Iterator represents the end of the sequence. This is a valid assumption for
  144. * iterator types such as @c boost::asio::ip::tcp::resolver::iterator.
  145. */
  146. template <typename Protocol, typename Executor, typename Iterator>
  147. Iterator connect(basic_socket<Protocol, Executor>& s, Iterator begin,
  148. constraint_t<!is_endpoint_sequence<Iterator>::value> = 0);
  149. /// (Deprecated: Use range overload.) Establishes a socket connection by trying
  150. /// each endpoint in a sequence.
  151. /**
  152. * This function attempts to connect a socket to one of a sequence of
  153. * endpoints. It does this by repeated calls to the socket's @c connect member
  154. * function, once for each endpoint in the sequence, until a connection is
  155. * successfully established.
  156. *
  157. * @param s The socket to be connected. If the socket is already open, it will
  158. * be closed.
  159. *
  160. * @param begin An iterator pointing to the start of a sequence of endpoints.
  161. *
  162. * @param ec Set to indicate what error occurred, if any. If the sequence is
  163. * empty, set to boost::asio::error::not_found. Otherwise, contains the error
  164. * from the last connection attempt.
  165. *
  166. * @returns On success, an iterator denoting the successfully connected
  167. * endpoint. Otherwise, the end iterator.
  168. *
  169. * @note This overload assumes that a default constructed object of type @c
  170. * Iterator represents the end of the sequence. This is a valid assumption for
  171. * iterator types such as @c boost::asio::ip::tcp::resolver::iterator.
  172. */
  173. template <typename Protocol, typename Executor, typename Iterator>
  174. Iterator connect(basic_socket<Protocol, Executor>& s,
  175. Iterator begin, boost::system::error_code& ec,
  176. constraint_t<!is_endpoint_sequence<Iterator>::value> = 0);
  177. #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
  178. /// Establishes a socket connection by trying each endpoint in a sequence.
  179. /**
  180. * This function attempts to connect a socket to one of a sequence of
  181. * endpoints. It does this by repeated calls to the socket's @c connect member
  182. * function, once for each endpoint in the sequence, until a connection is
  183. * successfully established.
  184. *
  185. * @param s The socket to be connected. If the socket is already open, it will
  186. * be closed.
  187. *
  188. * @param begin An iterator pointing to the start of a sequence of endpoints.
  189. *
  190. * @param end An iterator pointing to the end of a sequence of endpoints.
  191. *
  192. * @returns An iterator denoting the successfully connected endpoint.
  193. *
  194. * @throws boost::system::system_error Thrown on failure. If the sequence is
  195. * empty, the associated @c error_code is boost::asio::error::not_found.
  196. * Otherwise, contains the error from the last connection attempt.
  197. *
  198. * @par Example
  199. * @code tcp::resolver r(my_context);
  200. * tcp::resolver::query q("host", "service");
  201. * tcp::resolver::results_type e = r.resolve(q);
  202. * tcp::socket s(my_context);
  203. * boost::asio::connect(s, e.begin(), e.end()); @endcode
  204. */
  205. template <typename Protocol, typename Executor, typename Iterator>
  206. Iterator connect(basic_socket<Protocol, Executor>& s,
  207. Iterator begin, Iterator end);
  208. /// Establishes a socket connection by trying each endpoint in a sequence.
  209. /**
  210. * This function attempts to connect a socket to one of a sequence of
  211. * endpoints. It does this by repeated calls to the socket's @c connect member
  212. * function, once for each endpoint in the sequence, until a connection is
  213. * successfully established.
  214. *
  215. * @param s The socket to be connected. If the socket is already open, it will
  216. * be closed.
  217. *
  218. * @param begin An iterator pointing to the start of a sequence of endpoints.
  219. *
  220. * @param end An iterator pointing to the end of a sequence of endpoints.
  221. *
  222. * @param ec Set to indicate what error occurred, if any. If the sequence is
  223. * empty, set to boost::asio::error::not_found. Otherwise, contains the error
  224. * from the last connection attempt.
  225. *
  226. * @returns On success, an iterator denoting the successfully connected
  227. * endpoint. Otherwise, the end iterator.
  228. *
  229. * @par Example
  230. * @code tcp::resolver r(my_context);
  231. * tcp::resolver::query q("host", "service");
  232. * tcp::resolver::results_type e = r.resolve(q);
  233. * tcp::socket s(my_context);
  234. * boost::system::error_code ec;
  235. * boost::asio::connect(s, e.begin(), e.end(), ec);
  236. * if (ec)
  237. * {
  238. * // An error occurred.
  239. * } @endcode
  240. */
  241. template <typename Protocol, typename Executor, typename Iterator>
  242. Iterator connect(basic_socket<Protocol, Executor>& s,
  243. Iterator begin, Iterator end, boost::system::error_code& ec);
  244. /// Establishes a socket connection by trying each endpoint in a sequence.
  245. /**
  246. * This function attempts to connect a socket to one of a sequence of
  247. * endpoints. It does this by repeated calls to the socket's @c connect member
  248. * function, once for each endpoint in the sequence, until a connection is
  249. * successfully established.
  250. *
  251. * @param s The socket to be connected. If the socket is already open, it will
  252. * be closed.
  253. *
  254. * @param endpoints A sequence of endpoints.
  255. *
  256. * @param connect_condition A function object that is called prior to each
  257. * connection attempt. The signature of the function object must be:
  258. * @code bool connect_condition(
  259. * const boost::system::error_code& ec,
  260. * const typename Protocol::endpoint& next); @endcode
  261. * The @c ec parameter contains the result from the most recent connect
  262. * operation. Before the first connection attempt, @c ec is always set to
  263. * indicate success. The @c next parameter is the next endpoint to be tried.
  264. * The function object should return true if the next endpoint should be tried,
  265. * and false if it should be skipped.
  266. *
  267. * @returns The successfully connected endpoint.
  268. *
  269. * @throws boost::system::system_error Thrown on failure. If the sequence is
  270. * empty, the associated @c error_code is boost::asio::error::not_found.
  271. * Otherwise, contains the error from the last connection attempt.
  272. *
  273. * @par Example
  274. * The following connect condition function object can be used to output
  275. * information about the individual connection attempts:
  276. * @code struct my_connect_condition
  277. * {
  278. * bool operator()(
  279. * const boost::system::error_code& ec,
  280. * const::tcp::endpoint& next)
  281. * {
  282. * if (ec) std::cout << "Error: " << ec.message() << std::endl;
  283. * std::cout << "Trying: " << next << std::endl;
  284. * return true;
  285. * }
  286. * }; @endcode
  287. * It would be used with the boost::asio::connect function as follows:
  288. * @code tcp::resolver r(my_context);
  289. * tcp::resolver::query q("host", "service");
  290. * tcp::socket s(my_context);
  291. * tcp::endpoint e = boost::asio::connect(s,
  292. * r.resolve(q), my_connect_condition());
  293. * std::cout << "Connected to: " << e << std::endl; @endcode
  294. */
  295. template <typename Protocol, typename Executor,
  296. typename EndpointSequence, typename ConnectCondition>
  297. typename Protocol::endpoint connect(basic_socket<Protocol, Executor>& s,
  298. const EndpointSequence& endpoints, ConnectCondition connect_condition,
  299. constraint_t<is_endpoint_sequence<EndpointSequence>::value> = 0);
  300. /// Establishes a socket connection by trying each endpoint in a sequence.
  301. /**
  302. * This function attempts to connect a socket to one of a sequence of
  303. * endpoints. It does this by repeated calls to the socket's @c connect member
  304. * function, once for each endpoint in the sequence, until a connection is
  305. * successfully established.
  306. *
  307. * @param s The socket to be connected. If the socket is already open, it will
  308. * be closed.
  309. *
  310. * @param endpoints A sequence of endpoints.
  311. *
  312. * @param connect_condition A function object that is called prior to each
  313. * connection attempt. The signature of the function object must be:
  314. * @code bool connect_condition(
  315. * const boost::system::error_code& ec,
  316. * const typename Protocol::endpoint& next); @endcode
  317. * The @c ec parameter contains the result from the most recent connect
  318. * operation. Before the first connection attempt, @c ec is always set to
  319. * indicate success. The @c next parameter is the next endpoint to be tried.
  320. * The function object should return true if the next endpoint should be tried,
  321. * and false if it should be skipped.
  322. *
  323. * @param ec Set to indicate what error occurred, if any. If the sequence is
  324. * empty, set to boost::asio::error::not_found. Otherwise, contains the error
  325. * from the last connection attempt.
  326. *
  327. * @returns On success, the successfully connected endpoint. Otherwise, a
  328. * default-constructed endpoint.
  329. *
  330. * @par Example
  331. * The following connect condition function object can be used to output
  332. * information about the individual connection attempts:
  333. * @code struct my_connect_condition
  334. * {
  335. * bool operator()(
  336. * const boost::system::error_code& ec,
  337. * const::tcp::endpoint& next)
  338. * {
  339. * if (ec) std::cout << "Error: " << ec.message() << std::endl;
  340. * std::cout << "Trying: " << next << std::endl;
  341. * return true;
  342. * }
  343. * }; @endcode
  344. * It would be used with the boost::asio::connect function as follows:
  345. * @code tcp::resolver r(my_context);
  346. * tcp::resolver::query q("host", "service");
  347. * tcp::socket s(my_context);
  348. * boost::system::error_code ec;
  349. * tcp::endpoint e = boost::asio::connect(s,
  350. * r.resolve(q), my_connect_condition(), ec);
  351. * if (ec)
  352. * {
  353. * // An error occurred.
  354. * }
  355. * else
  356. * {
  357. * std::cout << "Connected to: " << e << std::endl;
  358. * } @endcode
  359. */
  360. template <typename Protocol, typename Executor,
  361. typename EndpointSequence, typename ConnectCondition>
  362. typename Protocol::endpoint connect(basic_socket<Protocol, Executor>& s,
  363. const EndpointSequence& endpoints, ConnectCondition connect_condition,
  364. boost::system::error_code& ec,
  365. constraint_t<is_endpoint_sequence<EndpointSequence>::value> = 0);
  366. #if !defined(BOOST_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 boost::system::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 boost::system::system_error Thrown on failure. If the sequence is
  395. * empty, the associated @c error_code is boost::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 boost::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. constraint_t<!is_endpoint_sequence<Iterator>::value> = 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 boost::system::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 boost::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 boost::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, boost::system::error_code& ec,
  446. constraint_t<!is_endpoint_sequence<Iterator>::value> = 0);
  447. #endif // !defined(BOOST_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 boost::system::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 boost::system::system_error Thrown on failure. If the sequence is
  476. * empty, the associated @c error_code is boost::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 boost::system::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 boost::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 = boost::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 boost::system::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 boost::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 boost::system::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 boost::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. * boost::system::error_code ec;
  558. * tcp::resolver::results_type::iterator i = boost::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. boost::system::error_code& ec);
  574. /*@}*/
  575. /**
  576. * @defgroup async_connect boost::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. * // boost::asio::error::not_found. Otherwise, contains the
  604. * // error from the last connection attempt.
  605. * const boost::system::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 boost::asio::post().
  615. *
  616. * @par Completion Signature
  617. * @code void(boost::system::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 boost::system::error_code& ec,
  632. * tcp::resolver::results_type results)
  633. * {
  634. * if (!ec)
  635. * {
  636. * boost::asio::async_connect(s, results, connect_handler);
  637. * }
  638. * }
  639. *
  640. * // ...
  641. *
  642. * void connect_handler(
  643. * const boost::system::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. * boost::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. BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code,
  661. typename Protocol::endpoint)) RangeConnectToken
  662. = default_completion_token_t<Executor>>
  663. auto async_connect(basic_socket<Protocol, Executor>& s,
  664. const EndpointSequence& endpoints,
  665. RangeConnectToken&& token = default_completion_token_t<Executor>(),
  666. constraint_t<is_endpoint_sequence<EndpointSequence>::value> = 0)
  667. -> decltype(
  668. async_initiate<RangeConnectToken,
  669. void (boost::system::error_code, typename Protocol::endpoint)>(
  670. declval<detail::initiate_async_range_connect<Protocol, Executor>>(),
  671. token, endpoints, declval<detail::default_connect_condition>()));
  672. #if !defined(BOOST_ASIO_NO_DEPRECATED)
  673. /// (Deprecated: Use range overload.) Asynchronously establishes a socket
  674. /// connection by trying each endpoint in a sequence.
  675. /**
  676. * This function attempts to connect a socket to one of a sequence of
  677. * endpoints. It does this by repeated calls to the socket's @c async_connect
  678. * member function, once for each endpoint in the sequence, until a connection
  679. * is successfully established. It is an initiating function for an @ref
  680. * asynchronous_operation, and always returns immediately.
  681. *
  682. * @param s The socket to be connected. If the socket is already open, it will
  683. * be closed.
  684. *
  685. * @param begin An iterator pointing to the start of a sequence of endpoints.
  686. *
  687. * @param token The @ref completion_token that will be used to produce a
  688. * completion handler, which will be called when the connect completes.
  689. * Potential completion tokens include @ref use_future, @ref use_awaitable,
  690. * @ref yield_context, or a function object with the correct completion
  691. * signature. The function signature of the completion handler must be:
  692. * @code void handler(
  693. * // Result of operation. if the sequence is empty, set to
  694. * // boost::asio::error::not_found. Otherwise, contains the
  695. * // error from the last connection attempt.
  696. * const boost::system::error_code& error,
  697. *
  698. * // On success, an iterator denoting the successfully
  699. * // connected endpoint. Otherwise, the end iterator.
  700. * Iterator iterator
  701. * ); @endcode
  702. * Regardless of whether the asynchronous operation completes immediately or
  703. * not, the completion handler will not be invoked from within this function.
  704. * On immediate completion, invocation of the handler will be performed in a
  705. * manner equivalent to using boost::asio::post().
  706. *
  707. * @par Completion Signature
  708. * @code void(boost::system::error_code, Iterator) @endcode
  709. *
  710. * @note This overload assumes that a default constructed object of type @c
  711. * Iterator represents the end of the sequence. This is a valid assumption for
  712. * iterator types such as @c boost::asio::ip::tcp::resolver::iterator.
  713. *
  714. * @par Per-Operation Cancellation
  715. * This asynchronous operation supports cancellation for the following
  716. * boost::asio::cancellation_type values:
  717. *
  718. * @li @c cancellation_type::terminal
  719. *
  720. * @li @c cancellation_type::partial
  721. *
  722. * if they are also supported by the socket's @c async_connect operation.
  723. */
  724. template <typename Protocol, typename Executor, typename Iterator,
  725. BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code,
  726. Iterator)) IteratorConnectToken = default_completion_token_t<Executor>>
  727. auto async_connect(basic_socket<Protocol, Executor>& s, Iterator begin,
  728. IteratorConnectToken&& token = default_completion_token_t<Executor>(),
  729. constraint_t<!is_endpoint_sequence<Iterator>::value> = 0)
  730. -> decltype(
  731. async_initiate<IteratorConnectToken,
  732. void (boost::system::error_code, Iterator)>(
  733. declval<detail::initiate_async_iterator_connect<Protocol, Executor>>(),
  734. token, begin, Iterator(),
  735. declval<detail::default_connect_condition>()));
  736. #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
  737. /// Asynchronously establishes a socket connection by trying each endpoint in a
  738. /// sequence.
  739. /**
  740. * This function attempts to connect a socket to one of a sequence of
  741. * endpoints. It does this by repeated calls to the socket's @c async_connect
  742. * member function, once for each endpoint in the sequence, until a connection
  743. * is successfully established. It is an initiating function for an @ref
  744. * asynchronous_operation, and always returns immediately.
  745. *
  746. * @param s The socket to be connected. If the socket is already open, it will
  747. * be closed.
  748. *
  749. * @param begin An iterator pointing to the start of a sequence of endpoints.
  750. *
  751. * @param end An iterator pointing to the end of a sequence of endpoints.
  752. *
  753. * @param token The @ref completion_token that will be used to produce a
  754. * completion handler, which will be called when the connect completes.
  755. * Potential completion tokens include @ref use_future, @ref use_awaitable,
  756. * @ref yield_context, or a function object with the correct completion
  757. * signature. The function signature of the completion handler must be:
  758. * @code void handler(
  759. * // Result of operation. if the sequence is empty, set to
  760. * // boost::asio::error::not_found. Otherwise, contains the
  761. * // error from the last connection attempt.
  762. * const boost::system::error_code& error,
  763. *
  764. * // On success, an iterator denoting the successfully
  765. * // connected endpoint. Otherwise, the end iterator.
  766. * Iterator iterator
  767. * ); @endcode
  768. * Regardless of whether the asynchronous operation completes immediately or
  769. * not, the completion handler will not be invoked from within this function.
  770. * On immediate completion, invocation of the handler will be performed in a
  771. * manner equivalent to using boost::asio::post().
  772. *
  773. * @par Completion Signature
  774. * @code void(boost::system::error_code, Iterator) @endcode
  775. *
  776. * @par Example
  777. * @code std::vector<tcp::endpoint> endpoints = ...;
  778. * tcp::socket s(my_context);
  779. * boost::asio::async_connect(s,
  780. * endpoints.begin(), endpoints.end(),
  781. * connect_handler);
  782. *
  783. * // ...
  784. *
  785. * void connect_handler(
  786. * const boost::system::error_code& ec,
  787. * std::vector<tcp::endpoint>::iterator i)
  788. * {
  789. * // ...
  790. * } @endcode
  791. *
  792. * @par Per-Operation Cancellation
  793. * This asynchronous operation supports cancellation for the following
  794. * boost::asio::cancellation_type values:
  795. *
  796. * @li @c cancellation_type::terminal
  797. *
  798. * @li @c cancellation_type::partial
  799. *
  800. * if they are also supported by the socket's @c async_connect operation.
  801. */
  802. template <typename Protocol, typename Executor, typename Iterator,
  803. BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code,
  804. Iterator)) IteratorConnectToken = default_completion_token_t<Executor>>
  805. auto async_connect(
  806. basic_socket<Protocol, Executor>& s, Iterator begin, Iterator end,
  807. IteratorConnectToken&& token = default_completion_token_t<Executor>())
  808. -> decltype(
  809. async_initiate<IteratorConnectToken,
  810. void (boost::system::error_code, Iterator)>(
  811. declval<detail::initiate_async_iterator_connect<Protocol, Executor>>(),
  812. token, begin, end, declval<detail::default_connect_condition>()));
  813. /// Asynchronously establishes a socket connection by trying each endpoint in a
  814. /// sequence.
  815. /**
  816. * This function attempts to connect a socket to one of a sequence of
  817. * endpoints. It does this by repeated calls to the socket's @c async_connect
  818. * member function, once for each endpoint in the sequence, until a connection
  819. * is successfully established. It is an initiating function for an @ref
  820. * asynchronous_operation, and always returns immediately.
  821. *
  822. * @param s The socket to be connected. If the socket is already open, it will
  823. * be closed.
  824. *
  825. * @param endpoints A sequence of endpoints.
  826. *
  827. * @param connect_condition A function object that is called prior to each
  828. * connection attempt. The signature of the function object must be:
  829. * @code bool connect_condition(
  830. * const boost::system::error_code& ec,
  831. * const typename Protocol::endpoint& next); @endcode
  832. * The @c ec parameter contains the result from the most recent connect
  833. * operation. Before the first connection attempt, @c ec is always set to
  834. * indicate success. The @c next parameter is the next endpoint to be tried.
  835. * The function object should return true if the next endpoint should be tried,
  836. * and false if it should be skipped.
  837. *
  838. * @param token The @ref completion_token that will be used to produce a
  839. * completion handler, which will be called when the connect completes.
  840. * Potential completion tokens include @ref use_future, @ref use_awaitable,
  841. * @ref yield_context, or a function object with the correct completion
  842. * signature. The function signature of the completion handler must be:
  843. * @code void handler(
  844. * // Result of operation. if the sequence is empty, set to
  845. * // boost::asio::error::not_found. Otherwise, contains the
  846. * // error from the last connection attempt.
  847. * const boost::system::error_code& error,
  848. *
  849. * // On success, an iterator denoting the successfully
  850. * // connected endpoint. Otherwise, the end iterator.
  851. * Iterator iterator
  852. * ); @endcode
  853. * Regardless of whether the asynchronous operation completes immediately or
  854. * not, the completion handler will not be invoked from within this function.
  855. * On immediate completion, invocation of the handler will be performed in a
  856. * manner equivalent to using boost::asio::post().
  857. *
  858. * @par Completion Signature
  859. * @code void(boost::system::error_code, typename Protocol::endpoint) @endcode
  860. *
  861. * @par Example
  862. * The following connect condition function object can be used to output
  863. * information about the individual connection attempts:
  864. * @code struct my_connect_condition
  865. * {
  866. * bool operator()(
  867. * const boost::system::error_code& ec,
  868. * const::tcp::endpoint& next)
  869. * {
  870. * if (ec) std::cout << "Error: " << ec.message() << std::endl;
  871. * std::cout << "Trying: " << next << std::endl;
  872. * return true;
  873. * }
  874. * }; @endcode
  875. * It would be used with the boost::asio::connect function as follows:
  876. * @code tcp::resolver r(my_context);
  877. * tcp::resolver::query q("host", "service");
  878. * tcp::socket s(my_context);
  879. *
  880. * // ...
  881. *
  882. * r.async_resolve(q, resolve_handler);
  883. *
  884. * // ...
  885. *
  886. * void resolve_handler(
  887. * const boost::system::error_code& ec,
  888. * tcp::resolver::results_type results)
  889. * {
  890. * if (!ec)
  891. * {
  892. * boost::asio::async_connect(s, results,
  893. * my_connect_condition(),
  894. * connect_handler);
  895. * }
  896. * }
  897. *
  898. * // ...
  899. *
  900. * void connect_handler(
  901. * const boost::system::error_code& ec,
  902. * const tcp::endpoint& endpoint)
  903. * {
  904. * if (ec)
  905. * {
  906. * // An error occurred.
  907. * }
  908. * else
  909. * {
  910. * std::cout << "Connected to: " << endpoint << std::endl;
  911. * }
  912. * } @endcode
  913. *
  914. * @par Per-Operation Cancellation
  915. * This asynchronous operation supports cancellation for the following
  916. * boost::asio::cancellation_type values:
  917. *
  918. * @li @c cancellation_type::terminal
  919. *
  920. * @li @c cancellation_type::partial
  921. *
  922. * if they are also supported by the socket's @c async_connect operation.
  923. */
  924. template <typename Protocol, typename Executor,
  925. typename EndpointSequence, typename ConnectCondition,
  926. BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code,
  927. typename Protocol::endpoint)) RangeConnectToken
  928. = default_completion_token_t<Executor>>
  929. auto async_connect(basic_socket<Protocol, Executor>& s,
  930. const EndpointSequence& endpoints, ConnectCondition connect_condition,
  931. RangeConnectToken&& token = default_completion_token_t<Executor>(),
  932. constraint_t<is_endpoint_sequence<EndpointSequence>::value> = 0)
  933. -> decltype(
  934. async_initiate<RangeConnectToken,
  935. void (boost::system::error_code, typename Protocol::endpoint)>(
  936. declval<detail::initiate_async_range_connect<Protocol, Executor>>(),
  937. token, endpoints, connect_condition));
  938. #if !defined(BOOST_ASIO_NO_DEPRECATED)
  939. /// (Deprecated: Use range overload.) Asynchronously establishes a socket
  940. /// connection by trying each endpoint in a sequence.
  941. /**
  942. * This function attempts to connect a socket to one of a sequence of
  943. * endpoints. It does this by repeated calls to the socket's @c async_connect
  944. * member function, once for each endpoint in the sequence, until a connection
  945. * is successfully established. It is an initiating function for an @ref
  946. * asynchronous_operation, and always returns immediately.
  947. *
  948. * @param s The socket to be connected. If the socket is already open, it will
  949. * be closed.
  950. *
  951. * @param begin An iterator pointing to the start of a sequence of endpoints.
  952. *
  953. * @param connect_condition A function object that is called prior to each
  954. * connection attempt. The signature of the function object must be:
  955. * @code bool connect_condition(
  956. * const boost::system::error_code& ec,
  957. * const typename Protocol::endpoint& next); @endcode
  958. * The @c ec parameter contains the result from the most recent connect
  959. * operation. Before the first connection attempt, @c ec is always set to
  960. * indicate success. The @c next parameter is the next endpoint to be tried.
  961. * The function object should return true if the next endpoint should be tried,
  962. * and false if it should be skipped.
  963. *
  964. * @param token The @ref completion_token that will be used to produce a
  965. * completion handler, which will be called when the connect completes.
  966. * Potential completion tokens include @ref use_future, @ref use_awaitable,
  967. * @ref yield_context, or a function object with the correct completion
  968. * signature. The function signature of the completion handler must be:
  969. * @code void handler(
  970. * // Result of operation. if the sequence is empty, set to
  971. * // boost::asio::error::not_found. Otherwise, contains the
  972. * // error from the last connection attempt.
  973. * const boost::system::error_code& error,
  974. *
  975. * // On success, an iterator denoting the successfully
  976. * // connected endpoint. Otherwise, the end iterator.
  977. * Iterator iterator
  978. * ); @endcode
  979. * Regardless of whether the asynchronous operation completes immediately or
  980. * not, the completion handler will not be invoked from within this function.
  981. * On immediate completion, invocation of the handler will be performed in a
  982. * manner equivalent to using boost::asio::post().
  983. *
  984. * @par Completion Signature
  985. * @code void(boost::system::error_code, Iterator) @endcode
  986. *
  987. * @note This overload assumes that a default constructed object of type @c
  988. * Iterator represents the end of the sequence. This is a valid assumption for
  989. * iterator types such as @c boost::asio::ip::tcp::resolver::iterator.
  990. *
  991. * @par Per-Operation Cancellation
  992. * This asynchronous operation supports cancellation for the following
  993. * boost::asio::cancellation_type values:
  994. *
  995. * @li @c cancellation_type::terminal
  996. *
  997. * @li @c cancellation_type::partial
  998. *
  999. * if they are also supported by the socket's @c async_connect operation.
  1000. */
  1001. template <typename Protocol, typename Executor,
  1002. typename Iterator, typename ConnectCondition,
  1003. BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code,
  1004. Iterator)) IteratorConnectToken = default_completion_token_t<Executor>>
  1005. auto async_connect(basic_socket<Protocol, Executor>& s,
  1006. Iterator begin, ConnectCondition connect_condition,
  1007. IteratorConnectToken&& token = default_completion_token_t<Executor>(),
  1008. constraint_t<!is_endpoint_sequence<Iterator>::value> = 0)
  1009. -> decltype(
  1010. async_initiate<IteratorConnectToken,
  1011. void (boost::system::error_code, Iterator)>(
  1012. declval<detail::initiate_async_iterator_connect<Protocol, Executor>>(),
  1013. token, begin, Iterator(), connect_condition));
  1014. #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
  1015. /// Asynchronously establishes a socket connection by trying each endpoint in a
  1016. /// sequence.
  1017. /**
  1018. * This function attempts to connect a socket to one of a sequence of
  1019. * endpoints. It does this by repeated calls to the socket's @c async_connect
  1020. * member function, once for each endpoint in the sequence, until a connection
  1021. * is successfully established. It is an initiating function for an @ref
  1022. * asynchronous_operation, and always returns immediately.
  1023. *
  1024. * @param s The socket to be connected. If the socket is already open, it will
  1025. * be closed.
  1026. *
  1027. * @param begin An iterator pointing to the start of a sequence of endpoints.
  1028. *
  1029. * @param end An iterator pointing to the end of a sequence of endpoints.
  1030. *
  1031. * @param connect_condition A function object that is called prior to each
  1032. * connection attempt. The signature of the function object must be:
  1033. * @code bool connect_condition(
  1034. * const boost::system::error_code& ec,
  1035. * const typename Protocol::endpoint& next); @endcode
  1036. * The @c ec parameter contains the result from the most recent connect
  1037. * operation. Before the first connection attempt, @c ec is always set to
  1038. * indicate success. The @c next parameter is the next endpoint to be tried.
  1039. * The function object should return true if the next endpoint should be tried,
  1040. * and false if it should be skipped.
  1041. *
  1042. * @param token The @ref completion_token that will be used to produce a
  1043. * completion handler, which will be called when the connect completes.
  1044. * Potential completion tokens include @ref use_future, @ref use_awaitable,
  1045. * @ref yield_context, or a function object with the correct completion
  1046. * signature. The function signature of the completion handler must be:
  1047. * @code void handler(
  1048. * // Result of operation. if the sequence is empty, set to
  1049. * // boost::asio::error::not_found. Otherwise, contains the
  1050. * // error from the last connection attempt.
  1051. * const boost::system::error_code& error,
  1052. *
  1053. * // On success, an iterator denoting the successfully
  1054. * // connected endpoint. Otherwise, the end iterator.
  1055. * Iterator iterator
  1056. * ); @endcode
  1057. * Regardless of whether the asynchronous operation completes immediately or
  1058. * not, the completion handler will not be invoked from within this function.
  1059. * On immediate completion, invocation of the handler will be performed in a
  1060. * manner equivalent to using boost::asio::post().
  1061. *
  1062. * @par Completion Signature
  1063. * @code void(boost::system::error_code, Iterator) @endcode
  1064. *
  1065. * @par Example
  1066. * The following connect condition function object can be used to output
  1067. * information about the individual connection attempts:
  1068. * @code struct my_connect_condition
  1069. * {
  1070. * bool operator()(
  1071. * const boost::system::error_code& ec,
  1072. * const::tcp::endpoint& next)
  1073. * {
  1074. * if (ec) std::cout << "Error: " << ec.message() << std::endl;
  1075. * std::cout << "Trying: " << next << std::endl;
  1076. * return true;
  1077. * }
  1078. * }; @endcode
  1079. * It would be used with the boost::asio::connect function as follows:
  1080. * @code tcp::resolver r(my_context);
  1081. * tcp::resolver::query q("host", "service");
  1082. * tcp::socket s(my_context);
  1083. *
  1084. * // ...
  1085. *
  1086. * r.async_resolve(q, resolve_handler);
  1087. *
  1088. * // ...
  1089. *
  1090. * void resolve_handler(
  1091. * const boost::system::error_code& ec,
  1092. * tcp::resolver::iterator i)
  1093. * {
  1094. * if (!ec)
  1095. * {
  1096. * tcp::resolver::iterator end;
  1097. * boost::asio::async_connect(s, i, end,
  1098. * my_connect_condition(),
  1099. * connect_handler);
  1100. * }
  1101. * }
  1102. *
  1103. * // ...
  1104. *
  1105. * void connect_handler(
  1106. * const boost::system::error_code& ec,
  1107. * tcp::resolver::iterator i)
  1108. * {
  1109. * if (ec)
  1110. * {
  1111. * // An error occurred.
  1112. * }
  1113. * else
  1114. * {
  1115. * std::cout << "Connected to: " << i->endpoint() << std::endl;
  1116. * }
  1117. * } @endcode
  1118. *
  1119. * @par Per-Operation Cancellation
  1120. * This asynchronous operation supports cancellation for the following
  1121. * boost::asio::cancellation_type values:
  1122. *
  1123. * @li @c cancellation_type::terminal
  1124. *
  1125. * @li @c cancellation_type::partial
  1126. *
  1127. * if they are also supported by the socket's @c async_connect operation.
  1128. */
  1129. template <typename Protocol, typename Executor,
  1130. typename Iterator, typename ConnectCondition,
  1131. BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code,
  1132. Iterator)) IteratorConnectToken = default_completion_token_t<Executor>>
  1133. auto async_connect(basic_socket<Protocol, Executor>& s,
  1134. Iterator begin, Iterator end, ConnectCondition connect_condition,
  1135. IteratorConnectToken&& token = default_completion_token_t<Executor>())
  1136. -> decltype(
  1137. async_initiate<IteratorConnectToken,
  1138. void (boost::system::error_code, Iterator)>(
  1139. declval<detail::initiate_async_iterator_connect<Protocol, Executor>>(),
  1140. token, begin, end, connect_condition));
  1141. /*@}*/
  1142. } // namespace asio
  1143. } // namespace boost
  1144. #include <boost/asio/detail/pop_options.hpp>
  1145. #include <boost/asio/impl/connect.hpp>
  1146. #endif