prng.hpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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_DETAIL_PRNG_HPP
  10. #define BOOST_BEAST_WEBSOCKET_DETAIL_PRNG_HPP
  11. #include <boost/beast/core/detail/config.hpp>
  12. #include <cstdint>
  13. #include <limits>
  14. #include <random>
  15. namespace boost {
  16. namespace beast {
  17. namespace websocket {
  18. namespace detail {
  19. // Type-erased UniformRandomBitGenerator
  20. // with 32-bit unsigned integer value_type
  21. //
  22. class prng
  23. {
  24. protected:
  25. virtual ~prng() = default;
  26. virtual prng& acquire() noexcept = 0;
  27. virtual void release() noexcept = 0;
  28. virtual std::uint32_t operator()() noexcept = 0;
  29. public:
  30. using value_type = std::uint32_t;
  31. class ref
  32. {
  33. prng& p_;
  34. public:
  35. ref& operator=(ref const&) = delete;
  36. ~ref()
  37. {
  38. p_.release();
  39. }
  40. explicit
  41. ref(prng& p) noexcept
  42. : p_(p.acquire())
  43. {
  44. }
  45. ref(ref const& other) noexcept
  46. : p_(other.p_.acquire())
  47. {
  48. }
  49. value_type
  50. operator()() noexcept
  51. {
  52. return p_();
  53. }
  54. static
  55. value_type constexpr
  56. min() noexcept
  57. {
  58. return (std::numeric_limits<
  59. value_type>::min)();
  60. }
  61. static
  62. value_type constexpr
  63. max() noexcept
  64. {
  65. return (std::numeric_limits<
  66. value_type>::max)();
  67. }
  68. };
  69. };
  70. //------------------------------------------------------------------------------
  71. // Manually seed the prngs, must be called
  72. // before acquiring a prng for the first time.
  73. //
  74. BOOST_BEAST_DECL
  75. std::uint32_t const*
  76. prng_seed(std::seed_seq* ss = nullptr);
  77. // Acquire a PRNG using the no-TLS implementation
  78. //
  79. BOOST_BEAST_DECL
  80. prng::ref
  81. make_prng_no_tls(bool secure);
  82. // Acquire a PRNG using the TLS implementation
  83. //
  84. #ifndef BOOST_NO_CXX11_THREAD_LOCAL
  85. BOOST_BEAST_DECL
  86. prng::ref
  87. make_prng_tls(bool secure);
  88. #endif
  89. // Acquire a PRNG using the TLS implementation if it
  90. // is available, otherwise using the no-TLS implementation.
  91. //
  92. BOOST_BEAST_DECL
  93. prng::ref
  94. make_prng(bool secure);
  95. } // detail
  96. } // websocket
  97. } // beast
  98. } // boost
  99. #ifdef BOOST_BEAST_HEADER_ONLY
  100. #include <boost/beast/websocket/detail/prng.ipp>
  101. #endif
  102. #endif