basic_stream.hpp 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588
  1. //
  2. // Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com)
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. // Official repository: https://github.com/boostorg/beast
  8. //
  9. #ifndef BOOST_BEAST_CORE_BASIC_STREAM_HPP
  10. #define BOOST_BEAST_CORE_BASIC_STREAM_HPP
  11. #include <boost/beast/core/detail/config.hpp>
  12. #include <boost/beast/core/detail/stream_base.hpp>
  13. #include <boost/beast/core/error.hpp>
  14. #include <boost/beast/core/rate_policy.hpp>
  15. #include <boost/beast/core/role.hpp>
  16. #include <boost/beast/core/stream_traits.hpp>
  17. #include <boost/asio/async_result.hpp>
  18. #include <boost/asio/basic_stream_socket.hpp>
  19. #include <boost/asio/connect.hpp>
  20. #include <boost/asio/executor.hpp>
  21. #include <boost/asio/is_executor.hpp>
  22. #include <boost/core/empty_value.hpp>
  23. #include <boost/config/workaround.hpp>
  24. #include <boost/enable_shared_from_this.hpp>
  25. #include <boost/shared_ptr.hpp>
  26. #include <chrono>
  27. #include <limits>
  28. #include <memory>
  29. #if ! BOOST_BEAST_DOXYGEN
  30. namespace boost {
  31. namespace asio {
  32. namespace ssl {
  33. template<typename> class stream;
  34. } // ssl
  35. } // asio
  36. } // boost
  37. #endif
  38. namespace boost {
  39. namespace beast {
  40. /** A stream socket wrapper with timeouts, an executor, and a rate limit policy.
  41. This stream wraps a `net::basic_stream_socket` to provide
  42. the following features:
  43. @li An <em>Executor</em> may be associated with the stream, which will
  44. be used to invoke any completion handlers which do not already have
  45. an associated executor. This achieves support for
  46. <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p1322r0.html">[P1322R0] Networking TS enhancement to enable custom I/O executors</a>.
  47. @li Timeouts may be specified for each logical asynchronous operation
  48. performing any reading, writing, or connecting.
  49. @li A <em>RatePolicy</em> may be associated with the stream, to implement
  50. rate limiting through the policy's interface.
  51. Although the stream supports multiple concurrent outstanding asynchronous
  52. operations, the stream object is not thread-safe. The caller is responsible
  53. for ensuring that the stream is accessed from only one thread at a time.
  54. This includes the times when the stream, and its underlying socket, are
  55. accessed by the networking implementation. To meet this thread safety
  56. requirement, all asynchronous operations must be performed by the stream
  57. within the same implicit strand (only one thread `net::io_context::run`)
  58. or within the same explicit strand, such as an instance of `net::strand`.
  59. Completion handlers with explicit associated executors (such as those
  60. arising from use of `net::bind_executor`) will be invoked by the stream
  61. using the associated executor. Otherwise, the completion handler will
  62. be invoked by the executor associated with the stream upon construction.
  63. The type of executor used with this stream must meet the following
  64. requirements:
  65. @li Function objects submitted to the executor shall never run
  66. concurrently with each other.
  67. The executor type `net::strand` meets these requirements. Use of a
  68. strand as the executor in the stream class template offers an additional
  69. notational convenience: the strand does not need to be specified in
  70. each individual initiating function call.
  71. Unlike other stream wrappers, the underlying socket is accessed
  72. through the @ref socket member function instead of `next_layer`.
  73. This causes the @ref basic_stream to be returned in calls
  74. to @ref get_lowest_layer.
  75. @par Usage
  76. To use this stream declare an instance of the class. Then, before
  77. each logical operation for which a timeout is desired, call
  78. @ref expires_after with a duration, or call @ref expires_at with a
  79. time point. Alternatively, call @ref expires_never to disable the
  80. timeout for subsequent logical operations. A logical operation
  81. is any series of one or more direct or indirect calls to the timeout
  82. stream's asynchronous read, asynchronous write, or asynchronous connect
  83. functions.
  84. When a timeout is set and a mixed operation is performed (one that
  85. includes both reads and writes, for example) the timeout applies
  86. to all of the intermediate asynchronous operations used in the
  87. enclosing operation. This allows timeouts to be applied to stream
  88. algorithms which were not written specifically to allow for timeouts,
  89. when those algorithms are passed a timeout stream with a timeout set.
  90. When a timeout occurs the socket will be closed, canceling any
  91. pending I/O operations. The completion handlers for these canceled
  92. operations will be invoked with the error @ref beast::error::timeout.
  93. @par Examples
  94. This function reads an HTTP request with a timeout, then sends the
  95. HTTP response with a different timeout.
  96. @code
  97. void process_http_1 (tcp_stream& stream, net::yield_context yield)
  98. {
  99. flat_buffer buffer;
  100. http::request<http::empty_body> req;
  101. // Read the request, with a 15 second timeout
  102. stream.expires_after(std::chrono::seconds(15));
  103. http::async_read(stream, buffer, req, yield);
  104. // Calculate the response
  105. http::response<http::string_body> res = make_response(req);
  106. // Send the response, with a 30 second timeout.
  107. stream.expires_after (std::chrono::seconds(30));
  108. http::async_write (stream, res, yield);
  109. }
  110. @endcode
  111. The example above could be expressed using a single timeout with a
  112. simple modification. The function that follows first reads an HTTP
  113. request then sends the HTTP response, with a single timeout that
  114. applies to the entire combined operation of reading and writing:
  115. @code
  116. void process_http_2 (tcp_stream& stream, net::yield_context yield)
  117. {
  118. flat_buffer buffer;
  119. http::request<http::empty_body> req;
  120. // Require that the read and write combined take no longer than 30 seconds
  121. stream.expires_after(std::chrono::seconds(30));
  122. http::async_read(stream, buffer, req, yield);
  123. http::response<http::string_body> res = make_response(req);
  124. http::async_write (stream, res, yield);
  125. }
  126. @endcode
  127. Some stream algorithms, such as `ssl::stream::async_handshake` perform
  128. both reads and writes. A timeout set before calling the initiating function
  129. of such composite stream algorithms will apply to the entire composite
  130. operation. For example, a timeout may be set on performing the SSL handshake
  131. thusly:
  132. @code
  133. void do_ssl_handshake (net::ssl::stream<tcp_stream>& stream, net::yield_context yield)
  134. {
  135. // Require that the SSL handshake take no longer than 10 seconds
  136. stream.expires_after(std::chrono::seconds(10));
  137. stream.async_handshake(net::ssl::stream_base::client, yield);
  138. }
  139. @endcode
  140. @par Blocking I/O
  141. Synchronous functions behave identically as that of the wrapped
  142. `net::basic_stream_socket`. Timeouts are not available when performing
  143. blocking calls.
  144. @tparam Protocol A type meeting the requirements of <em>Protocol</em>
  145. representing the protocol the protocol to use for the basic stream socket.
  146. A common choice is `net::ip::tcp`.
  147. @tparam Executor A type meeting the requirements of <em>Executor</em> to
  148. be used for submitting all completion handlers which do not already have an
  149. associated executor. If this type is omitted, the default of `net::any_io_executor`
  150. will be used.
  151. @par Thread Safety
  152. <em>Distinct objects</em>: Safe.@n
  153. <em>Shared objects</em>: Unsafe. The application must also ensure
  154. that all asynchronous operations are performed within the same
  155. implicit or explicit strand.
  156. @see
  157. @li <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p1322r0.html">[P1322R0] Networking TS enhancement to enable custom I/O executors</a>.
  158. */
  159. template<
  160. class Protocol,
  161. class Executor = net::any_io_executor,
  162. class RatePolicy = unlimited_rate_policy
  163. >
  164. class basic_stream
  165. #if ! BOOST_BEAST_DOXYGEN
  166. : private detail::stream_base
  167. #endif
  168. {
  169. public:
  170. /// The type of the underlying socket.
  171. using socket_type =
  172. net::basic_stream_socket<Protocol, Executor>;
  173. /** The type of the executor associated with the stream.
  174. This will be the type of executor used to invoke completion
  175. handlers which do not have an explicit associated executor.
  176. */
  177. using executor_type = beast::executor_type<socket_type>;
  178. /// Rebinds the stream type to another executor.
  179. template<class Executor1>
  180. struct rebind_executor
  181. {
  182. /// The stream type when rebound to the specified executor.
  183. using other = basic_stream<
  184. Protocol, Executor1, RatePolicy>;
  185. };
  186. /// The protocol type.
  187. using protocol_type = Protocol;
  188. /// The endpoint type.
  189. using endpoint_type = typename Protocol::endpoint;
  190. private:
  191. using op_state = basic_op_state<Executor>;
  192. static_assert(
  193. net::is_executor<Executor>::value || net::execution::is_executor<Executor>::value,
  194. "Executor type requirements not met");
  195. struct impl_type
  196. : boost::enable_shared_from_this<impl_type>
  197. , boost::empty_value<RatePolicy>
  198. {
  199. // must come first
  200. net::basic_stream_socket<
  201. Protocol, Executor> socket;
  202. op_state read;
  203. op_state write;
  204. net::basic_waitable_timer<
  205. std::chrono::steady_clock,
  206. net::wait_traits<
  207. std::chrono::steady_clock>,
  208. Executor> timer; // rate timer;
  209. int waiting = 0;
  210. impl_type(impl_type&&) = default;
  211. template<class... Args>
  212. explicit
  213. impl_type(std::false_type, Args&&...);
  214. template<class RatePolicy_, class... Args>
  215. explicit
  216. impl_type(std::true_type,
  217. RatePolicy_&& policy, Args&&...);
  218. impl_type& operator=(impl_type&&) = delete;
  219. beast::executor_type<socket_type>
  220. ex() noexcept
  221. {
  222. return this->socket.get_executor();
  223. }
  224. RatePolicy&
  225. policy() noexcept
  226. {
  227. return this->boost::empty_value<RatePolicy>::get();
  228. }
  229. RatePolicy const&
  230. policy() const noexcept
  231. {
  232. return this->boost::empty_value<RatePolicy>::get();
  233. }
  234. template<class Executor2>
  235. void on_timer(Executor2 const& ex2);
  236. void reset(); // set timeouts to never
  237. void close() noexcept; // cancel everything
  238. };
  239. // We use shared ownership for the state so it can
  240. // outlive the destruction of the stream_socket object,
  241. // in the case where there is no outstanding read or write
  242. // but the implementation is still waiting on a timer.
  243. boost::shared_ptr<impl_type> impl_;
  244. template<class Executor2>
  245. struct timeout_handler;
  246. struct ops;
  247. #if ! BOOST_BEAST_DOXYGEN
  248. // boost::asio::ssl::stream needs these
  249. // DEPRECATED
  250. template<class>
  251. friend class boost::asio::ssl::stream;
  252. // DEPRECATED
  253. using lowest_layer_type = socket_type;
  254. // DEPRECATED
  255. lowest_layer_type&
  256. lowest_layer() noexcept
  257. {
  258. return impl_->socket;
  259. }
  260. // DEPRECATED
  261. lowest_layer_type const&
  262. lowest_layer() const noexcept
  263. {
  264. return impl_->socket;
  265. }
  266. #endif
  267. public:
  268. /** Destructor
  269. This function destroys the stream, cancelling any outstanding
  270. asynchronous operations associated with the socket as if by
  271. calling cancel.
  272. */
  273. ~basic_stream();
  274. /** Constructor
  275. This constructor creates the stream by forwarding all arguments
  276. to the underlying socket. The socket then needs to be open and
  277. connected or accepted before data can be sent or received on it.
  278. @param args A list of parameters forwarded to the constructor of
  279. the underlying socket.
  280. */
  281. #if BOOST_BEAST_DOXYGEN
  282. template<class... Args>
  283. explicit
  284. basic_stream(Args&&... args);
  285. #else
  286. template<class Arg0, class... Args,
  287. class = typename std::enable_if<
  288. ! std::is_constructible<RatePolicy, Arg0>::value>::type>
  289. explicit
  290. basic_stream(Arg0&& argo, Args&&... args);
  291. /** Constructor
  292. *
  293. * A constructor that rebinds the executor.
  294. *
  295. * @tparam Executor_ The new executor
  296. * @param other The original socket to be rebound.
  297. */
  298. template<class Executor_>
  299. explicit
  300. basic_stream(basic_stream<Protocol, Executor_, RatePolicy> && other);
  301. template<typename, typename, typename>
  302. friend class basic_stream;
  303. #endif
  304. /** Constructor
  305. This constructor creates the stream with the specified rate
  306. policy, and forwards all remaining arguments to the underlying
  307. socket. The socket then needs to be open and connected or
  308. accepted before data can be sent or received on it.
  309. @param policy The rate policy object to use. The stream will
  310. take ownership of this object by decay-copy.
  311. @param args A list of parameters forwarded to the constructor of
  312. the underlying socket.
  313. */
  314. #if BOOST_BEAST_DOXYGEN
  315. template<class RatePolicy_, class... Args>
  316. explicit
  317. basic_stream(RatePolicy_&& policy, Args&&... args);
  318. #else
  319. template<class RatePolicy_, class Arg0, class... Args,
  320. class = typename std::enable_if<
  321. std::is_constructible<
  322. RatePolicy, RatePolicy_>::value>::type>
  323. basic_stream(
  324. RatePolicy_&& policy, Arg0&& arg, Args&&... args);
  325. #endif
  326. /** Move constructor
  327. @param other The other object from which the move will occur.
  328. @note Following the move, the moved-from object is in the
  329. same state as if newly constructed.
  330. */
  331. basic_stream(basic_stream&& other);
  332. /// Move assignment (deleted).
  333. basic_stream& operator=(basic_stream&&) = delete;
  334. /// Return a reference to the underlying socket
  335. socket_type&
  336. socket() noexcept
  337. {
  338. return impl_->socket;
  339. }
  340. /// Return a reference to the underlying socket
  341. socket_type const&
  342. socket() const noexcept
  343. {
  344. return impl_->socket;
  345. }
  346. /** Release ownership of the underlying socket.
  347. This function causes all outstanding asynchronous connect,
  348. read, and write operations to be canceled as if by a call
  349. to @ref cancel. Ownership of the underlying socket is then
  350. transferred to the caller.
  351. */
  352. socket_type
  353. release_socket();
  354. //--------------------------------------------------------------------------
  355. /// Returns the rate policy associated with the object
  356. RatePolicy&
  357. rate_policy() noexcept
  358. {
  359. return impl_->policy();
  360. }
  361. /// Returns the rate policy associated with the object
  362. RatePolicy const&
  363. rate_policy() const noexcept
  364. {
  365. return impl_->policy();
  366. }
  367. /** Set the timeout for subsequent logical operations.
  368. This sets either the read timer, the write timer, or
  369. both timers to expire after the specified amount of time
  370. has elapsed. If a timer expires when the corresponding
  371. asynchronous operation is outstanding, the stream will be
  372. closed and any outstanding operations will complete with the
  373. error @ref beast::error::timeout. Otherwise, if the timer
  374. expires while no operations are outstanding, and the expiraton
  375. is not set again, the next operation will time out immediately.
  376. The timer applies collectively to any asynchronous reads
  377. or writes initiated after the expiration is set, until the
  378. expiration is set again. A call to @ref async_connect
  379. counts as both a read and a write.
  380. @param expiry_time The amount of time after which a logical
  381. operation should be considered timed out.
  382. */
  383. void
  384. expires_after(
  385. net::steady_timer::duration expiry_time);
  386. /** Set the timeout for subsequent logical operations.
  387. This sets either the read timer, the write timer, or both
  388. timers to expire at the specified time point. If a timer
  389. expires when the corresponding asynchronous operation is
  390. outstanding, the stream will be closed and any outstanding
  391. operations will complete with the error @ref beast::error::timeout.
  392. Otherwise, if the timer expires while no operations are outstanding,
  393. and the expiraton is not set again, the next operation will time out
  394. immediately.
  395. The timer applies collectively to any asynchronous reads
  396. or writes initiated after the expiration is set, until the
  397. expiration is set again. A call to @ref async_connect
  398. counts as both a read and a write.
  399. @param expiry_time The time point after which a logical
  400. operation should be considered timed out.
  401. */
  402. void
  403. expires_at(net::steady_timer::time_point expiry_time);
  404. /// Disable the timeout for subsequent logical operations.
  405. void
  406. expires_never();
  407. /** Cancel all asynchronous operations associated with the socket.
  408. This function causes all outstanding asynchronous connect,
  409. read, and write operations to finish immediately. Completion
  410. handlers for cancelled operations will receive the error
  411. `net::error::operation_aborted`. Completion handlers not
  412. yet invoked whose operations have completed, will receive
  413. the error corresponding to the result of the operation (which
  414. may indicate success).
  415. */
  416. void
  417. cancel();
  418. /** Close the timed stream.
  419. This cancels all of the outstanding asynchronous operations
  420. as if by calling @ref cancel, and closes the underlying socket.
  421. */
  422. void
  423. close();
  424. //--------------------------------------------------------------------------
  425. /** Get the executor associated with the object.
  426. This function may be used to obtain the executor object that the
  427. stream uses to dispatch completion handlers without an assocaited
  428. executor.
  429. @return A copy of the executor that stream will use to dispatch handlers.
  430. */
  431. executor_type
  432. get_executor() noexcept
  433. {
  434. return impl_->ex();
  435. }
  436. /** Connect the stream to the specified endpoint.
  437. This function is used to connect the underlying socket to the
  438. specified remote endpoint. The function call will block until
  439. the connection is successfully made or an error occurs.
  440. The underlying socket is automatically opened if needed.
  441. An automatically opened socket is not returned to the
  442. closed state upon failure.
  443. @param ep The remote endpoint to connect to.
  444. @throws system_error Thrown on failure.
  445. @see connect
  446. */
  447. void
  448. connect(endpoint_type const& ep)
  449. {
  450. socket().connect(ep);
  451. }
  452. /** Connect the stream to the specified endpoint.
  453. This function is used to connect the underlying socket to the
  454. specified remote endpoint. The function call will block until
  455. the connection is successfully made or an error occurs.
  456. The underlying socket is automatically opened if needed.
  457. An automatically opened socket is not returned to the
  458. closed state upon failure.
  459. @param ep The remote endpoint to connect to.
  460. @param ec Set to indicate what error occurred, if any.
  461. @see connect
  462. */
  463. void
  464. connect(endpoint_type const& ep, error_code& ec)
  465. {
  466. socket().connect(ep, ec);
  467. }
  468. /** Establishes a connection by trying each endpoint in a sequence.
  469. This function attempts to connect the stream to one of a sequence of
  470. endpoints by trying each endpoint until a connection is successfully
  471. established.
  472. The underlying socket is automatically opened if needed.
  473. An automatically opened socket is not returned to the
  474. closed state upon failure.
  475. The algorithm, known as a <em>composed operation</em>, is implemented
  476. in terms of calls to the underlying socket's `connect` function.
  477. @param endpoints A sequence of endpoints.
  478. @returns The successfully connected endpoint.
  479. @throws system_error Thrown on failure. If the sequence is
  480. empty, the associated error code is `net::error::not_found`.
  481. Otherwise, contains the error from the last connection attempt.
  482. */
  483. template<class EndpointSequence
  484. #if ! BOOST_BEAST_DOXYGEN
  485. ,class = typename std::enable_if<
  486. net::is_endpoint_sequence<
  487. EndpointSequence>::value>::type
  488. #endif
  489. >
  490. typename Protocol::endpoint
  491. connect(EndpointSequence const& endpoints)
  492. {
  493. return net::connect(socket(), endpoints);
  494. }
  495. /** Establishes a connection by trying each endpoint in a sequence.
  496. This function attempts to connect the stream to one of a sequence of
  497. endpoints by trying each endpoint until a connection is successfully
  498. established.
  499. The underlying socket is automatically opened if needed.
  500. An automatically opened socket is not returned to the
  501. closed state upon failure.
  502. The algorithm, known as a <em>composed operation</em>, is implemented
  503. in terms of calls to the underlying socket's `connect` function.
  504. @param endpoints A sequence of endpoints.
  505. @param ec Set to indicate what error occurred, if any. If the sequence is
  506. empty, set to `net::error::not_found`. Otherwise, contains the error
  507. from the last connection attempt.
  508. @returns On success, the successfully connected endpoint. Otherwise, a
  509. default-constructed endpoint.
  510. */
  511. template<class EndpointSequence
  512. #if ! BOOST_BEAST_DOXYGEN
  513. ,class = typename std::enable_if<
  514. net::is_endpoint_sequence<
  515. EndpointSequence>::value>::type
  516. #endif
  517. >
  518. typename Protocol::endpoint
  519. connect(
  520. EndpointSequence const& endpoints,
  521. error_code& ec
  522. )
  523. {
  524. return net::connect(socket(), endpoints, ec);
  525. }
  526. /** Establishes a connection by trying each endpoint in a sequence.
  527. This function attempts to connect the stream to one of a sequence of
  528. endpoints by trying each endpoint until a connection is successfully
  529. established.
  530. The underlying socket is automatically opened if needed.
  531. An automatically opened socket is not returned to the
  532. closed state upon failure.
  533. The algorithm, known as a <em>composed operation</em>, is implemented
  534. in terms of calls to the underlying socket's `connect` function.
  535. @param begin An iterator pointing to the start of a sequence of endpoints.
  536. @param end An iterator pointing to the end of a sequence of endpoints.
  537. @returns An iterator denoting the successfully connected endpoint.
  538. @throws system_error Thrown on failure. If the sequence is
  539. empty, the associated error code is `net::error::not_found`.
  540. Otherwise, contains the error from the last connection attempt.
  541. */
  542. template<class Iterator>
  543. Iterator
  544. connect(
  545. Iterator begin, Iterator end)
  546. {
  547. return net::connect(socket(), begin, end);
  548. }
  549. /** Establishes a connection by trying each endpoint in a sequence.
  550. This function attempts to connect the stream to one of a sequence of
  551. endpoints by trying each endpoint until a connection is successfully
  552. established.
  553. The underlying socket is automatically opened if needed.
  554. An automatically opened socket is not returned to the
  555. closed state upon failure.
  556. The algorithm, known as a <em>composed operation</em>, is implemented
  557. in terms of calls to the underlying socket's `connect` function.
  558. @param begin An iterator pointing to the start of a sequence of endpoints.
  559. @param end An iterator pointing to the end of a sequence of endpoints.
  560. @param ec Set to indicate what error occurred, if any. If the sequence is
  561. empty, set to boost::asio::error::not_found. Otherwise, contains the error
  562. from the last connection attempt.
  563. @returns On success, an iterator denoting the successfully connected
  564. endpoint. Otherwise, the end iterator.
  565. */
  566. template<class Iterator>
  567. Iterator
  568. connect(
  569. Iterator begin, Iterator end,
  570. error_code& ec)
  571. {
  572. return net::connect(socket(), begin, end, ec);
  573. }
  574. /** Establishes a connection by trying each endpoint in a sequence.
  575. This function attempts to connect the stream to one of a sequence of
  576. endpoints by trying each endpoint until a connection is successfully
  577. established.
  578. The underlying socket is automatically opened if needed.
  579. An automatically opened socket is not returned to the
  580. closed state upon failure.
  581. The algorithm, known as a <em>composed operation</em>, is implemented
  582. in terms of calls to the underlying socket's `connect` function.
  583. @param endpoints A sequence of endpoints.
  584. @param connect_condition A function object that is called prior to each
  585. connection attempt. The signature of the function object must be:
  586. @code
  587. bool connect_condition(
  588. error_code const& ec,
  589. typename Protocol::endpoint const& next);
  590. @endcode
  591. The @c ec parameter contains the result from the most recent connect
  592. operation. Before the first connection attempt, @c ec is always set to
  593. indicate success. The @c next parameter is the next endpoint to be tried.
  594. The function object should return true if the next endpoint should be tried,
  595. and false if it should be skipped.
  596. @returns The successfully connected endpoint.
  597. @throws boost::system::system_error Thrown on failure. If the sequence is
  598. empty, the associated error code is `net::error::not_found`.
  599. Otherwise, contains the error from the last connection attempt.
  600. */
  601. template<
  602. class EndpointSequence, class ConnectCondition
  603. #if ! BOOST_BEAST_DOXYGEN
  604. ,class = typename std::enable_if<
  605. net::is_endpoint_sequence<
  606. EndpointSequence>::value>::type
  607. #endif
  608. >
  609. typename Protocol::endpoint
  610. connect(
  611. EndpointSequence const& endpoints,
  612. ConnectCondition connect_condition
  613. )
  614. {
  615. return net::connect(socket(), endpoints, connect_condition);
  616. }
  617. /** Establishes a connection by trying each endpoint in a sequence.
  618. This function attempts to connect the stream to one of a sequence of
  619. endpoints by trying each endpoint until a connection is successfully
  620. established.
  621. The underlying socket is automatically opened if needed.
  622. An automatically opened socket is not returned to the
  623. closed state upon failure.
  624. The algorithm, known as a <em>composed operation</em>, is implemented
  625. in terms of calls to the underlying socket's `connect` function.
  626. @param endpoints A sequence of endpoints.
  627. @param connect_condition A function object that is called prior to each
  628. connection attempt. The signature of the function object must be:
  629. @code
  630. bool connect_condition(
  631. error_code const& ec,
  632. typename Protocol::endpoint const& next);
  633. @endcode
  634. The @c ec parameter contains the result from the most recent connect
  635. operation. Before the first connection attempt, @c ec is always set to
  636. indicate success. The @c next parameter is the next endpoint to be tried.
  637. The function object should return true if the next endpoint should be tried,
  638. and false if it should be skipped.
  639. @param ec Set to indicate what error occurred, if any. If the sequence is
  640. empty, set to `net::error::not_found`. Otherwise, contains the error
  641. from the last connection attempt.
  642. @returns On success, the successfully connected endpoint. Otherwise, a
  643. default-constructed endpoint.
  644. */
  645. template<
  646. class EndpointSequence, class ConnectCondition
  647. #if ! BOOST_BEAST_DOXYGEN
  648. ,class = typename std::enable_if<
  649. net::is_endpoint_sequence<
  650. EndpointSequence>::value>::type
  651. #endif
  652. >
  653. typename Protocol::endpoint
  654. connect(
  655. EndpointSequence const& endpoints,
  656. ConnectCondition connect_condition,
  657. error_code& ec)
  658. {
  659. return net::connect(socket(), endpoints, connect_condition, ec);
  660. }
  661. /** Establishes a connection by trying each endpoint in a sequence.
  662. This function attempts to connect the stream to one of a sequence of
  663. endpoints by trying each endpoint until a connection is successfully
  664. established.
  665. The underlying socket is automatically opened if needed.
  666. An automatically opened socket is not returned to the
  667. closed state upon failure.
  668. The algorithm, known as a <em>composed operation</em>, is implemented
  669. in terms of calls to the underlying socket's `connect` function.
  670. @param begin An iterator pointing to the start of a sequence of endpoints.
  671. @param end An iterator pointing to the end of a sequence of endpoints.
  672. @param connect_condition A function object that is called prior to each
  673. connection attempt. The signature of the function object must be:
  674. @code
  675. bool connect_condition(
  676. error_code const& ec,
  677. typename Protocol::endpoint const& next);
  678. @endcode
  679. The @c ec parameter contains the result from the most recent connect
  680. operation. Before the first connection attempt, @c ec is always set to
  681. indicate success. The @c next parameter is the next endpoint to be tried.
  682. The function object should return true if the next endpoint should be tried,
  683. and false if it should be skipped.
  684. @returns An iterator denoting the successfully connected endpoint.
  685. @throws boost::system::system_error Thrown on failure. If the sequence is
  686. empty, the associated @c error_code is `net::error::not_found`.
  687. Otherwise, contains the error from the last connection attempt.
  688. */
  689. template<
  690. class Iterator, class ConnectCondition>
  691. Iterator
  692. connect(
  693. Iterator begin, Iterator end,
  694. ConnectCondition connect_condition)
  695. {
  696. return net::connect(socket(), begin, end, connect_condition);
  697. }
  698. /** Establishes a connection by trying each endpoint in a sequence.
  699. This function attempts to connect the stream to one of a sequence of
  700. endpoints by trying each endpoint until a connection is successfully
  701. established.
  702. The underlying socket is automatically opened if needed.
  703. An automatically opened socket is not returned to the
  704. closed state upon failure.
  705. The algorithm, known as a <em>composed operation</em>, is implemented
  706. in terms of calls to the underlying socket's `connect` function.
  707. @param begin An iterator pointing to the start of a sequence of endpoints.
  708. @param end An iterator pointing to the end of a sequence of endpoints.
  709. @param connect_condition A function object that is called prior to each
  710. connection attempt. The signature of the function object must be:
  711. @code
  712. bool connect_condition(
  713. error_code const& ec,
  714. typename Protocol::endpoint const& next);
  715. @endcode
  716. The @c ec parameter contains the result from the most recent connect
  717. operation. Before the first connection attempt, @c ec is always set to
  718. indicate success. The @c next parameter is the next endpoint to be tried.
  719. The function object should return true if the next endpoint should be tried,
  720. and false if it should be skipped.
  721. @param ec Set to indicate what error occurred, if any. If the sequence is
  722. empty, set to `net::error::not_found`. Otherwise, contains the error
  723. from the last connection attempt.
  724. @returns On success, an iterator denoting the successfully connected
  725. endpoint. Otherwise, the end iterator.
  726. */
  727. template<
  728. class Iterator, class ConnectCondition>
  729. Iterator
  730. connect(
  731. Iterator begin, Iterator end,
  732. ConnectCondition connect_condition,
  733. error_code& ec)
  734. {
  735. return net::connect(socket(), begin, end, connect_condition, ec);
  736. }
  737. /** Connect the stream to the specified endpoint asynchronously.
  738. This function is used to asynchronously connect the underlying
  739. socket to the specified remote endpoint. The function call always
  740. returns immediately.
  741. The underlying socket is automatically opened if needed.
  742. An automatically opened socket is not returned to the
  743. closed state upon failure.
  744. If the timeout timer expires while the operation is outstanding,
  745. the operation will be canceled and the completion handler will be
  746. invoked with the error @ref error::timeout.
  747. @param ep The remote endpoint to which the underlying socket will be
  748. connected. Copies will be made of the endpoint object as required.
  749. @param handler The completion handler to invoke when the operation
  750. completes. The implementation takes ownership of the handler by
  751. performing a decay-copy. The equivalent function signature of
  752. the handler must be:
  753. @code
  754. void handler(
  755. error_code ec // Result of operation
  756. );
  757. @endcode
  758. If the handler has an associated immediate executor,
  759. an immediate completion will be dispatched to it.
  760. Otherwise, the handler will not be invoked from within
  761. this function. Invocation of the handler will be performed
  762. by dispatching to the immediate executor. If no
  763. immediate executor is specified, this is equivalent
  764. to using `net::post`.
  765. @par Per-Operation Cancellation
  766. This asynchronous operation supports cancellation for the following
  767. net::cancellation_type values:
  768. @li @c net::cancellation_type::terminal
  769. @li @c net::cancellation_type::partial
  770. @li @c net::cancellation_type::total
  771. if they are also supported by the socket's @c async_connect operation.
  772. @see async_connect
  773. */
  774. template<
  775. BOOST_BEAST_ASYNC_TPARAM1 ConnectHandler =
  776. net::default_completion_token_t<executor_type>
  777. >
  778. BOOST_BEAST_ASYNC_RESULT1(ConnectHandler)
  779. async_connect(
  780. endpoint_type const& ep,
  781. ConnectHandler&& handler =
  782. net::default_completion_token_t<
  783. executor_type>{});
  784. /** Establishes a connection by trying each endpoint in a sequence asynchronously.
  785. This function attempts to connect the stream to one of a sequence of
  786. endpoints by trying each endpoint until a connection is successfully
  787. established.
  788. The underlying socket is automatically opened if needed.
  789. An automatically opened socket is not returned to the
  790. closed state upon failure.
  791. The algorithm, known as a <em>composed asynchronous operation</em>, is
  792. implemented in terms of calls to the underlying socket's `async_connect`
  793. function.
  794. If the timeout timer expires while the operation is outstanding,
  795. the current connection attempt will be canceled and the completion
  796. handler will be invoked with the error @ref error::timeout.
  797. @param endpoints A sequence of endpoints. This this object must meet
  798. the requirements of <em>EndpointSequence</em>.
  799. @param handler The completion handler to invoke when the operation
  800. completes. The implementation takes ownership of the handler by
  801. performing a decay-copy. The equivalent function signature of
  802. the handler must be:
  803. @code
  804. void handler(
  805. // Result of operation. if the sequence is empty, set to
  806. // net::error::not_found. Otherwise, contains the
  807. // error from the last connection attempt.
  808. error_code const& error,
  809. // On success, the successfully connected endpoint.
  810. // Otherwise, a default-constructed endpoint.
  811. typename Protocol::endpoint const& endpoint
  812. );
  813. @endcode
  814. If the handler has an associated immediate executor,
  815. an immediate completion will be dispatched to it.
  816. Otherwise, the handler will not be invoked from within
  817. this function. Invocation of the handler will be performed
  818. by dispatching to the immediate executor. If no
  819. immediate executor is specified, this is equivalent
  820. to using `net::post`.
  821. @par Per-Operation Cancellation
  822. This asynchronous operation supports cancellation for the following
  823. net::cancellation_type values:
  824. @li @c net::cancellation_type::terminal
  825. @li @c net::cancellation_type::partial
  826. @li @c net::cancellation_type::total
  827. if they are also supported by the socket's @c async_connect operation.
  828. */
  829. template<
  830. class EndpointSequence,
  831. BOOST_ASIO_COMPLETION_TOKEN_FOR(
  832. void(error_code, typename Protocol::endpoint))
  833. RangeConnectHandler =
  834. net::default_completion_token_t<executor_type>
  835. #if ! BOOST_BEAST_DOXYGEN
  836. ,class = typename std::enable_if<
  837. net::is_endpoint_sequence<
  838. EndpointSequence>::value>::type
  839. ,class = typename std::enable_if<
  840. !net::is_connect_condition<RangeConnectHandler,
  841. decltype(std::declval<const EndpointSequence&>().begin())>::value
  842. >::type
  843. #endif
  844. >
  845. BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(
  846. RangeConnectHandler,
  847. void(error_code, typename Protocol::endpoint))
  848. async_connect(
  849. EndpointSequence const& endpoints,
  850. RangeConnectHandler&& handler =
  851. net::default_completion_token_t<executor_type>{});
  852. /** Establishes a connection by trying each endpoint in a sequence asynchronously.
  853. This function attempts to connect the stream to one of a sequence of
  854. endpoints by trying each endpoint until a connection is successfully
  855. established.
  856. The underlying socket is automatically opened if needed.
  857. An automatically opened socket is not returned to the
  858. closed state upon failure.
  859. The algorithm, known as a <em>composed asynchronous operation</em>, is
  860. implemented in terms of calls to the underlying socket's `async_connect`
  861. function.
  862. If the timeout timer expires while the operation is outstanding,
  863. the current connection attempt will be canceled and the completion
  864. handler will be invoked with the error @ref error::timeout.
  865. @param endpoints A sequence of endpoints. This this object must meet
  866. the requirements of <em>EndpointSequence</em>.
  867. @param connect_condition A function object that is called prior to each
  868. connection attempt. The signature of the function object must be:
  869. @code
  870. bool connect_condition(
  871. error_code const& ec,
  872. typename Protocol::endpoint const& next);
  873. @endcode
  874. The @c ec parameter contains the result from the most recent connect
  875. operation. Before the first connection attempt, @c ec is always set to
  876. indicate success. The @c next parameter is the next endpoint to be tried.
  877. The function object should return true if the next endpoint should be tried,
  878. and false if it should be skipped.
  879. @param handler The completion handler to invoke when the operation
  880. completes. The implementation takes ownership of the handler by
  881. performing a decay-copy. The equivalent function signature of
  882. the handler must be:
  883. @code
  884. void handler(
  885. // Result of operation. if the sequence is empty, set to
  886. // net::error::not_found. Otherwise, contains the
  887. // error from the last connection attempt.
  888. error_code const& error,
  889. // On success, the successfully connected endpoint.
  890. // Otherwise, a default-constructed endpoint.
  891. typename Protocol::endpoint const& endpoint
  892. );
  893. @endcode
  894. If the handler has an associated immediate executor,
  895. an immediate completion will be dispatched to it.
  896. Otherwise, the handler will not be invoked from within
  897. this function. Invocation of the handler will be performed
  898. by dispatching to the immediate executor. If no
  899. immediate executor is specified, this is equivalent
  900. to using `net::post`.
  901. @par Example
  902. The following connect condition function object can be used to output
  903. information about the individual connection attempts:
  904. @code
  905. struct my_connect_condition
  906. {
  907. bool operator()(
  908. error_code const& ec,
  909. net::ip::tcp::endpoint const& next)
  910. {
  911. if (ec)
  912. std::cout << "Error: " << ec.message() << std::endl;
  913. std::cout << "Trying: " << next << std::endl;
  914. return true;
  915. }
  916. };
  917. @endcode
  918. @par Per-Operation Cancellation
  919. This asynchronous operation supports cancellation for the following
  920. net::cancellation_type values:
  921. @li @c net::cancellation_type::terminal
  922. @li @c net::cancellation_type::partial
  923. @li @c net::cancellation_type::total
  924. if they are also supported by the socket's @c async_connect operation.
  925. */
  926. template<
  927. class EndpointSequence,
  928. class ConnectCondition,
  929. BOOST_ASIO_COMPLETION_TOKEN_FOR(
  930. void(error_code, typename Protocol::endpoint))
  931. RangeConnectHandler =
  932. net::default_completion_token_t<executor_type>
  933. #if ! BOOST_BEAST_DOXYGEN
  934. ,class = typename std::enable_if<
  935. net::is_endpoint_sequence<
  936. EndpointSequence>::value>::type
  937. ,class = typename std::enable_if<
  938. net::is_connect_condition<ConnectCondition,
  939. decltype(std::declval<const EndpointSequence&>().begin())>::value
  940. >::type
  941. #endif
  942. >
  943. BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(
  944. RangeConnectHandler,
  945. void(error_code, typename Protocol::endpoint))
  946. async_connect(
  947. EndpointSequence const& endpoints,
  948. ConnectCondition connect_condition,
  949. RangeConnectHandler&& handler =
  950. net::default_completion_token_t<
  951. executor_type>{});
  952. /** Establishes a connection by trying each endpoint in a sequence asynchronously.
  953. This function attempts to connect the stream to one of a sequence of
  954. endpoints by trying each endpoint until a connection is successfully
  955. established.
  956. The underlying socket is automatically opened if needed.
  957. An automatically opened socket is not returned to the
  958. closed state upon failure.
  959. The algorithm, known as a <em>composed asynchronous operation</em>, is
  960. implemented in terms of calls to the underlying socket's `async_connect`
  961. function.
  962. If the timeout timer expires while the operation is outstanding,
  963. the current connection attempt will be canceled and the completion
  964. handler will be invoked with the error @ref error::timeout.
  965. @param begin An iterator pointing to the start of a sequence of endpoints.
  966. @param end An iterator pointing to the end of a sequence of endpoints.
  967. @param handler The completion handler to invoke when the operation
  968. completes. The implementation takes ownership of the handler by
  969. performing a decay-copy. The equivalent function signature of
  970. the handler must be:
  971. @code
  972. void handler(
  973. // Result of operation. if the sequence is empty, set to
  974. // net::error::not_found. Otherwise, contains the
  975. // error from the last connection attempt.
  976. error_code const& error,
  977. // On success, an iterator denoting the successfully
  978. // connected endpoint. Otherwise, the end iterator.
  979. Iterator iterator
  980. );
  981. @endcode
  982. If the handler has an associated immediate executor,
  983. an immediate completion will be dispatched to it.
  984. Otherwise, the handler will not be invoked from within
  985. this function. Invocation of the handler will be performed
  986. by dispatching to the immediate executor. If no
  987. immediate executor is specified, this is equivalent
  988. to using `net::post`.
  989. @par Per-Operation Cancellation
  990. This asynchronous operation supports cancellation for the following
  991. net::cancellation_type values:
  992. @li @c net::cancellation_type::terminal
  993. @li @c net::cancellation_type::partial
  994. @li @c net::cancellation_type::total
  995. if they are also supported by the socket's @c async_connect operation.
  996. */
  997. template<
  998. class Iterator,
  999. BOOST_ASIO_COMPLETION_TOKEN_FOR(
  1000. void(error_code, Iterator))
  1001. IteratorConnectHandler =
  1002. net::default_completion_token_t<executor_type>
  1003. #if ! BOOST_BEAST_DOXYGEN
  1004. ,class = typename std::enable_if<
  1005. !net::is_connect_condition<IteratorConnectHandler, Iterator>::value
  1006. >::type
  1007. #endif
  1008. >
  1009. BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(
  1010. IteratorConnectHandler,
  1011. void(error_code, Iterator))
  1012. async_connect(
  1013. Iterator begin, Iterator end,
  1014. IteratorConnectHandler&& handler =
  1015. net::default_completion_token_t<executor_type>{});
  1016. /** Establishes a connection by trying each endpoint in a sequence asynchronously.
  1017. This function attempts to connect the stream to one of a sequence of
  1018. endpoints by trying each endpoint until a connection is successfully
  1019. established.
  1020. The algorithm, known as a <em>composed asynchronous operation</em>, is
  1021. implemented in terms of calls to the underlying socket's `async_connect`
  1022. function.
  1023. If the timeout timer expires while the operation is outstanding,
  1024. the current connection attempt will be canceled and the completion
  1025. handler will be invoked with the error @ref error::timeout.
  1026. @param begin An iterator pointing to the start of a sequence of endpoints.
  1027. @param end An iterator pointing to the end of a sequence of endpoints.
  1028. @param connect_condition A function object that is called prior to each
  1029. connection attempt. The signature of the function object must be:
  1030. @code
  1031. bool connect_condition(
  1032. error_code const& ec,
  1033. typename Protocol::endpoint const& next);
  1034. @endcode
  1035. @param handler The completion handler to invoke when the operation
  1036. completes. The implementation takes ownership of the handler by
  1037. performing a decay-copy. The equivalent function signature of
  1038. the handler must be:
  1039. @code
  1040. void handler(
  1041. // Result of operation. if the sequence is empty, set to
  1042. // net::error::not_found. Otherwise, contains the
  1043. // error from the last connection attempt.
  1044. error_code const& error,
  1045. // On success, an iterator denoting the successfully
  1046. // connected endpoint. Otherwise, the end iterator.
  1047. Iterator iterator
  1048. );
  1049. @endcode
  1050. If the handler has an associated immediate executor,
  1051. an immediate completion will be dispatched to it.
  1052. Otherwise, the handler will not be invoked from within
  1053. this function. Invocation of the handler will be performed
  1054. by dispatching to the immediate executor. If no
  1055. immediate executor is specified, this is equivalent
  1056. to using `net::post`.
  1057. @par Per-Operation Cancellation
  1058. This asynchronous operation supports cancellation for the following
  1059. net::cancellation_type values:
  1060. @li @c net::cancellation_type::terminal
  1061. @li @c net::cancellation_type::partial
  1062. @li @c net::cancellation_type::total
  1063. if they are also supported by the socket's @c async_connect operation.
  1064. */
  1065. template<
  1066. class Iterator,
  1067. class ConnectCondition,
  1068. BOOST_ASIO_COMPLETION_TOKEN_FOR(
  1069. void(error_code, Iterator))
  1070. IteratorConnectHandler =
  1071. net::default_completion_token_t<executor_type>
  1072. #if ! BOOST_BEAST_DOXYGEN
  1073. ,class = typename std::enable_if<
  1074. net::is_connect_condition<ConnectCondition, Iterator>::value
  1075. >::type
  1076. #endif
  1077. >
  1078. BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(
  1079. IteratorConnectHandler,
  1080. void(error_code, Iterator))
  1081. async_connect(
  1082. Iterator begin, Iterator end,
  1083. ConnectCondition connect_condition,
  1084. IteratorConnectHandler&& handler =
  1085. net::default_completion_token_t<executor_type>{});
  1086. //--------------------------------------------------------------------------
  1087. /** Read some data.
  1088. This function is used to read some data from the stream.
  1089. The call blocks until one of the following is true:
  1090. @li One or more bytes are read from the stream.
  1091. @li An error occurs.
  1092. @param buffers The buffers into which the data will be read. If the
  1093. size of the buffers is zero bytes, the call always returns
  1094. immediately with no error.
  1095. @returns The number of bytes read.
  1096. @throws system_error Thrown on failure.
  1097. @note The `read_some` operation may not receive all of the requested
  1098. number of bytes. Consider using the function `net::read` if you need
  1099. to ensure that the requested amount of data is read before the
  1100. blocking operation completes.
  1101. */
  1102. template<class MutableBufferSequence>
  1103. std::size_t
  1104. read_some(MutableBufferSequence const& buffers)
  1105. {
  1106. return impl_->socket.read_some(buffers);
  1107. }
  1108. /** Read some data.
  1109. This function is used to read some data from the underlying socket.
  1110. The call blocks until one of the following is true:
  1111. @li One or more bytes are read from the stream.
  1112. @li An error occurs.
  1113. @param buffers The buffers into which the data will be read. If the
  1114. size of the buffers is zero bytes, the call always returns
  1115. immediately with no error.
  1116. @param ec Set to indicate what error occurred, if any.
  1117. @returns The number of bytes read.
  1118. @note The `read_some` operation may not receive all of the requested
  1119. number of bytes. Consider using the function `net::read` if you need
  1120. to ensure that the requested amount of data is read before the
  1121. blocking operation completes.
  1122. */
  1123. template<class MutableBufferSequence>
  1124. std::size_t
  1125. read_some(
  1126. MutableBufferSequence const& buffers,
  1127. error_code& ec)
  1128. {
  1129. return impl_->socket.read_some(buffers, ec);
  1130. }
  1131. /** Read some data asynchronously.
  1132. This function is used to asynchronously read data from the stream.
  1133. This call always returns immediately. The asynchronous operation
  1134. will continue until one of the following conditions is true:
  1135. @li One or more bytes are read from the stream.
  1136. @li An error occurs.
  1137. The algorithm, known as a <em>composed asynchronous operation</em>,
  1138. is implemented in terms of calls to the next layer's `async_read_some`
  1139. function. The program must ensure that no other calls to @ref read_some
  1140. or @ref async_read_some are performed until this operation completes.
  1141. If the timeout timer expires while the operation is outstanding,
  1142. the operation will be canceled and the completion handler will be
  1143. invoked with the error @ref error::timeout.
  1144. @param buffers The buffers into which the data will be read. If the size
  1145. of the buffers is zero bytes, the operation always completes immediately
  1146. with no error.
  1147. Although the buffers object may be copied as necessary, ownership of the
  1148. underlying memory blocks is retained by the caller, which must guarantee
  1149. that they remain valid until the handler is called.
  1150. @param handler The completion handler to invoke when the operation
  1151. completes. The implementation takes ownership of the handler by
  1152. performing a decay-copy. The equivalent function signature of
  1153. the handler must be:
  1154. @code
  1155. void handler(
  1156. error_code error, // Result of operation.
  1157. std::size_t bytes_transferred // Number of bytes read.
  1158. );
  1159. @endcode
  1160. If the handler has an associated immediate executor,
  1161. an immediate completion will be dispatched to it.
  1162. Otherwise, the handler will not be invoked from within
  1163. this function. Invocation of the handler will be performed
  1164. by dispatching to the immediate executor. If no
  1165. immediate executor is specified, this is equivalent
  1166. to using `net::post`.
  1167. @note The `async_read_some` operation may not receive all of the requested
  1168. number of bytes. Consider using the function `net::async_read` if you need
  1169. to ensure that the requested amount of data is read before the asynchronous
  1170. operation completes.
  1171. @par Per-Operation Cancellation
  1172. This asynchronous operation supports cancellation for the following
  1173. net::cancellation_type values:
  1174. @li @c net::cancellation_type::terminal
  1175. @li @c net::cancellation_type::partial
  1176. @li @c net::cancellation_type::total
  1177. if they are also supported by the socket's @c async_read_some operation.
  1178. */
  1179. template<
  1180. class MutableBufferSequence,
  1181. BOOST_BEAST_ASYNC_TPARAM2 ReadHandler =
  1182. net::default_completion_token_t<executor_type>
  1183. >
  1184. BOOST_BEAST_ASYNC_RESULT2(ReadHandler)
  1185. async_read_some(
  1186. MutableBufferSequence const& buffers,
  1187. ReadHandler&& handler =
  1188. net::default_completion_token_t<executor_type>{}
  1189. );
  1190. /** Write some data.
  1191. This function is used to write some data to the stream.
  1192. The call blocks until one of the following is true:
  1193. @li One or more bytes are written to the stream.
  1194. @li An error occurs.
  1195. @param buffers The buffers from which the data will be written. If the
  1196. size of the buffers is zero bytes, the call always returns immediately
  1197. with no error.
  1198. @returns The number of bytes written.
  1199. @throws system_error Thrown on failure.
  1200. @note The `write_some` operation may not transmit all of the requested
  1201. number of bytes. Consider using the function `net::write` if you need
  1202. to ensure that the requested amount of data is written before the
  1203. blocking operation completes.
  1204. */
  1205. template<class ConstBufferSequence>
  1206. std::size_t
  1207. write_some(ConstBufferSequence const& buffers)
  1208. {
  1209. return impl_->socket.write_some(buffers);
  1210. }
  1211. /** Write some data.
  1212. This function is used to write some data to the stream.
  1213. The call blocks until one of the following is true:
  1214. @li One or more bytes are written to the stream.
  1215. @li An error occurs.
  1216. @param buffers The buffers from which the data will be written. If the
  1217. size of the buffers is zero bytes, the call always returns immediately
  1218. with no error.
  1219. @param ec Set to indicate what error occurred, if any.
  1220. @returns The number of bytes written.
  1221. @throws system_error Thrown on failure.
  1222. @note The `write_some` operation may not transmit all of the requested
  1223. number of bytes. Consider using the function `net::write` if you need
  1224. to ensure that the requested amount of data is written before the
  1225. blocking operation completes.
  1226. */
  1227. template<class ConstBufferSequence>
  1228. std::size_t
  1229. write_some(
  1230. ConstBufferSequence const& buffers,
  1231. error_code& ec)
  1232. {
  1233. return impl_->socket.write_some(buffers, ec);
  1234. }
  1235. /** Write some data asynchronously.
  1236. This function is used to asynchronously write data to the underlying socket.
  1237. This call always returns immediately. The asynchronous operation
  1238. will continue until one of the following conditions is true:
  1239. @li One or more bytes are written to the stream.
  1240. @li An error occurs.
  1241. The algorithm, known as a <em>composed asynchronous operation</em>,
  1242. is implemented in terms of calls to the next layer's `async_write_some`
  1243. function. The program must ensure that no other calls to @ref async_write_some
  1244. are performed until this operation completes.
  1245. If the timeout timer expires while the operation is outstanding,
  1246. the operation will be canceled and the completion handler will be
  1247. invoked with the error @ref error::timeout.
  1248. @param buffers The buffers from which the data will be written. If the
  1249. size of the buffers is zero bytes, the operation always completes
  1250. immediately with no error.
  1251. Although the buffers object may be copied as necessary, ownership of the
  1252. underlying memory blocks is retained by the caller, which must guarantee
  1253. that they remain valid until the handler is called.
  1254. @param handler The completion handler to invoke when the operation
  1255. completes. The implementation takes ownership of the handler by
  1256. performing a decay-copy. The equivalent function signature of
  1257. the handler must be:
  1258. @code
  1259. void handler(
  1260. error_code error, // Result of operation.
  1261. std::size_t bytes_transferred // Number of bytes written.
  1262. );
  1263. @endcode
  1264. If the handler has an associated immediate executor,
  1265. an immediate completion will be dispatched to it.
  1266. Otherwise, the handler will not be invoked from within
  1267. this function. Invocation of the handler will be performed
  1268. by dispatching to the immediate executor. If no
  1269. immediate executor is specified, this is equivalent
  1270. to using `net::post`.
  1271. @note The `async_write_some` operation may not transmit all of the requested
  1272. number of bytes. Consider using the function `net::async_write` if you need
  1273. to ensure that the requested amount of data is sent before the asynchronous
  1274. operation completes.
  1275. @par Per-Operation Cancellation
  1276. This asynchronous operation supports cancellation for the following
  1277. net::cancellation_type values:
  1278. @li @c net::cancellation_type::terminal
  1279. @li @c net::cancellation_type::partial
  1280. @li @c net::cancellation_type::total
  1281. if they are also supported by the socket's @c async_write_some operation.
  1282. */
  1283. template<
  1284. class ConstBufferSequence,
  1285. BOOST_BEAST_ASYNC_TPARAM2 WriteHandler =
  1286. net::default_completion_token_t<Executor>
  1287. >
  1288. BOOST_BEAST_ASYNC_RESULT2(WriteHandler)
  1289. async_write_some(
  1290. ConstBufferSequence const& buffers,
  1291. WriteHandler&& handler =
  1292. net::default_completion_token_t<Executor>{});
  1293. };
  1294. } // beast
  1295. } // boost
  1296. #include <boost/beast/core/impl/basic_stream.hpp>
  1297. #endif