stream_state.ipp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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_IPP
  11. #define BOOST_BEAST_TEST_DETAIL_STREAM_STATE_IPP
  12. #include <boost/beast/_experimental/test/error.hpp>
  13. #include <boost/make_shared.hpp>
  14. namespace boost {
  15. namespace beast {
  16. namespace test {
  17. namespace detail {
  18. //------------------------------------------------------------------------------
  19. stream_service::
  20. stream_service(net::execution_context& ctx)
  21. : beast::detail::service_base<stream_service>(ctx)
  22. , sp_(boost::make_shared<stream_service_impl>())
  23. {
  24. }
  25. void
  26. stream_service::
  27. shutdown()
  28. {
  29. std::vector<std::unique_ptr<detail::stream_read_op_base>> v;
  30. std::lock_guard<std::mutex> g1(sp_->m_);
  31. v.reserve(sp_->v_.size());
  32. for(auto p : sp_->v_)
  33. {
  34. std::lock_guard<std::mutex> g2(p->m);
  35. v.emplace_back(std::move(p->op));
  36. p->code = detail::stream_status::eof;
  37. }
  38. }
  39. auto
  40. stream_service::
  41. make_impl(
  42. net::any_io_executor exec,
  43. test::fail_count* fc) ->
  44. boost::shared_ptr<detail::stream_state>
  45. {
  46. #if defined(BOOST_ASIO_USE_TS_EXECUTOR_AS_DEFAULT)
  47. auto& ctx = exec.context();
  48. #else
  49. auto& ctx = net::query(
  50. exec,
  51. net::execution::context);
  52. #endif
  53. auto& svc = net::use_service<stream_service>(ctx);
  54. auto sp = boost::make_shared<detail::stream_state>(exec, svc.sp_, fc);
  55. std::lock_guard<std::mutex> g(svc.sp_->m_);
  56. svc.sp_->v_.push_back(sp.get());
  57. return sp;
  58. }
  59. //------------------------------------------------------------------------------
  60. void
  61. stream_service_impl::
  62. remove(stream_state& impl)
  63. {
  64. std::lock_guard<std::mutex> g(m_);
  65. *std::find(
  66. v_.begin(), v_.end(),
  67. &impl) = std::move(v_.back());
  68. v_.pop_back();
  69. }
  70. //------------------------------------------------------------------------------
  71. stream_state::
  72. stream_state(
  73. net::any_io_executor exec_,
  74. boost::weak_ptr<stream_service_impl> wp_,
  75. fail_count* fc_)
  76. : exec(std::move(exec_))
  77. , wp(std::move(wp_))
  78. , fc(fc_)
  79. {
  80. }
  81. stream_state::
  82. ~stream_state()
  83. {
  84. // cancel outstanding read
  85. if(op != nullptr)
  86. (*op)(net::error::operation_aborted);
  87. }
  88. void
  89. stream_state::
  90. remove() noexcept
  91. {
  92. auto sp = wp.lock();
  93. // If this goes off, it means the lifetime of a test::stream object
  94. // extended beyond the lifetime of the associated execution context.
  95. BOOST_ASSERT(sp);
  96. sp->remove(*this);
  97. }
  98. void
  99. stream_state::
  100. notify_read()
  101. {
  102. if(op)
  103. {
  104. auto op_ = std::move(op);
  105. op_->operator()(error_code{});
  106. }
  107. else
  108. {
  109. cv.notify_all();
  110. }
  111. }
  112. void
  113. stream_state::
  114. cancel_read()
  115. {
  116. std::unique_ptr<stream_read_op_base> p;
  117. {
  118. std::lock_guard<std::mutex> lock(m);
  119. code = stream_status::eof;
  120. p = std::move(op);
  121. }
  122. if(p != nullptr)
  123. (*p)(net::error::operation_aborted);
  124. }
  125. } // detail
  126. } // test
  127. } // beast
  128. } // boost
  129. #endif // BOOST_BEAST_TEST_DETAIL_STREAM_STATE_IPP