![]() |
|
|||
File indexing completed on 2025-07-14 08:26:47
0001 // 0002 // Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com) 0003 // 0004 // Distributed under the Boost Software License, Version 1.0. (See accompanying 0005 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 0006 // 0007 // Official repository: https://github.com/boostorg/beast 0008 // 0009 0010 #ifndef BOOST_BEAST_CORE_BASIC_STREAM_HPP 0011 #define BOOST_BEAST_CORE_BASIC_STREAM_HPP 0012 0013 #include <boost/beast/core/detail/config.hpp> 0014 #include <boost/beast/core/detail/stream_base.hpp> 0015 #include <boost/beast/core/error.hpp> 0016 #include <boost/beast/core/rate_policy.hpp> 0017 #include <boost/beast/core/role.hpp> 0018 #include <boost/beast/core/stream_traits.hpp> 0019 #include <boost/asio/async_result.hpp> 0020 #include <boost/asio/basic_stream_socket.hpp> 0021 #include <boost/asio/connect.hpp> 0022 #include <boost/asio/executor.hpp> 0023 #include <boost/asio/is_executor.hpp> 0024 #include <boost/core/empty_value.hpp> 0025 #include <boost/config/workaround.hpp> 0026 #include <boost/enable_shared_from_this.hpp> 0027 #include <boost/shared_ptr.hpp> 0028 #include <chrono> 0029 #include <limits> 0030 #include <memory> 0031 0032 #if ! BOOST_BEAST_DOXYGEN 0033 namespace boost { 0034 namespace asio { 0035 namespace ssl { 0036 template<typename> class stream; 0037 } // ssl 0038 } // asio 0039 } // boost 0040 #endif 0041 0042 namespace boost { 0043 namespace beast { 0044 0045 /** A stream socket wrapper with timeouts, an executor, and a rate limit policy. 0046 0047 This stream wraps a `net::basic_stream_socket` to provide 0048 the following features: 0049 0050 @li An <em>Executor</em> may be associated with the stream, which will 0051 be used to invoke any completion handlers which do not already have 0052 an associated executor. This achieves support for 0053 <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>. 0054 0055 @li Timeouts may be specified for each logical asynchronous operation 0056 performing any reading, writing, or connecting. 0057 0058 @li A <em>RatePolicy</em> may be associated with the stream, to implement 0059 rate limiting through the policy's interface. 0060 0061 Although the stream supports multiple concurrent outstanding asynchronous 0062 operations, the stream object is not thread-safe. The caller is responsible 0063 for ensuring that the stream is accessed from only one thread at a time. 0064 This includes the times when the stream, and its underlying socket, are 0065 accessed by the networking implementation. To meet this thread safety 0066 requirement, all asynchronous operations must be performed by the stream 0067 within the same implicit strand (only one thread `net::io_context::run`) 0068 or within the same explicit strand, such as an instance of `net::strand`. 0069 0070 Completion handlers with explicit associated executors (such as those 0071 arising from use of `net::bind_executor`) will be invoked by the stream 0072 using the associated executor. Otherwise, the completion handler will 0073 be invoked by the executor associated with the stream upon construction. 0074 The type of executor used with this stream must meet the following 0075 requirements: 0076 0077 @li Function objects submitted to the executor shall never run 0078 concurrently with each other. 0079 0080 The executor type `net::strand` meets these requirements. Use of a 0081 strand as the executor in the stream class template offers an additional 0082 notational convenience: the strand does not need to be specified in 0083 each individual initiating function call. 0084 0085 Unlike other stream wrappers, the underlying socket is accessed 0086 through the @ref socket member function instead of `next_layer`. 0087 This causes the @ref basic_stream to be returned in calls 0088 to @ref get_lowest_layer. 0089 0090 @par Usage 0091 0092 To use this stream declare an instance of the class. Then, before 0093 each logical operation for which a timeout is desired, call 0094 @ref expires_after with a duration, or call @ref expires_at with a 0095 time point. Alternatively, call @ref expires_never to disable the 0096 timeout for subsequent logical operations. A logical operation 0097 is any series of one or more direct or indirect calls to the timeout 0098 stream's asynchronous read, asynchronous write, or asynchronous connect 0099 functions. 0100 0101 When a timeout is set and a mixed operation is performed (one that 0102 includes both reads and writes, for example) the timeout applies 0103 to all of the intermediate asynchronous operations used in the 0104 enclosing operation. This allows timeouts to be applied to stream 0105 algorithms which were not written specifically to allow for timeouts, 0106 when those algorithms are passed a timeout stream with a timeout set. 0107 0108 When a timeout occurs the socket will be closed, canceling any 0109 pending I/O operations. The completion handlers for these canceled 0110 operations will be invoked with the error @ref beast::error::timeout. 0111 0112 @par Examples 0113 0114 This function reads an HTTP request with a timeout, then sends the 0115 HTTP response with a different timeout. 0116 0117 @code 0118 void process_http_1 (tcp_stream& stream, net::yield_context yield) 0119 { 0120 flat_buffer buffer; 0121 http::request<http::empty_body> req; 0122 0123 // Read the request, with a 15 second timeout 0124 stream.expires_after(std::chrono::seconds(15)); 0125 http::async_read(stream, buffer, req, yield); 0126 0127 // Calculate the response 0128 http::response<http::string_body> res = make_response(req); 0129 0130 // Send the response, with a 30 second timeout. 0131 stream.expires_after (std::chrono::seconds(30)); 0132 http::async_write (stream, res, yield); 0133 } 0134 @endcode 0135 0136 The example above could be expressed using a single timeout with a 0137 simple modification. The function that follows first reads an HTTP 0138 request then sends the HTTP response, with a single timeout that 0139 applies to the entire combined operation of reading and writing: 0140 0141 @code 0142 void process_http_2 (tcp_stream& stream, net::yield_context yield) 0143 { 0144 flat_buffer buffer; 0145 http::request<http::empty_body> req; 0146 0147 // Require that the read and write combined take no longer than 30 seconds 0148 stream.expires_after(std::chrono::seconds(30)); 0149 0150 http::async_read(stream, buffer, req, yield); 0151 0152 http::response<http::string_body> res = make_response(req); 0153 http::async_write (stream, res, yield); 0154 } 0155 @endcode 0156 0157 Some stream algorithms, such as `ssl::stream::async_handshake` perform 0158 both reads and writes. A timeout set before calling the initiating function 0159 of such composite stream algorithms will apply to the entire composite 0160 operation. For example, a timeout may be set on performing the SSL handshake 0161 thusly: 0162 0163 @code 0164 void do_ssl_handshake (net::ssl::stream<tcp_stream>& stream, net::yield_context yield) 0165 { 0166 // Require that the SSL handshake take no longer than 10 seconds 0167 stream.expires_after(std::chrono::seconds(10)); 0168 0169 stream.async_handshake(net::ssl::stream_base::client, yield); 0170 } 0171 @endcode 0172 0173 @par Blocking I/O 0174 0175 Synchronous functions behave identically as that of the wrapped 0176 `net::basic_stream_socket`. Timeouts are not available when performing 0177 blocking calls. 0178 0179 @tparam Protocol A type meeting the requirements of <em>Protocol</em> 0180 representing the protocol the protocol to use for the basic stream socket. 0181 A common choice is `net::ip::tcp`. 0182 0183 @tparam Executor A type meeting the requirements of <em>Executor</em> to 0184 be used for submitting all completion handlers which do not already have an 0185 associated executor. If this type is omitted, the default of `net::any_io_executor` 0186 will be used. 0187 0188 @par Thread Safety 0189 <em>Distinct objects</em>: Safe.@n 0190 <em>Shared objects</em>: Unsafe. The application must also ensure 0191 that all asynchronous operations are performed within the same 0192 implicit or explicit strand. 0193 0194 @see 0195 0196 @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>. 0197 */ 0198 template< 0199 class Protocol, 0200 class Executor = net::any_io_executor, 0201 class RatePolicy = unlimited_rate_policy 0202 > 0203 class basic_stream 0204 #if ! BOOST_BEAST_DOXYGEN 0205 : private detail::stream_base 0206 #endif 0207 { 0208 public: 0209 /// The type of the underlying socket. 0210 using socket_type = 0211 net::basic_stream_socket<Protocol, Executor>; 0212 0213 /** The type of the executor associated with the stream. 0214 0215 This will be the type of executor used to invoke completion 0216 handlers which do not have an explicit associated executor. 0217 */ 0218 using executor_type = beast::executor_type<socket_type>; 0219 0220 /// Rebinds the stream type to another executor. 0221 template<class Executor1> 0222 struct rebind_executor 0223 { 0224 /// The stream type when rebound to the specified executor. 0225 using other = basic_stream< 0226 Protocol, Executor1, RatePolicy>; 0227 }; 0228 0229 /// The protocol type. 0230 using protocol_type = Protocol; 0231 0232 /// The endpoint type. 0233 using endpoint_type = typename Protocol::endpoint; 0234 0235 private: 0236 using op_state = basic_op_state<Executor>; 0237 static_assert( 0238 net::is_executor<Executor>::value || net::execution::is_executor<Executor>::value, 0239 "Executor type requirements not met"); 0240 0241 struct impl_type 0242 : boost::enable_shared_from_this<impl_type> 0243 , boost::empty_value<RatePolicy> 0244 { 0245 // must come first 0246 net::basic_stream_socket< 0247 Protocol, Executor> socket; 0248 0249 op_state read; 0250 op_state write; 0251 net::basic_waitable_timer< 0252 std::chrono::steady_clock, 0253 net::wait_traits< 0254 std::chrono::steady_clock>, 0255 Executor> timer; // rate timer; 0256 0257 int waiting = 0; 0258 0259 impl_type(impl_type&&) = default; 0260 0261 template<class... Args> 0262 explicit 0263 impl_type(std::false_type, Args&&...); 0264 0265 template<class RatePolicy_, class... Args> 0266 explicit 0267 impl_type(std::true_type, 0268 RatePolicy_&& policy, Args&&...); 0269 0270 impl_type& operator=(impl_type&&) = delete; 0271 0272 beast::executor_type<socket_type> 0273 ex() noexcept 0274 { 0275 return this->socket.get_executor(); 0276 } 0277 0278 RatePolicy& 0279 policy() noexcept 0280 { 0281 return this->boost::empty_value<RatePolicy>::get(); 0282 } 0283 0284 RatePolicy const& 0285 policy() const noexcept 0286 { 0287 return this->boost::empty_value<RatePolicy>::get(); 0288 } 0289 0290 template<class Executor2> 0291 void on_timer(Executor2 const& ex2); 0292 0293 void reset(); // set timeouts to never 0294 void close() noexcept; // cancel everything 0295 }; 0296 0297 // We use shared ownership for the state so it can 0298 // outlive the destruction of the stream_socket object, 0299 // in the case where there is no outstanding read or write 0300 // but the implementation is still waiting on a timer. 0301 boost::shared_ptr<impl_type> impl_; 0302 0303 template<class Executor2> 0304 struct timeout_handler; 0305 0306 struct ops; 0307 0308 #if ! BOOST_BEAST_DOXYGEN 0309 // boost::asio::ssl::stream needs these 0310 // DEPRECATED 0311 template<class> 0312 friend class boost::asio::ssl::stream; 0313 // DEPRECATED 0314 using lowest_layer_type = socket_type; 0315 // DEPRECATED 0316 lowest_layer_type& 0317 lowest_layer() noexcept 0318 { 0319 return impl_->socket; 0320 } 0321 // DEPRECATED 0322 lowest_layer_type const& 0323 lowest_layer() const noexcept 0324 { 0325 return impl_->socket; 0326 } 0327 #endif 0328 0329 public: 0330 /** Destructor 0331 0332 This function destroys the stream, cancelling any outstanding 0333 asynchronous operations associated with the socket as if by 0334 calling cancel. 0335 */ 0336 ~basic_stream(); 0337 0338 /** Constructor 0339 0340 This constructor creates the stream by forwarding all arguments 0341 to the underlying socket. The socket then needs to be open and 0342 connected or accepted before data can be sent or received on it. 0343 0344 @param args A list of parameters forwarded to the constructor of 0345 the underlying socket. 0346 */ 0347 #if BOOST_BEAST_DOXYGEN 0348 template<class... Args> 0349 explicit 0350 basic_stream(Args&&... args); 0351 #else 0352 template<class Arg0, class... Args, 0353 class = typename std::enable_if< 0354 ! std::is_constructible<RatePolicy, Arg0>::value>::type> 0355 explicit 0356 basic_stream(Arg0&& argo, Args&&... args); 0357 0358 0359 /** Constructor 0360 * 0361 * A constructor that rebinds the executor. 0362 * 0363 * @tparam Executor_ The new executor 0364 * @param other The original socket to be rebound. 0365 */ 0366 template<class Executor_> 0367 explicit 0368 basic_stream(basic_stream<Protocol, Executor_, RatePolicy> && other); 0369 0370 0371 template<typename, typename, typename> 0372 friend class basic_stream; 0373 #endif 0374 0375 /** Constructor 0376 0377 This constructor creates the stream with the specified rate 0378 policy, and forwards all remaining arguments to the underlying 0379 socket. The socket then needs to be open and connected or 0380 accepted before data can be sent or received on it. 0381 0382 @param policy The rate policy object to use. The stream will 0383 take ownership of this object by decay-copy. 0384 0385 @param args A list of parameters forwarded to the constructor of 0386 the underlying socket. 0387 */ 0388 #if BOOST_BEAST_DOXYGEN 0389 template<class RatePolicy_, class... Args> 0390 explicit 0391 basic_stream(RatePolicy_&& policy, Args&&... args); 0392 #else 0393 template<class RatePolicy_, class Arg0, class... Args, 0394 class = typename std::enable_if< 0395 std::is_constructible< 0396 RatePolicy, RatePolicy_>::value>::type> 0397 basic_stream( 0398 RatePolicy_&& policy, Arg0&& arg, Args&&... args); 0399 #endif 0400 0401 /** Move constructor 0402 0403 @param other The other object from which the move will occur. 0404 0405 @note Following the move, the moved-from object is in the 0406 same state as if newly constructed. 0407 */ 0408 basic_stream(basic_stream&& other); 0409 0410 /// Move assignment (deleted). 0411 basic_stream& operator=(basic_stream&&) = delete; 0412 0413 /// Return a reference to the underlying socket 0414 socket_type& 0415 socket() noexcept 0416 { 0417 return impl_->socket; 0418 } 0419 0420 /// Return a reference to the underlying socket 0421 socket_type const& 0422 socket() const noexcept 0423 { 0424 return impl_->socket; 0425 } 0426 0427 /** Release ownership of the underlying socket. 0428 0429 This function causes all outstanding asynchronous connect, 0430 read, and write operations to be canceled as if by a call 0431 to @ref cancel. Ownership of the underlying socket is then 0432 transferred to the caller. 0433 */ 0434 socket_type 0435 release_socket(); 0436 0437 //-------------------------------------------------------------------------- 0438 0439 /// Returns the rate policy associated with the object 0440 RatePolicy& 0441 rate_policy() noexcept 0442 { 0443 return impl_->policy(); 0444 } 0445 0446 /// Returns the rate policy associated with the object 0447 RatePolicy const& 0448 rate_policy() const noexcept 0449 { 0450 return impl_->policy(); 0451 } 0452 0453 /** Set the timeout for subsequent logical operations. 0454 0455 This sets either the read timer, the write timer, or 0456 both timers to expire after the specified amount of time 0457 has elapsed. If a timer expires when the corresponding 0458 asynchronous operation is outstanding, the stream will be 0459 closed and any outstanding operations will complete with the 0460 error @ref beast::error::timeout. Otherwise, if the timer 0461 expires while no operations are outstanding, and the expiraton 0462 is not set again, the next operation will time out immediately. 0463 0464 The timer applies collectively to any asynchronous reads 0465 or writes initiated after the expiration is set, until the 0466 expiration is set again. A call to @ref async_connect 0467 counts as both a read and a write. 0468 0469 @param expiry_time The amount of time after which a logical 0470 operation should be considered timed out. 0471 */ 0472 void 0473 expires_after( 0474 net::steady_timer::duration expiry_time); 0475 0476 /** Set the timeout for subsequent logical operations. 0477 0478 This sets either the read timer, the write timer, or both 0479 timers to expire at the specified time point. If a timer 0480 expires when the corresponding asynchronous operation is 0481 outstanding, the stream will be closed and any outstanding 0482 operations will complete with the error @ref beast::error::timeout. 0483 Otherwise, if the timer expires while no operations are outstanding, 0484 and the expiraton is not set again, the next operation will time out 0485 immediately. 0486 0487 The timer applies collectively to any asynchronous reads 0488 or writes initiated after the expiration is set, until the 0489 expiration is set again. A call to @ref async_connect 0490 counts as both a read and a write. 0491 0492 @param expiry_time The time point after which a logical 0493 operation should be considered timed out. 0494 */ 0495 void 0496 expires_at(net::steady_timer::time_point expiry_time); 0497 0498 /// Disable the timeout for subsequent logical operations. 0499 void 0500 expires_never(); 0501 0502 /** Cancel all asynchronous operations associated with the socket. 0503 0504 This function causes all outstanding asynchronous connect, 0505 read, and write operations to finish immediately. Completion 0506 handlers for cancelled operations will receive the error 0507 `net::error::operation_aborted`. Completion handlers not 0508 yet invoked whose operations have completed, will receive 0509 the error corresponding to the result of the operation (which 0510 may indicate success). 0511 */ 0512 void 0513 cancel(); 0514 0515 /** Close the timed stream. 0516 0517 This cancels all of the outstanding asynchronous operations 0518 as if by calling @ref cancel, and closes the underlying socket. 0519 */ 0520 void 0521 close(); 0522 0523 //-------------------------------------------------------------------------- 0524 0525 /** Get the executor associated with the object. 0526 0527 This function may be used to obtain the executor object that the 0528 stream uses to dispatch completion handlers without an assocaited 0529 executor. 0530 0531 @return A copy of the executor that stream will use to dispatch handlers. 0532 */ 0533 executor_type 0534 get_executor() noexcept 0535 { 0536 return impl_->ex(); 0537 } 0538 0539 /** Connect the stream to the specified endpoint. 0540 0541 This function is used to connect the underlying socket to the 0542 specified remote endpoint. The function call will block until 0543 the connection is successfully made or an error occurs. 0544 The underlying socket is automatically opened if needed. 0545 An automatically opened socket is not returned to the 0546 closed state upon failure. 0547 0548 @param ep The remote endpoint to connect to. 0549 0550 @throws system_error Thrown on failure. 0551 0552 @see connect 0553 */ 0554 void 0555 connect(endpoint_type const& ep) 0556 { 0557 socket().connect(ep); 0558 } 0559 0560 /** Connect the stream to the specified endpoint. 0561 0562 This function is used to connect the underlying socket to the 0563 specified remote endpoint. The function call will block until 0564 the connection is successfully made or an error occurs. 0565 The underlying socket is automatically opened if needed. 0566 An automatically opened socket is not returned to the 0567 closed state upon failure. 0568 0569 @param ep The remote endpoint to connect to. 0570 0571 @param ec Set to indicate what error occurred, if any. 0572 0573 @see connect 0574 */ 0575 void 0576 connect(endpoint_type const& ep, error_code& ec) 0577 { 0578 socket().connect(ep, ec); 0579 } 0580 0581 /** Establishes a connection by trying each endpoint in a sequence. 0582 0583 This function attempts to connect the stream to one of a sequence of 0584 endpoints by trying each endpoint until a connection is successfully 0585 established. 0586 The underlying socket is automatically opened if needed. 0587 An automatically opened socket is not returned to the 0588 closed state upon failure. 0589 0590 The algorithm, known as a <em>composed operation</em>, is implemented 0591 in terms of calls to the underlying socket's `connect` function. 0592 0593 @param endpoints A sequence of endpoints. 0594 0595 @returns The successfully connected endpoint. 0596 0597 @throws system_error Thrown on failure. If the sequence is 0598 empty, the associated error code is `net::error::not_found`. 0599 Otherwise, contains the error from the last connection attempt. 0600 */ 0601 template<class EndpointSequence 0602 #if ! BOOST_BEAST_DOXYGEN 0603 ,class = typename std::enable_if< 0604 net::is_endpoint_sequence< 0605 EndpointSequence>::value>::type 0606 #endif 0607 > 0608 typename Protocol::endpoint 0609 connect(EndpointSequence const& endpoints) 0610 { 0611 return net::connect(socket(), endpoints); 0612 } 0613 0614 /** Establishes a connection by trying each endpoint in a sequence. 0615 0616 This function attempts to connect the stream to one of a sequence of 0617 endpoints by trying each endpoint until a connection is successfully 0618 established. 0619 The underlying socket is automatically opened if needed. 0620 An automatically opened socket is not returned to the 0621 closed state upon failure. 0622 0623 The algorithm, known as a <em>composed operation</em>, is implemented 0624 in terms of calls to the underlying socket's `connect` function. 0625 0626 @param endpoints A sequence of endpoints. 0627 0628 @param ec Set to indicate what error occurred, if any. If the sequence is 0629 empty, set to `net::error::not_found`. Otherwise, contains the error 0630 from the last connection attempt. 0631 0632 @returns On success, the successfully connected endpoint. Otherwise, a 0633 default-constructed endpoint. 0634 */ 0635 template<class EndpointSequence 0636 #if ! BOOST_BEAST_DOXYGEN 0637 ,class = typename std::enable_if< 0638 net::is_endpoint_sequence< 0639 EndpointSequence>::value>::type 0640 #endif 0641 > 0642 typename Protocol::endpoint 0643 connect( 0644 EndpointSequence const& endpoints, 0645 error_code& ec 0646 ) 0647 { 0648 return net::connect(socket(), endpoints, ec); 0649 } 0650 0651 /** Establishes a connection by trying each endpoint in a sequence. 0652 0653 This function attempts to connect the stream to one of a sequence of 0654 endpoints by trying each endpoint until a connection is successfully 0655 established. 0656 The underlying socket is automatically opened if needed. 0657 An automatically opened socket is not returned to the 0658 closed state upon failure. 0659 0660 The algorithm, known as a <em>composed operation</em>, is implemented 0661 in terms of calls to the underlying socket's `connect` function. 0662 0663 @param begin An iterator pointing to the start of a sequence of endpoints. 0664 0665 @param end An iterator pointing to the end of a sequence of endpoints. 0666 0667 @returns An iterator denoting the successfully connected endpoint. 0668 0669 @throws system_error Thrown on failure. If the sequence is 0670 empty, the associated error code is `net::error::not_found`. 0671 Otherwise, contains the error from the last connection attempt. 0672 */ 0673 template<class Iterator> 0674 Iterator 0675 connect( 0676 Iterator begin, Iterator end) 0677 { 0678 return net::connect(socket(), begin, end); 0679 } 0680 0681 /** Establishes a connection by trying each endpoint in a sequence. 0682 0683 This function attempts to connect the stream to one of a sequence of 0684 endpoints by trying each endpoint until a connection is successfully 0685 established. 0686 The underlying socket is automatically opened if needed. 0687 An automatically opened socket is not returned to the 0688 closed state upon failure. 0689 0690 The algorithm, known as a <em>composed operation</em>, is implemented 0691 in terms of calls to the underlying socket's `connect` function. 0692 0693 @param begin An iterator pointing to the start of a sequence of endpoints. 0694 0695 @param end An iterator pointing to the end of a sequence of endpoints. 0696 0697 @param ec Set to indicate what error occurred, if any. If the sequence is 0698 empty, set to boost::asio::error::not_found. Otherwise, contains the error 0699 from the last connection attempt. 0700 0701 @returns On success, an iterator denoting the successfully connected 0702 endpoint. Otherwise, the end iterator. 0703 */ 0704 template<class Iterator> 0705 Iterator 0706 connect( 0707 Iterator begin, Iterator end, 0708 error_code& ec) 0709 { 0710 return net::connect(socket(), begin, end, ec); 0711 } 0712 0713 /** Establishes a connection by trying each endpoint in a sequence. 0714 0715 This function attempts to connect the stream to one of a sequence of 0716 endpoints by trying each endpoint until a connection is successfully 0717 established. 0718 The underlying socket is automatically opened if needed. 0719 An automatically opened socket is not returned to the 0720 closed state upon failure. 0721 0722 The algorithm, known as a <em>composed operation</em>, is implemented 0723 in terms of calls to the underlying socket's `connect` function. 0724 0725 @param endpoints A sequence of endpoints. 0726 0727 @param connect_condition A function object that is called prior to each 0728 connection attempt. The signature of the function object must be: 0729 @code 0730 bool connect_condition( 0731 error_code const& ec, 0732 typename Protocol::endpoint const& next); 0733 @endcode 0734 The @c ec parameter contains the result from the most recent connect 0735 operation. Before the first connection attempt, @c ec is always set to 0736 indicate success. The @c next parameter is the next endpoint to be tried. 0737 The function object should return true if the next endpoint should be tried, 0738 and false if it should be skipped. 0739 0740 @returns The successfully connected endpoint. 0741 0742 @throws boost::system::system_error Thrown on failure. If the sequence is 0743 empty, the associated error code is `net::error::not_found`. 0744 Otherwise, contains the error from the last connection attempt. 0745 */ 0746 template< 0747 class EndpointSequence, class ConnectCondition 0748 #if ! BOOST_BEAST_DOXYGEN 0749 ,class = typename std::enable_if< 0750 net::is_endpoint_sequence< 0751 EndpointSequence>::value>::type 0752 #endif 0753 > 0754 typename Protocol::endpoint 0755 connect( 0756 EndpointSequence const& endpoints, 0757 ConnectCondition connect_condition 0758 ) 0759 { 0760 return net::connect(socket(), endpoints, connect_condition); 0761 } 0762 0763 /** Establishes a connection by trying each endpoint in a sequence. 0764 0765 This function attempts to connect the stream to one of a sequence of 0766 endpoints by trying each endpoint until a connection is successfully 0767 established. 0768 The underlying socket is automatically opened if needed. 0769 An automatically opened socket is not returned to the 0770 closed state upon failure. 0771 0772 The algorithm, known as a <em>composed operation</em>, is implemented 0773 in terms of calls to the underlying socket's `connect` function. 0774 0775 @param endpoints A sequence of endpoints. 0776 0777 @param connect_condition A function object that is called prior to each 0778 connection attempt. The signature of the function object must be: 0779 @code 0780 bool connect_condition( 0781 error_code const& ec, 0782 typename Protocol::endpoint const& next); 0783 @endcode 0784 The @c ec parameter contains the result from the most recent connect 0785 operation. Before the first connection attempt, @c ec is always set to 0786 indicate success. The @c next parameter is the next endpoint to be tried. 0787 The function object should return true if the next endpoint should be tried, 0788 and false if it should be skipped. 0789 0790 @param ec Set to indicate what error occurred, if any. If the sequence is 0791 empty, set to `net::error::not_found`. Otherwise, contains the error 0792 from the last connection attempt. 0793 0794 @returns On success, the successfully connected endpoint. Otherwise, a 0795 default-constructed endpoint. 0796 */ 0797 template< 0798 class EndpointSequence, class ConnectCondition 0799 #if ! BOOST_BEAST_DOXYGEN 0800 ,class = typename std::enable_if< 0801 net::is_endpoint_sequence< 0802 EndpointSequence>::value>::type 0803 #endif 0804 > 0805 typename Protocol::endpoint 0806 connect( 0807 EndpointSequence const& endpoints, 0808 ConnectCondition connect_condition, 0809 error_code& ec) 0810 { 0811 return net::connect(socket(), endpoints, connect_condition, ec); 0812 } 0813 0814 /** Establishes a connection by trying each endpoint in a sequence. 0815 0816 This function attempts to connect the stream to one of a sequence of 0817 endpoints by trying each endpoint until a connection is successfully 0818 established. 0819 The underlying socket is automatically opened if needed. 0820 An automatically opened socket is not returned to the 0821 closed state upon failure. 0822 0823 The algorithm, known as a <em>composed operation</em>, is implemented 0824 in terms of calls to the underlying socket's `connect` function. 0825 0826 @param begin An iterator pointing to the start of a sequence of endpoints. 0827 0828 @param end An iterator pointing to the end of a sequence of endpoints. 0829 0830 @param connect_condition A function object that is called prior to each 0831 connection attempt. The signature of the function object must be: 0832 @code 0833 bool connect_condition( 0834 error_code const& ec, 0835 typename Protocol::endpoint const& next); 0836 @endcode 0837 The @c ec parameter contains the result from the most recent connect 0838 operation. Before the first connection attempt, @c ec is always set to 0839 indicate success. The @c next parameter is the next endpoint to be tried. 0840 The function object should return true if the next endpoint should be tried, 0841 and false if it should be skipped. 0842 0843 @returns An iterator denoting the successfully connected endpoint. 0844 0845 @throws boost::system::system_error Thrown on failure. If the sequence is 0846 empty, the associated @c error_code is `net::error::not_found`. 0847 Otherwise, contains the error from the last connection attempt. 0848 */ 0849 template< 0850 class Iterator, class ConnectCondition> 0851 Iterator 0852 connect( 0853 Iterator begin, Iterator end, 0854 ConnectCondition connect_condition) 0855 { 0856 return net::connect(socket(), begin, end, connect_condition); 0857 } 0858 0859 /** Establishes a connection by trying each endpoint in a sequence. 0860 0861 This function attempts to connect the stream to one of a sequence of 0862 endpoints by trying each endpoint until a connection is successfully 0863 established. 0864 The underlying socket is automatically opened if needed. 0865 An automatically opened socket is not returned to the 0866 closed state upon failure. 0867 0868 The algorithm, known as a <em>composed operation</em>, is implemented 0869 in terms of calls to the underlying socket's `connect` function. 0870 0871 @param begin An iterator pointing to the start of a sequence of endpoints. 0872 0873 @param end An iterator pointing to the end of a sequence of endpoints. 0874 0875 @param connect_condition A function object that is called prior to each 0876 connection attempt. The signature of the function object must be: 0877 @code 0878 bool connect_condition( 0879 error_code const& ec, 0880 typename Protocol::endpoint const& next); 0881 @endcode 0882 The @c ec parameter contains the result from the most recent connect 0883 operation. Before the first connection attempt, @c ec is always set to 0884 indicate success. The @c next parameter is the next endpoint to be tried. 0885 The function object should return true if the next endpoint should be tried, 0886 and false if it should be skipped. 0887 0888 @param ec Set to indicate what error occurred, if any. If the sequence is 0889 empty, set to `net::error::not_found`. Otherwise, contains the error 0890 from the last connection attempt. 0891 0892 @returns On success, an iterator denoting the successfully connected 0893 endpoint. Otherwise, the end iterator. 0894 */ 0895 template< 0896 class Iterator, class ConnectCondition> 0897 Iterator 0898 connect( 0899 Iterator begin, Iterator end, 0900 ConnectCondition connect_condition, 0901 error_code& ec) 0902 { 0903 return net::connect(socket(), begin, end, connect_condition, ec); 0904 } 0905 0906 /** Connect the stream to the specified endpoint asynchronously. 0907 0908 This function is used to asynchronously connect the underlying 0909 socket to the specified remote endpoint. The function call always 0910 returns immediately. 0911 The underlying socket is automatically opened if needed. 0912 An automatically opened socket is not returned to the 0913 closed state upon failure. 0914 0915 If the timeout timer expires while the operation is outstanding, 0916 the operation will be canceled and the completion handler will be 0917 invoked with the error @ref error::timeout. 0918 0919 @param ep The remote endpoint to which the underlying socket will be 0920 connected. Copies will be made of the endpoint object as required. 0921 0922 @param handler The completion handler to invoke when the operation 0923 completes. The implementation takes ownership of the handler by 0924 performing a decay-copy. The equivalent function signature of 0925 the handler must be: 0926 @code 0927 void handler( 0928 error_code ec // Result of operation 0929 ); 0930 @endcode 0931 If the handler has an associated immediate executor, 0932 an immediate completion will be dispatched to it. 0933 Otherwise, the handler will not be invoked from within 0934 this function. Invocation of the handler will be performed 0935 by dispatching to the immediate executor. If no 0936 immediate executor is specified, this is equivalent 0937 to using `net::post`. 0938 0939 @par Per-Operation Cancellation 0940 0941 This asynchronous operation supports cancellation for the following 0942 net::cancellation_type values: 0943 0944 @li @c net::cancellation_type::terminal 0945 @li @c net::cancellation_type::partial 0946 @li @c net::cancellation_type::total 0947 0948 if they are also supported by the socket's @c async_connect operation. 0949 0950 @see async_connect 0951 */ 0952 template< 0953 BOOST_BEAST_ASYNC_TPARAM1 ConnectHandler = 0954 net::default_completion_token_t<executor_type> 0955 > 0956 BOOST_BEAST_ASYNC_RESULT1(ConnectHandler) 0957 async_connect( 0958 endpoint_type const& ep, 0959 ConnectHandler&& handler = 0960 net::default_completion_token_t< 0961 executor_type>{}); 0962 0963 /** Establishes a connection by trying each endpoint in a sequence asynchronously. 0964 0965 This function attempts to connect the stream to one of a sequence of 0966 endpoints by trying each endpoint until a connection is successfully 0967 established. 0968 The underlying socket is automatically opened if needed. 0969 An automatically opened socket is not returned to the 0970 closed state upon failure. 0971 0972 The algorithm, known as a <em>composed asynchronous operation</em>, is 0973 implemented in terms of calls to the underlying socket's `async_connect` 0974 function. 0975 0976 If the timeout timer expires while the operation is outstanding, 0977 the current connection attempt will be canceled and the completion 0978 handler will be invoked with the error @ref error::timeout. 0979 0980 @param endpoints A sequence of endpoints. This this object must meet 0981 the requirements of <em>EndpointSequence</em>. 0982 0983 @param handler The completion handler to invoke when the operation 0984 completes. The implementation takes ownership of the handler by 0985 performing a decay-copy. The equivalent function signature of 0986 the handler must be: 0987 @code 0988 void handler( 0989 // Result of operation. if the sequence is empty, set to 0990 // net::error::not_found. Otherwise, contains the 0991 // error from the last connection attempt. 0992 error_code const& error, 0993 0994 // On success, the successfully connected endpoint. 0995 // Otherwise, a default-constructed endpoint. 0996 typename Protocol::endpoint const& endpoint 0997 ); 0998 @endcode 0999 If the handler has an associated immediate executor, 1000 an immediate completion will be dispatched to it. 1001 Otherwise, the handler will not be invoked from within 1002 this function. Invocation of the handler will be performed 1003 by dispatching to the immediate executor. If no 1004 immediate executor is specified, this is equivalent 1005 to using `net::post`. 1006 1007 @par Per-Operation Cancellation 1008 1009 This asynchronous operation supports cancellation for the following 1010 net::cancellation_type values: 1011 1012 @li @c net::cancellation_type::terminal 1013 @li @c net::cancellation_type::partial 1014 @li @c net::cancellation_type::total 1015 1016 if they are also supported by the socket's @c async_connect operation. 1017 1018 */ 1019 template< 1020 class EndpointSequence, 1021 BOOST_ASIO_COMPLETION_TOKEN_FOR( 1022 void(error_code, typename Protocol::endpoint)) 1023 RangeConnectHandler = 1024 net::default_completion_token_t<executor_type> 1025 #if ! BOOST_BEAST_DOXYGEN 1026 ,class = typename std::enable_if< 1027 net::is_endpoint_sequence< 1028 EndpointSequence>::value>::type 1029 ,class = typename std::enable_if< 1030 !net::is_connect_condition<RangeConnectHandler, 1031 decltype(std::declval<const EndpointSequence&>().begin())>::value 1032 >::type 1033 #endif 1034 > 1035 BOOST_ASIO_INITFN_AUTO_RESULT_TYPE( 1036 RangeConnectHandler, 1037 void(error_code, typename Protocol::endpoint)) 1038 async_connect( 1039 EndpointSequence const& endpoints, 1040 RangeConnectHandler&& handler = 1041 net::default_completion_token_t<executor_type>{}); 1042 1043 /** Establishes a connection by trying each endpoint in a sequence asynchronously. 1044 1045 This function attempts to connect the stream to one of a sequence of 1046 endpoints by trying each endpoint until a connection is successfully 1047 established. 1048 The underlying socket is automatically opened if needed. 1049 An automatically opened socket is not returned to the 1050 closed state upon failure. 1051 1052 The algorithm, known as a <em>composed asynchronous operation</em>, is 1053 implemented in terms of calls to the underlying socket's `async_connect` 1054 function. 1055 1056 If the timeout timer expires while the operation is outstanding, 1057 the current connection attempt will be canceled and the completion 1058 handler will be invoked with the error @ref error::timeout. 1059 1060 @param endpoints A sequence of endpoints. This this object must meet 1061 the requirements of <em>EndpointSequence</em>. 1062 1063 @param connect_condition A function object that is called prior to each 1064 connection attempt. The signature of the function object must be: 1065 @code 1066 bool connect_condition( 1067 error_code const& ec, 1068 typename Protocol::endpoint const& next); 1069 @endcode 1070 The @c ec parameter contains the result from the most recent connect 1071 operation. Before the first connection attempt, @c ec is always set to 1072 indicate success. The @c next parameter is the next endpoint to be tried. 1073 The function object should return true if the next endpoint should be tried, 1074 and false if it should be skipped. 1075 1076 @param handler The completion handler to invoke when the operation 1077 completes. The implementation takes ownership of the handler by 1078 performing a decay-copy. The equivalent function signature of 1079 the handler must be: 1080 @code 1081 void handler( 1082 // Result of operation. if the sequence is empty, set to 1083 // net::error::not_found. Otherwise, contains the 1084 // error from the last connection attempt. 1085 error_code const& error, 1086 1087 // On success, the successfully connected endpoint. 1088 // Otherwise, a default-constructed endpoint. 1089 typename Protocol::endpoint const& endpoint 1090 ); 1091 @endcode 1092 If the handler has an associated immediate executor, 1093 an immediate completion will be dispatched to it. 1094 Otherwise, the handler will not be invoked from within 1095 this function. Invocation of the handler will be performed 1096 by dispatching to the immediate executor. If no 1097 immediate executor is specified, this is equivalent 1098 to using `net::post`. 1099 @par Example 1100 The following connect condition function object can be used to output 1101 information about the individual connection attempts: 1102 @code 1103 struct my_connect_condition 1104 { 1105 bool operator()( 1106 error_code const& ec, 1107 net::ip::tcp::endpoint const& next) 1108 { 1109 if (ec) 1110 std::cout << "Error: " << ec.message() << std::endl; 1111 std::cout << "Trying: " << next << std::endl; 1112 return true; 1113 } 1114 }; 1115 @endcode 1116 1117 @par Per-Operation Cancellation 1118 1119 This asynchronous operation supports cancellation for the following 1120 net::cancellation_type values: 1121 1122 @li @c net::cancellation_type::terminal 1123 @li @c net::cancellation_type::partial 1124 @li @c net::cancellation_type::total 1125 1126 if they are also supported by the socket's @c async_connect operation. 1127 */ 1128 template< 1129 class EndpointSequence, 1130 class ConnectCondition, 1131 BOOST_ASIO_COMPLETION_TOKEN_FOR( 1132 void(error_code, typename Protocol::endpoint)) 1133 RangeConnectHandler = 1134 net::default_completion_token_t<executor_type> 1135 #if ! BOOST_BEAST_DOXYGEN 1136 ,class = typename std::enable_if< 1137 net::is_endpoint_sequence< 1138 EndpointSequence>::value>::type 1139 ,class = typename std::enable_if< 1140 net::is_connect_condition<ConnectCondition, 1141 decltype(std::declval<const EndpointSequence&>().begin())>::value 1142 >::type 1143 #endif 1144 > 1145 BOOST_ASIO_INITFN_AUTO_RESULT_TYPE( 1146 RangeConnectHandler, 1147 void(error_code, typename Protocol::endpoint)) 1148 async_connect( 1149 EndpointSequence const& endpoints, 1150 ConnectCondition connect_condition, 1151 RangeConnectHandler&& handler = 1152 net::default_completion_token_t< 1153 executor_type>{}); 1154 1155 /** Establishes a connection by trying each endpoint in a sequence asynchronously. 1156 1157 This function attempts to connect the stream to one of a sequence of 1158 endpoints by trying each endpoint until a connection is successfully 1159 established. 1160 The underlying socket is automatically opened if needed. 1161 An automatically opened socket is not returned to the 1162 closed state upon failure. 1163 1164 The algorithm, known as a <em>composed asynchronous operation</em>, is 1165 implemented in terms of calls to the underlying socket's `async_connect` 1166 function. 1167 1168 If the timeout timer expires while the operation is outstanding, 1169 the current connection attempt will be canceled and the completion 1170 handler will be invoked with the error @ref error::timeout. 1171 1172 @param begin An iterator pointing to the start of a sequence of endpoints. 1173 1174 @param end An iterator pointing to the end of a sequence of endpoints. 1175 1176 @param handler The completion handler to invoke when the operation 1177 completes. The implementation takes ownership of the handler by 1178 performing a decay-copy. The equivalent function signature of 1179 the handler must be: 1180 @code 1181 void handler( 1182 // Result of operation. if the sequence is empty, set to 1183 // net::error::not_found. Otherwise, contains the 1184 // error from the last connection attempt. 1185 error_code const& error, 1186 1187 // On success, an iterator denoting the successfully 1188 // connected endpoint. Otherwise, the end iterator. 1189 Iterator iterator 1190 ); 1191 @endcode 1192 If the handler has an associated immediate executor, 1193 an immediate completion will be dispatched to it. 1194 Otherwise, the handler will not be invoked from within 1195 this function. Invocation of the handler will be performed 1196 by dispatching to the immediate executor. If no 1197 immediate executor is specified, this is equivalent 1198 to using `net::post`. 1199 @par Per-Operation Cancellation 1200 1201 This asynchronous operation supports cancellation for the following 1202 net::cancellation_type values: 1203 1204 @li @c net::cancellation_type::terminal 1205 @li @c net::cancellation_type::partial 1206 @li @c net::cancellation_type::total 1207 1208 if they are also supported by the socket's @c async_connect operation. 1209 */ 1210 template< 1211 class Iterator, 1212 BOOST_ASIO_COMPLETION_TOKEN_FOR( 1213 void(error_code, Iterator)) 1214 IteratorConnectHandler = 1215 net::default_completion_token_t<executor_type> 1216 #if ! BOOST_BEAST_DOXYGEN 1217 ,class = typename std::enable_if< 1218 !net::is_connect_condition<IteratorConnectHandler, Iterator>::value 1219 >::type 1220 #endif 1221 > 1222 BOOST_ASIO_INITFN_AUTO_RESULT_TYPE( 1223 IteratorConnectHandler, 1224 void(error_code, Iterator)) 1225 async_connect( 1226 Iterator begin, Iterator end, 1227 IteratorConnectHandler&& handler = 1228 net::default_completion_token_t<executor_type>{}); 1229 1230 /** Establishes a connection by trying each endpoint in a sequence asynchronously. 1231 1232 This function attempts to connect the stream to one of a sequence of 1233 endpoints by trying each endpoint until a connection is successfully 1234 established. 1235 The algorithm, known as a <em>composed asynchronous operation</em>, is 1236 implemented in terms of calls to the underlying socket's `async_connect` 1237 function. 1238 1239 If the timeout timer expires while the operation is outstanding, 1240 the current connection attempt will be canceled and the completion 1241 handler will be invoked with the error @ref error::timeout. 1242 1243 @param begin An iterator pointing to the start of a sequence of endpoints. 1244 1245 @param end An iterator pointing to the end of a sequence of endpoints. 1246 1247 @param connect_condition A function object that is called prior to each 1248 connection attempt. The signature of the function object must be: 1249 @code 1250 bool connect_condition( 1251 error_code const& ec, 1252 typename Protocol::endpoint const& next); 1253 @endcode 1254 1255 @param handler The completion handler to invoke when the operation 1256 completes. The implementation takes ownership of the handler by 1257 performing a decay-copy. The equivalent function signature of 1258 the handler must be: 1259 @code 1260 void handler( 1261 // Result of operation. if the sequence is empty, set to 1262 // net::error::not_found. Otherwise, contains the 1263 // error from the last connection attempt. 1264 error_code const& error, 1265 1266 // On success, an iterator denoting the successfully 1267 // connected endpoint. Otherwise, the end iterator. 1268 Iterator iterator 1269 ); 1270 @endcode 1271 If the handler has an associated immediate executor, 1272 an immediate completion will be dispatched to it. 1273 Otherwise, the handler will not be invoked from within 1274 this function. Invocation of the handler will be performed 1275 by dispatching to the immediate executor. If no 1276 immediate executor is specified, this is equivalent 1277 to using `net::post`. 1278 @par Per-Operation Cancellation 1279 1280 This asynchronous operation supports cancellation for the following 1281 net::cancellation_type values: 1282 1283 @li @c net::cancellation_type::terminal 1284 @li @c net::cancellation_type::partial 1285 @li @c net::cancellation_type::total 1286 1287 if they are also supported by the socket's @c async_connect operation. 1288 */ 1289 template< 1290 class Iterator, 1291 class ConnectCondition, 1292 BOOST_ASIO_COMPLETION_TOKEN_FOR( 1293 void(error_code, Iterator)) 1294 IteratorConnectHandler = 1295 net::default_completion_token_t<executor_type> 1296 #if ! BOOST_BEAST_DOXYGEN 1297 ,class = typename std::enable_if< 1298 net::is_connect_condition<ConnectCondition, Iterator>::value 1299 >::type 1300 #endif 1301 > 1302 BOOST_ASIO_INITFN_AUTO_RESULT_TYPE( 1303 IteratorConnectHandler, 1304 void(error_code, Iterator)) 1305 async_connect( 1306 Iterator begin, Iterator end, 1307 ConnectCondition connect_condition, 1308 IteratorConnectHandler&& handler = 1309 net::default_completion_token_t<executor_type>{}); 1310 1311 //-------------------------------------------------------------------------- 1312 1313 /** Read some data. 1314 1315 This function is used to read some data from the stream. 1316 1317 The call blocks until one of the following is true: 1318 1319 @li One or more bytes are read from the stream. 1320 1321 @li An error occurs. 1322 1323 @param buffers The buffers into which the data will be read. If the 1324 size of the buffers is zero bytes, the call always returns 1325 immediately with no error. 1326 1327 @returns The number of bytes read. 1328 1329 @throws system_error Thrown on failure. 1330 1331 @note The `read_some` operation may not receive all of the requested 1332 number of bytes. Consider using the function `net::read` if you need 1333 to ensure that the requested amount of data is read before the 1334 blocking operation completes. 1335 */ 1336 template<class MutableBufferSequence> 1337 std::size_t 1338 read_some(MutableBufferSequence const& buffers) 1339 { 1340 return impl_->socket.read_some(buffers); 1341 } 1342 1343 /** Read some data. 1344 1345 This function is used to read some data from the underlying socket. 1346 1347 The call blocks until one of the following is true: 1348 1349 @li One or more bytes are read from the stream. 1350 1351 @li An error occurs. 1352 1353 @param buffers The buffers into which the data will be read. If the 1354 size of the buffers is zero bytes, the call always returns 1355 immediately with no error. 1356 1357 @param ec Set to indicate what error occurred, if any. 1358 1359 @returns The number of bytes read. 1360 1361 @note The `read_some` operation may not receive all of the requested 1362 number of bytes. Consider using the function `net::read` if you need 1363 to ensure that the requested amount of data is read before the 1364 blocking operation completes. 1365 */ 1366 template<class MutableBufferSequence> 1367 std::size_t 1368 read_some( 1369 MutableBufferSequence const& buffers, 1370 error_code& ec) 1371 { 1372 return impl_->socket.read_some(buffers, ec); 1373 } 1374 1375 /** Read some data asynchronously. 1376 1377 This function is used to asynchronously read data from the stream. 1378 1379 This call always returns immediately. The asynchronous operation 1380 will continue until one of the following conditions is true: 1381 1382 @li One or more bytes are read from the stream. 1383 1384 @li An error occurs. 1385 1386 The algorithm, known as a <em>composed asynchronous operation</em>, 1387 is implemented in terms of calls to the next layer's `async_read_some` 1388 function. The program must ensure that no other calls to @ref read_some 1389 or @ref async_read_some are performed until this operation completes. 1390 1391 If the timeout timer expires while the operation is outstanding, 1392 the operation will be canceled and the completion handler will be 1393 invoked with the error @ref error::timeout. 1394 1395 @param buffers The buffers into which the data will be read. If the size 1396 of the buffers is zero bytes, the operation always completes immediately 1397 with no error. 1398 Although the buffers object may be copied as necessary, ownership of the 1399 underlying memory blocks is retained by the caller, which must guarantee 1400 that they remain valid until the handler is called. 1401 1402 @param handler The completion handler to invoke when the operation 1403 completes. The implementation takes ownership of the handler by 1404 performing a decay-copy. The equivalent function signature of 1405 the handler must be: 1406 @code 1407 void handler( 1408 error_code error, // Result of operation. 1409 std::size_t bytes_transferred // Number of bytes read. 1410 ); 1411 @endcode 1412 If the handler has an associated immediate executor, 1413 an immediate completion will be dispatched to it. 1414 Otherwise, the handler will not be invoked from within 1415 this function. Invocation of the handler will be performed 1416 by dispatching to the immediate executor. If no 1417 immediate executor is specified, this is equivalent 1418 to using `net::post`. 1419 @note The `async_read_some` operation may not receive all of the requested 1420 number of bytes. Consider using the function `net::async_read` if you need 1421 to ensure that the requested amount of data is read before the asynchronous 1422 operation completes. 1423 1424 @par Per-Operation Cancellation 1425 1426 This asynchronous operation supports cancellation for the following 1427 net::cancellation_type values: 1428 1429 @li @c net::cancellation_type::terminal 1430 @li @c net::cancellation_type::partial 1431 @li @c net::cancellation_type::total 1432 1433 if they are also supported by the socket's @c async_read_some operation. 1434 */ 1435 template< 1436 class MutableBufferSequence, 1437 BOOST_BEAST_ASYNC_TPARAM2 ReadHandler = 1438 net::default_completion_token_t<executor_type> 1439 > 1440 BOOST_BEAST_ASYNC_RESULT2(ReadHandler) 1441 async_read_some( 1442 MutableBufferSequence const& buffers, 1443 ReadHandler&& handler = 1444 net::default_completion_token_t<executor_type>{} 1445 ); 1446 1447 /** Write some data. 1448 1449 This function is used to write some data to the stream. 1450 1451 The call blocks until one of the following is true: 1452 1453 @li One or more bytes are written to the stream. 1454 1455 @li An error occurs. 1456 1457 @param buffers The buffers from which the data will be written. If the 1458 size of the buffers is zero bytes, the call always returns immediately 1459 with no error. 1460 1461 @returns The number of bytes written. 1462 1463 @throws system_error Thrown on failure. 1464 1465 @note The `write_some` operation may not transmit all of the requested 1466 number of bytes. Consider using the function `net::write` if you need 1467 to ensure that the requested amount of data is written before the 1468 blocking operation completes. 1469 */ 1470 template<class ConstBufferSequence> 1471 std::size_t 1472 write_some(ConstBufferSequence const& buffers) 1473 { 1474 return impl_->socket.write_some(buffers); 1475 } 1476 1477 /** Write some data. 1478 1479 This function is used to write some data to the stream. 1480 1481 The call blocks until one of the following is true: 1482 1483 @li One or more bytes are written to the stream. 1484 1485 @li An error occurs. 1486 1487 @param buffers The buffers from which the data will be written. If the 1488 size of the buffers is zero bytes, the call always returns immediately 1489 with no error. 1490 1491 @param ec Set to indicate what error occurred, if any. 1492 1493 @returns The number of bytes written. 1494 1495 @throws system_error Thrown on failure. 1496 1497 @note The `write_some` operation may not transmit all of the requested 1498 number of bytes. Consider using the function `net::write` if you need 1499 to ensure that the requested amount of data is written before the 1500 blocking operation completes. 1501 */ 1502 template<class ConstBufferSequence> 1503 std::size_t 1504 write_some( 1505 ConstBufferSequence const& buffers, 1506 error_code& ec) 1507 { 1508 return impl_->socket.write_some(buffers, ec); 1509 } 1510 1511 /** Write some data asynchronously. 1512 1513 This function is used to asynchronously write data to the underlying socket. 1514 1515 This call always returns immediately. The asynchronous operation 1516 will continue until one of the following conditions is true: 1517 1518 @li One or more bytes are written to the stream. 1519 1520 @li An error occurs. 1521 1522 The algorithm, known as a <em>composed asynchronous operation</em>, 1523 is implemented in terms of calls to the next layer's `async_write_some` 1524 function. The program must ensure that no other calls to @ref async_write_some 1525 are performed until this operation completes. 1526 1527 If the timeout timer expires while the operation is outstanding, 1528 the operation will be canceled and the completion handler will be 1529 invoked with the error @ref error::timeout. 1530 1531 @param buffers The buffers from which the data will be written. If the 1532 size of the buffers is zero bytes, the operation always completes 1533 immediately with no error. 1534 Although the buffers object may be copied as necessary, ownership of the 1535 underlying memory blocks is retained by the caller, which must guarantee 1536 that they remain valid until the handler is called. 1537 1538 @param handler The completion handler to invoke when the operation 1539 completes. The implementation takes ownership of the handler by 1540 performing a decay-copy. The equivalent function signature of 1541 the handler must be: 1542 @code 1543 void handler( 1544 error_code error, // Result of operation. 1545 std::size_t bytes_transferred // Number of bytes written. 1546 ); 1547 @endcode 1548 If the handler has an associated immediate executor, 1549 an immediate completion will be dispatched to it. 1550 Otherwise, the handler will not be invoked from within 1551 this function. Invocation of the handler will be performed 1552 by dispatching to the immediate executor. If no 1553 immediate executor is specified, this is equivalent 1554 to using `net::post`. 1555 @note The `async_write_some` operation may not transmit all of the requested 1556 number of bytes. Consider using the function `net::async_write` if you need 1557 to ensure that the requested amount of data is sent before the asynchronous 1558 operation completes. 1559 1560 @par Per-Operation Cancellation 1561 1562 This asynchronous operation supports cancellation for the following 1563 net::cancellation_type values: 1564 1565 @li @c net::cancellation_type::terminal 1566 @li @c net::cancellation_type::partial 1567 @li @c net::cancellation_type::total 1568 1569 if they are also supported by the socket's @c async_write_some operation. 1570 */ 1571 template< 1572 class ConstBufferSequence, 1573 BOOST_BEAST_ASYNC_TPARAM2 WriteHandler = 1574 net::default_completion_token_t<Executor> 1575 > 1576 BOOST_BEAST_ASYNC_RESULT2(WriteHandler) 1577 async_write_some( 1578 ConstBufferSequence const& buffers, 1579 WriteHandler&& handler = 1580 net::default_completion_token_t<Executor>{}); 1581 }; 1582 1583 } // beast 1584 } // boost 1585 1586 #include <boost/beast/core/impl/basic_stream.hpp> 1587 1588 #endif
[ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |
![]() ![]() |