diff --git a/src/AsyncWebSocket.cpp b/src/AsyncWebSocket.cpp index 6bee38b..5a62b48 100644 --- a/src/AsyncWebSocket.cpp +++ b/src/AsyncWebSocket.cpp @@ -514,13 +514,16 @@ void AsyncWebSocketClient::_onAck(size_t len, uint32_t time){ _runQueue(); } -void AsyncWebSocketClient::_onPoll(){ - if(_client->canSend() && [this](){ AsyncWebLockGuard l(_lock); return !_controlQueue.empty() || !_messageQueue.empty(); }()) +void AsyncWebSocketClient::_onPoll() +{ + AsyncWebLockGuard l(_lock); + if(_client->canSend() && (!_controlQueue.empty() || !_messageQueue.empty())) { + l.unlock(); _runQueue(); - } else if(_keepAlivePeriod > 0 && (millis() - _lastMessageTime) >= _keepAlivePeriod && - [this](){ AsyncWebLockGuard l(_lock); return _controlQueue.empty() && _messageQueue.empty(); }()) + } else if(_keepAlivePeriod > 0 && (millis() - _lastMessageTime) >= _keepAlivePeriod && (_controlQueue.empty() && _messageQueue.empty())) { + l.unlock(); ping((uint8_t *)AWSC_PING_PAYLOAD, AWSC_PING_PAYLOAD_LEN); } } @@ -533,12 +536,12 @@ void AsyncWebSocketClient::_runQueue() if(!_controlQueue.empty() && (_messageQueue.empty() || _messageQueue.front().get().betweenFrames()) && webSocketSendFrameWindow(_client) > (size_t)(_controlQueue.front().len() - 1)) { - l.unlock(); + //l.unlock(); _controlQueue.front().send(_client); } else if (!_messageQueue.empty() && _messageQueue.front().get().betweenFrames() && webSocketSendFrameWindow(_client)) { - l.unlock(); + //l.unlock(); _messageQueue.front().get().send(_client); } } diff --git a/src/AsyncWebSynchronization.h b/src/AsyncWebSynchronization.h index 0591ea1..0ff8ab6 100644 --- a/src/AsyncWebSynchronization.h +++ b/src/AsyncWebSynchronization.h @@ -43,37 +43,38 @@ public: class AsyncWebLock { private: - SemaphoreHandle_t _lock; - mutable TaskHandle_t _lockedBy{}; + SemaphoreHandle_t _lock; + mutable TaskHandle_t _lockedBy{}; public: - AsyncWebLock() { - _lock = xSemaphoreCreateBinary(); - // In this fails, the system is likely that much out of memory that - // we should abort anyways. If assertions are disabled, nothing is lost.. - assert(_lock); - _lockedBy = NULL; - xSemaphoreGive(_lock); - } - - ~AsyncWebLock() { - vSemaphoreDelete(_lock); - } - - bool lock() const { - const auto currentTask = xTaskGetCurrentTaskHandle(); - if (_lockedBy != currentTask) { - xSemaphoreTake(_lock, portMAX_DELAY); - _lockedBy = currentTask; - return true; + AsyncWebLock() + { + _lock = xSemaphoreCreateBinary(); + // In this fails, the system is likely that much out of memory that + // we should abort anyways. If assertions are disabled, nothing is lost.. + assert(_lock); + _lockedBy = NULL; + xSemaphoreGive(_lock); } - return false; - } - void unlock() const { - _lockedBy = NULL; - xSemaphoreGive(_lock); - } + ~AsyncWebLock() { + vSemaphoreDelete(_lock); + } + + bool lock() const { + const auto currentTask = xTaskGetCurrentTaskHandle(); + if (_lockedBy != currentTask) { + xSemaphoreTake(_lock, portMAX_DELAY); + _lockedBy = currentTask; + return true; + } + return false; + } + + void unlock() const { + _lockedBy = NULL; + xSemaphoreGive(_lock); + } }; #else