1//
2// Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com)
3//
4// Distributed under the Boost Software License, Version 1.0. (See accompanying
5// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6//
7// Official repository: https://github.com/boostorg/beast
8//
9
10#ifndef BOOST_BEAST_WEBSOCKET_STREAM_BASE_HPP
11#define BOOST_BEAST_WEBSOCKET_STREAM_BASE_HPP
12
13#include <boost/beast/core/detail/config.hpp>
14#include <boost/beast/websocket/detail/decorator.hpp>
15#include <boost/beast/core/role.hpp>
16#include <chrono>
17#include <type_traits>
18
19namespace boost {
20namespace beast {
21namespace websocket {
22
23/** This class is used as a base for the @ref websocket::stream class template to group common types and constants.
24*/
25struct stream_base
26{
27 /// The type used to represent durations
28 using duration =
29 std::chrono::steady_clock::duration;
30
31 /// The type used to represent time points
32 using time_point =
33 std::chrono::steady_clock::time_point;
34
35 /// Returns the special time_point value meaning "never"
36 static
37 time_point
38 never() noexcept
39 {
40 return (time_point::max)();
41 }
42
43 /// Returns the special duration value meaning "none"
44 static
45 duration
46 none() noexcept
47 {
48 return (duration::max)();
49 }
50
51 /** Stream option used to adjust HTTP fields of WebSocket upgrade request and responses.
52 */
53 class decorator
54 {
55 detail::decorator d_;
56
57#ifndef BOOST_BEAST_DOXYGEN
58 template<class, bool>
59 friend class stream;
60#endif
61
62 public:
63 // Move Constructor
64 decorator(decorator&&) = default;
65
66 /** Construct a decorator option.
67
68 @param f An invocable function object. Ownership of
69 the function object is transferred by decay-copy.
70 */
71 template<class Decorator
72#ifndef BOOST_BEAST_DOXYGEN
73 ,class = typename std::enable_if<
74 detail::is_decorator<
75 Decorator>::value>::type
76#endif
77 >
78 explicit
79 decorator(Decorator&& f)
80 : d_(std::forward<Decorator>(f))
81 {
82 }
83 };
84
85 /** Stream option to control the behavior of websocket timeouts.
86
87 Timeout features are available for asynchronous operations only.
88 */
89 struct timeout
90 {
91 /** Time limit on handshake, accept, and close operations:
92
93 This value whether or not there is a time limit, and the
94 duration of that time limit, for asynchronous handshake,
95 accept, and close operations. If this is equal to the
96 value @ref none then there will be no time limit. Otherwise,
97 if any of the applicable operations takes longer than this
98 amount of time, the operation will be canceled and a
99 timeout error delivered to the completion handler.
100 */
101 duration handshake_timeout;
102
103 /** The time limit after which a connection is considered idle.
104 */
105 duration idle_timeout;
106
107 /** Automatic ping setting.
108
109 If the idle interval is set, this setting affects the
110 behavior of the stream when no data is received for the
111 timeout interval as follows:
112
113 @li When `keep_alive_pings` is `true`, an idle ping will be
114 sent automatically. If another timeout interval elapses
115 with no received data then the connection will be closed.
116 An outstanding read operation must be pending, which will
117 complete immediately the error @ref beast::error::timeout.
118
119 @li When `keep_alive_pings` is `false`, the connection will be closed.
120 An outstanding read operation must be pending, which will
121 complete immediately the error @ref beast::error::timeout.
122 */
123 bool keep_alive_pings;
124
125 /** Construct timeout settings with suggested values for a role.
126
127 This constructs the timeout settings with a predefined set
128 of values which varies depending on the desired role. The
129 values are selected upon construction, regardless of the
130 current or actual role in use on the stream.
131
132 @par Example
133 This statement sets the timeout settings of the stream to
134 the suggested values for the server role:
135 @code
136 @endcode
137
138 @param role The role of the websocket stream
139 (@ref role_type::client or @ref role_type::server).
140 */
141 static
142 timeout
143 suggested(role_type role) noexcept
144 {
145 timeout opt{};
146 switch(role)
147 {
148 case role_type::client:
149 opt.handshake_timeout = std::chrono::seconds(30);
150 opt.idle_timeout = none();
151 opt.keep_alive_pings = false;
152 break;
153
154 case role_type::server:
155 opt.handshake_timeout = std::chrono::seconds(30);
156 opt.idle_timeout = std::chrono::seconds(300);
157 opt.keep_alive_pings = true;
158 break;
159 }
160 return opt;
161 }
162 };
163
164protected:
165 enum class status
166 {
167 //none,
168 handshake,
169 open,
170 closing,
171 closed,
172 failed // VFALCO Is this needed?
173 };
174};
175
176} // websocket
177} // beast
178} // boost
179
180#endif
181

source code of boost/libs/beast/include/boost/beast/websocket/stream_base.hpp