mirror of
https://github.com/me-no-dev/ESPAsyncWebServer.git
synced 2025-08-05 21:54:38 +02:00
AsyncEventSource - replace _clients with std::list
use a list of unique_ptrs for _clients container
This commit is contained in:
committed by
Mathieu Carbou
parent
a1627af702
commit
bb4eb89c8e
@@ -274,17 +274,6 @@ void AsyncEventSourceClient::_runQueue() {
|
|||||||
|
|
||||||
|
|
||||||
// Handler
|
// 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){
|
void AsyncEventSource::onConnect(ArEventHandlerFunction cb){
|
||||||
_connectcb = cb;
|
_connectcb = cb;
|
||||||
}
|
}
|
||||||
@@ -294,22 +283,12 @@ void AsyncEventSource::authorizeConnect(ArAuthorizeConnectHandler cb){
|
|||||||
}
|
}
|
||||||
|
|
||||||
void AsyncEventSource::_addClient(AsyncEventSourceClient * client){
|
void AsyncEventSource::_addClient(AsyncEventSourceClient * client){
|
||||||
/*char * temp = (char *)malloc(2054);
|
if (!client)
|
||||||
if(temp != NULL){
|
return;
|
||||||
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);
|
|
||||||
}*/
|
|
||||||
#ifdef ESP32
|
#ifdef ESP32
|
||||||
std::lock_guard<std::mutex> lock(_client_queue_lock);
|
std::lock_guard<std::mutex> lock(_client_queue_lock);
|
||||||
#endif
|
#endif
|
||||||
_clients.add(client);
|
_clients.emplace_back(client);
|
||||||
if(_connectcb)
|
if(_connectcb)
|
||||||
_connectcb(client);
|
_connectcb(client);
|
||||||
}
|
}
|
||||||
@@ -318,7 +297,10 @@ void AsyncEventSource::_handleDisconnect(AsyncEventSourceClient * client){
|
|||||||
#ifdef ESP32
|
#ifdef ESP32
|
||||||
std::lock_guard<std::mutex> lock(_client_queue_lock);
|
std::lock_guard<std::mutex> lock(_client_queue_lock);
|
||||||
#endif
|
#endif
|
||||||
_clients.remove(client);
|
for (auto i = _clients.begin(); i != _clients.end(); ++i){
|
||||||
|
if (i->get() == client)
|
||||||
|
_clients.erase(i);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void AsyncEventSource::close(){
|
void AsyncEventSource::close(){
|
||||||
@@ -341,9 +323,8 @@ size_t AsyncEventSource::avgPacketsWaiting() const {
|
|||||||
#ifdef ESP32
|
#ifdef ESP32
|
||||||
std::lock_guard<std::mutex> lock(_client_queue_lock);
|
std::lock_guard<std::mutex> lock(_client_queue_lock);
|
||||||
#endif
|
#endif
|
||||||
if (_clients.isEmpty()) {
|
if (!_clients.size()) return 0;
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
for(const auto &c: _clients){
|
for(const auto &c: _clients){
|
||||||
if(c->connected()) {
|
if(c->connected()) {
|
||||||
aql += c->packetsWaiting();
|
aql += c->packetsWaiting();
|
||||||
@@ -367,13 +348,14 @@ void AsyncEventSource::send(
|
|||||||
}
|
}
|
||||||
|
|
||||||
size_t AsyncEventSource::count() const {
|
size_t AsyncEventSource::count() const {
|
||||||
size_t n_clients;
|
|
||||||
#ifdef ESP32
|
#ifdef ESP32
|
||||||
std::lock_guard<std::mutex> lock(_client_queue_lock);
|
std::lock_guard<std::mutex> lock(_client_queue_lock);
|
||||||
#endif
|
#endif
|
||||||
n_clients = _clients.count_if([](AsyncEventSourceClient *c){
|
size_t n_clients{0};
|
||||||
return c->connected();
|
for (const auto &i : _clients)
|
||||||
});
|
if (i->connected())
|
||||||
|
++n_clients;
|
||||||
|
|
||||||
return n_clients;
|
return n_clients;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -110,17 +110,17 @@ class AsyncEventSourceClient {
|
|||||||
class AsyncEventSource: public AsyncWebHandler {
|
class AsyncEventSource: public AsyncWebHandler {
|
||||||
private:
|
private:
|
||||||
String _url;
|
String _url;
|
||||||
LinkedList<AsyncEventSourceClient *> _clients;
|
std::list< std::unique_ptr<AsyncEventSourceClient> > _clients;
|
||||||
#ifdef ESP32
|
#ifdef ESP32
|
||||||
// Same as for individual messages, protect mutations of _clients list
|
// Same as for individual messages, protect mutations of _clients list
|
||||||
// since simultaneous access from different tasks is possible
|
// since simultaneous access from different tasks is possible
|
||||||
mutable std::mutex _client_queue_lock;
|
mutable std::mutex _client_queue_lock;
|
||||||
#endif
|
#endif
|
||||||
ArEventHandlerFunction _connectcb;
|
ArEventHandlerFunction _connectcb{nullptr};
|
||||||
ArAuthorizeConnectHandler _authorizeConnectHandler;
|
ArAuthorizeConnectHandler _authorizeConnectHandler;
|
||||||
public:
|
public:
|
||||||
AsyncEventSource(const String& url);
|
AsyncEventSource(const String& url) : _url(url){};
|
||||||
~AsyncEventSource();
|
~AsyncEventSource(){ close(); };
|
||||||
|
|
||||||
const char * url() const { return _url.c_str(); }
|
const char * url() const { return _url.c_str(); }
|
||||||
void close();
|
void close();
|
||||||
|
Reference in New Issue
Block a user