1<html> 2<head> 3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 4<title>Teardown</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_websocket.html" title="WebSocket"> 9<link rel="prev" href="timeouts.html" title="Timeouts"> 10<link rel="next" href="notes.html" title="Notes"> 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="timeouts.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../using_websocket.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="notes.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_websocket.teardown"></a><a class="link" href="teardown.html" title="Teardown">Teardown</a> 28</h3></div></div></div> 29<p> 30 The WebSocket protocol requirements described in rfc6455 section 7.1.1 outline 31 an operation described as <a href="https://tools.ietf.org/html/rfc6455#section-7.1.1" target="_top"><span class="emphasis"><em>Close 32 the WebSocket Connection</em></span></a>. This operation cleanly discards 33 bytes remaining at receiving endpoints and also closes the underlying TCP/IP 34 connection. Orderly shutdowns are always preferred; for TLS or SSL streams, 35 a protocol-level shutdown is desired. This presents a small issue for the 36 <a class="link" href="../ref/boost__beast__websocket__stream.html" title="websocket::stream"><code class="computeroutput"><span class="identifier">stream</span></code></a> 37 implementation: the stream's <code class="computeroutput"><span class="identifier">NextLayer</span></code> 38 template type requires only <a class="link" href="../concepts/streams.html#beast.concepts.streams.SyncStream"><span class="emphasis"><em>SyncStream</em></span></a> 39 or <a class="link" href="../concepts/streams.html#beast.concepts.streams.AsyncStream"><span class="emphasis"><em>AsyncStream</em></span></a>, 40 but those concepts do not support the operations to shut down the connection. 41 </p> 42<p> 43 To enable the implementation to perform the shutdown components of the close 44 operation, the library exposes two customization points expressed as free 45 functions associated with the next layer type: 46 </p> 47<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "> 48<li class="listitem"> 49 <a class="link" href="../ref/boost__beast__websocket__teardown.html" title="websocket::teardown"><code class="computeroutput"><span class="identifier">teardown</span></code></a>: Overloads of this 50 function drain and shut down a stream synchronously. 51 </li> 52<li class="listitem"> 53 <a class="link" href="../ref/boost__beast__websocket__teardown.html" title="websocket::teardown"><code class="computeroutput"><span class="identifier">async_teardown</span></code></a>: Overloads of 54 this function drain and shut down a stream asynchronously. 55 </li> 56</ul></div> 57<p> 58 The implementation provides suitable overloads of the teardown customization 59 points when websocket streams are instantiated using the Asio 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> 60 or <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> 61 for the next layer. In this case no user action is required. However, when 62 the websocket stream is instantiated for a user-defined type, compile errors 63 will result if the customization points are not provided for the user defined 64 type. Furthermore, user-defined types that wrap one of the Asio objects mentioned 65 earlier may wish to invoke a teardown customization point for the wrapped 66 object. This is how those tasks are accomplished. 67 </p> 68<h5> 69<a name="beast.using_websocket.teardown.h0"></a> 70 <span class="phrase"><a name="beast.using_websocket.teardown.user_defined_teardown"></a></span><a class="link" href="teardown.html#beast.using_websocket.teardown.user_defined_teardown">User-defined 71 Teardown</a> 72 </h5> 73<p> 74 To provide overloads of teardown for a user-defined type, simply declare 75 the two free functions with the correct signature, accepting a reference 76 to the user-defined type as the stream parameter: 77 </p> 78<pre class="programlisting"><span class="keyword">struct</span> <span class="identifier">custom_stream</span><span class="special">;</span> 79 80<span class="keyword">void</span> 81<span class="identifier">teardown</span><span class="special">(</span> 82 <span class="identifier">role_type</span> <span class="identifier">role</span><span class="special">,</span> 83 <span class="identifier">custom_stream</span><span class="special">&</span> <span class="identifier">stream</span><span class="special">,</span> 84 <span class="identifier">error_code</span><span class="special">&</span> <span class="identifier">ec</span><span class="special">);</span> 85 86<span class="keyword">template</span><span class="special"><</span><span class="keyword">class</span> <span class="identifier">TeardownHandler</span><span class="special">></span> 87<span class="keyword">void</span> 88<span class="identifier">async_teardown</span><span class="special">(</span> 89 <span class="identifier">role_type</span> <span class="identifier">role</span><span class="special">,</span> 90 <span class="identifier">custom_stream</span><span class="special">&</span> <span class="identifier">stream</span><span class="special">,</span> 91 <span class="identifier">TeardownHandler</span><span class="special">&&</span> <span class="identifier">handler</span><span class="special">);</span> 92</pre> 93<p> 94 When the implementation invokes the asynchronous teardown function, it always 95 uses an invokable completion handler. It is not necessary to specify the 96 return type customization when creating user-defined overloads of <code class="computeroutput"><span class="identifier">async_teardown</span></code>. 97 </p> 98<h5> 99<a name="beast.using_websocket.teardown.h1"></a> 100 <span class="phrase"><a name="beast.using_websocket.teardown.invoking_teardown"></a></span><a class="link" href="teardown.html#beast.using_websocket.teardown.invoking_teardown">Invoking 101 Teardown</a> 102 </h5> 103<p> 104 To invoke the customization point, first bring the default implementation 105 into scope with a <code class="computeroutput"><span class="keyword">using</span></code> statement. 106 Then call the customization point without namespace qualification, allowing 107 argument-dependent lookup to take effect: 108 </p> 109<pre class="programlisting"><span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">NextLayer</span><span class="special">></span> 110<span class="keyword">struct</span> <span class="identifier">custom_wrapper</span> 111<span class="special">{</span> 112 <span class="identifier">NextLayer</span> <span class="identifier">next_layer</span><span class="special">;</span> 113 114 <span class="keyword">template</span><span class="special"><</span><span class="keyword">class</span><span class="special">...</span> <span class="identifier">Args</span><span class="special">></span> 115 <span class="keyword">explicit</span> 116 <span class="identifier">custom_wrapper</span><span class="special">(</span><span class="identifier">Args</span><span class="special">&&...</span> <span class="identifier">args</span><span class="special">)</span> 117 <span class="special">:</span> <span class="identifier">next_layer</span><span class="special">(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">forward</span><span class="special"><</span><span class="identifier">Args</span><span class="special">>(</span><span class="identifier">args</span><span class="special">)...)</span> 118 <span class="special">{</span> 119 <span class="special">}</span> 120 121 <span class="keyword">friend</span> 122 <span class="keyword">void</span> 123 <span class="identifier">teardown</span><span class="special">(</span> 124 <span class="identifier">role_type</span> <span class="identifier">role</span><span class="special">,</span> 125 <span class="identifier">custom_wrapper</span><span class="special">&</span> <span class="identifier">stream</span><span class="special">,</span> 126 <span class="identifier">error_code</span><span class="special">&</span> <span class="identifier">ec</span><span class="special">)</span> 127 <span class="special">{</span> 128 <span class="keyword">using</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">beast</span><span class="special">::</span><span class="identifier">websocket</span><span class="special">::</span><span class="identifier">teardown</span><span class="special">;</span> 129 <span class="identifier">teardown</span><span class="special">(</span><span class="identifier">role</span><span class="special">,</span> <span class="identifier">stream</span><span class="special">.</span><span class="identifier">next_layer</span><span class="special">,</span> <span class="identifier">ec</span><span class="special">);</span> 130 <span class="special">}</span> 131 132 <span class="keyword">template</span><span class="special"><</span><span class="keyword">class</span> <span class="identifier">TeardownHandler</span><span class="special">></span> 133 <span class="keyword">friend</span> 134 <span class="keyword">void</span> 135 <span class="identifier">async_teardown</span><span class="special">(</span> 136 <span class="identifier">role_type</span> <span class="identifier">role</span><span class="special">,</span> 137 <span class="identifier">custom_wrapper</span><span class="special">&</span> <span class="identifier">stream</span><span class="special">,</span> 138 <span class="identifier">TeardownHandler</span><span class="special">&&</span> <span class="identifier">handler</span><span class="special">)</span> 139 <span class="special">{</span> 140 <span class="keyword">using</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">beast</span><span class="special">::</span><span class="identifier">websocket</span><span class="special">::</span><span class="identifier">async_teardown</span><span class="special">;</span> 141 <span class="identifier">async_teardown</span><span class="special">(</span><span class="identifier">role</span><span class="special">,</span> <span class="identifier">stream</span><span class="special">.</span><span class="identifier">next_layer</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">forward</span><span class="special"><</span><span class="identifier">TeardownHandler</span><span class="special">>(</span><span class="identifier">handler</span><span class="special">));</span> 142 <span class="special">}</span> 143<span class="special">};</span> 144</pre> 145</div> 146<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> 147<td align="left"></td> 148<td align="right"><div class="copyright-footer">Copyright © 2016-2019 Vinnie 149 Falco<p> 150 Distributed under the Boost Software License, Version 1.0. (See accompanying 151 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>) 152 </p> 153</div></td> 154</tr></table> 155<hr> 156<div class="spirit-nav"> 157<a accesskey="p" href="timeouts.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../using_websocket.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="notes.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a> 158</div> 159</body> 160</html> 161