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