Imported Upstream version 1.72.0
[platform/upstream/boost.git] / libs / beast / doc / html / beast / ref / boost__beast__close_socket.html
1 <html>
2 <head>
3 <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
4 <title>close_socket</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&#160;1.&#160;Boost.Beast">
8 <link rel="up" href="../ref.html" title="This Page Intentionally Left Blank 2/2">
9 <link rel="prev" href="boost__beast__buffers_type.html" title="buffers_type">
10 <link rel="next" href="boost__beast__condition.html" title="condition">
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="boost__beast__buffers_type.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../ref.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="boost__beast__condition.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="beast.ref.boost__beast__close_socket"></a><a class="link" href="boost__beast__close_socket.html" title="close_socket">close_socket</a>
28 </h4></div></div></div>
29 <p>
30         <a class="indexterm" name="idm45873511416704"></a>
31 Close a socket or socket-like object.
32       </p>
33 <h5>
34 <a name="beast.ref.boost__beast__close_socket.h0"></a>
35         <span class="phrase"><a name="beast.ref.boost__beast__close_socket.synopsis"></a></span><a class="link" href="boost__beast__close_socket.html#beast.ref.boost__beast__close_socket.synopsis">Synopsis</a>
36       </h5>
37 <p>
38         Defined in header <code class="literal">&lt;<a href="../../../../../../boost/beast/core/stream_traits.hpp" target="_top">boost/beast/core/stream_traits.hpp</a>&gt;</code>
39       </p>
40 <pre class="programlisting"><span class="keyword">template</span><span class="special">&lt;</span>
41     <span class="keyword">class</span> <span class="identifier">Socket</span><span class="special">&gt;</span>
42 <span class="keyword">void</span>
43 <span class="identifier">close_socket</span><span class="special">(</span>
44     <span class="identifier">Socket</span><span class="special">&amp;</span> <span class="identifier">sock</span><span class="special">);</span>
45 </pre>
46 <h5>
47 <a name="beast.ref.boost__beast__close_socket.h1"></a>
48         <span class="phrase"><a name="beast.ref.boost__beast__close_socket.description"></a></span><a class="link" href="boost__beast__close_socket.html#beast.ref.boost__beast__close_socket.description">Description</a>
49       </h5>
50 <p>
51         This function attempts to close an object representing a socket. In this
52         context, a socket is an object for which an unqualified call to the function
53         <code class="computeroutput"><span class="keyword">void</span> <span class="identifier">beast_close_socket</span><span class="special">(</span><span class="identifier">Socket</span><span class="special">&amp;)</span></code> is well-defined. The function <code class="computeroutput"><span class="identifier">beast_close_socket</span></code> is a <span class="emphasis"><em>customization
54         point</em></span>, allowing user-defined types to provide an algorithm for
55         performing the close operation by overloading this function for the type
56         in question. Since the customization point is a function call, the normal
57         rules for finding the correct overload are applied including the rules for
58         argument-dependent lookup ("ADL"). This permits classes derived
59         from a type for which a customization is provided to inherit the customization
60         point. An overload for the networking class template <code class="computeroutput"><span class="identifier">net</span><span class="special">::</span><span class="identifier">basic_socket</span></code>
61         is provided, which implements the close algorithm for all socket-like objects
62         (hence the name of this customization point). When used in conjunction with
63         <a class="link" href="boost__beast__get_lowest_layer.html" title="get_lowest_layer"><code class="computeroutput"><span class="identifier">get_lowest_layer</span></code></a>,
64         a generic algorithm operating on a layered stream can perform a closure of
65         the underlying socket without knowing the exact list of concrete types.
66       </p>
67 <h5>
68 <a name="beast.ref.boost__beast__close_socket.h2"></a>
69         <span class="phrase"><a name="beast.ref.boost__beast__close_socket.example_1"></a></span><a class="link" href="boost__beast__close_socket.html#beast.ref.boost__beast__close_socket.example_1">Example
70         1</a>
71       </h5>
72 <p>
73         The following generic function synchronously sends a message on the stream,
74         then closes the socket.
75       </p>
76 <pre class="programlisting"><span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">WriteStream</span><span class="special">&gt;</span>
77 <span class="keyword">void</span> <span class="identifier">hello_and_close</span> <span class="special">(</span><span class="identifier">WriteStream</span><span class="special">&amp;</span> <span class="identifier">stream</span><span class="special">)</span>
78 <span class="special">{</span>
79     <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="string">"Hello, world!"</span><span class="special">,</span> <span class="number">13</span><span class="special">));</span>
80     <span class="identifier">close_socket</span><span class="special">(</span><span class="identifier">get_lowest_layer</span><span class="special">(</span><span class="identifier">stream</span><span class="special">));</span>
81 <span class="special">}</span>
82 </pre>
83 <p>
84         To enable closure of user defined types, it is necessary to provide an overload
85         of the function <code class="computeroutput"><span class="identifier">beast_close_socket</span></code>
86         for the type.
87       </p>
88 <h5>
89 <a name="beast.ref.boost__beast__close_socket.h3"></a>
90         <span class="phrase"><a name="beast.ref.boost__beast__close_socket.example_2"></a></span><a class="link" href="boost__beast__close_socket.html#beast.ref.boost__beast__close_socket.example_2">Example
91         2</a>
92       </h5>
93 <p>
94         The following code declares a user-defined type which contains a private
95         socket, and provides an overload of the customization point which closes
96         the private socket.
97       </p>
98 <pre class="programlisting"><span class="keyword">class</span> <span class="identifier">my_socket</span>
99 <span class="special">{</span>
100     <span class="identifier">net</span><span class="special">::</span><span class="identifier">ip</span><span class="special">::</span><span class="identifier">tcp</span><span class="special">::</span><span class="identifier">socket</span> <span class="identifier">sock_</span><span class="special">;</span>
101
102 <span class="keyword">public</span><span class="special">:</span>
103     <span class="identifier">my_socket</span><span class="special">(</span><span class="identifier">net</span><span class="special">::</span><span class="identifier">io_context</span><span class="special">&amp;</span> <span class="identifier">ioc</span><span class="special">)</span>
104         <span class="special">:</span> <span class="identifier">sock_</span><span class="special">(</span><span class="identifier">ioc</span><span class="special">)</span>
105     <span class="special">{</span>
106     <span class="special">}</span>
107
108     <span class="keyword">friend</span> <span class="keyword">void</span> <span class="identifier">beast_close_socket</span><span class="special">(</span><span class="identifier">my_socket</span><span class="special">&amp;</span> <span class="identifier">s</span><span class="special">)</span>
109     <span class="special">{</span>
110         <span class="identifier">error_code</span> <span class="identifier">ec</span><span class="special">;</span>
111         <span class="identifier">s</span><span class="special">.</span><span class="identifier">sock_</span><span class="special">.</span><span class="identifier">close</span><span class="special">(</span><span class="identifier">ec</span><span class="special">);</span>
112         <span class="comment">// ignore the error</span>
113     <span class="special">}</span>
114 <span class="special">};</span>
115 </pre>
116 <h5>
117 <a name="beast.ref.boost__beast__close_socket.h4"></a>
118         <span class="phrase"><a name="beast.ref.boost__beast__close_socket.parameters"></a></span><a class="link" href="boost__beast__close_socket.html#beast.ref.boost__beast__close_socket.parameters">Parameters</a>
119       </h5>
120 <div class="informaltable"><table class="table">
121 <colgroup>
122 <col>
123 <col>
124 </colgroup>
125 <thead><tr>
126 <th>
127                 <p>
128                   Name
129                 </p>
130               </th>
131 <th>
132                 <p>
133                   Description
134                 </p>
135               </th>
136 </tr></thead>
137 <tbody><tr>
138 <td>
139                 <p>
140                   <code class="computeroutput"><span class="identifier">sock</span></code>
141                 </p>
142               </td>
143 <td>
144                 <p>
145                   The socket to close. If the customization point is not defined
146                   for the type of this object, or one of its base classes, then a
147                   compiler error results.
148                 </p>
149               </td>
150 </tr></tbody>
151 </table></div>
152 <h5>
153 <a name="beast.ref.boost__beast__close_socket.h5"></a>
154         <span class="phrase"><a name="beast.ref.boost__beast__close_socket.see_also"></a></span><a class="link" href="boost__beast__close_socket.html#beast.ref.boost__beast__close_socket.see_also">See
155         Also</a>
156       </h5>
157 <p>
158         <a class="link" href="boost__beast__beast_close_socket.html" title="beast_close_socket"><code class="computeroutput"><span class="identifier">beast_close_socket</span></code></a>
159       </p>
160 <p>
161         Convenience header <code class="literal">&lt;<a href="../../../../../../boost/beast/core.hpp" target="_top">boost/beast/core.hpp</a>&gt;</code>
162       </p>
163 </div>
164 <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
165 <td align="left"></td>
166 <td align="right"><div class="copyright-footer">Copyright &#169; 2016-2019 Vinnie
167       Falco<p>
168         Distributed under the Boost Software License, Version 1.0. (See accompanying
169         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>)
170       </p>
171 </div></td>
172 </tr></table>
173 <hr>
174 <div class="spirit-nav">
175 <a accesskey="p" href="boost__beast__buffers_type.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../ref.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="boost__beast__condition.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>
176 </div>
177 </body>
178 </html>