connect.hpp 41 KB

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