Skip to content

Commit

Permalink
App.closeSockets
Browse files Browse the repository at this point in the history
  • Loading branch information
uNetworkingAB committed Dec 28, 2022
1 parent 1612690 commit c603228
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
15 changes: 15 additions & 0 deletions src/App.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ struct TemplatedApp {
/* WebSocketContexts are of differing type, but we as owners and creators must delete them correctly */
std::vector<MoveOnlyFunction<void()>> webSocketContextDeleters;

std::vector<void *> webSocketContexts;

public:

TopicTree<TopicTreeMessage, TopicTreeBigMessage> *topicTree = nullptr;
Expand Down Expand Up @@ -205,6 +207,8 @@ struct TemplatedApp {
/* Move webSocketContextDeleters */
webSocketContextDeleters = std::move(other.webSocketContextDeleters);

webSocketContexts = std::move(other.webSocketContexts);

/* Move TopicTree */
topicTree = other.topicTree;
other.topicTree = nullptr;
Expand Down Expand Up @@ -245,6 +249,14 @@ struct TemplatedApp {
MoveOnlyFunction<void(WebSocket<SSL, true, UserData> *, int, std::string_view)> close = nullptr;
};

/* Closes all sockets. Does not close listen sockets. */
TemplatedApp &&closeSockets() {
us_socket_context_close(SSL, httpContext);
for (void *webSocketContext : webSocketContexts) {
us_socket_context_close(SSL, webSocketContext);
}
}

template <typename UserData>
TemplatedApp &&ws(std::string pattern, WebSocketBehavior<UserData> &&behavior) {
/* Don't compile if alignment rules cannot be satisfied */
Expand Down Expand Up @@ -335,6 +347,9 @@ struct TemplatedApp {
webSocketContext->free();
});

/* We also keep this list for easy closing */
webSocketContexts.push_back((void *)webSocketContext);

/* Quick fix to disable any compression if set */
#ifdef UWS_NO_ZLIB
behavior.compression = DISABLED;
Expand Down
2 changes: 1 addition & 1 deletion uSockets

0 comments on commit c603228

Please sign in to comment.