service.ipp 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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_WEBSOCKET_DETAIL_SERVICE_IPP
  10. #define BOOST_BEAST_WEBSOCKET_DETAIL_SERVICE_IPP
  11. #include <boost/beast/websocket/detail/service.hpp>
  12. namespace boost {
  13. namespace beast {
  14. namespace websocket {
  15. namespace detail {
  16. service::
  17. impl_type::
  18. impl_type(net::execution_context& ctx)
  19. : svc_(net::use_service<service>(ctx))
  20. {
  21. std::lock_guard<std::mutex> g(svc_.m_);
  22. index_ = svc_.v_.size();
  23. svc_.v_.push_back(this);
  24. }
  25. void
  26. service::
  27. impl_type::
  28. remove()
  29. {
  30. std::lock_guard<std::mutex> g(svc_.m_);
  31. auto& other = *svc_.v_.back();
  32. other.index_ = index_;
  33. svc_.v_[index_] = &other;
  34. svc_.v_.pop_back();
  35. }
  36. //---
  37. void
  38. service::
  39. shutdown()
  40. {
  41. std::vector<boost::weak_ptr<impl_type>> v;
  42. {
  43. std::lock_guard<std::mutex> g(m_);
  44. v.reserve(v_.size());
  45. for(auto p : v_)
  46. v.emplace_back(p->weak_from_this());
  47. }
  48. for(auto wp : v)
  49. if(auto sp = wp.lock())
  50. sp->shutdown();
  51. }
  52. } // detail
  53. } // websocket
  54. } // beast
  55. } // boost
  56. #endif