connect.hpp 41 KB

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