stream_base.hpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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_CORE_DETAIL_STREAM_BASE_HPP
  10. #define BOOST_BEAST_CORE_DETAIL_STREAM_BASE_HPP
  11. #include <boost/asio/steady_timer.hpp>
  12. #include <boost/assert.hpp>
  13. #include <boost/core/exchange.hpp>
  14. #include <chrono>
  15. #include <cstdint>
  16. #include <utility>
  17. namespace boost {
  18. namespace beast {
  19. namespace detail {
  20. struct any_endpoint
  21. {
  22. template<class Error, class Endpoint>
  23. bool
  24. operator()(
  25. Error const&, Endpoint const&) const noexcept
  26. {
  27. return true;
  28. }
  29. };
  30. struct stream_base
  31. {
  32. using clock_type = std::chrono::steady_clock;
  33. using time_point = typename
  34. std::chrono::steady_clock::time_point;
  35. using tick_type = std::uint64_t;
  36. template<typename Executor>
  37. struct basic_op_state
  38. {
  39. net::basic_waitable_timer<
  40. std::chrono::steady_clock,
  41. net::wait_traits<
  42. std::chrono::steady_clock>,
  43. Executor> timer; // for timing out
  44. tick_type tick = 0; // counts waits
  45. bool pending = false; // if op is pending
  46. bool timeout = false; // if timed out
  47. template<class... Args>
  48. explicit
  49. basic_op_state(Args&&... args)
  50. : timer(std::forward<Args>(args)...)
  51. {
  52. }
  53. };
  54. class pending_guard
  55. {
  56. bool* b_ = nullptr;
  57. bool clear_ = true;
  58. public:
  59. ~pending_guard()
  60. {
  61. if(clear_ && b_)
  62. *b_ = false;
  63. }
  64. pending_guard()
  65. : b_(nullptr)
  66. , clear_(true)
  67. {
  68. }
  69. explicit
  70. pending_guard(bool& b)
  71. : b_(&b)
  72. {
  73. // If this assert goes off, it means you are attempting
  74. // to issue two of the same asynchronous I/O operation
  75. // at the same time, without waiting for the first one
  76. // to complete. For example, attempting two simultaneous
  77. // calls to async_read_some. Only one pending call of
  78. // each I/O type (read and write) is permitted.
  79. //
  80. BOOST_ASSERT(! *b_);
  81. *b_ = true;
  82. }
  83. pending_guard(
  84. pending_guard&& other) noexcept
  85. : b_(other.b_)
  86. , clear_(boost::exchange(
  87. other.clear_, false))
  88. {
  89. }
  90. void assign(bool& b)
  91. {
  92. BOOST_ASSERT(!b_);
  93. BOOST_ASSERT(clear_);
  94. b_ = &b;
  95. // If this assert goes off, it means you are attempting
  96. // to issue two of the same asynchronous I/O operation
  97. // at the same time, without waiting for the first one
  98. // to complete. For example, attempting two simultaneous
  99. // calls to async_read_some. Only one pending call of
  100. // each I/O type (read and write) is permitted.
  101. //
  102. BOOST_ASSERT(! *b_);
  103. *b_ = true;
  104. }
  105. void
  106. reset()
  107. {
  108. BOOST_ASSERT(clear_);
  109. if (b_)
  110. *b_ = false;
  111. clear_ = false;
  112. }
  113. };
  114. static time_point never() noexcept
  115. {
  116. return (time_point::max)();
  117. }
  118. static time_point now() noexcept
  119. {
  120. return clock_type::now();
  121. }
  122. static std::size_t constexpr no_limit =
  123. (std::numeric_limits<std::size_t>::max)();
  124. };
  125. } // detail
  126. } // beast
  127. } // boost
  128. #endif