1<html> 2<head> 3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 4<title>Serializer Stream Operations</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_http.html" title="HTTP"> 9<link rel="prev" href="message_stream_operations.html" title="Message Stream Operations"> 10<link rel="next" href="parser_stream_operations.html" title="Parser Stream Operations"> 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="message_stream_operations.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../using_http.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="parser_stream_operations.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_http.serializer_stream_operations"></a><a class="link" href="serializer_stream_operations.html" title="Serializer Stream Operations">Serializer 28 Stream Operations</a> 29</h3></div></div></div> 30<p> 31 Non-trivial algorithms need to do more than send entire messages at once, 32 such as: 33 </p> 34<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "> 35<li class="listitem"> 36 Send the header first, and the body later. 37 </li> 38<li class="listitem"> 39 Send a message incrementally: bounded work in each I/O cycle. 40 </li> 41<li class="listitem"> 42 Use a series of caller-provided buffers to represent the body. 43 </li> 44</ul></div> 45<p> 46 These tasks may be performed by using the serializer stream interfaces. To 47 use these interfaces, first construct an appropriate serializer from the 48 message to be sent: 49 </p> 50<div class="table"> 51<a name="beast.using_http.serializer_stream_operations.serializer"></a><p class="title"><b>Table 1.20. Serializer</b></p> 52<div class="table-contents"><table class="table" summary="Serializer"> 53<colgroup> 54<col> 55<col> 56</colgroup> 57<thead><tr> 58<th> 59 <p> 60 Name 61 </p> 62 </th> 63<th> 64 <p> 65 Description 66 </p> 67 </th> 68</tr></thead> 69<tbody> 70<tr> 71<td> 72 <p> 73 <a class="link" href="../ref/boost__beast__http__serializer.html" title="http::serializer"><code class="computeroutput"><span class="identifier">serializer</span></code></a> 74 </p> 75 </td> 76<td> 77<pre class="programlisting"><span class="comment">/// Provides buffer oriented HTTP message serialization functionality.</span> 78<span class="keyword">template</span><span class="special"><</span> 79 <span class="keyword">bool</span> <span class="identifier">isRequest</span><span class="special">,</span> 80 <span class="keyword">class</span> <span class="identifier">Body</span><span class="special">,</span> 81 <span class="keyword">class</span> <span class="identifier">Fields</span> <span class="special">=</span> <span class="identifier">fields</span> 82<span class="special">></span> 83<span class="keyword">class</span> <span class="identifier">serializer</span><span class="special">;</span> 84</pre> 85 </td> 86</tr> 87<tr> 88<td> 89 <p> 90 <a class="link" href="../ref/boost__beast__http__request_serializer.html" title="http::request_serializer"><code class="computeroutput"><span class="identifier">request_serializer</span></code></a> 91 </p> 92 </td> 93<td> 94<pre class="programlisting"><span class="comment">/// A serializer for HTTP/1 requests</span> 95<span class="keyword">template</span><span class="special"><</span> 96 <span class="keyword">class</span> <span class="identifier">Body</span><span class="special">,</span> 97 <span class="keyword">class</span> <span class="identifier">Fields</span> <span class="special">=</span> <span class="identifier">fields</span> 98<span class="special">></span> 99<span class="keyword">using</span> <span class="identifier">request_serializer</span> <span class="special">=</span> <span class="identifier">serializer</span><span class="special"><</span><span class="keyword">true</span><span class="special">,</span> <span class="identifier">Body</span><span class="special">,</span> <span class="identifier">Fields</span><span class="special">>;</span> 100</pre> 101 </td> 102</tr> 103<tr> 104<td> 105 <p> 106 <a class="link" href="../ref/boost__beast__http__response_serializer.html" title="http::response_serializer"><code class="computeroutput"><span class="identifier">response_serializer</span></code></a> 107 </p> 108 </td> 109<td> 110<pre class="programlisting"><span class="comment">/// A serializer for HTTP/1 responses</span> 111<span class="keyword">template</span><span class="special"><</span> 112 <span class="keyword">class</span> <span class="identifier">Body</span><span class="special">,</span> 113 <span class="keyword">class</span> <span class="identifier">Fields</span> <span class="special">=</span> <span class="identifier">fields</span> 114<span class="special">></span> 115<span class="keyword">using</span> <span class="identifier">response_serializer</span> <span class="special">=</span> <span class="identifier">serializer</span><span class="special"><</span><span class="keyword">false</span><span class="special">,</span> <span class="identifier">Body</span><span class="special">,</span> <span class="identifier">Fields</span><span class="special">>;</span> 116</pre> 117 </td> 118</tr> 119</tbody> 120</table></div> 121</div> 122<br class="table-break"><p> 123 The choices for template types must match the message passed on construction. 124 This code creates an HTTP response and the corresponding serializer: 125 </p> 126<pre class="programlisting"><span class="identifier">response</span><span class="special"><</span><span class="identifier">string_body</span><span class="special">></span> <span class="identifier">res</span><span class="special">;</span> 127 128<span class="identifier">response_serializer</span><span class="special"><</span><span class="identifier">string_body</span><span class="special">></span> <span class="identifier">sr</span><span class="special">{</span><span class="identifier">res</span><span class="special">};</span> 129</pre> 130<p> 131 The stream operations which work on serializers are: 132 </p> 133<div class="table"> 134<a name="beast.using_http.serializer_stream_operations.serializer_stream_operations"></a><p class="title"><b>Table 1.21. Serializer Stream Operations</b></p> 135<div class="table-contents"><table class="table" summary="Serializer Stream Operations"> 136<colgroup> 137<col> 138<col> 139</colgroup> 140<thead><tr> 141<th> 142 <p> 143 Name 144 </p> 145 </th> 146<th> 147 <p> 148 Description 149 </p> 150 </th> 151</tr></thead> 152<tbody> 153<tr> 154<td> 155 <p> 156 <a class="link" href="../ref/boost__beast__http__write/overload1.html" title="http::write (1 of 6 overloads)"><span class="bold"><strong>write</strong></span></a> 157 </p> 158 </td> 159<td> 160 <p> 161 Send everything in a <a class="link" href="../ref/boost__beast__http__serializer.html" title="http::serializer"><code class="computeroutput"><span class="identifier">serializer</span></code></a> to a <a href="../../../../../../doc/html/boost_asio/reference/SyncWriteStream.html" target="_top"><span class="emphasis"><em>SyncWriteStream</em></span></a>. 162 </p> 163 </td> 164</tr> 165<tr> 166<td> 167 <p> 168 <a class="link" href="../ref/boost__beast__http__async_write/overload1.html" title="http::async_write (1 of 3 overloads)"><span class="bold"><strong>async_write</strong></span></a> 169 </p> 170 </td> 171<td> 172 <p> 173 Send everything in a <a class="link" href="../ref/boost__beast__http__serializer.html" title="http::serializer"><code class="computeroutput"><span class="identifier">serializer</span></code></a> asynchronously 174 to an <a href="../../../../../../doc/html/boost_asio/reference/AsyncWriteStream.html" target="_top"><span class="emphasis"><em>AsyncWriteStream</em></span></a>. 175 </p> 176 </td> 177</tr> 178<tr> 179<td> 180 <p> 181 <a class="link" href="../ref/boost__beast__http__write_header/overload1.html" title="http::write_header (1 of 2 overloads)"><span class="bold"><strong>write_header</strong></span></a> 182 </p> 183 </td> 184<td> 185 <p> 186 Send only the header from a <a class="link" href="../ref/boost__beast__http__serializer.html" title="http::serializer"><code class="computeroutput"><span class="identifier">serializer</span></code></a> to a <a href="../../../../../../doc/html/boost_asio/reference/SyncWriteStream.html" target="_top"><span class="emphasis"><em>SyncWriteStream</em></span></a>. 187 </p> 188 </td> 189</tr> 190<tr> 191<td> 192 <p> 193 <a class="link" href="../ref/boost__beast__http__async_write_header.html" title="http::async_write_header"><span class="bold"><strong>async_write_header</strong></span></a> 194 </p> 195 </td> 196<td> 197 <p> 198 Send only the header from a <a class="link" href="../ref/boost__beast__http__serializer.html" title="http::serializer"><code class="computeroutput"><span class="identifier">serializer</span></code></a> asynchronously 199 to an <a href="../../../../../../doc/html/boost_asio/reference/AsyncWriteStream.html" target="_top"><span class="emphasis"><em>AsyncWriteStream</em></span></a>. 200 </p> 201 </td> 202</tr> 203<tr> 204<td> 205 <p> 206 <a class="link" href="../ref/boost__beast__http__write_some/overload1.html" title="http::write_some (1 of 2 overloads)"><span class="bold"><strong>write_some</strong></span></a> 207 </p> 208 </td> 209<td> 210 <p> 211 Send part of a <a class="link" href="../ref/boost__beast__http__serializer.html" title="http::serializer"><code class="computeroutput"><span class="identifier">serializer</span></code></a> to a <a href="../../../../../../doc/html/boost_asio/reference/SyncWriteStream.html" target="_top"><span class="emphasis"><em>SyncWriteStream</em></span></a>. 212 </p> 213 </td> 214</tr> 215<tr> 216<td> 217 <p> 218 <a class="link" href="../ref/boost__beast__http__async_write_some.html" title="http::async_write_some"><span class="bold"><strong>async_write_some</strong></span></a> 219 </p> 220 </td> 221<td> 222 <p> 223 Send part of a <a class="link" href="../ref/boost__beast__http__serializer.html" title="http::serializer"><code class="computeroutput"><span class="identifier">serializer</span></code></a> asynchronously 224 to an <a href="../../../../../../doc/html/boost_asio/reference/AsyncWriteStream.html" target="_top"><span class="emphasis"><em>AsyncWriteStream</em></span></a>. 225 </p> 226 </td> 227</tr> 228</tbody> 229</table></div> 230</div> 231<br class="table-break"><p> 232 Here is an example of using a serializer to send a message on a stream synchronously. 233 This performs the same operation as calling <code class="computeroutput"><span class="identifier">write</span><span class="special">(</span><span class="identifier">stream</span><span class="special">,</span> <span class="identifier">m</span><span class="special">)</span></code>: 234 </p> 235<pre class="programlisting"><span class="comment">/** Send a message to a stream synchronously. 236 237 @param stream The stream to write to. This type must support 238 the <em>SyncWriteStream</em> concept. 239 240 @param m The message to send. The Body type must support 241 the <em>BodyWriter</em> concept. 242*/</span> 243<span class="keyword">template</span><span class="special"><</span> 244 <span class="keyword">class</span> <span class="identifier">SyncWriteStream</span><span class="special">,</span> 245 <span class="keyword">bool</span> <span class="identifier">isRequest</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">Body</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">Fields</span><span class="special">></span> 246<span class="keyword">void</span> 247<span class="identifier">send</span><span class="special">(</span> 248 <span class="identifier">SyncWriteStream</span><span class="special">&</span> <span class="identifier">stream</span><span class="special">,</span> 249 <span class="identifier">message</span><span class="special"><</span><span class="identifier">isRequest</span><span class="special">,</span> <span class="identifier">Body</span><span class="special">,</span> <span class="identifier">Fields</span><span class="special">></span> <span class="keyword">const</span><span class="special">&</span> <span class="identifier">m</span><span class="special">)</span> 250<span class="special">{</span> 251 <span class="comment">// Check the template types</span> 252 <span class="keyword">static_assert</span><span class="special">(</span><span class="identifier">is_sync_write_stream</span><span class="special"><</span><span class="identifier">SyncWriteStream</span><span class="special">>::</span><span class="identifier">value</span><span class="special">,</span> 253 <span class="string">"SyncWriteStream type requirements not met"</span><span class="special">);</span> 254 <span class="keyword">static_assert</span><span class="special">(</span><span class="identifier">is_body_writer</span><span class="special"><</span><span class="identifier">Body</span><span class="special">>::</span><span class="identifier">value</span><span class="special">,</span> 255 <span class="string">"BodyWriter type requirements not met"</span><span class="special">);</span> 256 257 <span class="comment">// Create the instance of serializer for the message</span> 258 <span class="identifier">serializer</span><span class="special"><</span><span class="identifier">isRequest</span><span class="special">,</span> <span class="identifier">Body</span><span class="special">,</span> <span class="identifier">Fields</span><span class="special">></span> <span class="identifier">sr</span><span class="special">{</span><span class="identifier">m</span><span class="special">};</span> 259 260 <span class="comment">// Loop until the serializer is finished</span> 261 <span class="keyword">do</span> 262 <span class="special">{</span> 263 <span class="comment">// This call guarantees it will make some</span> 264 <span class="comment">// forward progress, or otherwise return an error.</span> 265 <span class="identifier">write_some</span><span class="special">(</span><span class="identifier">stream</span><span class="special">,</span> <span class="identifier">sr</span><span class="special">);</span> 266 <span class="special">}</span> 267 <span class="keyword">while</span><span class="special">(!</span> <span class="identifier">sr</span><span class="special">.</span><span class="identifier">is_done</span><span class="special">());</span> 268<span class="special">}</span> 269</pre> 270</div> 271<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> 272<td align="left"></td> 273<td align="right"><div class="copyright-footer">Copyright © 2016-2019 Vinnie 274 Falco<p> 275 Distributed under the Boost Software License, Version 1.0. (See accompanying 276 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>) 277 </p> 278</div></td> 279</tr></table> 280<hr> 281<div class="spirit-nav"> 282<a accesskey="p" href="message_stream_operations.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../using_http.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="parser_stream_operations.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a> 283</div> 284</body> 285</html> 286