@@ -110,7 +110,6 @@ AsyncEventSourceClient::AsyncEventSourceClient(AsyncWebServerRequest *request, A
110110 _client = request->client ();
111111 _server = server;
112112 _lastId = 0 ;
113- next = NULL ;
114113 if (request->hasHeader (" Last-Event-ID" ))
115114 _lastId = atoi (request->getHeader (" Last-Event-ID" )->value ().c_str ());
116115
@@ -119,7 +118,7 @@ AsyncEventSourceClient::AsyncEventSourceClient(AsyncWebServerRequest *request, A
119118 _client->onAck (NULL , NULL );
120119 _client->onPoll (NULL , NULL );
121120 _client->onData (NULL , NULL );
122- _client->onTimeout ([](void *r, AsyncClient* c, uint32_t time){ ((AsyncEventSourceClient*)(r))->_onTimeout (time); }, this );
121+ _client->onTimeout ([](void *r, AsyncClient* c __attribute__ ((unused)) , uint32_t time){ ((AsyncEventSourceClient*)(r))->_onTimeout (time); }, this );
123122 _client->onDisconnect ([](void *r, AsyncClient* c){ ((AsyncEventSourceClient*)(r))->_onDisconnect (); delete c; }, this );
124123 _server->_addClient (this );
125124 delete request;
@@ -129,7 +128,7 @@ AsyncEventSourceClient::~AsyncEventSourceClient(){
129128 close ();
130129}
131130
132- void AsyncEventSourceClient::_onTimeout (uint32_t time){
131+ void AsyncEventSourceClient::_onTimeout (uint32_t time __attribute__ ((unused)) ){
133132 _client->close (true );
134133}
135134
@@ -161,9 +160,9 @@ void AsyncEventSourceClient::send(const char *message, const char *event, uint32
161160
162161// Handler
163162
164- AsyncEventSource::AsyncEventSource (String url)
163+ AsyncEventSource::AsyncEventSource (const String& url)
165164 : _url(url)
166- , _clients(NULL )
165+ , _clients(LinkedList<AsyncEventSourceClient *>([](AsyncEventSourceClient *c){ delete c; }) )
167166 , _connectcb(NULL )
168167{}
169168
@@ -188,68 +187,38 @@ void AsyncEventSource::_addClient(AsyncEventSourceClient * client){
188187 client->write((const char *)temp, 2053);
189188 free(temp);
190189 }*/
191- if (_clients == NULL ){
192- _clients = client;
193- if (_connectcb)
194- _connectcb (client);
195- return ;
196- }
197- AsyncEventSourceClient * c = _clients;
198- while (c->next != NULL ) c = c->next ;
199- c->next = client;
190+
191+ _clients.add (client);
200192 if (_connectcb)
201193 _connectcb (client);
202194}
203195
204196void AsyncEventSource::_handleDisconnect (AsyncEventSourceClient * client){
205- if (_clients == NULL ){
206- return ;
207- }
208- if (_clients == client){
209- _clients = client->next ;
210- delete client;
211- return ;
212- }
213- AsyncEventSourceClient * c = _clients;
214- while (c->next != NULL && c->next != client) c = c->next ;
215- if (c->next == NULL ){
216- return ;
217- }
218- c->next = client->next ;
219- delete client;
197+ _clients.remove (client);
220198}
221199
222200void AsyncEventSource::close (){
223- AsyncEventSourceClient * c = _clients;
224- while (c != NULL ){
201+ for (const auto &c: _clients){
225202 if (c->connected ())
226203 c->close ();
227- c = c->next ;
228204 }
229205}
230206
231207void AsyncEventSource::send (const char *message, const char *event, uint32_t id, uint32_t reconnect){
232- if (_clients == NULL )
208+ if (_clients. isEmpty () )
233209 return ;
234210
235211 String ev = generateEventMessage (message, event, id, reconnect);
236- AsyncEventSourceClient * c = _clients;
237- while (c != NULL ){
212+ for (const auto &c: _clients){
238213 if (c->connected ())
239214 c->write (ev.c_str (), ev.length ());
240- c = c->next ;
241215 }
242216}
243217
244- size_t AsyncEventSource::count (){
245- size_t i = 0 ;
246- AsyncEventSourceClient * c = _clients;
247- while (c != NULL ){
248- if (c->connected ())
249- i++;
250- c = c->next ;
251- }
252- return i;
218+ size_t AsyncEventSource::count () const {
219+ return _clients.count_if ([](AsyncEventSourceClient *c){
220+ return c->connected ();
221+ });
253222}
254223
255224bool AsyncEventSource::canHandle (AsyncWebServerRequest *request){
@@ -280,7 +249,7 @@ void AsyncEventSourceResponse::_respond(AsyncWebServerRequest *request){
280249 _state = RESPONSE_WAIT_ACK;
281250}
282251
283- size_t AsyncEventSourceResponse::_ack (AsyncWebServerRequest *request, size_t len, uint32_t time){
252+ size_t AsyncEventSourceResponse::_ack (AsyncWebServerRequest *request, size_t len, uint32_t time __attribute__ ((unused)) ){
284253 if (len){
285254 new AsyncEventSourceClient (request, _server);
286255 }
0 commit comments