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.

connect.hpp 40KB

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