@@ -298,38 +298,34 @@ void AsyncEventSource::_addClient(AsyncEventSourceClient * client){
298
298
client->write((const char *)temp, 2053);
299
299
free(temp);
300
300
}*/
301
- _client_queue_lock. lock ( );
301
+ AsyncWebLockGuard l (_client_queue_lock );
302
302
_clients.add (client);
303
303
if (_connectcb)
304
304
_connectcb (client);
305
- _client_queue_lock.unlock ();
306
305
}
307
306
308
307
void AsyncEventSource::_handleDisconnect (AsyncEventSourceClient * client){
309
- _client_queue_lock. lock ( );
308
+ AsyncWebLockGuard l (_client_queue_lock );
310
309
_clients.remove (client);
311
- _client_queue_lock.unlock ();
312
310
}
313
311
314
312
void AsyncEventSource::close (){
315
313
// While the whole loop is not done, the linked list is locked and so the
316
314
// iterator should remain valid even when AsyncEventSource::_handleDisconnect()
317
315
// is called very early
318
- _client_queue_lock. lock ( );
316
+ AsyncWebLockGuard l (_client_queue_lock );
319
317
for (const auto &c: _clients){
320
318
if (c->connected ())
321
319
c->close ();
322
320
}
323
- _client_queue_lock.unlock ();
324
321
}
325
322
326
323
// pmb fix
327
324
size_t AsyncEventSource::avgPacketsWaiting () const {
328
325
size_t aql = 0 ;
329
326
uint32_t nConnectedClients = 0 ;
330
- _client_queue_lock. lock ( );
327
+ AsyncWebLockGuard l (_client_queue_lock );
331
328
if (_clients.isEmpty ()) {
332
- _client_queue_lock.unlock ();
333
329
return 0 ;
334
330
}
335
331
for (const auto &c: _clients){
@@ -338,29 +334,26 @@ size_t AsyncEventSource::avgPacketsWaiting() const {
338
334
++nConnectedClients;
339
335
}
340
336
}
341
- _client_queue_lock.unlock ();
342
337
return ((aql) + (nConnectedClients/2 )) / (nConnectedClients); // round up
343
338
}
344
339
345
340
void AsyncEventSource::send (
346
341
const char *message, const char *event, uint32_t id, uint32_t reconnect){
347
342
String ev = generateEventMessage (message, event, id, reconnect);
348
- _client_queue_lock. lock ( );
343
+ AsyncWebLockGuard l (_client_queue_lock );
349
344
for (const auto &c: _clients){
350
345
if (c->connected ()) {
351
346
c->_write (ev.c_str (), ev.length ());
352
347
}
353
348
}
354
- _client_queue_lock.unlock ();
355
349
}
356
350
357
351
size_t AsyncEventSource::count () const {
358
352
size_t n_clients;
359
- _client_queue_lock. lock ( );
353
+ AsyncWebLockGuard l (_client_queue_lock );
360
354
n_clients = _clients.count_if ([](AsyncEventSourceClient *c){
361
355
return c->connected ();
362
356
});
363
- _client_queue_lock.unlock ();
364
357
return n_clients;
365
358
}
366
359
0 commit comments