1<html> 2<head> 3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 4<title>async_read_until (4 of 12 overloads)</title> 5<link rel="stylesheet" href="../../../../../doc/src/boostbook.css" type="text/css"> 6<meta name="generator" content="DocBook XSL Stylesheets V1.79.1"> 7<link rel="home" href="../../../boost_asio.html" title="Boost.Asio"> 8<link rel="up" href="../async_read_until.html" title="async_read_until"> 9<link rel="prev" href="overload3.html" title="async_read_until (3 of 12 overloads)"> 10<link rel="next" href="overload5.html" title="async_read_until (5 of 12 overloads)"> 11</head> 12<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> 13<table cellpadding="2" width="100%"><tr> 14<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../boost.png"></td> 15<td align="center"><a href="../../../../../index.html">Home</a></td> 16<td align="center"><a href="../../../../../libs/libraries.htm">Libraries</a></td> 17<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td> 18<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td> 19<td align="center"><a href="../../../../../more/index.htm">More</a></td> 20</tr></table> 21<hr> 22<div class="spirit-nav"> 23<a accesskey="p" href="overload3.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../async_read_until.html"><img src="../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../boost_asio.html"><img src="../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="overload5.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a> 24</div> 25<div class="section"> 26<div class="titlepage"><div><div><h4 class="title"> 27<a name="boost_asio.reference.async_read_until.overload4"></a><a class="link" href="overload4.html" title="async_read_until (4 of 12 overloads)">async_read_until 28 (4 of 12 overloads)</a> 29</h4></div></div></div> 30<p> 31 Start an asynchronous operation to read data into a dynamic buffer sequence 32 until a function object indicates a match. 33 </p> 34<pre class="programlisting">template< 35 typename <a class="link" href="../AsyncReadStream.html" title="Buffer-oriented asynchronous read stream requirements">AsyncReadStream</a>, 36 typename <a class="link" href="../DynamicBuffer_v1.html" title="Dynamic buffer requirements (version 1)">DynamicBuffer_v1</a>, 37 typename MatchCondition, 38 typename <a class="link" href="../ReadHandler.html" title="Read handler requirements">ReadHandler</a> = <a class="link" href="../asynchronous_operations.html#boost_asio.reference.asynchronous_operations.default_completion_tokens"><span class="emphasis"><em>DEFAULT</em></span></a>> 39<a class="link" href="../asynchronous_operations.html#boost_asio.reference.asynchronous_operations.automatic_deduction_of_initiating_function_return_type"><span class="emphasis"><em>DEDUCED</em></span></a> async_read_until( 40 AsyncReadStream & s, 41 DynamicBuffer_v1 && buffers, 42 MatchCondition match_condition, 43 ReadHandler && handler = <a class="link" href="../asynchronous_operations.html#boost_asio.reference.asynchronous_operations.default_completion_tokens"><span class="emphasis"><em>DEFAULT</em></span></a>, 44 typename enable_if< is_match_condition< MatchCondition >::value &&is_dynamic_buffer_v1< typename decay< DynamicBuffer_v1 >::type >::value &&!is_dynamic_buffer_v2< typename decay< DynamicBuffer_v1 >::type >::value >::type * = 0); 45</pre> 46<p> 47 This function is used to asynchronously read data into the specified dynamic 48 buffer sequence until a user-defined match condition function object, when 49 applied to the data contained in the dynamic buffer sequence, indicates 50 a successful match. The function call always returns immediately. The asynchronous 51 operation will continue until one of the following conditions is true: 52 </p> 53<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "> 54<li class="listitem"> 55 The match condition function object returns a std::pair where the second 56 element evaluates to true. 57 </li> 58<li class="listitem"> 59 An error occurred. 60 </li> 61</ul></div> 62<p> 63 This operation is implemented in terms of zero or more calls to the stream's 64 async_read_some function, and is known as a <span class="emphasis"><em>composed operation</em></span>. 65 If the match condition function object already indicates a match, this 66 asynchronous operation completes immediately. The program must ensure that 67 the stream performs no other read operations (such as async_read, async_read_until, 68 the stream's async_read_some function, or any other composed operations 69 that perform reads) until this operation completes. 70 </p> 71<h6> 72<a name="boost_asio.reference.async_read_until.overload4.h0"></a> 73 <span class="phrase"><a name="boost_asio.reference.async_read_until.overload4.parameters"></a></span><a class="link" href="overload4.html#boost_asio.reference.async_read_until.overload4.parameters">Parameters</a> 74 </h6> 75<div class="variablelist"> 76<p class="title"><b></b></p> 77<dl class="variablelist"> 78<dt><span class="term">s</span></dt> 79<dd><p> 80 The stream from which the data is to be read. The type must support 81 the AsyncReadStream concept. 82 </p></dd> 83<dt><span class="term">buffers</span></dt> 84<dd><p> 85 The dynamic buffer sequence into which the data will be read. Although 86 the buffers object may be copied as necessary, ownership of the underlying 87 memory blocks is retained by the caller, which must guarantee that 88 they remain valid until the handler is called. 89 </p></dd> 90<dt><span class="term">match_condition</span></dt> 91<dd> 92<p> 93 The function object to be called to determine whether a match exists. 94 The signature of the function object must be: 95</p> 96<pre class="programlisting">pair<iterator, bool> match_condition(iterator begin, iterator end); 97</pre> 98<p> 99 where <code class="computeroutput">iterator</code> represents the type: 100</p> 101<pre class="programlisting">buffers_iterator<typename DynamicBuffer_v1::const_buffers_type> 102</pre> 103<p> 104 The iterator parameters <code class="computeroutput">begin</code> and <code class="computeroutput">end</code> define 105 the range of bytes to be scanned to determine whether there is a 106 match. The <code class="computeroutput">first</code> member of the return value is an iterator 107 marking one-past-the-end of the bytes that have been consumed by 108 the match function. This iterator is used to calculate the <code class="computeroutput">begin</code> 109 parameter for any subsequent invocation of the match condition. The 110 <code class="computeroutput">second</code> member of the return value is true if a match 111 has been found, false otherwise. 112 </p> 113</dd> 114<dt><span class="term">handler</span></dt> 115<dd> 116<p> 117 The handler to be called when the read operation completes. Copies 118 will be made of the handler as required. The function signature of 119 the handler must be: 120</p> 121<pre class="programlisting">void handler( 122 // Result of operation. 123 const boost::system::error_code& error, 124 125 // The number of bytes in the dynamic buffer sequence's 126 // get area that have been fully consumed by the match 127 // function. O if an error occurred. 128 std::size_t bytes_transferred 129); 130</pre> 131<p> 132 Regardless of whether the asynchronous operation completes immediately 133 or not, the handler will not be invoked from within this function. 134 On immediate completion, invocation of the handler will be performed 135 in a manner equivalent to using <a class="link" href="../post.html" title="post"><code class="computeroutput">post</code></a>. 136 </p> 137</dd> 138</dl> 139</div> 140<h6> 141<a name="boost_asio.reference.async_read_until.overload4.h1"></a> 142 <span class="phrase"><a name="boost_asio.reference.async_read_until.overload4.remarks"></a></span><a class="link" href="overload4.html#boost_asio.reference.async_read_until.overload4.remarks">Remarks</a> 143 </h6> 144<p> 145 After a successful async_read_until operation, the dynamic buffer sequence 146 may contain additional data beyond that which matched the function object. 147 An application will typically leave that data in the dynamic buffer sequence 148 for a subsequent async_read_until operation to examine. 149 </p> 150<p> 151 The default implementation of the <code class="computeroutput">is_match_condition</code> type 152 trait evaluates to true for function pointers and function objects with 153 a <code class="computeroutput">result_type</code> typedef. It must be specialised for other user-defined 154 function objects. 155 </p> 156<h6> 157<a name="boost_asio.reference.async_read_until.overload4.h2"></a> 158 <span class="phrase"><a name="boost_asio.reference.async_read_until.overload4.examples"></a></span><a class="link" href="overload4.html#boost_asio.reference.async_read_until.overload4.examples">Examples</a> 159 </h6> 160<p> 161 To asynchronously read data into a <code class="computeroutput">std::string</code> until whitespace 162 is encountered: 163 </p> 164<pre class="programlisting">typedef boost::asio::buffers_iterator< 165 boost::asio::const_buffers_1> iterator; 166 167std::pair<iterator, bool> 168match_whitespace(iterator begin, iterator end) 169{ 170 iterator i = begin; 171 while (i != end) 172 if (std::isspace(*i++)) 173 return std::make_pair(i, true); 174 return std::make_pair(i, false); 175} 176... 177void handler(const boost::system::error_code& e, std::size_t size); 178... 179std::string data; 180boost::asio::async_read_until(s, data, match_whitespace, handler); 181</pre> 182<p> 183 To asynchronously read data into a <code class="computeroutput">std::string</code> until a matching 184 character is found: 185 </p> 186<pre class="programlisting">class match_char 187{ 188public: 189 explicit match_char(char c) : c_(c) {} 190 191 template <typename Iterator> 192 std::pair<Iterator, bool> operator()( 193 Iterator begin, Iterator end) const 194 { 195 Iterator i = begin; 196 while (i != end) 197 if (c_ == *i++) 198 return std::make_pair(i, true); 199 return std::make_pair(i, false); 200 } 201 202private: 203 char c_; 204}; 205 206namespace asio { 207 template <> struct is_match_condition<match_char> 208 : public boost::true_type {}; 209} // namespace asio 210... 211void handler(const boost::system::error_code& e, std::size_t size); 212... 213std::string data; 214boost::asio::async_read_until(s, data, match_char('a'), handler); 215</pre> 216</div> 217<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> 218<td align="left"></td> 219<td align="right"><div class="copyright-footer">Copyright © 2003-2020 Christopher M. 220 Kohlhoff<p> 221 Distributed under the Boost Software License, Version 1.0. (See accompanying 222 file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>) 223 </p> 224</div></td> 225</tr></table> 226<hr> 227<div class="spirit-nav"> 228<a accesskey="p" href="overload3.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../async_read_until.html"><img src="../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../boost_asio.html"><img src="../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="overload5.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a> 229</div> 230</body> 231</html> 232