3 <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
4 <title>async_read_until (1 of 4 overloads)</title>
5 <link rel="stylesheet" href="../../../../../doc/src/boostbook.css" type="text/css">
6 <meta name="generator" content="DocBook XSL Stylesheets V1.78.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="../async_read_until.html" title="async_read_until">
10 <link rel="next" href="overload2.html" title="async_read_until (2 of 4 overloads)">
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>
22 <div class="spirit-nav">
23 <a accesskey="p" href="../async_read_until.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="overload2.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a>
26 <div class="titlepage"><div><div><h4 class="title">
27 <a name="boost_asio.reference.async_read_until.overload1"></a><a class="link" href="overload1.html" title="async_read_until (1 of 4 overloads)">async_read_until
28 (1 of 4 overloads)</a>
29 </h4></div></div></div>
31 Start an asynchronous operation to read data into a streambuf until it
32 contains a specified delimiter.
34 <pre class="programlisting"><span class="keyword">template</span><span class="special"><</span>
35 <span class="keyword">typename</span> <a class="link" href="../AsyncReadStream.html" title="Buffer-oriented asynchronous read stream requirements">AsyncReadStream</a><span class="special">,</span>
36 <span class="keyword">typename</span> <span class="identifier">Allocator</span><span class="special">,</span>
37 <span class="keyword">typename</span> <a class="link" href="../ReadHandler.html" title="Read handler requirements">ReadHandler</a><span class="special">></span>
38 <a class="link" href="../asynchronous_operations.html#boost_asio.reference.asynchronous_operations.return_type_of_an_initiating_function"><span class="emphasis"><em>void-or-deduced</em></span></a> <span class="identifier">async_read_until</span><span class="special">(</span>
39 <span class="identifier">AsyncReadStream</span> <span class="special">&</span> <span class="identifier">s</span><span class="special">,</span>
40 <span class="identifier">boost</span><span class="special">::</span><span class="identifier">asio</span><span class="special">::</span><span class="identifier">basic_streambuf</span><span class="special"><</span> <span class="identifier">Allocator</span> <span class="special">></span> <span class="special">&</span> <span class="identifier">b</span><span class="special">,</span>
41 <span class="keyword">char</span> <span class="identifier">delim</span><span class="special">,</span>
42 <span class="identifier">ReadHandler</span> <span class="identifier">handler</span><span class="special">);</span>
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:
50 <div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
52 The get area of the streambuf contains the specified delimiter.
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.
68 <a name="boost_asio.reference.async_read_until.overload1.h0"></a>
69 <span class="phrase"><a name="boost_asio.reference.async_read_until.overload1.parameters"></a></span><a class="link" href="overload1.html#boost_asio.reference.async_read_until.overload1.parameters">Parameters</a>
71 <div class="variablelist">
72 <p class="title"><b></b></p>
73 <dl class="variablelist">
74 <dt><span class="term">s</span></dt>
76 The stream from which the data is to be read. The type must support
77 the AsyncReadStream concept.
79 <dt><span class="term">b</span></dt>
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.
85 <dt><span class="term">delim</span></dt>
87 The delimiter character.
89 <dt><span class="term">handler</span></dt>
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
96 <pre class="programlisting"><span class="keyword">void</span> <span class="identifier">handler</span><span class="special">(</span>
97 <span class="comment">// Result of operation.</span>
98 <span class="keyword">const</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">system</span><span class="special">::</span><span class="identifier">error_code</span><span class="special">&</span> <span class="identifier">error</span><span class="special">,</span>
100 <span class="comment">// The number of bytes in the streambuf's get</span>
101 <span class="comment">// area up to and including the delimiter.</span>
102 <span class="comment">// 0 if an error occurred.</span>
103 <span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span> <span class="identifier">bytes_transferred</span>
104 <span class="special">);</span>
107 Regardless of whether the asynchronous operation completes immediately
108 or not, the handler will not be invoked from within this function.
109 Invocation of the handler will be performed in a manner equivalent
110 to using <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">asio</span><span class="special">::</span><span class="identifier">io_service</span><span class="special">::</span><span class="identifier">post</span><span class="special">()</span></code>.
116 <a name="boost_asio.reference.async_read_until.overload1.h1"></a>
117 <span class="phrase"><a name="boost_asio.reference.async_read_until.overload1.remarks"></a></span><a class="link" href="overload1.html#boost_asio.reference.async_read_until.overload1.remarks">Remarks</a>
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
126 <a name="boost_asio.reference.async_read_until.overload1.h2"></a>
127 <span class="phrase"><a name="boost_asio.reference.async_read_until.overload1.example"></a></span><a class="link" href="overload1.html#boost_asio.reference.async_read_until.overload1.example">Example</a>
130 To asynchronously read data into a streambuf until a newline is encountered:
132 <pre class="programlisting"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">asio</span><span class="special">::</span><span class="identifier">streambuf</span> <span class="identifier">b</span><span class="special">;</span>
133 <span class="special">...</span>
134 <span class="keyword">void</span> <span class="identifier">handler</span><span class="special">(</span><span class="keyword">const</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">system</span><span class="special">::</span><span class="identifier">error_code</span><span class="special">&</span> <span class="identifier">e</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span> <span class="identifier">size</span><span class="special">)</span>
135 <span class="special">{</span>
136 <span class="keyword">if</span> <span class="special">(!</span><span class="identifier">e</span><span class="special">)</span>
137 <span class="special">{</span>
138 <span class="identifier">std</span><span class="special">::</span><span class="identifier">istream</span> <span class="identifier">is</span><span class="special">(&</span><span class="identifier">b</span><span class="special">);</span>
139 <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="identifier">line</span><span class="special">;</span>
140 <span class="identifier">std</span><span class="special">::</span><span class="identifier">getline</span><span class="special">(</span><span class="identifier">is</span><span class="special">,</span> <span class="identifier">line</span><span class="special">);</span>
141 <span class="special">...</span>
142 <span class="special">}</span>
143 <span class="special">}</span>
144 <span class="special">...</span>
145 <span class="identifier">boost</span><span class="special">::</span><span class="identifier">asio</span><span class="special">::</span><span class="identifier">async_read_until</span><span class="special">(</span><span class="identifier">s</span><span class="special">,</span> <span class="identifier">b</span><span class="special">,</span> <span class="char">'\n'</span><span class="special">,</span> <span class="identifier">handler</span><span class="special">);</span>
148 After the <code class="computeroutput"><span class="identifier">async_read_until</span></code>
149 operation completes successfully, the buffer <code class="computeroutput"><span class="identifier">b</span></code>
150 contains the delimiter:
152 <pre class="programlisting"><span class="special">{</span> <span class="char">'a'</span><span class="special">,</span> <span class="char">'b'</span><span class="special">,</span> <span class="special">...,</span> <span class="char">'c'</span><span class="special">,</span> <span class="char">'\n'</span><span class="special">,</span> <span class="char">'d'</span><span class="special">,</span> <span class="char">'e'</span><span class="special">,</span> <span class="special">...</span> <span class="special">}</span>
155 The call to <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">getline</span></code> then extracts the data up to
156 and including the delimiter, so that the string <code class="computeroutput"><span class="identifier">line</span></code>
159 <pre class="programlisting"><span class="special">{</span> <span class="char">'a'</span><span class="special">,</span> <span class="char">'b'</span><span class="special">,</span> <span class="special">...,</span> <span class="char">'c'</span><span class="special">,</span> <span class="char">'\n'</span> <span class="special">}</span>
162 The remaining data is left in the buffer <code class="computeroutput"><span class="identifier">b</span></code>
165 <pre class="programlisting"><span class="special">{</span> <span class="char">'d'</span><span class="special">,</span> <span class="char">'e'</span><span class="special">,</span> <span class="special">...</span> <span class="special">}</span>
168 This data may be the start of a new line, to be extracted by a subsequent
169 <code class="computeroutput"><span class="identifier">async_read_until</span></code> operation.
172 <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
173 <td align="left"></td>
174 <td align="right"><div class="copyright-footer">Copyright © 2003-2014 Christopher M. Kohlhoff<p>
175 Distributed under the Boost Software License, Version 1.0. (See accompanying
176 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>)
181 <div class="spirit-nav">
182 <a accesskey="p" href="../async_read_until.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="overload2.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a>