SNode.C
Loading...
Searching...
No Matches
GroupsManager.cpp
Go to the documentation of this file.
1/*
2 * SNode.C - a slim toolkit for network communication
3 * Copyright (C) Volker Christian <me@vchrist.at>
4 * 2020, 2021, 2022, 2023, 2024, 2025
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as published
8 * by the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include "web/websocket/server/GroupsManager.h"
21
22#include "web/websocket/server/SubProtocol.h"
23
24#ifndef DOXYGEN_SHOULD_SKIP_THIS
25
26#endif /* DOXYGEN_SHOULD_SKIP_THIS */
27
28namespace web::websocket::server {
29
31
35
39
40 void GroupsManager::subscribe(SubProtocol* subProtocol, std::string group) {
41 if (group.empty()) {
42 group = subProtocol->getName();
43 }
44
45 if (subProtocol->group != group) {
46 const std::string newChannel = subProtocol->getName() + "/" + group;
47
48 groups[newChannel].insert(subProtocol);
49
50 if (!subProtocol->group.empty()) {
51 unsubscribe(subProtocol);
52 }
53
54 subProtocol->group = newChannel;
55 }
56 }
57
59 if (groups.contains(subProtocol->group)) {
60 groups[subProtocol->group].erase(subProtocol);
61
62 if (groups[subProtocol->group].empty()) {
63 groups.erase(subProtocol->group);
64 if (groups.empty()) {
65 delete this;
66 }
67 }
68 }
69 }
70
71 void GroupsManager::sendBroadcast(const std::string& group,
72 const char* message,
73 std::size_t messageLength,
74 const SubProtocol* excludedClient) {
75 if (groups.contains(group)) {
76 for (const SubProtocol* client : groups[group]) {
77 if (client != excludedClient) {
78 client->sendMessage(message, messageLength);
79 }
80 }
81 }
82 }
83
84 void GroupsManager::sendBroadcast(const std::string& group, const std::string& message, const SubProtocol* excludedClient) {
85 if (groups.contains(group)) {
86 for (const SubProtocol* client : groups[group]) {
87 if (client != excludedClient) {
88 client->sendMessage(message);
89 }
90 }
91 }
92 }
93
94 void GroupsManager::sendBroadcastStart(const std::string& group,
95 const char* message,
96 std::size_t messageLength,
97 const SubProtocol* excludedClient) {
98 if (groups.contains(group)) {
99 for (const SubProtocol* client : groups[group]) {
100 if (client != excludedClient) {
101 client->sendMessageStart(message, messageLength);
102 }
103 }
104 }
105 }
106
107 void GroupsManager::sendBroadcastStart(const std::string& group, const std::string& message, const SubProtocol* excludedClient) {
108 sendBroadcastStart(group, message.data(), message.length(), excludedClient);
109 }
110
111 void GroupsManager::sendBroadcastFrame(const std::string& group,
112 const char* message,
113 std::size_t messageLength,
114 const SubProtocol* excludedClient) {
115 if (groups.contains(group)) {
116 for (const SubProtocol* client : groups[group]) {
117 if (client != excludedClient) {
118 client->sendMessageFrame(message, messageLength);
119 }
120 }
121 }
122 }
123
124 void GroupsManager::sendBroadcastFrame(const std::string& group, const std::string& message, const SubProtocol* excludedClient) {
125 sendBroadcastFrame(group, message.data(), message.length(), excludedClient);
126 }
127
128 void GroupsManager::sendBroadcastEnd(const std::string& group,
129 const char* message,
130 std::size_t messageLength,
131 const SubProtocol* excludedClient) {
132 if (groups.contains(group)) {
133 for (const SubProtocol* client : groups[group]) {
134 if (client != excludedClient) {
135 client->sendMessageEnd(message, messageLength);
136 }
137 }
138 }
139 }
140
141 void GroupsManager::sendBroadcastEnd(const std::string& group, const std::string& message, const SubProtocol* excludedClient) {
142 sendBroadcastEnd(group, message.data(), message.length(), excludedClient);
143 }
144
145 void GroupsManager::forEachClient(const std::string& group,
146 const std::function<void(const SubProtocol*)>& sendToClient,
147 const SubProtocol* excludedClient) {
148 if (groups.contains(group)) {
149 for (const SubProtocol* client : groups[group]) {
150 if (client != excludedClient) {
151 sendToClient(client);
152 }
153 }
154 }
155 }
156
157} // namespace web::websocket::server
static GroupsManager * groupsManager
void sendBroadcast(const std::string &group, const char *message, std::size_t messageLength, const SubProtocol *excludedClient)
void sendBroadcastEnd(const std::string &group, const std::string &message, const SubProtocol *excludedClient)
void sendBroadcastFrame(const std::string &group, const std::string &message, const SubProtocol *excludedClient)
void sendBroadcastStart(const std::string &group, const std::string &message, const SubProtocol *excludedClient)
void forEachClient(const std::string &group, const std::function< void(const SubProtocol *)> &sendToClient, const SubProtocol *excludedClient)
void sendBroadcastStart(const std::string &group, const char *message, std::size_t messageLength, const SubProtocol *excludedClient)
void subscribe(SubProtocol *subProtocol, std::string group="")
void sendBroadcastEnd(const std::string &group, const char *message, std::size_t messageLength, const SubProtocol *excludedClient)
void sendBroadcast(const std::string &group, const std::string &message, const SubProtocol *excludedClient)
void unsubscribe(SubProtocol *subProtocol)
void sendBroadcastFrame(const std::string &group, const char *message, std::size_t messageLength, const SubProtocol *excludedClient)