• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<html>
2<head>
3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
4<title>Streams</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="../../index.html" title="Chapter 1. Boost.Beast">
8<link rel="up" href="../using_io.html" title="Networking">
9<link rel="prev" href="asio_refresher.html" title="Refresher">
10<link rel="next" href="timeouts.html" title="Timeouts ��">
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="asio_refresher.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../using_io.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="timeouts.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>
24</div>
25<div class="section">
26<div class="titlepage"><div><div><h3 class="title">
27<a name="beast.using_io.stream_types"></a><a class="link" href="stream_types.html" title="Streams">Streams</a>
28</h3></div></div></div>
29<p>
30        A <a class="link" href="../concepts/streams.html" title="Streams"><span class="emphasis"><em>Stream</em></span></a>
31        is a communication channel where data is reliably transferred as an ordered
32        sequence of bytes. Streams are either synchronous or asynchronous, and may
33        allow reading, writing, or both. Note that a particular type may model more
34        than one concept. For example, the networking types <a href="../../../../../../doc/html/boost_asio/reference/ip__tcp/socket.html" target="_top"><code class="computeroutput"><span class="identifier">tcp</span><span class="special">::</span><span class="identifier">socket</span></code></a>
35        and <a href="../../../../../../doc/html/boost_asio/reference/ssl__stream.html" target="_top"><code class="computeroutput"><span class="identifier">net</span><span class="special">::</span><span class="identifier">ssl</span><span class="special">::</span><span class="identifier">stream</span></code></a>
36        support both <a class="link" href="../concepts/streams.html#beast.concepts.streams.SyncStream"><span class="emphasis"><em>SyncStream</em></span></a>
37        and <a class="link" href="../concepts/streams.html#beast.concepts.streams.AsyncStream"><span class="emphasis"><em>AsyncStream</em></span></a>.
38        All stream algorithms in Beast are declared as template functions using these
39        concepts:
40      </p>
41<div class="table">
42<a name="beast.using_io.stream_types.stream_concepts"></a><p class="title"><b>Table 1.4. Stream Concepts</b></p>
43<div class="table-contents"><table class="table" summary="Stream Concepts">
44<colgroup>
45<col>
46<col>
47</colgroup>
48<thead><tr>
49<th>
50                <p>
51                  Concept
52                </p>
53              </th>
54<th>
55                <p>
56                  Description
57                </p>
58              </th>
59</tr></thead>
60<tbody>
61<tr>
62<td>
63                <p>
64                  <a href="../../../../../../doc/html/boost_asio/reference/SyncReadStream.html" target="_top"><span class="emphasis"><em>SyncReadStream</em></span></a>
65                </p>
66              </td>
67<td>
68                <p>
69                  Supports buffer-oriented blocking reads.
70                </p>
71              </td>
72</tr>
73<tr>
74<td>
75                <p>
76                  <a href="../../../../../../doc/html/boost_asio/reference/SyncWriteStream.html" target="_top"><span class="emphasis"><em>SyncWriteStream</em></span></a>
77                </p>
78              </td>
79<td>
80                <p>
81                  Supports buffer-oriented blocking writes.
82                </p>
83              </td>
84</tr>
85<tr>
86<td>
87                <p>
88                  <a class="link" href="../concepts/streams.html#beast.concepts.streams.SyncStream"><span class="emphasis"><em>SyncStream</em></span></a>
89                </p>
90              </td>
91<td>
92                <p>
93                  A stream supporting buffer-oriented blocking reads and writes.
94                </p>
95              </td>
96</tr>
97<tr>
98<td>
99                <p>
100                  <a href="../../../../../../doc/html/boost_asio/reference/AsyncReadStream.html" target="_top"><span class="emphasis"><em>AsyncReadStream</em></span></a>
101                </p>
102              </td>
103<td>
104                <p>
105                  Supports buffer-oriented asynchronous reads.
106                </p>
107              </td>
108</tr>
109<tr>
110<td>
111                <p>
112                  <a href="../../../../../../doc/html/boost_asio/reference/AsyncWriteStream.html" target="_top"><span class="emphasis"><em>AsyncWriteStream</em></span></a>
113                </p>
114              </td>
115<td>
116                <p>
117                  Supports buffer-oriented asynchronous writes.
118                </p>
119              </td>
120</tr>
121<tr>
122<td>
123                <p>
124                  <a class="link" href="../concepts/streams.html#beast.concepts.streams.AsyncStream"><span class="emphasis"><em>AsyncStream</em></span></a>
125                </p>
126              </td>
127<td>
128                <p>
129                  A stream supporting buffer-oriented asynchronous reads and writes.
130                </p>
131              </td>
132</tr>
133</tbody>
134</table></div>
135</div>
136<br class="table-break"><p>
137        These template metafunctions check whether a given type meets the requirements
138        for the various stream concepts, and some additional useful utilities. The
139        library uses these type checks internally and also provides them as public
140        interfaces so users may use the same techniques to augment their own code.
141        The use of these type checks helps provide more concise errors during compilation:
142      </p>
143<div class="table">
144<a name="beast.using_io.stream_types.type_traits_and_metafunctions"></a><p class="title"><b>Table 1.5. Type Traits and Metafunctions</b></p>
145<div class="table-contents"><table class="table" summary="Type Traits and Metafunctions">
146<colgroup>
147<col>
148<col>
149</colgroup>
150<thead><tr>
151<th>
152                <p>
153                  Name
154                </p>
155              </th>
156<th>
157                <p>
158                  Description
159                </p>
160              </th>
161</tr></thead>
162<tbody>
163<tr>
164<td>
165                <p>
166                  <a class="link" href="../ref/boost__beast__executor_type.html" title="executor_type"><code class="computeroutput"><span class="identifier">executor_type</span></code></a>
167                </p>
168              </td>
169<td>
170                <p>
171                  An alias for the type of object returned by <code class="computeroutput"><span class="identifier">get_executor</span></code>.
172                </p>
173              </td>
174</tr>
175<tr>
176<td>
177                <p>
178                  <a class="link" href="../ref/boost__beast__has_get_executor.html" title="has_get_executor"><code class="computeroutput"><span class="identifier">has_get_executor</span></code></a>
179                </p>
180              </td>
181<td>
182                <p>
183                  Determine if the <code class="computeroutput"><span class="identifier">get_executor</span></code>
184                  member function is present.
185                </p>
186              </td>
187</tr>
188<tr>
189<td>
190                <p>
191                  <a class="link" href="../ref/boost__beast__is_async_read_stream.html" title="is_async_read_stream"><code class="computeroutput"><span class="identifier">is_async_read_stream</span></code></a>
192                </p>
193              </td>
194<td>
195                <p>
196                  Determine if a type meets the requirements of <a href="../../../../../../doc/html/boost_asio/reference/AsyncReadStream.html" target="_top"><span class="emphasis"><em>AsyncReadStream</em></span></a>.
197                </p>
198              </td>
199</tr>
200<tr>
201<td>
202                <p>
203                  <a class="link" href="../ref/boost__beast__is_async_stream.html" title="is_async_stream"><code class="computeroutput"><span class="identifier">is_async_stream</span></code></a>
204                </p>
205              </td>
206<td>
207                <p>
208                  Determine if a type meets the requirements of both <a href="../../../../../../doc/html/boost_asio/reference/AsyncReadStream.html" target="_top"><span class="emphasis"><em>AsyncReadStream</em></span></a>
209                  and <a href="../../../../../../doc/html/boost_asio/reference/AsyncWriteStream.html" target="_top"><span class="emphasis"><em>AsyncWriteStream</em></span></a>.
210                </p>
211              </td>
212</tr>
213<tr>
214<td>
215                <p>
216                  <a class="link" href="../ref/boost__beast__is_async_write_stream.html" title="is_async_write_stream"><code class="computeroutput"><span class="identifier">is_async_write_stream</span></code></a>
217                </p>
218              </td>
219<td>
220                <p>
221                  Determine if a type meets the requirements of <a href="../../../../../../doc/html/boost_asio/reference/AsyncWriteStream.html" target="_top"><span class="emphasis"><em>AsyncWriteStream</em></span></a>.
222                </p>
223              </td>
224</tr>
225<tr>
226<td>
227                <p>
228                  <a class="link" href="../ref/boost__beast__is_sync_read_stream.html" title="is_sync_read_stream"><code class="computeroutput"><span class="identifier">is_sync_read_stream</span></code></a>
229                </p>
230              </td>
231<td>
232                <p>
233                  Determine if a type meets the requirements of <a href="../../../../../../doc/html/boost_asio/reference/SyncReadStream.html" target="_top"><span class="emphasis"><em>SyncReadStream</em></span></a>.
234                </p>
235              </td>
236</tr>
237<tr>
238<td>
239                <p>
240                  <a class="link" href="../ref/boost__beast__is_sync_stream.html" title="is_sync_stream"><code class="computeroutput"><span class="identifier">is_sync_stream</span></code></a>
241                </p>
242              </td>
243<td>
244                <p>
245                  Determine if a type meets the requirements of both <a href="../../../../../../doc/html/boost_asio/reference/SyncReadStream.html" target="_top"><span class="emphasis"><em>SyncReadStream</em></span></a>
246                  and <a href="../../../../../../doc/html/boost_asio/reference/SyncWriteStream.html" target="_top"><span class="emphasis"><em>SyncWriteStream</em></span></a>.
247                </p>
248              </td>
249</tr>
250<tr>
251<td>
252                <p>
253                  <a class="link" href="../ref/boost__beast__is_sync_write_stream.html" title="is_sync_write_stream"><code class="computeroutput"><span class="identifier">is_sync_write_stream</span></code></a>
254                </p>
255              </td>
256<td>
257                <p>
258                  Determine if a type meets the requirements of <a href="../../../../../../doc/html/boost_asio/reference/SyncWriteStream.html" target="_top"><span class="emphasis"><em>SyncWriteStream</em></span></a>.
259                </p>
260              </td>
261</tr>
262</tbody>
263</table></div>
264</div>
265<br class="table-break"><p>
266        Using the type checks with <code class="computeroutput"><span class="keyword">static_assert</span></code>
267        on function or class template types will provide users with helpful error
268        messages and prevent undefined behaviors. This example shows how a template
269        function which writes to a synchronous stream may check its argument:
270      </p>
271<pre class="programlisting"><span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">SyncWriteStream</span><span class="special">&gt;</span>
272<span class="keyword">void</span> <span class="identifier">write_string</span><span class="special">(</span><span class="identifier">SyncWriteStream</span><span class="special">&amp;</span> <span class="identifier">stream</span><span class="special">,</span> <span class="identifier">string_view</span> <span class="identifier">s</span><span class="special">)</span>
273<span class="special">{</span>
274    <span class="keyword">static_assert</span><span class="special">(</span><span class="identifier">is_sync_write_stream</span><span class="special">&lt;</span><span class="identifier">SyncWriteStream</span><span class="special">&gt;::</span><span class="identifier">value</span><span class="special">,</span>
275        <span class="string">"SyncWriteStream type requirements not met"</span><span class="special">);</span>
276    <span class="identifier">net</span><span class="special">::</span><span class="identifier">write</span><span class="special">(</span><span class="identifier">stream</span><span class="special">,</span> <span class="identifier">net</span><span class="special">::</span><span class="identifier">const_buffer</span><span class="special">(</span><span class="identifier">s</span><span class="special">.</span><span class="identifier">data</span><span class="special">(),</span> <span class="identifier">s</span><span class="special">.</span><span class="identifier">size</span><span class="special">()));</span>
277<span class="special">}</span>
278</pre>
279</div>
280<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
281<td align="left"></td>
282<td align="right"><div class="copyright-footer">Copyright © 2016-2019 Vinnie
283      Falco<p>
284        Distributed under the Boost Software License, Version 1.0. (See accompanying
285        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>)
286      </p>
287</div></td>
288</tr></table>
289<hr>
290<div class="spirit-nav">
291<a accesskey="p" href="asio_refresher.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../using_io.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="timeouts.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>
292</div>
293</body>
294</html>
295