@@ -357,11 +357,12 @@ void AsyncWebSocketClient::_onAck(size_t len, uint32_t time) {
357357}
358358
359359void AsyncWebSocketClient::_onPoll () {
360+ asyncsrv::unique_lock_type lock (_lock);
361+
360362 if (!_client) {
361363 return ;
362364 }
363365
364- asyncsrv::unique_lock_type lock (_lock);
365366 if (_client && _client->canSend () && (!_controlQueue.empty () || !_messageQueue.empty ())) {
366367 _runQueue ();
367368 } else if (_keepAlivePeriod > 0 && (millis () - _lastMessageTime) >= _keepAlivePeriod && (_controlQueue.empty () && _messageQueue.empty ())) {
@@ -444,12 +445,12 @@ bool AsyncWebSocketClient::canSend() const {
444445}
445446
446447bool AsyncWebSocketClient::_queueControl (uint8_t opcode, const uint8_t *data, size_t len, bool mask) {
448+ asyncsrv::lock_guard_type lock (_lock);
449+
447450 if (!_client) {
448451 return false ;
449452 }
450453
451- asyncsrv::lock_guard_type lock (_lock);
452-
453454 _controlQueue.emplace_back (opcode, data, len, mask);
454455 async_ws_log_v (" [%s][%" PRIu32 " ] QUEUE CTRL (%u) << %" PRIu8, _server->url (), _clientId, _controlQueue.size (), opcode);
455456
@@ -461,12 +462,12 @@ bool AsyncWebSocketClient::_queueControl(uint8_t opcode, const uint8_t *data, si
461462}
462463
463464bool AsyncWebSocketClient::_queueMessage (AsyncWebSocketSharedBuffer buffer, uint8_t opcode, bool mask) {
464- if (!_client || buffer->size () == 0 || _status != WS_CONNECTED) {
465+ asyncsrv::unique_lock_type lock (_lock);
466+
467+ if (!_client || !buffer || buffer->empty () || _status != WS_CONNECTED) {
465468 return false ;
466469 }
467470
468- asyncsrv::unique_lock_type lock (_lock);
469-
470471 if (_messageQueue.size () >= WS_MAX_QUEUED_MESSAGES) {
471472 if (closeWhenFull) {
472473 _status = WS_DISCONNECTED;
@@ -545,6 +546,7 @@ void AsyncWebSocketClient::_onError(int8_t err) {
545546}
546547
547548void AsyncWebSocketClient::_onTimeout (uint32_t time) {
549+ asyncsrv::lock_guard_type lock (_lock);
548550 if (!_client) {
549551 return ;
550552 }
@@ -553,7 +555,9 @@ void AsyncWebSocketClient::_onTimeout(uint32_t time) {
553555}
554556
555557void AsyncWebSocketClient::_onDisconnect () {
558+ asyncsrv::lock_guard_type lock (_lock);
556559 async_ws_log_v (" [%s][%" PRIu32 " ] DISCONNECT" , _server->url (), _clientId);
560+ _status = WS_DISCONNECTED;
557561 _client = nullptr ;
558562 _server->_handleDisconnect (this );
559563}
@@ -947,6 +951,8 @@ bool AsyncWebSocketClient::binary(const __FlashStringHelper *data, size_t len) {
947951#endif
948952
949953IPAddress AsyncWebSocketClient::remoteIP () const {
954+ asyncsrv::lock_guard_type lock (_lock);
955+
950956 if (!_client) {
951957 return IPAddress ((uint32_t )0U );
952958 }
@@ -955,6 +961,8 @@ IPAddress AsyncWebSocketClient::remoteIP() const {
955961}
956962
957963uint16_t AsyncWebSocketClient::remotePort () const {
964+ asyncsrv::lock_guard_type lock (_lock);
965+
958966 if (!_client) {
959967 return 0 ;
960968 }
@@ -1031,6 +1039,7 @@ AsyncWebSocketClient *AsyncWebSocket::client(uint32_t id) {
10311039}
10321040
10331041void AsyncWebSocket::close (uint32_t id, uint16_t code, const char *message) {
1042+ asyncsrv::lock_guard_type lock (_lock);
10341043 if (AsyncWebSocketClient *c = client (id)) {
10351044 c->close (code, message);
10361045 }
@@ -1062,6 +1071,7 @@ void AsyncWebSocket::cleanupClients(uint16_t maxClients) {
10621071}
10631072
10641073bool AsyncWebSocket::ping (uint32_t id, const uint8_t *data, size_t len) {
1074+ asyncsrv::lock_guard_type lock (_lock);
10651075 AsyncWebSocketClient *c = client (id);
10661076 return c && c->ping (data, len);
10671077}
@@ -1081,6 +1091,7 @@ AsyncWebSocket::SendStatus AsyncWebSocket::pingAll(const uint8_t *data, size_t l
10811091}
10821092
10831093bool AsyncWebSocket::text (uint32_t id, const uint8_t *message, size_t len) {
1094+ asyncsrv::lock_guard_type lock (_lock);
10841095 AsyncWebSocketClient *c = client (id);
10851096 return c && c->text (makeSharedBuffer (message, len));
10861097}
@@ -1127,6 +1138,7 @@ bool AsyncWebSocket::text(uint32_t id, AsyncWebSocketMessageBuffer *buffer) {
11271138 return enqueued;
11281139}
11291140bool AsyncWebSocket::text (uint32_t id, AsyncWebSocketSharedBuffer buffer) {
1141+ asyncsrv::lock_guard_type lock (_lock);
11301142 AsyncWebSocketClient *c = client (id);
11311143 return c && c->text (buffer);
11321144}
@@ -1190,6 +1202,7 @@ AsyncWebSocket::SendStatus AsyncWebSocket::textAll(AsyncWebSocketSharedBuffer bu
11901202}
11911203
11921204bool AsyncWebSocket::binary (uint32_t id, const uint8_t *message, size_t len) {
1205+ asyncsrv::lock_guard_type lock (_lock);
11931206 AsyncWebSocketClient *c = client (id);
11941207 return c && c->binary (makeSharedBuffer (message, len));
11951208}
@@ -1226,6 +1239,7 @@ bool AsyncWebSocket::binary(uint32_t id, AsyncWebSocketMessageBuffer *buffer) {
12261239 return enqueued;
12271240}
12281241bool AsyncWebSocket::binary (uint32_t id, AsyncWebSocketSharedBuffer buffer) {
1242+ asyncsrv::lock_guard_type lock (_lock);
12291243 AsyncWebSocketClient *c = client (id);
12301244 return c && c->binary (buffer);
12311245}
0 commit comments