Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1056 lines
40KB

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