1<html> 2<head> 3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 4<title>async_read_until (3 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="overload2.html" title="async_read_until (2 of 12 overloads)"> 10<link rel="next" href="overload4.html" title="async_read_until (4 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="overload2.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="overload4.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.overload3"></a><a class="link" href="overload3.html" title="async_read_until (3 of 12 overloads)">async_read_until 28 (3 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 some part of its data matches a regular expression. 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 <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>> 38<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( 39 AsyncReadStream & s, 40 DynamicBuffer_v1 && buffers, 41 const boost::regex & expr, 42 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>, 43 typename enable_if< is_dynamic_buffer_v1< typename decay< DynamicBuffer_v1 >::type >::value &&!is_dynamic_buffer_v2< typename decay< DynamicBuffer_v1 >::type >::value >::type * = 0); 44</pre> 45<p> 46 This function is used to asynchronously read data into the specified dynamic 47 buffer sequence until the dynamic buffer sequence's get area contains some 48 data that matches a regular expression. The function call always returns 49 immediately. The asynchronous operation will continue until one of the 50 following conditions is true: 51 </p> 52<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "> 53<li class="listitem"> 54 A substring of the dynamic buffer sequence's get area matches the regular 55 expression. 56 </li> 57<li class="listitem"> 58 An error occurred. 59 </li> 60</ul></div> 61<p> 62 This operation is implemented in terms of zero or more calls to the stream's 63 async_read_some function, and is known as a <span class="emphasis"><em>composed operation</em></span>. 64 If the dynamic buffer sequence's get area already contains data that matches 65 the regular expression, this asynchronous operation completes immediately. 66 The program must ensure that the stream performs no other read operations 67 (such as async_read, async_read_until, the stream's async_read_some function, 68 or any other composed operations that perform reads) until this operation 69 completes. 70 </p> 71<h6> 72<a name="boost_asio.reference.async_read_until.overload3.h0"></a> 73 <span class="phrase"><a name="boost_asio.reference.async_read_until.overload3.parameters"></a></span><a class="link" href="overload3.html#boost_asio.reference.async_read_until.overload3.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">expr</span></dt> 91<dd><p> 92 The regular expression. 93 </p></dd> 94<dt><span class="term">handler</span></dt> 95<dd> 96<p> 97 The handler to be called when the read operation completes. Copies 98 will be made of the handler as required. The function signature of 99 the handler must be: 100</p> 101<pre class="programlisting">void handler( 102 // Result of operation. 103 const boost::system::error_code& error, 104 105 // The number of bytes in the dynamic buffer 106 // sequence's get area up to and including the 107 // substring that matches the regular expression. 108 // 0 if an error occurred. 109 std::size_t bytes_transferred 110); 111</pre> 112<p> 113 Regardless of whether the asynchronous operation completes immediately 114 or not, the handler will not be invoked from within this function. 115 On immediate completion, invocation of the handler will be performed 116 in a manner equivalent to using <a class="link" href="../post.html" title="post"><code class="computeroutput">post</code></a>. 117 </p> 118</dd> 119</dl> 120</div> 121<h6> 122<a name="boost_asio.reference.async_read_until.overload3.h1"></a> 123 <span class="phrase"><a name="boost_asio.reference.async_read_until.overload3.remarks"></a></span><a class="link" href="overload3.html#boost_asio.reference.async_read_until.overload3.remarks">Remarks</a> 124 </h6> 125<p> 126 After a successful async_read_until operation, the dynamic buffer sequence 127 may contain additional data beyond that which matched the regular expression. 128 An application will typically leave that data in the dynamic buffer sequence 129 for a subsequent async_read_until operation to examine. 130 </p> 131<h6> 132<a name="boost_asio.reference.async_read_until.overload3.h2"></a> 133 <span class="phrase"><a name="boost_asio.reference.async_read_until.overload3.example"></a></span><a class="link" href="overload3.html#boost_asio.reference.async_read_until.overload3.example">Example</a> 134 </h6> 135<p> 136 To asynchronously read data into a <code class="computeroutput">std::string</code> until a CR-LF 137 sequence is encountered: 138 </p> 139<pre class="programlisting">std::string data; 140... 141void handler(const boost::system::error_code& e, std::size_t size) 142{ 143 if (!e) 144 { 145 std::string line = data.substr(0, n); 146 data.erase(0, n); 147 ... 148 } 149} 150... 151boost::asio::async_read_until(s, data, 152 boost::regex("\r\n"), handler); 153</pre> 154<p> 155 After the <code class="computeroutput">async_read_until</code> operation completes successfully, 156 the string <code class="computeroutput">data</code> contains the data which matched the regular 157 expression: 158 </p> 159<pre class="programlisting">{ 'a', 'b', ..., 'c', '\r', '\n', 'd', 'e', ... } 160</pre> 161<p> 162 The call to <code class="computeroutput">substr</code> then extracts the data up to and including 163 the match, so that the string <code class="computeroutput">line</code> contains: 164 </p> 165<pre class="programlisting">{ 'a', 'b', ..., 'c', '\r', '\n' } 166</pre> 167<p> 168 After the call to <code class="computeroutput">erase</code>, the remaining data is left in the 169 string <code class="computeroutput">data</code> as follows: 170 </p> 171<pre class="programlisting">{ 'd', 'e', ... } 172</pre> 173<p> 174 This data may be the start of a new line, to be extracted by a subsequent 175 <code class="computeroutput">async_read_until</code> operation. 176 </p> 177</div> 178<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> 179<td align="left"></td> 180<td align="right"><div class="copyright-footer">Copyright © 2003-2020 Christopher M. 181 Kohlhoff<p> 182 Distributed under the Boost Software License, Version 1.0. (See accompanying 183 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>) 184 </p> 185</div></td> 186</tr></table> 187<hr> 188<div class="spirit-nav"> 189<a accesskey="p" href="overload2.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="overload4.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a> 190</div> 191</body> 192</html> 193