basic_stream.hpp 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459
  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::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::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. static_assert(net::is_executor<Executor>::value,
  192. "Executor type requirements not met");
  193. struct impl_type
  194. : boost::enable_shared_from_this<impl_type>
  195. , boost::empty_value<RatePolicy>
  196. {
  197. // must come first
  198. net::basic_stream_socket<
  199. Protocol, Executor> socket;
  200. op_state read;
  201. op_state write;
  202. #if 0
  203. net::basic_waitable_timer<
  204. std::chrono::steady_clock,
  205. net::wait_traits<
  206. std::chrono::steady_clock>,
  207. Executor> timer; // rate timer;
  208. #else
  209. net::steady_timer timer;
  210. #endif
  211. int waiting = 0;
  212. impl_type(impl_type&&) = default;
  213. template<class... Args>
  214. explicit
  215. impl_type(std::false_type, Args&&...);
  216. template<class RatePolicy_, class... Args>
  217. explicit
  218. impl_type(std::true_type,
  219. RatePolicy_&& policy, Args&&...);
  220. impl_type& operator=(impl_type&&) = delete;
  221. beast::executor_type<socket_type>
  222. ex() noexcept
  223. {
  224. return this->socket.get_executor();
  225. }
  226. RatePolicy&
  227. policy() noexcept
  228. {
  229. return this->boost::empty_value<RatePolicy>::get();
  230. }
  231. RatePolicy const&
  232. policy() const noexcept
  233. {
  234. return this->boost::empty_value<RatePolicy>::get();
  235. }
  236. template<class Executor2>
  237. void on_timer(Executor2 const& ex2);
  238. void reset(); // set timeouts to never
  239. void close(); // cancel everything
  240. };
  241. // We use shared ownership for the state so it can
  242. // outlive the destruction of the stream_socket object,
  243. // in the case where there is no outstanding read or write
  244. // but the implementation is still waiting on a timer.
  245. boost::shared_ptr<impl_type> impl_;
  246. template<class Executor2>
  247. struct timeout_handler;
  248. struct ops;
  249. #if ! BOOST_BEAST_DOXYGEN
  250. // boost::asio::ssl::stream needs these
  251. // DEPRECATED
  252. template<class>
  253. friend class boost::asio::ssl::stream;
  254. // DEPRECATED
  255. using lowest_layer_type = socket_type;
  256. // DEPRECATED
  257. lowest_layer_type&
  258. lowest_layer() noexcept
  259. {
  260. return impl_->socket;
  261. }
  262. // DEPRECATED
  263. lowest_layer_type const&
  264. lowest_layer() const noexcept
  265. {
  266. return impl_->socket;
  267. }
  268. #endif
  269. public:
  270. /** Destructor
  271. This function destroys the stream, cancelling any outstanding
  272. asynchronous operations associated with the socket as if by
  273. calling cancel.
  274. */
  275. ~basic_stream();
  276. /** Constructor
  277. This constructor creates the stream by forwarding all arguments
  278. to the underlying socket. The socket then needs to be open and
  279. connected or accepted before data can be sent or received on it.
  280. @param args A list of parameters forwarded to the constructor of
  281. the underlying socket.
  282. */
  283. #if BOOST_BEAST_DOXYGEN
  284. template<class... Args>
  285. explicit
  286. basic_stream(Args&&... args);
  287. #else
  288. template<class Arg0, class... Args,
  289. class = typename std::enable_if<
  290. ! std::is_constructible<RatePolicy, Arg0>::value>::type>
  291. explicit
  292. basic_stream(Arg0&& argo, Args&&... args);
  293. #endif
  294. /** Constructor
  295. This constructor creates the stream with the specified rate
  296. policy, and forwards all remaining arguments to the underlying
  297. socket. The socket then needs to be open and connected or
  298. accepted before data can be sent or received on it.
  299. @param policy The rate policy object to use. The stream will
  300. take ownership of this object by decay-copy.
  301. @param args A list of parameters forwarded to the constructor of
  302. the underlying socket.
  303. */
  304. #if BOOST_BEAST_DOXYGEN
  305. template<class RatePolicy_, class... Args>
  306. explicit
  307. basic_stream(RatePolicy_&& policy, Args&&... args);
  308. #else
  309. template<class RatePolicy_, class Arg0, class... Args,
  310. class = typename std::enable_if<
  311. std::is_constructible<
  312. RatePolicy, RatePolicy_>::value>::type>
  313. basic_stream(
  314. RatePolicy_&& policy, Arg0&& arg, Args&&... args);
  315. #endif
  316. /** Move constructor
  317. @param other The other object from which the move will occur.
  318. @note Following the move, the moved-from object is in the
  319. same state as if newly constructed.
  320. */
  321. basic_stream(basic_stream&& other);
  322. /// Move assignment (deleted).
  323. basic_stream& operator=(basic_stream&&) = delete;
  324. /// Return a reference to the underlying socket
  325. socket_type&
  326. socket() noexcept
  327. {
  328. return impl_->socket;
  329. }
  330. /// Return a reference to the underlying socket
  331. socket_type const&
  332. socket() const noexcept
  333. {
  334. return impl_->socket;
  335. }
  336. /** Release ownership of the underlying socket.
  337. This function causes all outstanding asynchronous connect,
  338. read, and write operations to be canceled as if by a call
  339. to @ref cancel. Ownership of the underlying socket is then
  340. transferred to the caller.
  341. */
  342. socket_type
  343. release_socket();
  344. //--------------------------------------------------------------------------
  345. /// Returns the rate policy associated with the object
  346. RatePolicy&
  347. rate_policy() noexcept
  348. {
  349. return impl_->policy();
  350. }
  351. /// Returns the rate policy associated with the object
  352. RatePolicy const&
  353. rate_policy() const noexcept
  354. {
  355. return impl_->policy();
  356. }
  357. /** Set the timeout for the next logical operation.
  358. This sets either the read timer, the write timer, or
  359. both timers to expire after the specified amount of time
  360. has elapsed. If a timer expires when the corresponding
  361. asynchronous operation is outstanding, the stream will be
  362. closed and any outstanding operations will complete with the
  363. error @ref beast::error::timeout. Otherwise, if the timer
  364. expires while no operations are outstanding, and the expiraton
  365. is not set again, the next operation will time out immediately.
  366. The timer applies collectively to any asynchronous reads
  367. or writes initiated after the expiration is set, until the
  368. expiration is set again. A call to @ref async_connect
  369. counts as both a read and a write.
  370. @param expiry_time The amount of time after which a logical
  371. operation should be considered timed out.
  372. */
  373. void
  374. expires_after(
  375. std::chrono::nanoseconds expiry_time);
  376. /** Set the timeout for the next logical operation.
  377. This sets either the read timer, the write timer, or both
  378. timers to expire at the specified time point. If a timer
  379. expires when the corresponding asynchronous operation is
  380. outstanding, the stream will be closed and any outstanding
  381. operations will complete with the error @ref beast::error::timeout.
  382. Otherwise, if the timer expires while no operations are outstanding,
  383. and the expiraton is not set again, the next operation will time out
  384. immediately.
  385. The timer applies collectively to any asynchronous reads
  386. or writes initiated after the expiration is set, until the
  387. expiration is set again. A call to @ref async_connect
  388. counts as both a read and a write.
  389. @param expiry_time The time point after which a logical
  390. operation should be considered timed out.
  391. */
  392. void
  393. expires_at(net::steady_timer::time_point expiry_time);
  394. /// Disable the timeout for the next logical operation.
  395. void
  396. expires_never();
  397. /** Cancel all asynchronous operations associated with the socket.
  398. This function causes all outstanding asynchronous connect,
  399. read, and write operations to finish immediately. Completion
  400. handlers for cancelled operations will receive the error
  401. `net::error::operation_aborted`. Completion handlers not
  402. yet invoked whose operations have completed, will receive
  403. the error corresponding to the result of the operation (which
  404. may indicate success).
  405. */
  406. void
  407. cancel();
  408. /** Close the timed stream.
  409. This cancels all of the outstanding asynchronous operations
  410. as if by calling @ref cancel, and closes the underlying socket.
  411. */
  412. void
  413. close();
  414. //--------------------------------------------------------------------------
  415. /** Get the executor associated with the object.
  416. This function may be used to obtain the executor object that the
  417. stream uses to dispatch completion handlers without an assocaited
  418. executor.
  419. @return A copy of the executor that stream will use to dispatch handlers.
  420. */
  421. executor_type
  422. get_executor() noexcept
  423. {
  424. return impl_->ex();
  425. }
  426. /** Connect the stream to the specified endpoint.
  427. This function is used to connect the underlying socket to the
  428. specified remote endpoint. The function call will block until
  429. the connection is successfully made or an error occurs.
  430. The underlying socket is automatically opened if needed.
  431. An automatically opened socket is not returned to the
  432. closed state upon failure.
  433. @param ep The remote endpoint to connect to.
  434. @throws system_error Thrown on failure.
  435. @see connect
  436. */
  437. void
  438. connect(endpoint_type const& ep)
  439. {
  440. socket().connect(ep);
  441. }
  442. /** Connect the stream to the specified endpoint.
  443. This function is used to connect the underlying socket to the
  444. specified remote endpoint. The function call will block until
  445. the connection is successfully made or an error occurs.
  446. The underlying socket is automatically opened if needed.
  447. An automatically opened socket is not returned to the
  448. closed state upon failure.
  449. @param ep The remote endpoint to connect to.
  450. @param ec Set to indicate what error occurred, if any.
  451. @see connect
  452. */
  453. void
  454. connect(endpoint_type const& ep, error_code& ec)
  455. {
  456. socket().connect(ep, ec);
  457. }
  458. /** Establishes a connection by trying each endpoint in a sequence.
  459. This function attempts to connect the stream to one of a sequence of
  460. endpoints by trying each endpoint until a connection is successfully
  461. established.
  462. The underlying socket is automatically opened if needed.
  463. An automatically opened socket is not returned to the
  464. closed state upon failure.
  465. The algorithm, known as a <em>composed operation</em>, is implemented
  466. in terms of calls to the underlying socket's `connect` function.
  467. @param endpoints A sequence of endpoints.
  468. @returns The successfully connected endpoint.
  469. @throws system_error Thrown on failure. If the sequence is
  470. empty, the associated error code is `net::error::not_found`.
  471. Otherwise, contains the error from the last connection attempt.
  472. */
  473. template<class EndpointSequence
  474. #if ! BOOST_BEAST_DOXYGEN
  475. ,class = typename std::enable_if<
  476. net::is_endpoint_sequence<
  477. EndpointSequence>::value>::type
  478. #endif
  479. >
  480. typename Protocol::endpoint
  481. connect(EndpointSequence const& endpoints)
  482. {
  483. return net::connect(socket(), endpoints);
  484. }
  485. /** Establishes a connection by trying each endpoint in a sequence.
  486. This function attempts to connect the stream to one of a sequence of
  487. endpoints by trying each endpoint until a connection is successfully
  488. established.
  489. The underlying socket is automatically opened if needed.
  490. An automatically opened socket is not returned to the
  491. closed state upon failure.
  492. The algorithm, known as a <em>composed operation</em>, is implemented
  493. in terms of calls to the underlying socket's `connect` function.
  494. @param endpoints A sequence of endpoints.
  495. @param ec Set to indicate what error occurred, if any. If the sequence is
  496. empty, set to `net::error::not_found`. Otherwise, contains the error
  497. from the last connection attempt.
  498. @returns On success, the successfully connected endpoint. Otherwise, a
  499. default-constructed endpoint.
  500. */
  501. template<class EndpointSequence
  502. #if ! BOOST_BEAST_DOXYGEN
  503. ,class = typename std::enable_if<
  504. net::is_endpoint_sequence<
  505. EndpointSequence>::value>::type
  506. #endif
  507. >
  508. typename Protocol::endpoint
  509. connect(
  510. EndpointSequence const& endpoints,
  511. error_code& ec
  512. )
  513. {
  514. return net::connect(socket(), endpoints, ec);
  515. }
  516. /** Establishes a connection by trying each endpoint in a sequence.
  517. This function attempts to connect the stream to one of a sequence of
  518. endpoints by trying each endpoint until a connection is successfully
  519. established.
  520. The underlying socket is automatically opened if needed.
  521. An automatically opened socket is not returned to the
  522. closed state upon failure.
  523. The algorithm, known as a <em>composed operation</em>, is implemented
  524. in terms of calls to the underlying socket's `connect` function.
  525. @param begin An iterator pointing to the start of a sequence of endpoints.
  526. @param end An iterator pointing to the end of a sequence of endpoints.
  527. @returns An iterator denoting the successfully connected endpoint.
  528. @throws system_error Thrown on failure. If the sequence is
  529. empty, the associated error code is `net::error::not_found`.
  530. Otherwise, contains the error from the last connection attempt.
  531. */
  532. template<class Iterator>
  533. Iterator
  534. connect(
  535. Iterator begin, Iterator end)
  536. {
  537. return net::connect(socket(), begin, end);
  538. }
  539. /** Establishes a connection by trying each endpoint in a sequence.
  540. This function attempts to connect the stream to one of a sequence of
  541. endpoints by trying each endpoint until a connection is successfully
  542. established.
  543. The underlying socket is automatically opened if needed.
  544. An automatically opened socket is not returned to the
  545. closed state upon failure.
  546. The algorithm, known as a <em>composed operation</em>, is implemented
  547. in terms of calls to the underlying socket's `connect` function.
  548. @param begin An iterator pointing to the start of a sequence of endpoints.
  549. @param end An iterator pointing to the end of a sequence of endpoints.
  550. @param ec Set to indicate what error occurred, if any. If the sequence is
  551. empty, set to boost::asio::error::not_found. Otherwise, contains the error
  552. from the last connection attempt.
  553. @returns On success, an iterator denoting the successfully connected
  554. endpoint. Otherwise, the end iterator.
  555. */
  556. template<class Iterator>
  557. Iterator
  558. connect(
  559. Iterator begin, Iterator end,
  560. error_code& ec)
  561. {
  562. return net::connect(socket(), begin, end, ec);
  563. }
  564. /** Establishes a connection by trying each endpoint in a sequence.
  565. This function attempts to connect the stream to one of a sequence of
  566. endpoints by trying each endpoint until a connection is successfully
  567. established.
  568. The underlying socket is automatically opened if needed.
  569. An automatically opened socket is not returned to the
  570. closed state upon failure.
  571. The algorithm, known as a <em>composed operation</em>, is implemented
  572. in terms of calls to the underlying socket's `connect` function.
  573. @param endpoints A sequence of endpoints.
  574. @param connect_condition A function object that is called prior to each
  575. connection attempt. The signature of the function object must be:
  576. @code
  577. bool connect_condition(
  578. error_code const& ec,
  579. typename Protocol::endpoint const& next);
  580. @endcode
  581. The @c ec parameter contains the result from the most recent connect
  582. operation. Before the first connection attempt, @c ec is always set to
  583. indicate success. The @c next parameter is the next endpoint to be tried.
  584. The function object should return true if the next endpoint should be tried,
  585. and false if it should be skipped.
  586. @returns The successfully connected endpoint.
  587. @throws boost::system::system_error Thrown on failure. If the sequence is
  588. empty, the associated error code is `net::error::not_found`.
  589. Otherwise, contains the error from the last connection attempt.
  590. */
  591. template<
  592. class EndpointSequence, class ConnectCondition
  593. #if ! BOOST_BEAST_DOXYGEN
  594. ,class = typename std::enable_if<
  595. net::is_endpoint_sequence<
  596. EndpointSequence>::value>::type
  597. #endif
  598. >
  599. typename Protocol::endpoint
  600. connect(
  601. EndpointSequence const& endpoints,
  602. ConnectCondition connect_condition
  603. )
  604. {
  605. return net::connect(socket(), endpoints, connect_condition);
  606. }
  607. /** Establishes a connection by trying each endpoint in a sequence.
  608. This function attempts to connect the stream to one of a sequence of
  609. endpoints by trying each endpoint until a connection is successfully
  610. established.
  611. The underlying socket is automatically opened if needed.
  612. An automatically opened socket is not returned to the
  613. closed state upon failure.
  614. The algorithm, known as a <em>composed operation</em>, is implemented
  615. in terms of calls to the underlying socket's `connect` function.
  616. @param endpoints A sequence of endpoints.
  617. @param connect_condition A function object that is called prior to each
  618. connection attempt. The signature of the function object must be:
  619. @code
  620. bool connect_condition(
  621. error_code const& ec,
  622. typename Protocol::endpoint const& next);
  623. @endcode
  624. The @c ec parameter contains the result from the most recent connect
  625. operation. Before the first connection attempt, @c ec is always set to
  626. indicate success. The @c next parameter is the next endpoint to be tried.
  627. The function object should return true if the next endpoint should be tried,
  628. and false if it should be skipped.
  629. @param ec Set to indicate what error occurred, if any. If the sequence is
  630. empty, set to `net::error::not_found`. Otherwise, contains the error
  631. from the last connection attempt.
  632. @returns On success, the successfully connected endpoint. Otherwise, a
  633. default-constructed endpoint.
  634. */
  635. template<
  636. class EndpointSequence, class ConnectCondition
  637. #if ! BOOST_BEAST_DOXYGEN
  638. ,class = typename std::enable_if<
  639. net::is_endpoint_sequence<
  640. EndpointSequence>::value>::type
  641. #endif
  642. >
  643. typename Protocol::endpoint
  644. connect(
  645. EndpointSequence const& endpoints,
  646. ConnectCondition connect_condition,
  647. error_code& ec)
  648. {
  649. return net::connect(socket(), endpoints, connect_condition, ec);
  650. }
  651. /** Establishes a connection by trying each endpoint in a sequence.
  652. This function attempts to connect the stream to one of a sequence of
  653. endpoints by trying each endpoint until a connection is successfully
  654. established.
  655. The underlying socket is automatically opened if needed.
  656. An automatically opened socket is not returned to the
  657. closed state upon failure.
  658. The algorithm, known as a <em>composed operation</em>, is implemented
  659. in terms of calls to the underlying socket's `connect` function.
  660. @param begin An iterator pointing to the start of a sequence of endpoints.
  661. @param end An iterator pointing to the end of a sequence of endpoints.
  662. @param connect_condition A function object that is called prior to each
  663. connection attempt. The signature of the function object must be:
  664. @code
  665. bool connect_condition(
  666. error_code const& ec,
  667. typename Protocol::endpoint const& next);
  668. @endcode
  669. The @c ec parameter contains the result from the most recent connect
  670. operation. Before the first connection attempt, @c ec is always set to
  671. indicate success. The @c next parameter is the next endpoint to be tried.
  672. The function object should return true if the next endpoint should be tried,
  673. and false if it should be skipped.
  674. @returns An iterator denoting the successfully connected endpoint.
  675. @throws boost::system::system_error Thrown on failure. If the sequence is
  676. empty, the associated @c error_code is `net::error::not_found`.
  677. Otherwise, contains the error from the last connection attempt.
  678. */
  679. template<
  680. class Iterator, class ConnectCondition>
  681. Iterator
  682. connect(
  683. Iterator begin, Iterator end,
  684. ConnectCondition connect_condition)
  685. {
  686. return net::connect(socket(), begin, end, connect_condition);
  687. }
  688. /** Establishes a connection by trying each endpoint in a sequence.
  689. This function attempts to connect the stream to one of a sequence of
  690. endpoints by trying each endpoint until a connection is successfully
  691. established.
  692. The underlying socket is automatically opened if needed.
  693. An automatically opened socket is not returned to the
  694. closed state upon failure.
  695. The algorithm, known as a <em>composed operation</em>, is implemented
  696. in terms of calls to the underlying socket's `connect` function.
  697. @param begin An iterator pointing to the start of a sequence of endpoints.
  698. @param end An iterator pointing to the end of a sequence of endpoints.
  699. @param connect_condition A function object that is called prior to each
  700. connection attempt. The signature of the function object must be:
  701. @code
  702. bool connect_condition(
  703. error_code const& ec,
  704. typename Protocol::endpoint const& next);
  705. @endcode
  706. The @c ec parameter contains the result from the most recent connect
  707. operation. Before the first connection attempt, @c ec is always set to
  708. indicate success. The @c next parameter is the next endpoint to be tried.
  709. The function object should return true if the next endpoint should be tried,
  710. and false if it should be skipped.
  711. @param ec Set to indicate what error occurred, if any. If the sequence is
  712. empty, set to `net::error::not_found`. Otherwise, contains the error
  713. from the last connection attempt.
  714. @returns On success, an iterator denoting the successfully connected
  715. endpoint. Otherwise, the end iterator.
  716. */
  717. template<
  718. class Iterator, class ConnectCondition>
  719. Iterator
  720. connect(
  721. Iterator begin, Iterator end,
  722. ConnectCondition connect_condition,
  723. error_code& ec)
  724. {
  725. return net::connect(socket(), begin, end, connect_condition, ec);
  726. }
  727. /** Connect the stream to the specified endpoint asynchronously.
  728. This function is used to asynchronously connect the underlying
  729. socket to the specified remote endpoint. The function call always
  730. returns immediately.
  731. The underlying socket is automatically opened if needed.
  732. An automatically opened socket is not returned to the
  733. closed state upon failure.
  734. If the timeout timer expires while the operation is outstanding,
  735. the operation will be canceled and the completion handler will be
  736. invoked with the error @ref error::timeout.
  737. @param ep The remote endpoint to which the underlying socket will be
  738. connected. Copies will be made of the endpoint object as required.
  739. @param handler The completion handler to invoke when the operation
  740. completes. The implementation takes ownership of the handler by
  741. performing a decay-copy. The equivalent function signature of
  742. the handler must be:
  743. @code
  744. void handler(
  745. error_code ec // Result of operation
  746. );
  747. @endcode
  748. Regardless of whether the asynchronous operation completes
  749. immediately or not, the handler will not be invoked from within
  750. this function. Invocation of the handler will be performed in a
  751. manner equivalent to using `net::post`.
  752. @see async_connect
  753. */
  754. template<
  755. BOOST_BEAST_ASYNC_TPARAM1 ConnectHandler =
  756. net::default_completion_token_t<executor_type>
  757. >
  758. BOOST_BEAST_ASYNC_RESULT1(ConnectHandler)
  759. async_connect(
  760. endpoint_type const& ep,
  761. ConnectHandler&& handler =
  762. net::default_completion_token_t<
  763. executor_type>{});
  764. /** Establishes a connection by trying each endpoint in a sequence asynchronously.
  765. This function attempts to connect the stream to one of a sequence of
  766. endpoints by trying each endpoint until a connection is successfully
  767. established.
  768. The underlying socket is automatically opened if needed.
  769. An automatically opened socket is not returned to the
  770. closed state upon failure.
  771. The algorithm, known as a <em>composed asynchronous operation</em>, is
  772. implemented in terms of calls to the underlying socket's `async_connect`
  773. function.
  774. If the timeout timer expires while the operation is outstanding,
  775. the current connection attempt will be canceled and the completion
  776. handler will be invoked with the error @ref error::timeout.
  777. @param endpoints A sequence of endpoints. This this object must meet
  778. the requirements of <em>EndpointSequence</em>.
  779. @param handler The completion handler to invoke when the operation
  780. completes. The implementation takes ownership of the handler by
  781. performing a decay-copy. The equivalent function signature of
  782. the handler must be:
  783. @code
  784. void handler(
  785. // Result of operation. if the sequence is empty, set to
  786. // net::error::not_found. Otherwise, contains the
  787. // error from the last connection attempt.
  788. error_code const& error,
  789. // On success, the successfully connected endpoint.
  790. // Otherwise, a default-constructed endpoint.
  791. typename Protocol::endpoint const& endpoint
  792. );
  793. @endcode
  794. Regardless of whether the asynchronous operation completes
  795. immediately or not, the handler will not be invoked from within
  796. this function. Invocation of the handler will be performed in a
  797. manner equivalent to using `net::post`.
  798. */
  799. template<
  800. class EndpointSequence,
  801. BOOST_ASIO_COMPLETION_TOKEN_FOR(
  802. void(error_code, typename Protocol::endpoint))
  803. RangeConnectHandler =
  804. net::default_completion_token_t<executor_type>
  805. #if ! BOOST_BEAST_DOXYGEN
  806. ,class = typename std::enable_if<
  807. net::is_endpoint_sequence<
  808. EndpointSequence>::value>::type
  809. #endif
  810. >
  811. BOOST_ASIO_INITFN_RESULT_TYPE(
  812. RangeConnectHandler,
  813. void(error_code, typename Protocol::endpoint))
  814. async_connect(
  815. EndpointSequence const& endpoints,
  816. RangeConnectHandler&& handler =
  817. net::default_completion_token_t<executor_type>{});
  818. /** Establishes a connection by trying each endpoint in a sequence asynchronously.
  819. This function attempts to connect the stream to one of a sequence of
  820. endpoints by trying each endpoint until a connection is successfully
  821. established.
  822. The underlying socket is automatically opened if needed.
  823. An automatically opened socket is not returned to the
  824. closed state upon failure.
  825. The algorithm, known as a <em>composed asynchronous operation</em>, is
  826. implemented in terms of calls to the underlying socket's `async_connect`
  827. function.
  828. If the timeout timer expires while the operation is outstanding,
  829. the current connection attempt will be canceled and the completion
  830. handler will be invoked with the error @ref error::timeout.
  831. @param endpoints A sequence of endpoints. This this object must meet
  832. the requirements of <em>EndpointSequence</em>.
  833. @param connect_condition A function object that is called prior to each
  834. connection attempt. The signature of the function object must be:
  835. @code
  836. bool connect_condition(
  837. error_code const& ec,
  838. typename Protocol::endpoint const& next);
  839. @endcode
  840. The @c ec parameter contains the result from the most recent connect
  841. operation. Before the first connection attempt, @c ec is always set to
  842. indicate success. The @c next parameter is the next endpoint to be tried.
  843. The function object should return true if the next endpoint should be tried,
  844. and false if it should be skipped.
  845. @param handler The completion handler to invoke when the operation
  846. completes. The implementation takes ownership of the handler by
  847. performing a decay-copy. The equivalent function signature of
  848. the handler must be:
  849. @code
  850. void handler(
  851. // Result of operation. if the sequence is empty, set to
  852. // net::error::not_found. Otherwise, contains the
  853. // error from the last connection attempt.
  854. error_code const& error,
  855. // On success, the successfully connected endpoint.
  856. // Otherwise, a default-constructed endpoint.
  857. typename Protocol::endpoint const& endpoint
  858. );
  859. @endcode
  860. Regardless of whether the asynchronous operation completes
  861. immediately or not, the handler will not be invoked from within
  862. this function. Invocation of the handler will be performed in a
  863. manner equivalent to using `net::post`.
  864. @par Example
  865. The following connect condition function object can be used to output
  866. information about the individual connection attempts:
  867. @code
  868. struct my_connect_condition
  869. {
  870. bool operator()(
  871. error_code const& ec,
  872. net::ip::tcp::endpoint const& next)
  873. {
  874. if (ec)
  875. std::cout << "Error: " << ec.message() << std::endl;
  876. std::cout << "Trying: " << next << std::endl;
  877. return true;
  878. }
  879. };
  880. @endcode
  881. */
  882. template<
  883. class EndpointSequence,
  884. class ConnectCondition,
  885. BOOST_ASIO_COMPLETION_TOKEN_FOR(
  886. void(error_code, typename Protocol::endpoint))
  887. RangeConnectHandler =
  888. net::default_completion_token_t<executor_type>
  889. #if ! BOOST_BEAST_DOXYGEN
  890. ,class = typename std::enable_if<
  891. net::is_endpoint_sequence<
  892. EndpointSequence>::value>::type
  893. #endif
  894. >
  895. BOOST_ASIO_INITFN_RESULT_TYPE(
  896. RangeConnectHandler,
  897. void(error_code, typename Protocol::endpoint))
  898. async_connect(
  899. EndpointSequence const& endpoints,
  900. ConnectCondition connect_condition,
  901. RangeConnectHandler&& handler =
  902. net::default_completion_token_t<
  903. executor_type>{});
  904. /** Establishes a connection by trying each endpoint in a sequence asynchronously.
  905. This function attempts to connect the stream to one of a sequence of
  906. endpoints by trying each endpoint until a connection is successfully
  907. established.
  908. The underlying socket is automatically opened if needed.
  909. An automatically opened socket is not returned to the
  910. closed state upon failure.
  911. The algorithm, known as a <em>composed asynchronous operation</em>, is
  912. implemented in terms of calls to the underlying socket's `async_connect`
  913. function.
  914. If the timeout timer expires while the operation is outstanding,
  915. the current connection attempt will be canceled and the completion
  916. handler will be invoked with the error @ref error::timeout.
  917. @param begin An iterator pointing to the start of a sequence of endpoints.
  918. @param end An iterator pointing to the end of a sequence of endpoints.
  919. @param handler The completion handler to invoke when the operation
  920. completes. The implementation takes ownership of the handler by
  921. performing a decay-copy. The equivalent function signature of
  922. the handler must be:
  923. @code
  924. void handler(
  925. // Result of operation. if the sequence is empty, set to
  926. // net::error::not_found. Otherwise, contains the
  927. // error from the last connection attempt.
  928. error_code const& error,
  929. // On success, an iterator denoting the successfully
  930. // connected endpoint. Otherwise, the end iterator.
  931. Iterator iterator
  932. );
  933. @endcode
  934. Regardless of whether the asynchronous operation completes
  935. immediately or not, the handler will not be invoked from within
  936. this function. Invocation of the handler will be performed in a
  937. manner equivalent to using `net::post`.
  938. */
  939. template<
  940. class Iterator,
  941. BOOST_ASIO_COMPLETION_TOKEN_FOR(
  942. void(error_code, Iterator))
  943. IteratorConnectHandler =
  944. net::default_completion_token_t<executor_type>>
  945. BOOST_ASIO_INITFN_RESULT_TYPE(
  946. IteratorConnectHandler,
  947. void(error_code, Iterator))
  948. async_connect(
  949. Iterator begin, Iterator end,
  950. IteratorConnectHandler&& handler =
  951. net::default_completion_token_t<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 algorithm, known as a <em>composed asynchronous operation</em>, is
  957. implemented in terms of calls to the underlying socket's `async_connect`
  958. function.
  959. If the timeout timer expires while the operation is outstanding,
  960. the current connection attempt will be canceled and the completion
  961. handler will be invoked with the error @ref error::timeout.
  962. @param begin An iterator pointing to the start of a sequence of endpoints.
  963. @param end An iterator pointing to the end of a sequence of endpoints.
  964. @param connect_condition A function object that is called prior to each
  965. connection attempt. The signature of the function object must be:
  966. @code
  967. bool connect_condition(
  968. error_code const& ec,
  969. Iterator next);
  970. @endcode
  971. @param handler The completion handler to invoke when the operation
  972. completes. The implementation takes ownership of the handler by
  973. performing a decay-copy. The equivalent function signature of
  974. the handler must be:
  975. @code
  976. void handler(
  977. // Result of operation. if the sequence is empty, set to
  978. // net::error::not_found. Otherwise, contains the
  979. // error from the last connection attempt.
  980. error_code const& error,
  981. // On success, an iterator denoting the successfully
  982. // connected endpoint. Otherwise, the end iterator.
  983. Iterator iterator
  984. );
  985. @endcode
  986. Regardless of whether the asynchronous operation completes
  987. immediately or not, the handler will not be invoked from within
  988. this function. Invocation of the handler will be performed in a
  989. manner equivalent to using `net::post`.
  990. */
  991. template<
  992. class Iterator,
  993. class ConnectCondition,
  994. BOOST_ASIO_COMPLETION_TOKEN_FOR(
  995. void(error_code, Iterator))
  996. IteratorConnectHandler =
  997. net::default_completion_token_t<executor_type>>
  998. BOOST_ASIO_INITFN_RESULT_TYPE(
  999. IteratorConnectHandler,
  1000. void(error_code, Iterator))
  1001. async_connect(
  1002. Iterator begin, Iterator end,
  1003. ConnectCondition connect_condition,
  1004. IteratorConnectHandler&& handler =
  1005. net::default_completion_token_t<executor_type>{});
  1006. //--------------------------------------------------------------------------
  1007. /** Read some data.
  1008. This function is used to read some data from the stream.
  1009. The call blocks until one of the following is true:
  1010. @li One or more bytes are read from the stream.
  1011. @li An error occurs.
  1012. @param buffers The buffers into which the data will be read. If the
  1013. size of the buffers is zero bytes, the call always returns
  1014. immediately with no error.
  1015. @returns The number of bytes read.
  1016. @throws system_error Thrown on failure.
  1017. @note The `read_some` operation may not receive all of the requested
  1018. number of bytes. Consider using the function `net::read` if you need
  1019. to ensure that the requested amount of data is read before the
  1020. blocking operation completes.
  1021. */
  1022. template<class MutableBufferSequence>
  1023. std::size_t
  1024. read_some(MutableBufferSequence const& buffers)
  1025. {
  1026. return impl_->socket.read_some(buffers);
  1027. }
  1028. /** Read some data.
  1029. This function is used to read some data from the underlying socket.
  1030. The call blocks until one of the following is true:
  1031. @li One or more bytes are read from the stream.
  1032. @li An error occurs.
  1033. @param buffers The buffers into which the data will be read. If the
  1034. size of the buffers is zero bytes, the call always returns
  1035. immediately with no error.
  1036. @param ec Set to indicate what error occurred, if any.
  1037. @returns The number of bytes read.
  1038. @note The `read_some` operation may not receive all of the requested
  1039. number of bytes. Consider using the function `net::read` if you need
  1040. to ensure that the requested amount of data is read before the
  1041. blocking operation completes.
  1042. */
  1043. template<class MutableBufferSequence>
  1044. std::size_t
  1045. read_some(
  1046. MutableBufferSequence const& buffers,
  1047. error_code& ec)
  1048. {
  1049. return impl_->socket.read_some(buffers, ec);
  1050. }
  1051. /** Read some data asynchronously.
  1052. This function is used to asynchronously read data from the stream.
  1053. This call always returns immediately. The asynchronous operation
  1054. will continue until one of the following conditions is true:
  1055. @li One or more bytes are read from the stream.
  1056. @li An error occurs.
  1057. The algorithm, known as a <em>composed asynchronous operation</em>,
  1058. is implemented in terms of calls to the next layer's `async_read_some`
  1059. function. The program must ensure that no other calls to @ref read_some
  1060. or @ref async_read_some are performed until this operation completes.
  1061. If the timeout timer expires while the operation is outstanding,
  1062. the operation will be canceled and the completion handler will be
  1063. invoked with the error @ref error::timeout.
  1064. @param buffers The buffers into which the data will be read. If the size
  1065. of the buffers is zero bytes, the operation always completes immediately
  1066. with no error.
  1067. Although the buffers object may be copied as necessary, ownership of the
  1068. underlying memory blocks is retained by the caller, which must guarantee
  1069. that they remain valid until the handler is called.
  1070. @param handler The completion handler to invoke when the operation
  1071. completes. The implementation takes ownership of the handler by
  1072. performing a decay-copy. The equivalent function signature of
  1073. the handler must be:
  1074. @code
  1075. void handler(
  1076. error_code error, // Result of operation.
  1077. std::size_t bytes_transferred // Number of bytes read.
  1078. );
  1079. @endcode
  1080. Regardless of whether the asynchronous operation completes
  1081. immediately or not, the handler will not be invoked from within
  1082. this function. Invocation of the handler will be performed in a
  1083. manner equivalent to using `net::post`.
  1084. @note The `async_read_some` operation may not receive all of the requested
  1085. number of bytes. Consider using the function `net::async_read` if you need
  1086. to ensure that the requested amount of data is read before the asynchronous
  1087. operation completes.
  1088. */
  1089. template<
  1090. class MutableBufferSequence,
  1091. BOOST_BEAST_ASYNC_TPARAM2 ReadHandler =
  1092. net::default_completion_token_t<executor_type>
  1093. >
  1094. BOOST_BEAST_ASYNC_RESULT2(ReadHandler)
  1095. async_read_some(
  1096. MutableBufferSequence const& buffers,
  1097. ReadHandler&& handler =
  1098. net::default_completion_token_t<executor_type>{}
  1099. );
  1100. /** Write some data.
  1101. This function is used to write some data to the stream.
  1102. The call blocks until one of the following is true:
  1103. @li One or more bytes are written to the stream.
  1104. @li An error occurs.
  1105. @param buffers The buffers from which the data will be written. If the
  1106. size of the buffers is zero bytes, the call always returns immediately
  1107. with no error.
  1108. @returns The number of bytes written.
  1109. @throws system_error Thrown on failure.
  1110. @note The `write_some` operation may not transmit all of the requested
  1111. number of bytes. Consider using the function `net::write` if you need
  1112. to ensure that the requested amount of data is written before the
  1113. blocking operation completes.
  1114. */
  1115. template<class ConstBufferSequence>
  1116. std::size_t
  1117. write_some(ConstBufferSequence const& buffers)
  1118. {
  1119. return impl_->socket.write_some(buffers);
  1120. }
  1121. /** Write some data.
  1122. This function is used to write some data to the stream.
  1123. The call blocks until one of the following is true:
  1124. @li One or more bytes are written to the stream.
  1125. @li An error occurs.
  1126. @param buffers The buffers from which the data will be written. If the
  1127. size of the buffers is zero bytes, the call always returns immediately
  1128. with no error.
  1129. @param ec Set to indicate what error occurred, if any.
  1130. @returns The number of bytes written.
  1131. @throws system_error Thrown on failure.
  1132. @note The `write_some` operation may not transmit all of the requested
  1133. number of bytes. Consider using the function `net::write` if you need
  1134. to ensure that the requested amount of data is written before the
  1135. blocking operation completes.
  1136. */
  1137. template<class ConstBufferSequence>
  1138. std::size_t
  1139. write_some(
  1140. ConstBufferSequence const& buffers,
  1141. error_code& ec)
  1142. {
  1143. return impl_->socket.write_some(buffers, ec);
  1144. }
  1145. /** Write some data asynchronously.
  1146. This function is used to asynchronously write data to the underlying socket.
  1147. This call always returns immediately. The asynchronous operation
  1148. will continue until one of the following conditions is true:
  1149. @li One or more bytes are written to the stream.
  1150. @li An error occurs.
  1151. The algorithm, known as a <em>composed asynchronous operation</em>,
  1152. is implemented in terms of calls to the next layer's `async_write_some`
  1153. function. The program must ensure that no other calls to @ref async_write_some
  1154. are performed until this operation completes.
  1155. If the timeout timer expires while the operation is outstanding,
  1156. the operation will be canceled and the completion handler will be
  1157. invoked with the error @ref error::timeout.
  1158. @param buffers The buffers from which the data will be written. If the
  1159. size of the buffers is zero bytes, the operation always completes
  1160. immediately with no error.
  1161. Although the buffers object may be copied as necessary, ownership of the
  1162. underlying memory blocks is retained by the caller, which must guarantee
  1163. that they remain valid until the handler is called.
  1164. @param handler The completion handler to invoke when the operation
  1165. completes. The implementation takes ownership of the handler by
  1166. performing a decay-copy. The equivalent function signature of
  1167. the handler must be:
  1168. @code
  1169. void handler(
  1170. error_code error, // Result of operation.
  1171. std::size_t bytes_transferred // Number of bytes written.
  1172. );
  1173. @endcode
  1174. Regardless of whether the asynchronous operation completes
  1175. immediately or not, the handler will not be invoked from within
  1176. this function. Invocation of the handler will be performed in a
  1177. manner equivalent to using `net::post`.
  1178. @note The `async_write_some` operation may not transmit all of the requested
  1179. number of bytes. Consider using the function `net::async_write` if you need
  1180. to ensure that the requested amount of data is sent before the asynchronous
  1181. operation completes.
  1182. */
  1183. template<
  1184. class ConstBufferSequence,
  1185. BOOST_BEAST_ASYNC_TPARAM2 WriteHandler =
  1186. net::default_completion_token_t<Executor>
  1187. >
  1188. BOOST_BEAST_ASYNC_RESULT2(WriteHandler)
  1189. async_write_some(
  1190. ConstBufferSequence const& buffers,
  1191. WriteHandler&& handler =
  1192. net::default_completion_token_t<Executor>{});
  1193. };
  1194. } // beast
  1195. } // boost
  1196. #include <boost/beast/core/impl/basic_stream.hpp>
  1197. #endif