@@ -540,7 +540,6 @@ AsyncWebSocketClient::AsyncWebSocketClient(AsyncWebServerRequest *request, Async
540
540
_client->onTimeout ([](void *r, AsyncClient* c, uint32_t time){ (void )c; ((AsyncWebSocketClient*)(r))->_onTimeout (time); }, this );
541
541
_client->onData ([](void *r, AsyncClient* c, void *buf, size_t len){ (void )c; ((AsyncWebSocketClient*)(r))->_onData (buf, len); }, this );
542
542
_client->onPoll ([](void *r, AsyncClient* c){ (void )c; ((AsyncWebSocketClient*)(r))->_onPoll (); }, this );
543
- _server->_addClient (this );
544
543
_server->_handleEvent (this , WS_EVT_CONNECT, request, NULL , 0 );
545
544
delete request;
546
545
memset (&_pinfo,0 ,sizeof (_pinfo));
@@ -614,9 +613,8 @@ void AsyncWebSocketClient::_runQueue(){
614
613
_clearQueue ();
615
614
}
616
615
617
- bool AsyncWebSocketClient::queueIsFull (){
618
- if ((_messageQueue.length () >= WS_MAX_QUEUED_MESSAGES) || (_status != WS_CONNECTED) ) return true ;
619
- return false ;
616
+ bool AsyncWebSocketClient::queueIsFull () const {
617
+ return (_messageQueue.length () >= WS_MAX_QUEUED_MESSAGES) || (_status != WS_CONNECTED);
620
618
}
621
619
622
620
void AsyncWebSocketClient::_queueMessage (AsyncWebSocketMessage *dataMessage){
@@ -909,14 +907,14 @@ void AsyncWebSocketClient::binary(AsyncWebSocketMessageBuffer * buffer)
909
907
_queueMessage (new AsyncWebSocketMultiMessage (buffer, WS_BINARY));
910
908
}
911
909
912
- IPAddress AsyncWebSocketClient::remoteIP () {
910
+ IPAddress AsyncWebSocketClient::remoteIP () const {
913
911
if (!_client) {
914
912
return IPAddress (0U );
915
913
}
916
914
return _client->remoteIP ();
917
915
}
918
916
919
- uint16_t AsyncWebSocketClient::remotePort () {
917
+ uint16_t AsyncWebSocketClient::remotePort () const {
920
918
if (!_client) {
921
919
return 0 ;
922
920
}
@@ -931,7 +929,6 @@ uint16_t AsyncWebSocketClient::remotePort() {
931
929
932
930
AsyncWebSocket::AsyncWebSocket (const String& url)
933
931
:_url(url)
934
- ,_clients(LinkedList<AsyncWebSocketClient *>([](AsyncWebSocketClient *c){ delete c; }))
935
932
,_cNextId(1 )
936
933
,_enabled(true )
937
934
{
@@ -946,64 +943,65 @@ void AsyncWebSocket::_handleEvent(AsyncWebSocketClient * client, AwsEventType ty
946
943
}
947
944
}
948
945
949
- void AsyncWebSocket::_addClient (AsyncWebSocketClient * client){
950
- _clients.add (client);
946
+ AsyncWebSocketClient *AsyncWebSocket::_newClient (AsyncWebServerRequest *request)
947
+ {
948
+ _clients.emplace_back (request, this );
949
+ return &_clients.back ();
951
950
}
952
951
953
952
void AsyncWebSocket::_handleDisconnect (AsyncWebSocketClient * client){
954
-
955
- _clients.remove_first ([=](AsyncWebSocketClient * c){
956
- return c->id () == client->id ();
957
- });
953
+ const auto client_id = client->id ();
954
+ const auto iter = std::find_if (std::begin (_clients), std::end (_clients),
955
+ [client_id](const AsyncWebSocketClient &c){ return c.id () == client_id; });
956
+ if (iter != std::end (_clients))
957
+ _clients.erase (iter);
958
958
}
959
959
960
960
bool AsyncWebSocket::availableForWriteAll (){
961
- for (const auto & c: _clients){
962
- if (c->queueIsFull ()) return false ;
963
- }
964
- return true ;
961
+ return std::none_of (std::begin (_clients), std::end (_clients),
962
+ [](const AsyncWebSocketClient &c){ return c.queueIsFull (); });
965
963
}
966
964
967
965
bool AsyncWebSocket::availableForWrite (uint32_t id){
968
- for (const auto & c: _clients){
969
- if (c->queueIsFull () && (c->id () == id )) return false ;
970
- }
971
- return true ;
966
+ const auto iter = std::find_if (std::begin (_clients), std::end (_clients),
967
+ [id](const AsyncWebSocketClient &c){ return c.id () == id; });
968
+ if (iter == std::end (_clients))
969
+ return true ; // don't know why me-no-dev decided like this?
970
+ return !iter->queueIsFull ();
972
971
}
973
972
974
973
size_t AsyncWebSocket::count () const {
975
- return _clients.count_if ([](AsyncWebSocketClient * c){
976
- return c->status () == WS_CONNECTED;
977
- });
974
+ return std::count_if (std::begin (_clients), std::end (_clients),
975
+ [](const AsyncWebSocketClient &c){ return c.status () == WS_CONNECTED; });
978
976
}
979
977
980
978
AsyncWebSocketClient * AsyncWebSocket::client (uint32_t id){
981
- for ( const auto &c: _clients){
982
- if (c-> id () == id && c-> status () == WS_CONNECTED){
983
- return c;
984
- }
985
- }
986
- return nullptr ;
979
+ const auto iter = std::find_if ( std::begin (_clients), std::end ( _clients),
980
+ [id]( const AsyncWebSocketClient &c){ return c. id () == id && c. status () == WS_CONNECTED; });
981
+ if (iter == std::end (_clients))
982
+ return nullptr ;
983
+
984
+ return &(*iter) ;
987
985
}
988
986
989
987
990
988
void AsyncWebSocket::close (uint32_t id, uint16_t code, const char * message){
991
- AsyncWebSocketClient * c = client (id);
992
- if (c)
989
+ AsyncWebSocketClient *c = client (id);
990
+ if (c)
993
991
c->close (code, message);
994
992
}
995
993
996
994
void AsyncWebSocket::closeAll (uint16_t code, const char * message){
997
- for (const auto & c: _clients){
998
- if (c-> status () == WS_CONNECTED)
999
- c-> close (code, message);
995
+ for (auto & c: _clients){
996
+ if (c. status () == WS_CONNECTED)
997
+ c. close (code, message);
1000
998
}
1001
999
}
1002
1000
1003
1001
void AsyncWebSocket::cleanupClients (uint16_t maxClients)
1004
1002
{
1005
1003
if (count () > maxClients){
1006
- _clients.front ()-> close ();
1004
+ _clients.front (). close ();
1007
1005
}
1008
1006
}
1009
1007
@@ -1014,24 +1012,24 @@ void AsyncWebSocket::ping(uint32_t id, uint8_t *data, size_t len){
1014
1012
}
1015
1013
1016
1014
void AsyncWebSocket::pingAll (uint8_t *data, size_t len){
1017
- for (const auto & c: _clients){
1018
- if (c-> status () == WS_CONNECTED)
1019
- c-> ping (data, len);
1015
+ for (auto & c: _clients){
1016
+ if (c. status () == WS_CONNECTED)
1017
+ c. ping (data, len);
1020
1018
}
1021
1019
}
1022
1020
1023
1021
void AsyncWebSocket::text (uint32_t id, const char * message, size_t len){
1024
- AsyncWebSocketClient * c = client (id);
1022
+ AsyncWebSocketClient *c = client (id);
1025
1023
if (c)
1026
1024
c->text (message, len);
1027
1025
}
1028
1026
1029
1027
void AsyncWebSocket::textAll (AsyncWebSocketMessageBuffer * buffer){
1030
1028
if (!buffer) return ;
1031
1029
buffer->lock ();
1032
- for (const auto & c: _clients){
1033
- if (c-> status () == WS_CONNECTED){
1034
- c-> text (buffer);
1030
+ for (auto & c: _clients){
1031
+ if (c. status () == WS_CONNECTED){
1032
+ c. text (buffer);
1035
1033
}
1036
1034
}
1037
1035
buffer->unlock ();
@@ -1060,24 +1058,24 @@ void AsyncWebSocket::binaryAll(AsyncWebSocketMessageBuffer * buffer)
1060
1058
{
1061
1059
if (!buffer) return ;
1062
1060
buffer->lock ();
1063
- for (const auto & c: _clients){
1064
- if (c-> status () == WS_CONNECTED)
1065
- c-> binary (buffer);
1061
+ for (auto & c: _clients){
1062
+ if (c. status () == WS_CONNECTED)
1063
+ c. binary (buffer);
1066
1064
}
1067
1065
buffer->unlock ();
1068
1066
_cleanBuffers ();
1069
1067
}
1070
1068
1071
1069
void AsyncWebSocket::message (uint32_t id, AsyncWebSocketMessage *message){
1072
- AsyncWebSocketClient * c = client (id);
1073
- if (c)
1070
+ AsyncWebSocketClient *c = client (id);
1071
+ if (c)
1074
1072
c->message (message);
1075
1073
}
1076
1074
1077
1075
void AsyncWebSocket::messageAll (AsyncWebSocketMultiMessage *message){
1078
- for (const auto & c: _clients){
1079
- if (c-> status () == WS_CONNECTED)
1080
- c-> message (message);
1076
+ for (auto & c: _clients){
1077
+ if (c. status () == WS_CONNECTED)
1078
+ c. message (message);
1081
1079
}
1082
1080
_cleanBuffers ();
1083
1081
}
@@ -1186,9 +1184,9 @@ void AsyncWebSocket::textAll(const String &message){
1186
1184
textAll (message.c_str (), message.length ());
1187
1185
}
1188
1186
void AsyncWebSocket::textAll (const __FlashStringHelper *message){
1189
- for (const auto & c: _clients){
1190
- if (c-> status () == WS_CONNECTED)
1191
- c-> text (message);
1187
+ for (auto & c: _clients){
1188
+ if (c. status () == WS_CONNECTED)
1189
+ c. text (message);
1192
1190
}
1193
1191
}
1194
1192
void AsyncWebSocket::binary (uint32_t id, const char * message){
@@ -1221,9 +1219,9 @@ void AsyncWebSocket::binaryAll(const String &message){
1221
1219
binaryAll (message.c_str (), message.length ());
1222
1220
}
1223
1221
void AsyncWebSocket::binaryAll (const __FlashStringHelper *message, size_t len){
1224
- for (const auto & c: _clients){
1225
- if (c-> status () == WS_CONNECTED)
1226
- c-> binary (message, len);
1222
+ for (auto & c: _clients){
1223
+ if (c. status () == WS_CONNECTED)
1224
+ c. binary (message, len);
1227
1225
}
1228
1226
}
1229
1227
@@ -1333,7 +1331,7 @@ void AsyncWebSocket::_cleanBuffers()
1333
1331
}
1334
1332
}
1335
1333
1336
- AsyncWebSocket::AsyncWebSocketClientLinkedList AsyncWebSocket::getClients () const {
1334
+ const AsyncWebSocket::AsyncWebSocketClientLinkedList & AsyncWebSocket::getClients () const {
1337
1335
return _clients;
1338
1336
}
1339
1337
@@ -1391,7 +1389,7 @@ void AsyncWebSocketResponse::_respond(AsyncWebServerRequest *request){
1391
1389
size_t AsyncWebSocketResponse::_ack (AsyncWebServerRequest *request, size_t len, uint32_t time){
1392
1390
(void )time;
1393
1391
if (len){
1394
- new AsyncWebSocketClient (request, _server );
1392
+ _server-> _newClient (request);
1395
1393
}
1396
1394
return 0 ;
1397
1395
}
0 commit comments