stream_base.hpp 5.3 KB

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