Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-10-20 08:18:43

0001 //
0002 // Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com)
0003 //
0004 // Distributed under the Boost Software License, Version 1.0. (See accompanying
0005 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0006 //
0007 // Official repository: https://github.com/boostorg/beast
0008 //
0009 
0010 #ifndef BOOST_BEAST_WEBSOCKET_STREAM_BASE_HPP
0011 #define BOOST_BEAST_WEBSOCKET_STREAM_BASE_HPP
0012 
0013 #include <boost/beast/core/detail/config.hpp>
0014 #include <boost/beast/websocket/detail/decorator.hpp>
0015 #include <boost/beast/core/role.hpp>
0016 #include <chrono>
0017 #include <type_traits>
0018 
0019 namespace boost {
0020 namespace beast {
0021 namespace websocket {
0022 
0023 /** This class is used as a base for the @ref websocket::stream class template to group common types and constants.
0024 */
0025 struct stream_base
0026 {
0027     /// The type used to represent durations
0028     using duration =
0029         std::chrono::steady_clock::duration;
0030 
0031     /// The type used to represent time points
0032     using time_point =
0033         std::chrono::steady_clock::time_point;
0034 
0035     /// Returns the special time_point value meaning "never"
0036     static
0037     time_point
0038     never() noexcept
0039     {
0040         return (time_point::max)();
0041     }
0042 
0043     /// Returns the special duration value meaning "none"
0044     static
0045     duration
0046     none() noexcept
0047     {
0048         return (duration::max)();
0049     }
0050 
0051     /** Stream option used to adjust HTTP fields of WebSocket upgrade request and responses.
0052     */
0053     class decorator
0054     {
0055         detail::decorator d_;
0056 
0057 #ifndef BOOST_BEAST_DOXYGEN
0058         template<class, bool>
0059         friend class stream;
0060 #endif
0061 
0062     public:
0063         // Move Constructor
0064         decorator(decorator&&) = default;
0065 
0066         /** Construct a decorator option.
0067             
0068             @param f An invocable function object. Ownership of
0069             the function object is transferred by decay-copy.
0070         */
0071         template<class Decorator
0072 #ifndef BOOST_BEAST_DOXYGEN
0073             ,class = typename std::enable_if<
0074                 detail::is_decorator<
0075                     Decorator>::value>::type
0076 #endif
0077         >
0078         explicit
0079         decorator(Decorator&& f)
0080             : d_(std::forward<Decorator>(f))
0081         {
0082         }
0083     };
0084 
0085     /** Stream option to control the behavior of websocket timeouts.
0086 
0087         Timeout features are available for asynchronous operations only.
0088     */
0089     struct timeout
0090     {
0091         /** Time limit on handshake, accept, and close operations:
0092 
0093             This value whether or not there is a time limit, and the
0094             duration of that time limit, for asynchronous handshake,
0095             accept, and close operations. If this is equal to the
0096             value @ref none then there will be no time limit. Otherwise,
0097             if any of the applicable operations takes longer than this
0098             amount of time, the operation will be canceled and a
0099             timeout error delivered to the completion handler.
0100         */
0101         duration handshake_timeout;
0102 
0103         /** The time limit after which a connection is considered idle.
0104         */
0105         duration idle_timeout;
0106 
0107         /** Automatic ping setting.
0108 
0109             If the idle interval is set, this setting affects the
0110             behavior of the stream when no data is received for the
0111             timeout interval as follows:
0112 
0113             @li When `keep_alive_pings` is `true`, an idle ping will be
0114             sent automatically. If another timeout interval elapses
0115             with no received data then the connection will be closed.
0116             An outstanding read operation must be pending, which will
0117             complete immediately the error @ref beast::error::timeout.
0118 
0119             @li When `keep_alive_pings` is `false`, the connection will
0120             be closed if there has been no activity. Both websocket
0121             message frames and control frames count as activity. An
0122             outstanding read operation must be pending, which will
0123             complete immediately the error @ref beast::error::timeout.
0124         */
0125         bool keep_alive_pings;
0126 
0127         /** Construct timeout settings with suggested values for a role.
0128 
0129             This constructs the timeout settings with a predefined set
0130             of values which varies depending on the desired role. The
0131             values are selected upon construction, regardless of the
0132             current or actual role in use on the stream.
0133 
0134             @par Example
0135             This statement sets the timeout settings of the stream to
0136             the suggested values for the server role:
0137             @code
0138             @endcode
0139 
0140             @param role The role of the websocket stream
0141             (@ref role_type::client or @ref role_type::server).
0142         */
0143         static
0144         timeout
0145         suggested(role_type role) noexcept
0146         {
0147             timeout opt{};
0148             switch(role)
0149             {
0150             case role_type::client:
0151                 opt.handshake_timeout = std::chrono::seconds(30);
0152                 opt.idle_timeout = none();
0153                 opt.keep_alive_pings = false;
0154                 break;
0155 
0156             case role_type::server:
0157                 opt.handshake_timeout = std::chrono::seconds(30);
0158                 opt.idle_timeout = std::chrono::seconds(300);
0159                 opt.keep_alive_pings = true;
0160                 break;
0161             }
0162             return opt;
0163         }
0164     };
0165 
0166 protected:
0167     enum class status
0168     {
0169         //none,
0170         handshake,
0171         open,
0172         closing,
0173         closed,
0174         failed // VFALCO Is this needed?
0175     };
0176 };
0177 
0178 } // websocket
0179 } // beast
0180 } // boost
0181 
0182 #endif