Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:29:35

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 be closed.
0120             An outstanding read operation must be pending, which will
0121             complete immediately the error @ref beast::error::timeout.
0122         */
0123         bool keep_alive_pings;
0124 
0125         /** Construct timeout settings with suggested values for a role.
0126 
0127             This constructs the timeout settings with a predefined set
0128             of values which varies depending on the desired role. The
0129             values are selected upon construction, regardless of the
0130             current or actual role in use on the stream.
0131 
0132             @par Example
0133             This statement sets the timeout settings of the stream to
0134             the suggested values for the server role:
0135             @code
0136             @endcode
0137 
0138             @param role The role of the websocket stream
0139             (@ref role_type::client or @ref role_type::server).
0140         */
0141         static
0142         timeout
0143         suggested(role_type role) noexcept
0144         {
0145             timeout opt{};
0146             switch(role)
0147             {
0148             case role_type::client:
0149                 opt.handshake_timeout = std::chrono::seconds(30);
0150                 opt.idle_timeout = none();
0151                 opt.keep_alive_pings = false;
0152                 break;
0153 
0154             case role_type::server:
0155                 opt.handshake_timeout = std::chrono::seconds(30);
0156                 opt.idle_timeout = std::chrono::seconds(300);
0157                 opt.keep_alive_pings = true;
0158                 break;
0159             }
0160             return opt;
0161         }
0162     };
0163 
0164 protected:
0165     enum class status
0166     {
0167         //none,
0168         handshake,
0169         open,
0170         closing,
0171         closed,
0172         failed // VFALCO Is this needed?
0173     };
0174 };
0175 
0176 } // websocket
0177 } // beast
0178 } // boost
0179 
0180 #endif