connect.hpp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823
  1. //
  2. // connect.hpp
  3. // ~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2015 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  6. //
  7. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  8. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. //
  10. #ifndef ASIO_CONNECT_HPP
  11. #define ASIO_CONNECT_HPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include "asio/detail/config.hpp"
  16. #include "asio/async_result.hpp"
  17. #include "asio/basic_socket.hpp"
  18. #include "asio/error.hpp"
  19. #include "asio/detail/push_options.hpp"
  20. namespace asio {
  21. /**
  22. * @defgroup connect asio::connect
  23. *
  24. * @brief Establishes a socket connection by trying each endpoint in a sequence.
  25. */
  26. /*@{*/
  27. /// Establishes a socket connection by trying each endpoint in a sequence.
  28. /**
  29. * This function attempts to connect a socket to one of a sequence of
  30. * endpoints. It does this by repeated calls to the socket's @c connect member
  31. * function, once for each endpoint in the sequence, until a connection is
  32. * successfully established.
  33. *
  34. * @param s The socket to be connected. If the socket is already open, it will
  35. * be closed.
  36. *
  37. * @param begin An iterator pointing to the start of a sequence of endpoints.
  38. *
  39. * @returns On success, an iterator denoting the successfully connected
  40. * endpoint. Otherwise, the end iterator.
  41. *
  42. * @throws asio::system_error Thrown on failure. If the sequence is
  43. * empty, the associated @c error_code is asio::error::not_found.
  44. * Otherwise, contains the error from the last connection attempt.
  45. *
  46. * @note This overload assumes that a default constructed object of type @c
  47. * Iterator represents the end of the sequence. This is a valid assumption for
  48. * iterator types such as @c asio::ip::tcp::resolver::iterator.
  49. *
  50. * @par Example
  51. * @code tcp::resolver r(io_service);
  52. * tcp::resolver::query q("host", "service");
  53. * tcp::socket s(io_service);
  54. * asio::connect(s, r.resolve(q)); @endcode
  55. */
  56. template <typename Protocol, typename SocketService, typename Iterator>
  57. Iterator connect(basic_socket<Protocol, SocketService>& s, Iterator begin);
  58. /// Establishes a socket connection by trying each endpoint in a sequence.
  59. /**
  60. * This function attempts to connect a socket to one of a sequence of
  61. * endpoints. It does this by repeated calls to the socket's @c connect member
  62. * function, once for each endpoint in the sequence, until a connection is
  63. * successfully established.
  64. *
  65. * @param s The socket to be connected. If the socket is already open, it will
  66. * be closed.
  67. *
  68. * @param begin An iterator pointing to the start of a sequence of endpoints.
  69. *
  70. * @param ec Set to indicate what error occurred, if any. If the sequence is
  71. * empty, set to asio::error::not_found. Otherwise, contains the error
  72. * from the last connection attempt.
  73. *
  74. * @returns On success, an iterator denoting the successfully connected
  75. * endpoint. Otherwise, the end iterator.
  76. *
  77. * @note This overload assumes that a default constructed object of type @c
  78. * Iterator represents the end of the sequence. This is a valid assumption for
  79. * iterator types such as @c asio::ip::tcp::resolver::iterator.
  80. *
  81. * @par Example
  82. * @code tcp::resolver r(io_service);
  83. * tcp::resolver::query q("host", "service");
  84. * tcp::socket s(io_service);
  85. * asio::error_code ec;
  86. * asio::connect(s, r.resolve(q), ec);
  87. * if (ec)
  88. * {
  89. * // An error occurred.
  90. * } @endcode
  91. */
  92. template <typename Protocol, typename SocketService, typename Iterator>
  93. Iterator connect(basic_socket<Protocol, SocketService>& s,
  94. Iterator begin, asio::error_code& ec);
  95. /// Establishes a socket connection by trying each endpoint in a sequence.
  96. /**
  97. * This function attempts to connect a socket to one of a sequence of
  98. * endpoints. It does this by repeated calls to the socket's @c connect member
  99. * function, once for each endpoint in the sequence, until a connection is
  100. * successfully established.
  101. *
  102. * @param s The socket to be connected. If the socket is already open, it will
  103. * be closed.
  104. *
  105. * @param begin An iterator pointing to the start of a sequence of endpoints.
  106. *
  107. * @param end An iterator pointing to the end of a sequence of endpoints.
  108. *
  109. * @returns On success, an iterator denoting the successfully connected
  110. * endpoint. Otherwise, the end iterator.
  111. *
  112. * @throws asio::system_error Thrown on failure. If the sequence is
  113. * empty, the associated @c error_code is asio::error::not_found.
  114. * Otherwise, contains the error from the last connection attempt.
  115. *
  116. * @par Example
  117. * @code tcp::resolver r(io_service);
  118. * tcp::resolver::query q("host", "service");
  119. * tcp::resolver::iterator i = r.resolve(q), end;
  120. * tcp::socket s(io_service);
  121. * asio::connect(s, i, end); @endcode
  122. */
  123. template <typename Protocol, typename SocketService, typename Iterator>
  124. Iterator connect(basic_socket<Protocol, SocketService>& s,
  125. Iterator begin, Iterator end);
  126. /// Establishes a socket connection by trying each endpoint in a sequence.
  127. /**
  128. * This function attempts to connect a socket to one of a sequence of
  129. * endpoints. It does this by repeated calls to the socket's @c connect member
  130. * function, once for each endpoint in the sequence, until a connection is
  131. * successfully established.
  132. *
  133. * @param s The socket to be connected. If the socket is already open, it will
  134. * be closed.
  135. *
  136. * @param begin An iterator pointing to the start of a sequence of endpoints.
  137. *
  138. * @param end An iterator pointing to the end of a sequence of endpoints.
  139. *
  140. * @param ec Set to indicate what error occurred, if any. If the sequence is
  141. * empty, set to asio::error::not_found. Otherwise, contains the error
  142. * from the last connection attempt.
  143. *
  144. * @returns On success, an iterator denoting the successfully connected
  145. * endpoint. Otherwise, the end iterator.
  146. *
  147. * @par Example
  148. * @code tcp::resolver r(io_service);
  149. * tcp::resolver::query q("host", "service");
  150. * tcp::resolver::iterator i = r.resolve(q), end;
  151. * tcp::socket s(io_service);
  152. * asio::error_code ec;
  153. * asio::connect(s, i, end, ec);
  154. * if (ec)
  155. * {
  156. * // An error occurred.
  157. * } @endcode
  158. */
  159. template <typename Protocol, typename SocketService, typename Iterator>
  160. Iterator connect(basic_socket<Protocol, SocketService>& s,
  161. Iterator begin, Iterator end, asio::error_code& ec);
  162. /// Establishes a socket connection by trying each endpoint in a sequence.
  163. /**
  164. * This function attempts to connect a socket to one of a sequence of
  165. * endpoints. It does this by repeated calls to the socket's @c connect member
  166. * function, once for each endpoint in the sequence, until a connection is
  167. * successfully established.
  168. *
  169. * @param s The socket to be connected. If the socket is already open, it will
  170. * be closed.
  171. *
  172. * @param begin An iterator pointing to the start of a sequence of endpoints.
  173. *
  174. * @param connect_condition A function object that is called prior to each
  175. * connection attempt. The signature of the function object must be:
  176. * @code Iterator connect_condition(
  177. * const asio::error_code& ec,
  178. * Iterator next); @endcode
  179. * The @c ec parameter contains the result from the most recent connect
  180. * operation. Before the first connection attempt, @c ec is always set to
  181. * indicate success. The @c next parameter is an iterator pointing to the next
  182. * endpoint to be tried. The function object should return the next iterator,
  183. * but is permitted to return a different iterator so that endpoints may be
  184. * skipped. The implementation guarantees that the function object will never
  185. * be called with the end iterator.
  186. *
  187. * @returns On success, an iterator denoting the successfully connected
  188. * endpoint. Otherwise, the end iterator.
  189. *
  190. * @throws asio::system_error Thrown on failure. If the sequence is
  191. * empty, the associated @c error_code is asio::error::not_found.
  192. * Otherwise, contains the error from the last connection attempt.
  193. *
  194. * @note This overload assumes that a default constructed object of type @c
  195. * Iterator represents the end of the sequence. This is a valid assumption for
  196. * iterator types such as @c asio::ip::tcp::resolver::iterator.
  197. *
  198. * @par Example
  199. * The following connect condition function object can be used to output
  200. * information about the individual connection attempts:
  201. * @code struct my_connect_condition
  202. * {
  203. * template <typename Iterator>
  204. * Iterator operator()(
  205. * const asio::error_code& ec,
  206. * Iterator next)
  207. * {
  208. * if (ec) std::cout << "Error: " << ec.message() << std::endl;
  209. * std::cout << "Trying: " << next->endpoint() << std::endl;
  210. * return next;
  211. * }
  212. * }; @endcode
  213. * It would be used with the asio::connect function as follows:
  214. * @code tcp::resolver r(io_service);
  215. * tcp::resolver::query q("host", "service");
  216. * tcp::socket s(io_service);
  217. * tcp::resolver::iterator i = asio::connect(
  218. * s, r.resolve(q), my_connect_condition());
  219. * std::cout << "Connected to: " << i->endpoint() << std::endl; @endcode
  220. */
  221. template <typename Protocol, typename SocketService,
  222. typename Iterator, typename ConnectCondition>
  223. Iterator connect(basic_socket<Protocol, SocketService>& s,
  224. Iterator begin, ConnectCondition connect_condition);
  225. /// Establishes a socket connection by trying each endpoint in a sequence.
  226. /**
  227. * This function attempts to connect a socket to one of a sequence of
  228. * endpoints. It does this by repeated calls to the socket's @c connect member
  229. * function, once for each endpoint in the sequence, until a connection is
  230. * successfully established.
  231. *
  232. * @param s The socket to be connected. If the socket is already open, it will
  233. * be closed.
  234. *
  235. * @param begin An iterator pointing to the start of a sequence of endpoints.
  236. *
  237. * @param connect_condition A function object that is called prior to each
  238. * connection attempt. The signature of the function object must be:
  239. * @code Iterator connect_condition(
  240. * const asio::error_code& ec,
  241. * Iterator next); @endcode
  242. * The @c ec parameter contains the result from the most recent connect
  243. * operation. Before the first connection attempt, @c ec is always set to
  244. * indicate success. The @c next parameter is an iterator pointing to the next
  245. * endpoint to be tried. The function object should return the next iterator,
  246. * but is permitted to return a different iterator so that endpoints may be
  247. * skipped. The implementation guarantees that the function object will never
  248. * be called with the end iterator.
  249. *
  250. * @param ec Set to indicate what error occurred, if any. If the sequence is
  251. * empty, set to asio::error::not_found. Otherwise, contains the error
  252. * from the last connection attempt.
  253. *
  254. * @returns On success, an iterator denoting the successfully connected
  255. * endpoint. Otherwise, the end iterator.
  256. *
  257. * @note This overload assumes that a default constructed object of type @c
  258. * Iterator represents the end of the sequence. This is a valid assumption for
  259. * iterator types such as @c asio::ip::tcp::resolver::iterator.
  260. *
  261. * @par Example
  262. * The following connect condition function object can be used to output
  263. * information about the individual connection attempts:
  264. * @code struct my_connect_condition
  265. * {
  266. * template <typename Iterator>
  267. * Iterator operator()(
  268. * const asio::error_code& ec,
  269. * Iterator next)
  270. * {
  271. * if (ec) std::cout << "Error: " << ec.message() << std::endl;
  272. * std::cout << "Trying: " << next->endpoint() << std::endl;
  273. * return next;
  274. * }
  275. * }; @endcode
  276. * It would be used with the asio::connect function as follows:
  277. * @code tcp::resolver r(io_service);
  278. * tcp::resolver::query q("host", "service");
  279. * tcp::socket s(io_service);
  280. * asio::error_code ec;
  281. * tcp::resolver::iterator i = asio::connect(
  282. * s, r.resolve(q), my_connect_condition(), ec);
  283. * if (ec)
  284. * {
  285. * // An error occurred.
  286. * }
  287. * else
  288. * {
  289. * std::cout << "Connected to: " << i->endpoint() << std::endl;
  290. * } @endcode
  291. */
  292. template <typename Protocol, typename SocketService,
  293. typename Iterator, typename ConnectCondition>
  294. Iterator connect(basic_socket<Protocol, SocketService>& s, Iterator begin,
  295. ConnectCondition connect_condition, asio::error_code& ec);
  296. /// Establishes a socket connection by trying each endpoint in a sequence.
  297. /**
  298. * This function attempts to connect a socket to one of a sequence of
  299. * endpoints. It does this by repeated calls to the socket's @c connect member
  300. * function, once for each endpoint in the sequence, until a connection is
  301. * successfully established.
  302. *
  303. * @param s The socket to be connected. If the socket is already open, it will
  304. * be closed.
  305. *
  306. * @param begin An iterator pointing to the start of a sequence of endpoints.
  307. *
  308. * @param end An iterator pointing to the end of a sequence of endpoints.
  309. *
  310. * @param connect_condition A function object that is called prior to each
  311. * connection attempt. The signature of the function object must be:
  312. * @code Iterator connect_condition(
  313. * const asio::error_code& ec,
  314. * Iterator next); @endcode
  315. * The @c ec parameter contains the result from the most recent connect
  316. * operation. Before the first connection attempt, @c ec is always set to
  317. * indicate success. The @c next parameter is an iterator pointing to the next
  318. * endpoint to be tried. The function object should return the next iterator,
  319. * but is permitted to return a different iterator so that endpoints may be
  320. * skipped. The implementation guarantees that the function object will never
  321. * be called with the end iterator.
  322. *
  323. * @returns On success, an iterator denoting the successfully connected
  324. * endpoint. Otherwise, the end iterator.
  325. *
  326. * @throws asio::system_error Thrown on failure. If the sequence is
  327. * empty, the associated @c error_code is asio::error::not_found.
  328. * Otherwise, contains the error from the last connection attempt.
  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. * template <typename Iterator>
  336. * Iterator operator()(
  337. * const asio::error_code& ec,
  338. * Iterator next)
  339. * {
  340. * if (ec) std::cout << "Error: " << ec.message() << std::endl;
  341. * std::cout << "Trying: " << next->endpoint() << std::endl;
  342. * return next;
  343. * }
  344. * }; @endcode
  345. * It would be used with the asio::connect function as follows:
  346. * @code tcp::resolver r(io_service);
  347. * tcp::resolver::query q("host", "service");
  348. * tcp::resolver::iterator i = r.resolve(q), end;
  349. * tcp::socket s(io_service);
  350. * i = asio::connect(s, i, end, my_connect_condition());
  351. * std::cout << "Connected to: " << i->endpoint() << std::endl; @endcode
  352. */
  353. template <typename Protocol, typename SocketService,
  354. typename Iterator, typename ConnectCondition>
  355. Iterator connect(basic_socket<Protocol, SocketService>& s, Iterator begin,
  356. Iterator end, ConnectCondition connect_condition);
  357. /// Establishes a socket connection by trying each endpoint in a sequence.
  358. /**
  359. * This function attempts to connect a socket to one of a sequence of
  360. * endpoints. It does this by repeated calls to the socket's @c connect member
  361. * function, once for each endpoint in the sequence, until a connection is
  362. * successfully established.
  363. *
  364. * @param s The socket to be connected. If the socket is already open, it will
  365. * be closed.
  366. *
  367. * @param begin An iterator pointing to the start of a sequence of endpoints.
  368. *
  369. * @param end An iterator pointing to the end of a sequence of endpoints.
  370. *
  371. * @param connect_condition A function object that is called prior to each
  372. * connection attempt. The signature of the function object must be:
  373. * @code Iterator connect_condition(
  374. * const asio::error_code& ec,
  375. * Iterator next); @endcode
  376. * The @c ec parameter contains the result from the most recent connect
  377. * operation. Before the first connection attempt, @c ec is always set to
  378. * indicate success. The @c next parameter is an iterator pointing to the next
  379. * endpoint to be tried. The function object should return the next iterator,
  380. * but is permitted to return a different iterator so that endpoints may be
  381. * skipped. The implementation guarantees that the function object will never
  382. * be called with the end iterator.
  383. *
  384. * @param ec Set to indicate what error occurred, if any. If the sequence is
  385. * empty, set to asio::error::not_found. Otherwise, contains the error
  386. * from the last connection attempt.
  387. *
  388. * @returns On success, an iterator denoting the successfully connected
  389. * endpoint. Otherwise, the end iterator.
  390. *
  391. * @par Example
  392. * The following connect condition function object can be used to output
  393. * information about the individual connection attempts:
  394. * @code struct my_connect_condition
  395. * {
  396. * template <typename Iterator>
  397. * Iterator operator()(
  398. * const asio::error_code& ec,
  399. * Iterator next)
  400. * {
  401. * if (ec) std::cout << "Error: " << ec.message() << std::endl;
  402. * std::cout << "Trying: " << next->endpoint() << std::endl;
  403. * return next;
  404. * }
  405. * }; @endcode
  406. * It would be used with the asio::connect function as follows:
  407. * @code tcp::resolver r(io_service);
  408. * tcp::resolver::query q("host", "service");
  409. * tcp::resolver::iterator i = r.resolve(q), end;
  410. * tcp::socket s(io_service);
  411. * asio::error_code ec;
  412. * i = asio::connect(s, i, end, my_connect_condition(), ec);
  413. * if (ec)
  414. * {
  415. * // An error occurred.
  416. * }
  417. * else
  418. * {
  419. * std::cout << "Connected to: " << i->endpoint() << std::endl;
  420. * } @endcode
  421. */
  422. template <typename Protocol, typename SocketService,
  423. typename Iterator, typename ConnectCondition>
  424. Iterator connect(basic_socket<Protocol, SocketService>& s,
  425. Iterator begin, Iterator end, ConnectCondition connect_condition,
  426. asio::error_code& ec);
  427. /*@}*/
  428. /**
  429. * @defgroup async_connect asio::async_connect
  430. *
  431. * @brief Asynchronously establishes a socket connection by trying each
  432. * endpoint in a sequence.
  433. */
  434. /*@{*/
  435. /// Asynchronously establishes a socket connection by trying each endpoint in a
  436. /// sequence.
  437. /**
  438. * This function attempts to connect a socket to one of a sequence of
  439. * endpoints. It does this by repeated calls to the socket's @c async_connect
  440. * member function, once for each endpoint in the sequence, until a connection
  441. * is successfully established.
  442. *
  443. * @param s The socket to be connected. If the socket is already open, it will
  444. * be closed.
  445. *
  446. * @param begin An iterator pointing to the start of a sequence of endpoints.
  447. *
  448. * @param handler The handler to be called when the connect operation
  449. * completes. Copies will be made of the handler as required. The function
  450. * signature of the handler must be:
  451. * @code void handler(
  452. * // Result of operation. if the sequence is empty, set to
  453. * // asio::error::not_found. Otherwise, contains the
  454. * // error from the last connection attempt.
  455. * const asio::error_code& error,
  456. *
  457. * // On success, an iterator denoting the successfully
  458. * // connected endpoint. Otherwise, the end iterator.
  459. * Iterator iterator
  460. * ); @endcode
  461. * Regardless of whether the asynchronous operation completes immediately or
  462. * not, the handler will not be invoked from within this function. Invocation
  463. * of the handler will be performed in a manner equivalent to using
  464. * asio::io_service::post().
  465. *
  466. * @note This overload assumes that a default constructed object of type @c
  467. * Iterator represents the end of the sequence. This is a valid assumption for
  468. * iterator types such as @c asio::ip::tcp::resolver::iterator.
  469. *
  470. * @par Example
  471. * @code tcp::resolver r(io_service);
  472. * tcp::resolver::query q("host", "service");
  473. * tcp::socket s(io_service);
  474. *
  475. * // ...
  476. *
  477. * r.async_resolve(q, resolve_handler);
  478. *
  479. * // ...
  480. *
  481. * void resolve_handler(
  482. * const asio::error_code& ec,
  483. * tcp::resolver::iterator i)
  484. * {
  485. * if (!ec)
  486. * {
  487. * asio::async_connect(s, i, connect_handler);
  488. * }
  489. * }
  490. *
  491. * // ...
  492. *
  493. * void connect_handler(
  494. * const asio::error_code& ec,
  495. * tcp::resolver::iterator i)
  496. * {
  497. * // ...
  498. * } @endcode
  499. */
  500. template <typename Protocol, typename SocketService,
  501. typename Iterator, typename ComposedConnectHandler>
  502. ASIO_INITFN_RESULT_TYPE(ComposedConnectHandler,
  503. void (asio::error_code, Iterator))
  504. async_connect(basic_socket<Protocol, SocketService>& s,
  505. Iterator begin, ASIO_MOVE_ARG(ComposedConnectHandler) handler);
  506. /// Asynchronously establishes a socket connection by trying each endpoint in a
  507. /// sequence.
  508. /**
  509. * This function attempts to connect a socket to one of a sequence of
  510. * endpoints. It does this by repeated calls to the socket's @c async_connect
  511. * member function, once for each endpoint in the sequence, until a connection
  512. * is successfully established.
  513. *
  514. * @param s The socket to be connected. If the socket is already open, it will
  515. * be closed.
  516. *
  517. * @param begin An iterator pointing to the start of a sequence of endpoints.
  518. *
  519. * @param end An iterator pointing to the end of a sequence of endpoints.
  520. *
  521. * @param handler The handler to be called when the connect operation
  522. * completes. Copies will be made of the handler as required. The function
  523. * signature of the handler must be:
  524. * @code void handler(
  525. * // Result of operation. if the sequence is empty, set to
  526. * // asio::error::not_found. Otherwise, contains the
  527. * // error from the last connection attempt.
  528. * const asio::error_code& error,
  529. *
  530. * // On success, an iterator denoting the successfully
  531. * // connected endpoint. Otherwise, the end iterator.
  532. * Iterator iterator
  533. * ); @endcode
  534. * Regardless of whether the asynchronous operation completes immediately or
  535. * not, the handler will not be invoked from within this function. Invocation
  536. * of the handler will be performed in a manner equivalent to using
  537. * asio::io_service::post().
  538. *
  539. * @par Example
  540. * @code tcp::resolver r(io_service);
  541. * tcp::resolver::query q("host", "service");
  542. * tcp::socket s(io_service);
  543. *
  544. * // ...
  545. *
  546. * r.async_resolve(q, resolve_handler);
  547. *
  548. * // ...
  549. *
  550. * void resolve_handler(
  551. * const asio::error_code& ec,
  552. * tcp::resolver::iterator i)
  553. * {
  554. * if (!ec)
  555. * {
  556. * tcp::resolver::iterator end;
  557. * asio::async_connect(s, i, end, connect_handler);
  558. * }
  559. * }
  560. *
  561. * // ...
  562. *
  563. * void connect_handler(
  564. * const asio::error_code& ec,
  565. * tcp::resolver::iterator i)
  566. * {
  567. * // ...
  568. * } @endcode
  569. */
  570. template <typename Protocol, typename SocketService,
  571. typename Iterator, typename ComposedConnectHandler>
  572. ASIO_INITFN_RESULT_TYPE(ComposedConnectHandler,
  573. void (asio::error_code, Iterator))
  574. async_connect(basic_socket<Protocol, SocketService>& s,
  575. Iterator begin, Iterator end,
  576. ASIO_MOVE_ARG(ComposedConnectHandler) handler);
  577. /// Asynchronously establishes a socket connection by trying each endpoint in a
  578. /// sequence.
  579. /**
  580. * This function attempts to connect a socket to one of a sequence of
  581. * endpoints. It does this by repeated calls to the socket's @c async_connect
  582. * member function, once for each endpoint in the sequence, until a connection
  583. * is successfully established.
  584. *
  585. * @param s The socket to be connected. If the socket is already open, it will
  586. * be closed.
  587. *
  588. * @param begin An iterator pointing to the start of a sequence of endpoints.
  589. *
  590. * @param connect_condition A function object that is called prior to each
  591. * connection attempt. The signature of the function object must be:
  592. * @code Iterator connect_condition(
  593. * const asio::error_code& ec,
  594. * Iterator next); @endcode
  595. * The @c ec parameter contains the result from the most recent connect
  596. * operation. Before the first connection attempt, @c ec is always set to
  597. * indicate success. The @c next parameter is an iterator pointing to the next
  598. * endpoint to be tried. The function object should return the next iterator,
  599. * but is permitted to return a different iterator so that endpoints may be
  600. * skipped. The implementation guarantees that the function object will never
  601. * be called with the end iterator.
  602. *
  603. * @param handler The handler to be called when the connect operation
  604. * completes. Copies will be made of the handler as required. The function
  605. * signature of the handler must be:
  606. * @code void handler(
  607. * // Result of operation. if the sequence is empty, set to
  608. * // asio::error::not_found. Otherwise, contains the
  609. * // error from the last connection attempt.
  610. * const asio::error_code& error,
  611. *
  612. * // On success, an iterator denoting the successfully
  613. * // connected endpoint. Otherwise, the end iterator.
  614. * Iterator iterator
  615. * ); @endcode
  616. * Regardless of whether the asynchronous operation completes immediately or
  617. * not, the handler will not be invoked from within this function. Invocation
  618. * of the handler will be performed in a manner equivalent to using
  619. * asio::io_service::post().
  620. *
  621. * @note This overload assumes that a default constructed object of type @c
  622. * Iterator represents the end of the sequence. This is a valid assumption for
  623. * iterator types such as @c asio::ip::tcp::resolver::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. * template <typename Iterator>
  631. * Iterator operator()(
  632. * const asio::error_code& ec,
  633. * Iterator next)
  634. * {
  635. * if (ec) std::cout << "Error: " << ec.message() << std::endl;
  636. * std::cout << "Trying: " << next->endpoint() << std::endl;
  637. * return next;
  638. * }
  639. * }; @endcode
  640. * It would be used with the asio::connect function as follows:
  641. * @code tcp::resolver r(io_service);
  642. * tcp::resolver::query q("host", "service");
  643. * tcp::socket s(io_service);
  644. *
  645. * // ...
  646. *
  647. * r.async_resolve(q, resolve_handler);
  648. *
  649. * // ...
  650. *
  651. * void resolve_handler(
  652. * const asio::error_code& ec,
  653. * tcp::resolver::iterator i)
  654. * {
  655. * if (!ec)
  656. * {
  657. * asio::async_connect(s, i,
  658. * my_connect_condition(),
  659. * connect_handler);
  660. * }
  661. * }
  662. *
  663. * // ...
  664. *
  665. * void connect_handler(
  666. * const asio::error_code& ec,
  667. * tcp::resolver::iterator i)
  668. * {
  669. * if (ec)
  670. * {
  671. * // An error occurred.
  672. * }
  673. * else
  674. * {
  675. * std::cout << "Connected to: " << i->endpoint() << std::endl;
  676. * }
  677. * } @endcode
  678. */
  679. template <typename Protocol, typename SocketService, typename Iterator,
  680. typename ConnectCondition, typename ComposedConnectHandler>
  681. ASIO_INITFN_RESULT_TYPE(ComposedConnectHandler,
  682. void (asio::error_code, Iterator))
  683. async_connect(basic_socket<Protocol, SocketService>& s, Iterator begin,
  684. ConnectCondition connect_condition,
  685. ASIO_MOVE_ARG(ComposedConnectHandler) handler);
  686. /// Asynchronously establishes a socket connection by trying each endpoint in a
  687. /// sequence.
  688. /**
  689. * This function attempts to connect a socket to one of a sequence of
  690. * endpoints. It does this by repeated calls to the socket's @c async_connect
  691. * member function, once for each endpoint in the sequence, until a connection
  692. * is successfully established.
  693. *
  694. * @param s The socket to be connected. If the socket is already open, it will
  695. * be closed.
  696. *
  697. * @param begin An iterator pointing to the start of a sequence of endpoints.
  698. *
  699. * @param end An iterator pointing to the end of a sequence of endpoints.
  700. *
  701. * @param connect_condition A function object that is called prior to each
  702. * connection attempt. The signature of the function object must be:
  703. * @code Iterator connect_condition(
  704. * const asio::error_code& ec,
  705. * Iterator next); @endcode
  706. * The @c ec parameter contains the result from the most recent connect
  707. * operation. Before the first connection attempt, @c ec is always set to
  708. * indicate success. The @c next parameter is an iterator pointing to the next
  709. * endpoint to be tried. The function object should return the next iterator,
  710. * but is permitted to return a different iterator so that endpoints may be
  711. * skipped. The implementation guarantees that the function object will never
  712. * be called with the end iterator.
  713. *
  714. * @param handler The handler to be called when the connect operation
  715. * completes. Copies will be made of the handler as required. The function
  716. * signature of the handler must be:
  717. * @code void handler(
  718. * // Result of operation. if the sequence is empty, set to
  719. * // asio::error::not_found. Otherwise, contains the
  720. * // error from the last connection attempt.
  721. * const asio::error_code& error,
  722. *
  723. * // On success, an iterator denoting the successfully
  724. * // connected endpoint. Otherwise, the end iterator.
  725. * Iterator iterator
  726. * ); @endcode
  727. * Regardless of whether the asynchronous operation completes immediately or
  728. * not, the handler will not be invoked from within this function. Invocation
  729. * of the handler will be performed in a manner equivalent to using
  730. * asio::io_service::post().
  731. *
  732. * @par Example
  733. * The following connect condition function object can be used to output
  734. * information about the individual connection attempts:
  735. * @code struct my_connect_condition
  736. * {
  737. * template <typename Iterator>
  738. * Iterator operator()(
  739. * const asio::error_code& ec,
  740. * Iterator next)
  741. * {
  742. * if (ec) std::cout << "Error: " << ec.message() << std::endl;
  743. * std::cout << "Trying: " << next->endpoint() << std::endl;
  744. * return next;
  745. * }
  746. * }; @endcode
  747. * It would be used with the asio::connect function as follows:
  748. * @code tcp::resolver r(io_service);
  749. * tcp::resolver::query q("host", "service");
  750. * tcp::socket s(io_service);
  751. *
  752. * // ...
  753. *
  754. * r.async_resolve(q, resolve_handler);
  755. *
  756. * // ...
  757. *
  758. * void resolve_handler(
  759. * const asio::error_code& ec,
  760. * tcp::resolver::iterator i)
  761. * {
  762. * if (!ec)
  763. * {
  764. * tcp::resolver::iterator end;
  765. * asio::async_connect(s, i, end,
  766. * my_connect_condition(),
  767. * connect_handler);
  768. * }
  769. * }
  770. *
  771. * // ...
  772. *
  773. * void connect_handler(
  774. * const asio::error_code& ec,
  775. * tcp::resolver::iterator i)
  776. * {
  777. * if (ec)
  778. * {
  779. * // An error occurred.
  780. * }
  781. * else
  782. * {
  783. * std::cout << "Connected to: " << i->endpoint() << std::endl;
  784. * }
  785. * } @endcode
  786. */
  787. template <typename Protocol, typename SocketService, typename Iterator,
  788. typename ConnectCondition, typename ComposedConnectHandler>
  789. ASIO_INITFN_RESULT_TYPE(ComposedConnectHandler,
  790. void (asio::error_code, Iterator))
  791. async_connect(basic_socket<Protocol, SocketService>& s,
  792. Iterator begin, Iterator end, ConnectCondition connect_condition,
  793. ASIO_MOVE_ARG(ComposedConnectHandler) handler);
  794. /*@}*/
  795. } // namespace asio
  796. #include "asio/detail/pop_options.hpp"
  797. #include "asio/impl/connect.hpp"
  798. #endif