Skip to content

Commit 53662c4

Browse files
committed
Address websocket review feedback
1 parent 0f9c4f1 commit 53662c4

2 files changed

Lines changed: 21 additions & 16 deletions

File tree

src/AsyncWebSocket.cpp

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1055,26 +1055,30 @@ void AsyncWebSocket::closeAll(uint16_t code, const char *message) {
10551055
}
10561056

10571057
void AsyncWebSocket::cleanupClients(uint16_t maxClients) {
1058-
asyncsrv::lock_guard_type lock(_lock);
1059-
const size_t connected = std::count_if(std::begin(_clients), std::end(_clients), [](const AsyncWebSocketClient &c) {
1060-
return c.status() == WS_CONNECTED;
1061-
});
1062-
1063-
if (connected > maxClients) {
1064-
const auto connected_iter = std::find_if(std::begin(_clients), std::end(_clients), [](const AsyncWebSocketClient &c) {
1058+
std::list<AsyncWebSocketClient> removed_clients;
1059+
{
1060+
asyncsrv::lock_guard_type lock(_lock);
1061+
const size_t connected = std::count_if(std::begin(_clients), std::end(_clients), [](const AsyncWebSocketClient &c) {
10651062
return c.status() == WS_CONNECTED;
10661063
});
1067-
if (connected_iter != std::end(_clients)) {
1068-
async_ws_log_v("[%s] CLEANUP %" PRIu32 " (%u/%" PRIu16 ")", _url.c_str(), connected_iter->id(), connected, maxClients);
1069-
connected_iter->close();
1064+
1065+
if (connected > maxClients) {
1066+
const auto connected_iter = std::find_if(std::begin(_clients), std::end(_clients), [](const AsyncWebSocketClient &c) {
1067+
return c.status() == WS_CONNECTED;
1068+
});
1069+
if (connected_iter != std::end(_clients)) {
1070+
async_ws_log_v("[%s] CLEANUP %" PRIu32 " (%u/%" PRIu16 ")", _url.c_str(), connected_iter->id(), connected, maxClients);
1071+
connected_iter->close();
1072+
}
10701073
}
1071-
}
10721074

1073-
for (auto iter = _clients.begin(); iter != _clients.end();) {
1074-
if (iter->shouldBeDeleted()) {
1075-
iter = _clients.erase(iter);
1076-
} else {
1077-
++iter;
1075+
for (auto iter = _clients.begin(); iter != _clients.end();) {
1076+
if (iter->shouldBeDeleted()) {
1077+
auto current = iter++;
1078+
removed_clients.splice(removed_clients.end(), _clients, current);
1079+
} else {
1080+
++iter;
1081+
}
10781082
}
10791083
}
10801084
}

src/AsyncWebSocket.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,7 @@ class AsyncWebSocketClient {
256256
return _clientId;
257257
}
258258
AwsClientStatus status() const {
259+
asyncsrv::lock_guard_type lock(_lock);
259260
return _status;
260261
}
261262
AsyncClient *client() {

0 commit comments

Comments
 (0)