Imported Upstream version 1.72.0
[platform/upstream/boost.git] / boost / beast / core / detail / stream_base.hpp
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
10 #ifndef BOOST_BEAST_CORE_DETAIL_STREAM_BASE_HPP
11 #define BOOST_BEAST_CORE_DETAIL_STREAM_BASE_HPP
12
13 #include <boost/asio/steady_timer.hpp>
14 #include <boost/assert.hpp>
15 #include <boost/core/exchange.hpp>
16 #include <chrono>
17 #include <cstdint>
18 #include <utility>
19
20 namespace boost {
21 namespace beast {
22 namespace detail {
23
24 struct any_endpoint
25 {
26     template<class Error, class Endpoint>
27     bool
28     operator()(
29         Error const&, Endpoint const&) const noexcept
30     {
31         return true;
32     }
33 };
34
35 struct stream_base
36 {
37     using clock_type = std::chrono::steady_clock;
38     using time_point = typename
39         std::chrono::steady_clock::time_point;
40     using tick_type = std::uint64_t;
41
42     struct op_state
43     {
44         net::steady_timer timer;    // for timing out
45         tick_type tick = 0;         // counts waits
46         bool pending = false;       // if op is pending
47         bool timeout = false;       // if timed out
48
49         template<class... Args>
50         explicit
51         op_state(Args&&... args)
52             : timer(std::forward<Args>(args)...)
53         {
54         }
55     };
56
57     class pending_guard
58     {
59         bool& b_;
60         bool clear_ = true;
61
62     public:
63         ~pending_guard()
64         {
65             if(clear_)
66                 b_ = false;
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
84         pending_guard(
85             pending_guard&& other) noexcept
86             : b_(other.b_)
87             , clear_(boost::exchange(
88                 other.clear_, false))
89         {
90         }
91
92         void
93         reset()
94         {
95             BOOST_ASSERT(clear_);
96             b_ = false;
97             clear_ = false;
98         }
99     };
100
101     static time_point never() noexcept
102     {
103         return (time_point::max)();
104     }
105
106     static std::size_t constexpr no_limit =
107         (std::numeric_limits<std::size_t>::max)();
108 };
109
110 } // detail
111 } // beast
112 } // boost
113
114 #endif