Skip to content

Commit 61b3411

Browse files
committed
Rename _lock to _queue_lock for clarity and fixed the wrong usage of _queue_lock
_queue_lock is a lock guarding the queues and should not be used to guard the internal client pointer
1 parent 07a20e6 commit 61b3411

2 files changed

Lines changed: 10 additions & 19 deletions

File tree

src/AsyncWebSocket.cpp

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ AsyncWebSocketClient::AsyncWebSocketClient(AsyncClient *client, AsyncWebSocket *
297297

298298
AsyncWebSocketClient::~AsyncWebSocketClient() {
299299
{
300-
asyncsrv::lock_guard_type lock(_lock);
300+
asyncsrv::lock_guard_type lock(_queue_lock);
301301
_messageQueue.clear();
302302
_controlQueue.clear();
303303
}
@@ -313,7 +313,7 @@ void AsyncWebSocketClient::_clearQueue() {
313313
void AsyncWebSocketClient::_onAck(size_t len, uint32_t time) {
314314
_lastMessageTime = millis();
315315

316-
asyncsrv::unique_lock_type lock(_lock);
316+
asyncsrv::unique_lock_type lock(_queue_lock);
317317

318318
async_ws_log_v("[%s][%" PRIu32 "] START ACK(%u, %" PRIu32 ") Q:%u", _server->url(), _clientId, len, time, _messageQueue.size());
319319

@@ -357,7 +357,7 @@ void AsyncWebSocketClient::_onAck(size_t len, uint32_t time) {
357357
}
358358

359359
void AsyncWebSocketClient::_onPoll() {
360-
asyncsrv::unique_lock_type lock(_lock);
360+
asyncsrv::unique_lock_type lock(_queue_lock);
361361

362362
if (!_client) {
363363
return;
@@ -430,22 +430,22 @@ void AsyncWebSocketClient::_runQueue() {
430430
}
431431

432432
bool AsyncWebSocketClient::queueIsFull() const {
433-
asyncsrv::lock_guard_type lock(_lock);
433+
asyncsrv::lock_guard_type lock(_queue_lock);
434434
return (_messageQueue.size() >= WS_MAX_QUEUED_MESSAGES) || (_status != WS_CONNECTED);
435435
}
436436

437437
size_t AsyncWebSocketClient::queueLen() const {
438-
asyncsrv::lock_guard_type lock(_lock);
438+
asyncsrv::lock_guard_type lock(_queue_lock);
439439
return _messageQueue.size();
440440
}
441441

442442
bool AsyncWebSocketClient::canSend() const {
443-
asyncsrv::lock_guard_type lock(_lock);
443+
asyncsrv::lock_guard_type lock(_queue_lock);
444444
return _messageQueue.size() < WS_MAX_QUEUED_MESSAGES;
445445
}
446446

447447
bool AsyncWebSocketClient::_queueControl(uint8_t opcode, const uint8_t *data, size_t len, bool mask) {
448-
asyncsrv::lock_guard_type lock(_lock);
448+
asyncsrv::lock_guard_type lock(_queue_lock);
449449

450450
if (!_client) {
451451
return false;
@@ -462,7 +462,7 @@ bool AsyncWebSocketClient::_queueControl(uint8_t opcode, const uint8_t *data, si
462462
}
463463

464464
bool AsyncWebSocketClient::_queueMessage(AsyncWebSocketSharedBuffer buffer, uint8_t opcode, bool mask) {
465-
asyncsrv::unique_lock_type lock(_lock);
465+
asyncsrv::unique_lock_type lock(_queue_lock);
466466

467467
if (!_client || !buffer || buffer->empty() || _status != WS_CONNECTED) {
468468
return false;
@@ -546,7 +546,6 @@ void AsyncWebSocketClient::_onError(int8_t err) {
546546
}
547547

548548
void AsyncWebSocketClient::_onTimeout(uint32_t time) {
549-
asyncsrv::lock_guard_type lock(_lock);
550549
if (!_client) {
551550
return;
552551
}
@@ -555,7 +554,6 @@ void AsyncWebSocketClient::_onTimeout(uint32_t time) {
555554
}
556555

557556
void AsyncWebSocketClient::_onDisconnect() {
558-
asyncsrv::lock_guard_type lock(_lock);
559557
async_ws_log_v("[%s][%" PRIu32 "] DISCONNECT", _server->url(), _clientId);
560558
_status = WS_DISCONNECTED;
561559
_client = nullptr;
@@ -951,22 +949,16 @@ bool AsyncWebSocketClient::binary(const __FlashStringHelper *data, size_t len) {
951949
#endif
952950

953951
IPAddress AsyncWebSocketClient::remoteIP() const {
954-
asyncsrv::lock_guard_type lock(_lock);
955-
956952
if (!_client) {
957953
return IPAddress((uint32_t)0U);
958954
}
959-
960955
return _client->remoteIP();
961956
}
962957

963958
uint16_t AsyncWebSocketClient::remotePort() const {
964-
asyncsrv::lock_guard_type lock(_lock);
965-
966959
if (!_client) {
967960
return 0;
968961
}
969-
970962
return _client->remotePort();
971963
}
972964

src/AsyncWebSocket.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ class AsyncWebSocketClient {
222222
uint8_t _pstate;
223223
uint32_t _lastMessageTime;
224224
uint32_t _keepAlivePeriod;
225-
mutable asyncsrv::mutex_type _lock;
225+
mutable asyncsrv::mutex_type _queue_lock;
226226
std::deque<AsyncWebSocketControl> _controlQueue;
227227
std::deque<AsyncWebSocketMessage> _messageQueue;
228228
bool closeWhenFull = true;
@@ -303,7 +303,6 @@ class AsyncWebSocketClient {
303303
uint16_t remotePort() const;
304304

305305
bool shouldBeDeleted() const {
306-
asyncsrv::lock_guard_type lock(_lock);
307306
return !_client;
308307
}
309308

@@ -371,7 +370,7 @@ class AsyncWebSocket : public AsyncWebHandler {
371370
AwsEventHandler _eventHandler;
372371
AwsHandshakeHandler _handshakeHandler;
373372
bool _enabled;
374-
mutable asyncsrv::mutex_type _lock;
373+
mutable asyncsrv::mutex_type _ws_clients_lock;
375374

376375
public:
377376
typedef enum {

0 commit comments

Comments
 (0)