AsyncEventSource - replace _clients with std::list

use a list of unique_ptrs for _clients container
This commit is contained in:
Emil Muratov
2024-06-26 18:26:02 +09:00
committed by Mathieu Carbou
parent a1627af702
commit bb4eb89c8e
2 changed files with 18 additions and 36 deletions

View File

@@ -274,17 +274,6 @@ void AsyncEventSourceClient::_runQueue() {
// Handler
AsyncEventSource::AsyncEventSource(const String& url)
: _url(url)
, _clients(LinkedList<AsyncEventSourceClient *>([](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<std::mutex> 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<std::mutex> 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<std::mutex> 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<std::mutex> 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;
}

View File

@@ -110,17 +110,17 @@ class AsyncEventSourceClient {
class AsyncEventSource: public AsyncWebHandler {
private:
String _url;
LinkedList<AsyncEventSourceClient *> _clients;
std::list< std::unique_ptr<AsyncEventSourceClient> > _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();