From bb4eb89c8e028005ef84f875417d32ca095147e7 Mon Sep 17 00:00:00 2001 From: Emil Muratov Date: Wed, 26 Jun 2024 18:26:02 +0900 Subject: [PATCH] AsyncEventSource - replace _clients with std::list use a list of unique_ptrs for _clients container --- src/AsyncEventSource.cpp | 46 ++++++++++++---------------------------- src/AsyncEventSource.h | 8 +++---- 2 files changed, 18 insertions(+), 36 deletions(-) diff --git a/src/AsyncEventSource.cpp b/src/AsyncEventSource.cpp index 0efdbd5..77589bb 100644 --- a/src/AsyncEventSource.cpp +++ b/src/AsyncEventSource.cpp @@ -274,17 +274,6 @@ void AsyncEventSourceClient::_runQueue() { // Handler - -AsyncEventSource::AsyncEventSource(const String& url) - : _url(url) - , _clients(LinkedList([](AsyncEventSourceClient *c){ delete c; })) - , _connectcb(NULL) -{} - -AsyncEventSource::~AsyncEventSource(){ - close(); -} - void AsyncEventSource::onConnect(ArEventHandlerFunction cb){ _connectcb = cb; } @@ -294,22 +283,12 @@ void AsyncEventSource::authorizeConnect(ArAuthorizeConnectHandler cb){ } void AsyncEventSource::_addClient(AsyncEventSourceClient * client){ - /*char * temp = (char *)malloc(2054); - if(temp != NULL){ - memset(temp+1,' ',2048); - temp[0] = ':'; - temp[2049] = '\r'; - temp[2050] = '\n'; - temp[2051] = '\r'; - temp[2052] = '\n'; - temp[2053] = 0; - client->write((const char *)temp, 2053); - free(temp); - }*/ + if (!client) + return; #ifdef ESP32 std::lock_guard lock(_client_queue_lock); #endif - _clients.add(client); + _clients.emplace_back(client); if(_connectcb) _connectcb(client); } @@ -318,7 +297,10 @@ void AsyncEventSource::_handleDisconnect(AsyncEventSourceClient * client){ #ifdef ESP32 std::lock_guard lock(_client_queue_lock); #endif - _clients.remove(client); + for (auto i = _clients.begin(); i != _clients.end(); ++i){ + if (i->get() == client) + _clients.erase(i); + } } void AsyncEventSource::close(){ @@ -341,9 +323,8 @@ size_t AsyncEventSource::avgPacketsWaiting() const { #ifdef ESP32 std::lock_guard lock(_client_queue_lock); #endif - if (_clients.isEmpty()) { - return 0; - } + if (!_clients.size()) return 0; + for(const auto &c: _clients){ if(c->connected()) { aql += c->packetsWaiting(); @@ -367,13 +348,14 @@ void AsyncEventSource::send( } size_t AsyncEventSource::count() const { - size_t n_clients; #ifdef ESP32 std::lock_guard lock(_client_queue_lock); #endif - n_clients = _clients.count_if([](AsyncEventSourceClient *c){ - return c->connected(); - }); + size_t n_clients{0}; + for (const auto &i : _clients) + if (i->connected()) + ++n_clients; + return n_clients; } diff --git a/src/AsyncEventSource.h b/src/AsyncEventSource.h index 79ccc26..351e92c 100644 --- a/src/AsyncEventSource.h +++ b/src/AsyncEventSource.h @@ -110,17 +110,17 @@ class AsyncEventSourceClient { class AsyncEventSource: public AsyncWebHandler { private: String _url; - LinkedList _clients; + std::list< std::unique_ptr > _clients; #ifdef ESP32 // Same as for individual messages, protect mutations of _clients list // since simultaneous access from different tasks is possible mutable std::mutex _client_queue_lock; #endif - ArEventHandlerFunction _connectcb; + ArEventHandlerFunction _connectcb{nullptr}; ArAuthorizeConnectHandler _authorizeConnectHandler; public: - AsyncEventSource(const String& url); - ~AsyncEventSource(); + AsyncEventSource(const String& url) : _url(url){}; + ~AsyncEventSource(){ close(); }; const char * url() const { return _url.c_str(); } void close();