basic_stream.hpp 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568
  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 BHO_BEAST_CORE_BASIC_STREAM_HPP
  10. #define BHO_BEAST_CORE_BASIC_STREAM_HPP
  11. #include <asio2/bho/beast/core/detail/config.hpp>
  12. #include <asio2/bho/beast/core/detail/stream_base.hpp>
  13. #include <asio2/bho/beast/core/error.hpp>
  14. #include <asio2/bho/beast/core/rate_policy.hpp>
  15. #include <asio2/bho/beast/core/role.hpp>
  16. #include <asio2/bho/beast/core/stream_traits.hpp>
  17. #include <asio/async_result.hpp>
  18. #include <asio/basic_stream_socket.hpp>
  19. #include <asio/connect.hpp>
  20. #include <asio/executor.hpp>
  21. #include <asio/is_executor.hpp>
  22. #include <asio2/bho/core/empty_value.hpp>
  23. #include <asio2/bho/config/workaround.hpp>
  24. #include <chrono>
  25. #include <limits>
  26. #include <memory>
  27. #if ! BHO_BEAST_DOXYGEN
  28. #ifdef ASIO_STANDALONE
  29. namespace asio {
  30. #else
  31. namespace boost::asio {
  32. #endif
  33. namespace ssl {
  34. template<typename> class stream;
  35. } // ssl
  36. } // asio
  37. #endif
  38. namespace bho {
  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 ! BHO_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. : std::enable_shared_from_this<impl_type>
  197. , bho::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->bho::empty_value<RatePolicy>::get();
  228. }
  229. RatePolicy const&
  230. policy() const noexcept
  231. {
  232. return this->bho::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. std::shared_ptr<impl_type> impl_;
  244. template<class Executor2>
  245. struct timeout_handler;
  246. struct ops;
  247. #if ! BHO_BEAST_DOXYGEN
  248. // asio::ssl::stream needs these
  249. // DEPRECATED
  250. template<class>
  251. friend class 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 BHO_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 BHO_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 the next logical operation.
  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 the next logical operation.
  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 the next logical operation.
  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 ! BHO_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 ! BHO_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 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 asio::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 ! BHO_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 ! BHO_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 asio::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. BHO_BEAST_ASYNC_TPARAM1 ConnectHandler =
  776. net::default_completion_token_t<executor_type>
  777. >
  778. BHO_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. ASIO_COMPLETION_TOKEN_FOR(
  832. void(error_code, typename Protocol::endpoint))
  833. RangeConnectHandler =
  834. net::default_completion_token_t<executor_type>
  835. #if ! BHO_BEAST_DOXYGEN
  836. ,class = typename std::enable_if<
  837. net::is_endpoint_sequence<
  838. EndpointSequence>::value>::type
  839. #endif
  840. >
  841. ASIO_INITFN_AUTO_RESULT_TYPE(
  842. RangeConnectHandler,
  843. void(error_code, typename Protocol::endpoint))
  844. async_connect(
  845. EndpointSequence const& endpoints,
  846. RangeConnectHandler&& handler =
  847. net::default_completion_token_t<executor_type>{});
  848. /** Establishes a connection by trying each endpoint in a sequence asynchronously.
  849. This function attempts to connect the stream to one of a sequence of
  850. endpoints by trying each endpoint until a connection is successfully
  851. established.
  852. The underlying socket is automatically opened if needed.
  853. An automatically opened socket is not returned to the
  854. closed state upon failure.
  855. The algorithm, known as a <em>composed asynchronous operation</em>, is
  856. implemented in terms of calls to the underlying socket's `async_connect`
  857. function.
  858. If the timeout timer expires while the operation is outstanding,
  859. the current connection attempt will be canceled and the completion
  860. handler will be invoked with the error @ref error::timeout.
  861. @param endpoints A sequence of endpoints. This this object must meet
  862. the requirements of <em>EndpointSequence</em>.
  863. @param connect_condition A function object that is called prior to each
  864. connection attempt. The signature of the function object must be:
  865. @code
  866. bool connect_condition(
  867. error_code const& ec,
  868. typename Protocol::endpoint const& next);
  869. @endcode
  870. The @c ec parameter contains the result from the most recent connect
  871. operation. Before the first connection attempt, @c ec is always set to
  872. indicate success. The @c next parameter is the next endpoint to be tried.
  873. The function object should return true if the next endpoint should be tried,
  874. and false if it should be skipped.
  875. @param handler The completion handler to invoke when the operation
  876. completes. The implementation takes ownership of the handler by
  877. performing a decay-copy. The equivalent function signature of
  878. the handler must be:
  879. @code
  880. void handler(
  881. // Result of operation. if the sequence is empty, set to
  882. // net::error::not_found. Otherwise, contains the
  883. // error from the last connection attempt.
  884. error_code const& error,
  885. // On success, the successfully connected endpoint.
  886. // Otherwise, a default-constructed endpoint.
  887. typename Protocol::endpoint const& endpoint
  888. );
  889. @endcode
  890. If the handler has an associated immediate executor,
  891. an immediate completion will be dispatched to it.
  892. Otherwise, the handler will not be invoked from within
  893. this function. Invocation of the handler will be performed
  894. by dispatching to the immediate executor. If no
  895. immediate executor is specified, this is equivalent
  896. to using `net::post`.
  897. @par Example
  898. The following connect condition function object can be used to output
  899. information about the individual connection attempts:
  900. @code
  901. struct my_connect_condition
  902. {
  903. bool operator()(
  904. error_code const& ec,
  905. net::ip::tcp::endpoint const& next)
  906. {
  907. if (ec)
  908. std::cout << "Error: " << ec.message() << std::endl;
  909. std::cout << "Trying: " << next << std::endl;
  910. return true;
  911. }
  912. };
  913. @endcode
  914. @par Per-Operation Cancellation
  915. This asynchronous operation supports cancellation for the following
  916. net::cancellation_type values:
  917. @li @c net::cancellation_type::terminal
  918. @li @c net::cancellation_type::partial
  919. @li @c net::cancellation_type::total
  920. if they are also supported by the socket's @c async_connect operation.
  921. */
  922. template<
  923. class EndpointSequence,
  924. class ConnectCondition,
  925. ASIO_COMPLETION_TOKEN_FOR(
  926. void(error_code, typename Protocol::endpoint))
  927. RangeConnectHandler =
  928. net::default_completion_token_t<executor_type>
  929. #if ! BHO_BEAST_DOXYGEN
  930. ,class = typename std::enable_if<
  931. net::is_endpoint_sequence<
  932. EndpointSequence>::value>::type
  933. #endif
  934. >
  935. ASIO_INITFN_AUTO_RESULT_TYPE(
  936. RangeConnectHandler,
  937. void(error_code, typename Protocol::endpoint))
  938. async_connect(
  939. EndpointSequence const& endpoints,
  940. ConnectCondition connect_condition,
  941. RangeConnectHandler&& handler =
  942. net::default_completion_token_t<
  943. executor_type>{});
  944. /** Establishes a connection by trying each endpoint in a sequence asynchronously.
  945. This function attempts to connect the stream to one of a sequence of
  946. endpoints by trying each endpoint until a connection is successfully
  947. established.
  948. The underlying socket is automatically opened if needed.
  949. An automatically opened socket is not returned to the
  950. closed state upon failure.
  951. The algorithm, known as a <em>composed asynchronous operation</em>, is
  952. implemented in terms of calls to the underlying socket's `async_connect`
  953. function.
  954. If the timeout timer expires while the operation is outstanding,
  955. the current connection attempt will be canceled and the completion
  956. handler will be invoked with the error @ref error::timeout.
  957. @param begin An iterator pointing to the start of a sequence of endpoints.
  958. @param end An iterator pointing to the end of a sequence of endpoints.
  959. @param handler The completion handler to invoke when the operation
  960. completes. The implementation takes ownership of the handler by
  961. performing a decay-copy. The equivalent function signature of
  962. the handler must be:
  963. @code
  964. void handler(
  965. // Result of operation. if the sequence is empty, set to
  966. // net::error::not_found. Otherwise, contains the
  967. // error from the last connection attempt.
  968. error_code const& error,
  969. // On success, an iterator denoting the successfully
  970. // connected endpoint. Otherwise, the end iterator.
  971. Iterator iterator
  972. );
  973. @endcode
  974. If the handler has an associated immediate executor,
  975. an immediate completion will be dispatched to it.
  976. Otherwise, the handler will not be invoked from within
  977. this function. Invocation of the handler will be performed
  978. by dispatching to the immediate executor. If no
  979. immediate executor is specified, this is equivalent
  980. to using `net::post`.
  981. @par Per-Operation Cancellation
  982. This asynchronous operation supports cancellation for the following
  983. net::cancellation_type values:
  984. @li @c net::cancellation_type::terminal
  985. @li @c net::cancellation_type::partial
  986. @li @c net::cancellation_type::total
  987. if they are also supported by the socket's @c async_connect operation.
  988. */
  989. template<
  990. class Iterator,
  991. ASIO_COMPLETION_TOKEN_FOR(
  992. void(error_code, Iterator))
  993. IteratorConnectHandler =
  994. net::default_completion_token_t<executor_type>>
  995. ASIO_INITFN_AUTO_RESULT_TYPE(
  996. IteratorConnectHandler,
  997. void(error_code, Iterator))
  998. async_connect(
  999. Iterator begin, Iterator end,
  1000. IteratorConnectHandler&& handler =
  1001. net::default_completion_token_t<executor_type>{});
  1002. /** Establishes a connection by trying each endpoint in a sequence asynchronously.
  1003. This function attempts to connect the stream to one of a sequence of
  1004. endpoints by trying each endpoint until a connection is successfully
  1005. established.
  1006. The algorithm, known as a <em>composed asynchronous operation</em>, is
  1007. implemented in terms of calls to the underlying socket's `async_connect`
  1008. function.
  1009. If the timeout timer expires while the operation is outstanding,
  1010. the current connection attempt will be canceled and the completion
  1011. handler will be invoked with the error @ref error::timeout.
  1012. @param begin An iterator pointing to the start of a sequence of endpoints.
  1013. @param end An iterator pointing to the end of a sequence of endpoints.
  1014. @param connect_condition A function object that is called prior to each
  1015. connection attempt. The signature of the function object must be:
  1016. @code
  1017. bool connect_condition(
  1018. error_code const& ec,
  1019. typename Protocol::endpoint const& next);
  1020. @endcode
  1021. @param handler The completion handler to invoke when the operation
  1022. completes. The implementation takes ownership of the handler by
  1023. performing a decay-copy. The equivalent function signature of
  1024. the handler must be:
  1025. @code
  1026. void handler(
  1027. // Result of operation. if the sequence is empty, set to
  1028. // net::error::not_found. Otherwise, contains the
  1029. // error from the last connection attempt.
  1030. error_code const& error,
  1031. // On success, an iterator denoting the successfully
  1032. // connected endpoint. Otherwise, the end iterator.
  1033. Iterator iterator
  1034. );
  1035. @endcode
  1036. If the handler has an associated immediate executor,
  1037. an immediate completion will be dispatched to it.
  1038. Otherwise, the handler will not be invoked from within
  1039. this function. Invocation of the handler will be performed
  1040. by dispatching to the immediate executor. If no
  1041. immediate executor is specified, this is equivalent
  1042. to using `net::post`.
  1043. @par Per-Operation Cancellation
  1044. This asynchronous operation supports cancellation for the following
  1045. net::cancellation_type values:
  1046. @li @c net::cancellation_type::terminal
  1047. @li @c net::cancellation_type::partial
  1048. @li @c net::cancellation_type::total
  1049. if they are also supported by the socket's @c async_connect operation.
  1050. */
  1051. template<
  1052. class Iterator,
  1053. class ConnectCondition,
  1054. ASIO_COMPLETION_TOKEN_FOR(
  1055. void(error_code, Iterator))
  1056. IteratorConnectHandler =
  1057. net::default_completion_token_t<executor_type>>
  1058. ASIO_INITFN_AUTO_RESULT_TYPE(
  1059. IteratorConnectHandler,
  1060. void(error_code, Iterator))
  1061. async_connect(
  1062. Iterator begin, Iterator end,
  1063. ConnectCondition connect_condition,
  1064. IteratorConnectHandler&& handler =
  1065. net::default_completion_token_t<executor_type>{});
  1066. //--------------------------------------------------------------------------
  1067. /** Read some data.
  1068. This function is used to read some data from the stream.
  1069. The call blocks until one of the following is true:
  1070. @li One or more bytes are read from the stream.
  1071. @li An error occurs.
  1072. @param buffers The buffers into which the data will be read. If the
  1073. size of the buffers is zero bytes, the call always returns
  1074. immediately with no error.
  1075. @returns The number of bytes read.
  1076. @throws system_error Thrown on failure.
  1077. @note The `read_some` operation may not receive all of the requested
  1078. number of bytes. Consider using the function `net::read` if you need
  1079. to ensure that the requested amount of data is read before the
  1080. blocking operation completes.
  1081. */
  1082. template<class MutableBufferSequence>
  1083. std::size_t
  1084. read_some(MutableBufferSequence const& buffers)
  1085. {
  1086. return impl_->socket.read_some(buffers);
  1087. }
  1088. /** Read some data.
  1089. This function is used to read some data from the underlying socket.
  1090. The call blocks until one of the following is true:
  1091. @li One or more bytes are read from the stream.
  1092. @li An error occurs.
  1093. @param buffers The buffers into which the data will be read. If the
  1094. size of the buffers is zero bytes, the call always returns
  1095. immediately with no error.
  1096. @param ec Set to indicate what error occurred, if any.
  1097. @returns The number of bytes read.
  1098. @note The `read_some` operation may not receive all of the requested
  1099. number of bytes. Consider using the function `net::read` if you need
  1100. to ensure that the requested amount of data is read before the
  1101. blocking operation completes.
  1102. */
  1103. template<class MutableBufferSequence>
  1104. std::size_t
  1105. read_some(
  1106. MutableBufferSequence const& buffers,
  1107. error_code& ec)
  1108. {
  1109. return impl_->socket.read_some(buffers, ec);
  1110. }
  1111. /** Read some data asynchronously.
  1112. This function is used to asynchronously read data from the stream.
  1113. This call always returns immediately. The asynchronous operation
  1114. will continue until one of the following conditions is true:
  1115. @li One or more bytes are read from the stream.
  1116. @li An error occurs.
  1117. The algorithm, known as a <em>composed asynchronous operation</em>,
  1118. is implemented in terms of calls to the next layer's `async_read_some`
  1119. function. The program must ensure that no other calls to @ref read_some
  1120. or @ref async_read_some are performed until this operation completes.
  1121. If the timeout timer expires while the operation is outstanding,
  1122. the operation will be canceled and the completion handler will be
  1123. invoked with the error @ref error::timeout.
  1124. @param buffers The buffers into which the data will be read. If the size
  1125. of the buffers is zero bytes, the operation always completes immediately
  1126. with no error.
  1127. Although the buffers object may be copied as necessary, ownership of the
  1128. underlying memory blocks is retained by the caller, which must guarantee
  1129. that they remain valid until the handler is called.
  1130. @param handler The completion handler to invoke when the operation
  1131. completes. The implementation takes ownership of the handler by
  1132. performing a decay-copy. The equivalent function signature of
  1133. the handler must be:
  1134. @code
  1135. void handler(
  1136. error_code error, // Result of operation.
  1137. std::size_t bytes_transferred // Number of bytes read.
  1138. );
  1139. @endcode
  1140. If the handler has an associated immediate executor,
  1141. an immediate completion will be dispatched to it.
  1142. Otherwise, the handler will not be invoked from within
  1143. this function. Invocation of the handler will be performed
  1144. by dispatching to the immediate executor. If no
  1145. immediate executor is specified, this is equivalent
  1146. to using `net::post`.
  1147. @note The `async_read_some` operation may not receive all of the requested
  1148. number of bytes. Consider using the function `net::async_read` if you need
  1149. to ensure that the requested amount of data is read before the asynchronous
  1150. operation completes.
  1151. @par Per-Operation Cancellation
  1152. This asynchronous operation supports cancellation for the following
  1153. net::cancellation_type values:
  1154. @li @c net::cancellation_type::terminal
  1155. @li @c net::cancellation_type::partial
  1156. @li @c net::cancellation_type::total
  1157. if they are also supported by the socket's @c async_read_some operation.
  1158. */
  1159. template<
  1160. class MutableBufferSequence,
  1161. BHO_BEAST_ASYNC_TPARAM2 ReadHandler =
  1162. net::default_completion_token_t<executor_type>
  1163. >
  1164. BHO_BEAST_ASYNC_RESULT2(ReadHandler)
  1165. async_read_some(
  1166. MutableBufferSequence const& buffers,
  1167. ReadHandler&& handler =
  1168. net::default_completion_token_t<executor_type>{}
  1169. );
  1170. /** Write some data.
  1171. This function is used to write some data to the stream.
  1172. The call blocks until one of the following is true:
  1173. @li One or more bytes are written to the stream.
  1174. @li An error occurs.
  1175. @param buffers The buffers from which the data will be written. If the
  1176. size of the buffers is zero bytes, the call always returns immediately
  1177. with no error.
  1178. @returns The number of bytes written.
  1179. @throws system_error Thrown on failure.
  1180. @note The `write_some` operation may not transmit all of the requested
  1181. number of bytes. Consider using the function `net::write` if you need
  1182. to ensure that the requested amount of data is written before the
  1183. blocking operation completes.
  1184. */
  1185. template<class ConstBufferSequence>
  1186. std::size_t
  1187. write_some(ConstBufferSequence const& buffers)
  1188. {
  1189. return impl_->socket.write_some(buffers);
  1190. }
  1191. /** Write some data.
  1192. This function is used to write some data to the stream.
  1193. The call blocks until one of the following is true:
  1194. @li One or more bytes are written to the stream.
  1195. @li An error occurs.
  1196. @param buffers The buffers from which the data will be written. If the
  1197. size of the buffers is zero bytes, the call always returns immediately
  1198. with no error.
  1199. @param ec Set to indicate what error occurred, if any.
  1200. @returns The number of bytes written.
  1201. @throws system_error Thrown on failure.
  1202. @note The `write_some` operation may not transmit all of the requested
  1203. number of bytes. Consider using the function `net::write` if you need
  1204. to ensure that the requested amount of data is written before the
  1205. blocking operation completes.
  1206. */
  1207. template<class ConstBufferSequence>
  1208. std::size_t
  1209. write_some(
  1210. ConstBufferSequence const& buffers,
  1211. error_code& ec)
  1212. {
  1213. return impl_->socket.write_some(buffers, ec);
  1214. }
  1215. /** Write some data asynchronously.
  1216. This function is used to asynchronously write data to the underlying socket.
  1217. This call always returns immediately. The asynchronous operation
  1218. will continue until one of the following conditions is true:
  1219. @li One or more bytes are written to the stream.
  1220. @li An error occurs.
  1221. The algorithm, known as a <em>composed asynchronous operation</em>,
  1222. is implemented in terms of calls to the next layer's `async_write_some`
  1223. function. The program must ensure that no other calls to @ref async_write_some
  1224. are performed until this operation completes.
  1225. If the timeout timer expires while the operation is outstanding,
  1226. the operation will be canceled and the completion handler will be
  1227. invoked with the error @ref error::timeout.
  1228. @param buffers The buffers from which the data will be written. If the
  1229. size of the buffers is zero bytes, the operation always completes
  1230. immediately with no error.
  1231. Although the buffers object may be copied as necessary, ownership of the
  1232. underlying memory blocks is retained by the caller, which must guarantee
  1233. that they remain valid until the handler is called.
  1234. @param handler The completion handler to invoke when the operation
  1235. completes. The implementation takes ownership of the handler by
  1236. performing a decay-copy. The equivalent function signature of
  1237. the handler must be:
  1238. @code
  1239. void handler(
  1240. error_code error, // Result of operation.
  1241. std::size_t bytes_transferred // Number of bytes written.
  1242. );
  1243. @endcode
  1244. If the handler has an associated immediate executor,
  1245. an immediate completion will be dispatched to it.
  1246. Otherwise, the handler will not be invoked from within
  1247. this function. Invocation of the handler will be performed
  1248. by dispatching to the immediate executor. If no
  1249. immediate executor is specified, this is equivalent
  1250. to using `net::post`.
  1251. @note The `async_write_some` operation may not transmit all of the requested
  1252. number of bytes. Consider using the function `net::async_write` if you need
  1253. to ensure that the requested amount of data is sent before the asynchronous
  1254. operation completes.
  1255. @par Per-Operation Cancellation
  1256. This asynchronous operation supports cancellation for the following
  1257. net::cancellation_type values:
  1258. @li @c net::cancellation_type::terminal
  1259. @li @c net::cancellation_type::partial
  1260. @li @c net::cancellation_type::total
  1261. if they are also supported by the socket's @c async_write_some operation.
  1262. */
  1263. template<
  1264. class ConstBufferSequence,
  1265. BHO_BEAST_ASYNC_TPARAM2 WriteHandler =
  1266. net::default_completion_token_t<Executor>
  1267. >
  1268. BHO_BEAST_ASYNC_RESULT2(WriteHandler)
  1269. async_write_some(
  1270. ConstBufferSequence const& buffers,
  1271. WriteHandler&& handler =
  1272. net::default_completion_token_t<Executor>{});
  1273. };
  1274. } // beast
  1275. } // bho
  1276. #include <asio2/bho/beast/core/impl/basic_stream.hpp>
  1277. #endif