stream_base.hpp 5.3 KB

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