| 1 | // |
|---|---|
| 2 | // Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com) |
| 3 | // Copyright (c) 2020 Richard Hodges (hodges.r@gmail.com) |
| 4 | // |
| 5 | // Distributed under the Boost Software License, Version 1.0. (See accompanying |
| 6 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
| 7 | // |
| 8 | // Official repository: https://github.com/boostorg/beast |
| 9 | // |
| 10 | |
| 11 | #ifndef BOOST_BEAST_TEST_DETAIL_STREAM_STATE_HPP |
| 12 | #define BOOST_BEAST_TEST_DETAIL_STREAM_STATE_HPP |
| 13 | |
| 14 | #include <boost/asio/any_io_executor.hpp> |
| 15 | #include <boost/beast/core/detail/config.hpp> |
| 16 | #include <boost/beast/_experimental/test/fail_count.hpp> |
| 17 | #include <boost/beast/core/detail/service_base.hpp> |
| 18 | #include <boost/beast/core/flat_buffer.hpp> |
| 19 | #include <boost/smart_ptr/weak_ptr.hpp> |
| 20 | |
| 21 | #include <condition_variable> |
| 22 | #include <memory> |
| 23 | #include <mutex> |
| 24 | #include <vector> |
| 25 | |
| 26 | namespace boost { |
| 27 | namespace beast { |
| 28 | namespace test { |
| 29 | namespace detail { |
| 30 | |
| 31 | struct stream_state; |
| 32 | |
| 33 | struct stream_service_impl |
| 34 | { |
| 35 | std::mutex m_; |
| 36 | std::vector<stream_state*> v_; |
| 37 | |
| 38 | BOOST_BEAST_DECL |
| 39 | void |
| 40 | remove(stream_state& impl); |
| 41 | }; |
| 42 | |
| 43 | //------------------------------------------------------------------------------ |
| 44 | |
| 45 | class stream_service |
| 46 | : public beast::detail::service_base<stream_service> |
| 47 | { |
| 48 | boost::shared_ptr<detail::stream_service_impl> sp_; |
| 49 | |
| 50 | BOOST_BEAST_DECL |
| 51 | void |
| 52 | shutdown() override; |
| 53 | |
| 54 | public: |
| 55 | BOOST_BEAST_DECL |
| 56 | explicit |
| 57 | stream_service(net::execution_context& ctx); |
| 58 | |
| 59 | BOOST_BEAST_DECL |
| 60 | static |
| 61 | auto |
| 62 | make_impl( |
| 63 | net::any_io_executor exec, |
| 64 | test::fail_count* fc) -> |
| 65 | boost::shared_ptr<detail::stream_state>; |
| 66 | }; |
| 67 | |
| 68 | //------------------------------------------------------------------------------ |
| 69 | |
| 70 | struct stream_read_op_base |
| 71 | { |
| 72 | virtual ~stream_read_op_base() = default; |
| 73 | virtual void operator()(error_code ec) = 0; |
| 74 | }; |
| 75 | |
| 76 | //------------------------------------------------------------------------------ |
| 77 | |
| 78 | enum class stream_status |
| 79 | { |
| 80 | ok, |
| 81 | eof, |
| 82 | }; |
| 83 | |
| 84 | //------------------------------------------------------------------------------ |
| 85 | |
| 86 | struct stream_state |
| 87 | { |
| 88 | net::any_io_executor exec; |
| 89 | boost::weak_ptr<stream_service_impl> wp; |
| 90 | std::mutex m; |
| 91 | flat_buffer b; |
| 92 | std::condition_variable cv; |
| 93 | std::unique_ptr<stream_read_op_base> op; |
| 94 | stream_status code = stream_status::ok; |
| 95 | fail_count* fc = nullptr; |
| 96 | std::size_t nread = 0; |
| 97 | std::size_t nread_bytes = 0; |
| 98 | std::size_t nwrite = 0; |
| 99 | std::size_t nwrite_bytes = 0; |
| 100 | std::size_t read_max = |
| 101 | (std::numeric_limits<std::size_t>::max)(); |
| 102 | std::size_t write_max = |
| 103 | (std::numeric_limits<std::size_t>::max)(); |
| 104 | |
| 105 | BOOST_BEAST_DECL |
| 106 | stream_state( |
| 107 | net::any_io_executor exec_, |
| 108 | boost::weak_ptr<stream_service_impl> wp_, |
| 109 | fail_count* fc_); |
| 110 | |
| 111 | BOOST_BEAST_DECL |
| 112 | ~stream_state(); |
| 113 | |
| 114 | BOOST_BEAST_DECL |
| 115 | void |
| 116 | remove() noexcept; |
| 117 | |
| 118 | BOOST_BEAST_DECL |
| 119 | void |
| 120 | notify_read(); |
| 121 | |
| 122 | BOOST_BEAST_DECL |
| 123 | void |
| 124 | cancel_read(); |
| 125 | }; |
| 126 | |
| 127 | |
| 128 | |
| 129 | } // detail |
| 130 | } // test |
| 131 | } // beast |
| 132 | } // boost |
| 133 | |
| 134 | #ifdef BOOST_BEAST_HEADER_ONLY |
| 135 | #include <boost/beast/_experimental/test/detail/stream_state.ipp> |
| 136 | #endif |
| 137 | |
| 138 | #endif // BOOST_BEAST_TEST_DETAIL_STREAM_STATE_HPP |
| 139 |
