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