• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
10 #ifndef BOOST_BEAST_EXAMPLE_WEBSOCKET_CHAT_MULTI_SHARED_STATE_HPP
11 #define BOOST_BEAST_EXAMPLE_WEBSOCKET_CHAT_MULTI_SHARED_STATE_HPP
12 
13 #include <boost/smart_ptr.hpp>
14 #include <memory>
15 #include <mutex>
16 #include <string>
17 #include <unordered_set>
18 
19 // Forward declaration
20 class websocket_session;
21 
22 // Represents the shared server state
23 class shared_state
24 {
25     std::string const doc_root_;
26 
27     // This mutex synchronizes all access to sessions_
28     std::mutex mutex_;
29 
30     // Keep a list of all the connected clients
31     std::unordered_set<websocket_session*> sessions_;
32 
33 public:
34     explicit
35     shared_state(std::string doc_root);
36 
37     std::string const&
doc_root() const38     doc_root() const noexcept
39     {
40         return doc_root_;
41     }
42 
43     void join  (websocket_session* session);
44     void leave (websocket_session* session);
45     void send  (std::string message);
46 };
47 
48 #endif
49