diff options
Diffstat (limited to 'connectionregistry.h')
-rw-r--r-- | connectionregistry.h | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/connectionregistry.h b/connectionregistry.h new file mode 100644 index 0000000..2b14553 --- /dev/null +++ b/connectionregistry.h @@ -0,0 +1,46 @@ +#pragma once + +#include <algorithm> +#include <memory> +#include <unordered_map> +#include <unordered_set> + +#include <boost/asio/ip/tcp.hpp> +#include <boost/beast/websocket.hpp> + +class ConnectionRegistry +{ +public: + using connection = std::shared_ptr<boost::beast::websocket::stream<boost::asio::ip::tcp::socket>>; + + ConnectionRegistry() = default; + ~ConnectionRegistry() = default; + + void setId(connection c, const std::string& id); + + // used via RegistryGuard + void addConnection(connection c); + void delConnection(connection c); + + // map connection to id + std::unordered_map<connection, std::string> m_connections; + // map id to list of related connections, used for iteration over connections to notify about changes + std::unordered_map<std::string, std::unordered_set<connection>> m_ids; + + std::unordered_set<connection>::iterator begin(const std::string& id); + std::unordered_set<connection>::iterator end(const std::string& id); + + void dump(); + + class RegistryGuard + { + public: + RegistryGuard(ConnectionRegistry& registry, connection c); + ~RegistryGuard(); + private: + ConnectionRegistry& m_registry; + connection m_connection; + }; + +}; + |