Skip to content

Commit 48f0580

Browse files
committed
Emit new 'subscription' events
1 parent c4d45fb commit 48f0580

File tree

4 files changed

+27
-12
lines changed

4 files changed

+27
-12
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<i>Simple, secure</i><sup><a href="https://github.com/uNetworking/uWebSockets/tree/master/fuzzing#fuzz-testing-of-various-parsers-and-mocked-examples">1</a></sup><i> & standards compliant</i><sup><a href="https://unetworking.github.io/uWebSockets.js/report.pdf">2</a></sup><i> web server for the most demanding</i><sup><a href="https://github.com/uNetworking/uWebSockets/tree/master/benchmarks#benchmark-driven-development">3</a></sup><i> of applications.</i> <a href="https://github.com/uNetworking/uWebSockets/blob/master/misc/READMORE.md">Read more...</a>
55
<br><br>
66

7-
<a href="https://github.com/uNetworking/uWebSockets/releases"><img src="https://img.shields.io/github/v/release/uNetworking/uWebSockets"></a> <a href="https://lgtm.com/projects/g/uNetworking/uWebSockets/context:cpp"><img alt="Language grade: C/C++" src="https://img.shields.io/lgtm/grade/cpp/g/uNetworking/uWebSockets.svg?logo=lgtm&logoWidth=18"/></a> <a href="https://osv.dev/list?q=uwebsockets&affected_only=true&page=1&ecosystem=OSS-Fuzz"><img src="https://oss-fuzz-build-logs.storage.googleapis.com/badges/uwebsockets.svg" /></a> <img src="https://img.shields.io/badge/downloads-65%20million-pink" />
7+
<a href="https://github.com/uNetworking/uWebSockets/releases"><img src="https://img.shields.io/github/v/release/uNetworking/uWebSockets"></a> <a href="https://osv.dev/list?q=uwebsockets&affected_only=true&page=1&ecosystem=OSS-Fuzz"><img src="https://oss-fuzz-build-logs.storage.googleapis.com/badges/uwebsockets.svg" /></a> <img src="https://img.shields.io/badge/downloads-70%20million-pink" />
88

99
</div>
1010
<br><br>

src/TopicTree.h

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ struct TopicTree {
158158
}
159159

160160
/* Subscribe fails if we already are subscribed */
161-
bool subscribe(Subscriber *s, std::string_view topic) {
161+
Topic *subscribe(Subscriber *s, std::string_view topic) {
162162
/* Notify user that they are doing something wrong here */
163163
checkIteratingSubscriber(s);
164164

@@ -173,42 +173,44 @@ struct TopicTree {
173173
/* Insert us in topic, insert topic in us */
174174
auto [it, inserted] = s->topics.insert(topicPtr);
175175
if (!inserted) {
176-
return false;
176+
return nullptr;
177177
}
178178
topicPtr->insert(s);
179179

180180
/* Success */
181-
return true;
181+
return topicPtr;
182182
}
183183

184-
/* Returns ok, last */
185-
std::pair<bool, bool> unsubscribe(Subscriber *s, std::string_view topic) {
184+
/* Returns ok, last, newCount */
185+
std::tuple<bool, bool, int> unsubscribe(Subscriber *s, std::string_view topic) {
186186
/* Notify user that they are doing something wrong here */
187187
checkIteratingSubscriber(s);
188188

189189
/* Lookup topic */
190190
Topic *topicPtr = lookupTopic(topic);
191191
if (!topicPtr) {
192192
/* If the topic doesn't exist we are assumed to still be subscribers of something */
193-
return {false, false};
193+
return {false, false, -1};
194194
}
195195

196196
/* Erase from our list first */
197197
if (s->topics.erase(topicPtr) == 0) {
198-
return {false, false};
198+
return {false, false, -1};
199199
}
200200

201201
/* Remove us from topic */
202202
topicPtr->erase(s);
203203

204+
int newCount = topicPtr->size();
205+
204206
/* If there is no subscriber to this topic, remove it */
205207
if (!topicPtr->size()) {
206208
/* Unique_ptr deletes the topic */
207209
topics.erase(topic);
208210
}
209211

210212
/* If we don't hold any topics we are to be freed altogether */
211-
return {true, topics.size() == 0};
213+
return {true, s->topics.size() == 0, newCount};
212214
}
213215

214216
/* Factory function for creating a Subscriber */

src/WebSocket.h

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,11 @@ struct WebSocket : AsyncSocket<SSL> {
195195
/* Set shorter timeout (use ping-timeout) to avoid long hanging sockets after end() on broken connections */
196196
Super::timeout(webSocketContextData->idleTimeoutComponents.second);
197197

198+
/* At this point we iterate all currently held subscriptions and emit an event for all of them */
199+
for (Topic *t : webSocketData->subscriber->topics) {
200+
webSocketContextData->subscriptionHandler(this, t->name, (int) t->size() - 1, (int) t->size());
201+
}
202+
198203
/* Make sure to unsubscribe from any pub/sub node at exit */
199204
webSocketContextData->topicTree->freeSubscriber(webSocketData->subscriber);
200205
webSocketData->subscriber = nullptr;
@@ -234,7 +239,11 @@ struct WebSocket : AsyncSocket<SSL> {
234239
}
235240

236241
/* Cannot return numSubscribers as this is only for this particular websocket context */
237-
webSocketContextData->topicTree->subscribe(webSocketData->subscriber, topic);
242+
Topic *topicOrNull = webSocketContextData->topicTree->subscribe(webSocketData->subscriber, topic);
243+
if (topicOrNull && webSocketContextData->subscriptionHandler) {
244+
/* Emit this socket, the topic, new count, old count */
245+
webSocketContextData->subscriptionHandler(this, topic, (int) topicOrNull->size(), (int) topicOrNull->size() - 1);
246+
}
238247

239248
/* Subscribe always succeeds */
240249
return true;
@@ -251,7 +260,11 @@ struct WebSocket : AsyncSocket<SSL> {
251260
if (!webSocketData->subscriber) { return false; }
252261

253262
/* Cannot return numSubscribers as this is only for this particular websocket context */
254-
auto [ok, last] = webSocketContextData->topicTree->unsubscribe(webSocketData->subscriber, topic);
263+
auto [ok, last, newCount] = webSocketContextData->topicTree->unsubscribe(webSocketData->subscriber, topic);
264+
/* Emit subscription event if last */
265+
if (ok && webSocketContextData->subscriptionHandler) {
266+
webSocketContextData->subscriptionHandler(this, topic, newCount, newCount + 1);
267+
}
255268

256269
/* Free us as subscribers if we unsubscribed from our last topic */
257270
if (ok && last) {

uSockets

0 commit comments

Comments
 (0)