From 04672f45729599538c1ed10eb97edd12d55ee46e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alex=20Villac=C3=ADs=20Lasso?= Date: Sat, 2 Jan 2021 18:06:19 -0500 Subject: [PATCH] Code formatting plus queue unlocking change Based on commit f7c5c452df5b0918357fc98003aafde9e18304f9 of dumbfixes branch of 0xFEEDC0DE64 fork of ESPAsyncWebServer. In addition to indentation changes, now the queues are no longer unlocked before attempting to write the messages to the socket. --- src/AsyncWebSocket.cpp | 15 ++++++---- src/AsyncWebSynchronization.h | 55 ++++++++++++++++++----------------- 2 files changed, 37 insertions(+), 33 deletions(-) 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