connect.hpp 51 KB

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