summaryrefslogtreecommitdiffhomepage
path: root/connectionregistry.h
diff options
context:
space:
mode:
authorRoland Reichwein <mail@reichwein.it>2023-01-26 20:46:30 +0100
committerRoland Reichwein <mail@reichwein.it>2023-01-26 20:46:30 +0100
commit789e5555ab4c44a1ae779eccf6ccf8340602cf22 (patch)
treefd1c15ac38ec4d43965d8e12a149ae52a0808a73 /connectionregistry.h
parent004db5e7e4e9ab6ac5b4730873c6b8f58da92930 (diff)
Websockets: Notify other clients of changes
Diffstat (limited to 'connectionregistry.h')
-rw-r--r--connectionregistry.h46
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;
+ };
+
+};
+