stream_state.hpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. #ifndef BOOST_BEAST_TEST_DETAIL_STREAM_STATE_HPP
  11. #define BOOST_BEAST_TEST_DETAIL_STREAM_STATE_HPP
  12. #include <boost/asio/any_io_executor.hpp>
  13. #include <boost/beast/core/detail/config.hpp>
  14. #include <boost/beast/_experimental/test/fail_count.hpp>
  15. #include <boost/beast/core/detail/service_base.hpp>
  16. #include <boost/beast/core/flat_buffer.hpp>
  17. #include <boost/smart_ptr/weak_ptr.hpp>
  18. #include <condition_variable>
  19. #include <memory>
  20. #include <mutex>
  21. #include <vector>
  22. namespace boost {
  23. namespace beast {
  24. namespace test {
  25. namespace detail {
  26. struct stream_state;
  27. struct stream_service_impl
  28. {
  29. std::mutex m_;
  30. std::vector<stream_state*> v_;
  31. BOOST_BEAST_DECL
  32. void
  33. remove(stream_state& impl);
  34. };
  35. //------------------------------------------------------------------------------
  36. class stream_service
  37. : public beast::detail::service_base<stream_service>
  38. {
  39. boost::shared_ptr<detail::stream_service_impl> sp_;
  40. BOOST_BEAST_DECL
  41. void
  42. shutdown() override;
  43. public:
  44. BOOST_BEAST_DECL
  45. explicit
  46. stream_service(net::execution_context& ctx);
  47. BOOST_BEAST_DECL
  48. static
  49. auto
  50. make_impl(
  51. net::any_io_executor exec,
  52. test::fail_count* fc) ->
  53. boost::shared_ptr<detail::stream_state>;
  54. };
  55. //------------------------------------------------------------------------------
  56. struct stream_read_op_base
  57. {
  58. virtual ~stream_read_op_base() = default;
  59. virtual void operator()(error_code ec) = 0;
  60. };
  61. //------------------------------------------------------------------------------
  62. enum class stream_status
  63. {
  64. ok,
  65. eof,
  66. };
  67. //------------------------------------------------------------------------------
  68. struct stream_state
  69. {
  70. net::any_io_executor exec;
  71. boost::weak_ptr<stream_service_impl> wp;
  72. std::mutex m;
  73. flat_buffer b;
  74. std::condition_variable cv;
  75. std::unique_ptr<stream_read_op_base> op;
  76. stream_status code = stream_status::ok;
  77. fail_count* fc = nullptr;
  78. std::size_t nread = 0;
  79. std::size_t nread_bytes = 0;
  80. std::size_t nwrite = 0;
  81. std::size_t nwrite_bytes = 0;
  82. std::size_t read_max =
  83. (std::numeric_limits<std::size_t>::max)();
  84. std::size_t write_max =
  85. (std::numeric_limits<std::size_t>::max)();
  86. BOOST_BEAST_DECL
  87. stream_state(
  88. net::any_io_executor exec_,
  89. boost::weak_ptr<stream_service_impl> wp_,
  90. fail_count* fc_);
  91. BOOST_BEAST_DECL
  92. ~stream_state();
  93. BOOST_BEAST_DECL
  94. void
  95. remove() noexcept;
  96. BOOST_BEAST_DECL
  97. void
  98. notify_read();
  99. BOOST_BEAST_DECL
  100. void
  101. cancel_read();
  102. };
  103. } // detail
  104. } // test
  105. } // beast
  106. } // boost
  107. #ifdef BOOST_BEAST_HEADER_ONLY
  108. #include <boost/beast/_experimental/test/detail/stream_state.ipp>
  109. #endif
  110. #endif // BOOST_BEAST_TEST_DETAIL_STREAM_STATE_HPP