stream_base.hpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. struct op_state
  37. {
  38. net::steady_timer timer; // for timing out
  39. tick_type tick = 0; // counts waits
  40. bool pending = false; // if op is pending
  41. bool timeout = false; // if timed out
  42. template<class... Args>
  43. explicit
  44. op_state(Args&&... args)
  45. : timer(std::forward<Args>(args)...)
  46. {
  47. }
  48. };
  49. class pending_guard
  50. {
  51. bool& b_;
  52. bool clear_ = true;
  53. public:
  54. ~pending_guard()
  55. {
  56. if(clear_)
  57. b_ = false;
  58. }
  59. explicit
  60. pending_guard(bool& b)
  61. : b_(b)
  62. {
  63. // If this assert goes off, it means you are attempting
  64. // to issue two of the same asynchronous I/O operation
  65. // at the same time, without waiting for the first one
  66. // to complete. For example, attempting two simultaneous
  67. // calls to async_read_some. Only one pending call of
  68. // each I/O type (read and write) is permitted.
  69. //
  70. BOOST_ASSERT(! b_);
  71. b_ = true;
  72. }
  73. pending_guard(
  74. pending_guard&& other) noexcept
  75. : b_(other.b_)
  76. , clear_(boost::exchange(
  77. other.clear_, false))
  78. {
  79. }
  80. void
  81. reset()
  82. {
  83. BOOST_ASSERT(clear_);
  84. b_ = false;
  85. clear_ = false;
  86. }
  87. };
  88. static time_point never() noexcept
  89. {
  90. return (time_point::max)();
  91. }
  92. static std::size_t constexpr no_limit =
  93. (std::numeric_limits<std::size_t>::max)();
  94. };
  95. } // detail
  96. } // beast
  97. } // boost
  98. #endif