shared_state.hpp 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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/vinniefalco/CppCon2018
  8. //
  9. #ifndef BOOST_BEAST_EXAMPLE_WEBSOCKET_CHAT_MULTI_SHARED_STATE_HPP
  10. #define BOOST_BEAST_EXAMPLE_WEBSOCKET_CHAT_MULTI_SHARED_STATE_HPP
  11. #include <boost/smart_ptr.hpp>
  12. #include <memory>
  13. #include <mutex>
  14. #include <string>
  15. #include <unordered_set>
  16. // Forward declaration
  17. class websocket_session;
  18. // Represents the shared server state
  19. class shared_state
  20. {
  21. std::string const doc_root_;
  22. // This mutex synchronizes all access to sessions_
  23. std::mutex mutex_;
  24. // Keep a list of all the connected clients
  25. std::unordered_set<websocket_session*> sessions_;
  26. public:
  27. explicit
  28. shared_state(std::string doc_root);
  29. std::string const&
  30. doc_root() const noexcept
  31. {
  32. return doc_root_;
  33. }
  34. void join (websocket_session* session);
  35. void leave (websocket_session* session);
  36. void send (std::string message);
  37. };
  38. #endif