From f7c5c452df5b0918357fc98003aafde9e18304f9 Mon Sep 17 00:00:00 2001 From: 0xFEEDC0DE64 Date: Sun, 20 Dec 2020 05:48:13 +0100 Subject: [PATCH] Code formatting --- src/AsyncWebSocket.cpp | 41 +++++++++--------- src/AsyncWebSynchronization.h | 78 +++++++++++++++++------------------ 2 files changed, 58 insertions(+), 61 deletions(-) diff --git a/src/AsyncWebSocket.cpp b/src/AsyncWebSocket.cpp index 82cb2c0..2b2f447 100644 --- a/src/AsyncWebSocket.cpp +++ b/src/AsyncWebSocket.cpp @@ -517,6 +517,7 @@ void AsyncWebSocketClient::_onAck(size_t len, uint32_t time) if (_status == WS_DISCONNECTING && head.opcode() == WS_DISCONNECT){ _controlQueue.pop(); _status = WS_DISCONNECTED; + l.unlock(); _client->close(true); return; } @@ -537,13 +538,15 @@ void AsyncWebSocketClient::_onPoll() { Serial.printf("AsyncWebSocketClient::_onPoll this=0x%llx task=0x%llx %s\r\n", uint64_t(this), uint64_t(xTaskGetCurrentTaskHandle()), pcTaskGetTaskName(xTaskGetCurrentTaskHandle())); - if(_client->canSend() && [this](){ AsyncWebLockGuard l(_lock, "AsyncWebSocketClient::_onPoll(1)"); return !_controlQueue.empty() || !_messageQueue.empty(); }()) + AsyncWebLockGuard l(_lock, "AsyncWebSocketClient::_onPoll"); + if(_client->canSend() && (!_controlQueue.empty() || !_messageQueue.empty())) { + l.unlock(); _runQueue(); } - else if(_keepAlivePeriod > 0 && (millis() - _lastMessageTime) >= _keepAlivePeriod && - [this](){ AsyncWebLockGuard l(_lock, "AsyncWebSocketClient::_onPoll(1)"); 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); } } @@ -552,27 +555,21 @@ void AsyncWebSocketClient::_runQueue() { Serial.printf("AsyncWebSocketClient::_runQueue this=0x%llx task=0x%llx %s\r\n", uint64_t(this), uint64_t(xTaskGetCurrentTaskHandle()), pcTaskGetTaskName(xTaskGetCurrentTaskHandle())); + AsyncWebLockGuard l(_lock, "AsyncWebSocketClient::_runQueue()"); + + while (!_messageQueue.empty() && _messageQueue.front().get().finished()) + _messageQueue.pop(); + + if (!_controlQueue.empty() && (_messageQueue.empty() || _messageQueue.front().get().betweenFrames()) && webSocketSendFrameWindow(_client) > (size_t)(_controlQueue.front().len() - 1)) { - AsyncWebLockGuard l(_lock, "AsyncWebSocketClient::_runQueue()"); - - while (!_messageQueue.empty() && _messageQueue.front().get().finished()) - { - _messageQueue.pop(); - } - - if (!_controlQueue.empty() && (_messageQueue.empty() || _messageQueue.front().get().betweenFrames()) && webSocketSendFrameWindow(_client) > (size_t)(_controlQueue.front().len() - 1)) - { - l.unlock(); - _controlQueue.front().send(_client); - } - else if (!_messageQueue.empty() && _messageQueue.front().get().betweenFrames() && webSocketSendFrameWindow(_client)) - { - l.unlock(); - _messageQueue.front().get().send(_client); - } + //l.unlock(); + _controlQueue.front().send(_client); + } + else if (!_messageQueue.empty() && _messageQueue.front().get().betweenFrames() && webSocketSendFrameWindow(_client)) + { + //l.unlock(); + _messageQueue.front().get().send(_client); } - - Serial.printf("AsyncWebSocketClient::_runQueue this=0x%llx task=0x%llx %s\r\n", uint64_t(this), uint64_t(xTaskGetCurrentTaskHandle()), pcTaskGetTaskName(xTaskGetCurrentTaskHandle())); } bool AsyncWebSocketClient::queueIsFull() const diff --git a/src/AsyncWebSynchronization.h b/src/AsyncWebSynchronization.h index 1837e93..3b08aa2 100644 --- a/src/AsyncWebSynchronization.h +++ b/src/AsyncWebSynchronization.h @@ -11,49 +11,49 @@ class AsyncWebLock { private: - SemaphoreHandle_t _lock; - mutable TaskHandle_t _lockedBy{}; - mutable const char *_lastLockerName; + SemaphoreHandle_t _lock; + mutable TaskHandle_t _lockedBy{}; + mutable const char *_lastLockerName; public: - const char * const lockName; - AsyncWebLock(const char *_lockName) : - lockName{_lockName} - { - _lock = xSemaphoreCreateBinary(); - _lockedBy = NULL; - xSemaphoreGive(_lock); - } - - ~AsyncWebLock() { - vSemaphoreDelete(_lock); - } - - bool lock(const char *lockerName) const { - const auto currentTask = xTaskGetCurrentTaskHandle(); - if (_lockedBy != currentTask) { - while (true) - { - Serial.printf("AsyncWebLock::lock this=0x%llx name=%s locker=%s task=0x%llx %s\r\n", uint64_t(this), lockName, lockerName, uint64_t(currentTask), pcTaskGetTaskName(currentTask)); - - if (xSemaphoreTake(_lock, 200 / portTICK_PERIOD_MS)) - break; - else - Serial.printf("AsyncWebLock::lock FAILED this=0x%llx name=%s locker=%s task=0x%llx %s lastLockedBy=%s\r\n", uint64_t(this), lockName, lockerName, uint64_t(currentTask), pcTaskGetTaskName(xTaskGetCurrentTaskHandle()), _lastLockerName); - } - _lockedBy = currentTask; - _lastLockerName = lockerName; - return true; + const char * const lockName; + AsyncWebLock(const char *_lockName) : + lockName{_lockName} + { + _lock = xSemaphoreCreateBinary(); + _lockedBy = NULL; + xSemaphoreGive(_lock); } - return false; - } - void unlock(const char *lockerName) const { - Serial.printf("AsyncWebLock::unlock this=0x%llx name=%s locker=%s task=0x%llx %s\r\n", uint64_t(this), lockName, lockerName, uint64_t(xTaskGetCurrentTaskHandle()), pcTaskGetTaskName(xTaskGetCurrentTaskHandle())); - _lockedBy = NULL; - _lastLockerName = NULL; - xSemaphoreGive(_lock); - } + ~AsyncWebLock() { + vSemaphoreDelete(_lock); + } + + bool lock(const char *lockerName) const { + const auto currentTask = xTaskGetCurrentTaskHandle(); + if (_lockedBy != currentTask) { + while (true) + { + Serial.printf("AsyncWebLock::lock this=0x%llx name=%s locker=%s task=0x%llx %s\r\n", uint64_t(this), lockName, lockerName, uint64_t(currentTask), pcTaskGetTaskName(currentTask)); + + if (xSemaphoreTake(_lock, 200 / portTICK_PERIOD_MS)) + break; + else + Serial.printf("AsyncWebLock::lock FAILED this=0x%llx name=%s locker=%s task=0x%llx %s lastLockedBy=%s\r\n", uint64_t(this), lockName, lockerName, uint64_t(currentTask), pcTaskGetTaskName(xTaskGetCurrentTaskHandle()), _lastLockerName); + } + _lockedBy = currentTask; + _lastLockerName = lockerName; + return true; + } + return false; + } + + void unlock(const char *lockerName) const { + Serial.printf("AsyncWebLock::unlock this=0x%llx name=%s locker=%s task=0x%llx %s\r\n", uint64_t(this), lockName, lockerName, uint64_t(xTaskGetCurrentTaskHandle()), pcTaskGetTaskName(xTaskGetCurrentTaskHandle())); + _lockedBy = NULL; + _lastLockerName = NULL; + xSemaphoreGive(_lock); + } }; #else