From 7a0d05849a99b34080bddd34a4e8797bf04932b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alex=20Villac=C3=ADs=20Lasso?= Date: Sat, 2 Jan 2021 20:15:21 -0500 Subject: [PATCH] Some code formatting Fraction of commit 6ac642b019dda9bd703a5b9c35e6f1a5a7738aa9 of dumbfixes branch of 0xFEEDC0DE64 fork of ESPAsyncWebServer. Additionally, lock is no longer released between _clearQueue() and _runQueue() in _onAck(). --- src/AsyncWebSocket.cpp | 85 ++++++++++++++++++++++-------------------- 1 file changed, 44 insertions(+), 41 deletions(-) diff --git a/src/AsyncWebSocket.cpp b/src/AsyncWebSocket.cpp index 58bc457..de81f3e 100644 --- a/src/AsyncWebSocket.cpp +++ b/src/AsyncWebSocket.cpp @@ -307,31 +307,29 @@ void AsyncWebSocketClient::_clearQueue() void AsyncWebSocketClient::_onAck(size_t len, uint32_t time){ _lastMessageTime = millis(); - { - AsyncWebLockGuard l(_lock); + AsyncWebLockGuard l(_lock); - if (!_controlQueue.empty()) { - auto &head = _controlQueue.front(); - if (head.finished()){ - len -= head.len(); - if (_status == WS_DISCONNECTING && head.opcode() == WS_DISCONNECT){ - _controlQueue.pop_front(); - _status = WS_DISCONNECTED; - l.unlock(); - _client->close(true); - return; - } + if (!_controlQueue.empty()) { + auto &head = _controlQueue.front(); + if (head.finished()){ + len -= head.len(); + if (_status == WS_DISCONNECTING && head.opcode() == WS_DISCONNECT){ _controlQueue.pop_front(); + _status = WS_DISCONNECTED; + l.unlock(); + _client->close(true); + return; } + _controlQueue.pop_front(); } - - if(len && !_messageQueue.empty()){ - _messageQueue.front().ack(len, time); - } - - _clearQueue(); } + if(len && !_messageQueue.empty()){ + _messageQueue.front().ack(len, time); + } + + _clearQueue(); + _runQueue(); } @@ -428,29 +426,34 @@ void AsyncWebSocketClient::_queueMessage(std::shared_ptr> b _runQueue(); } -void AsyncWebSocketClient::close(uint16_t code, const char * message){ - if(_status != WS_CONNECTED) - return; - if(code){ - uint8_t packetLen = 2; - if(message != NULL){ - size_t mlen = strlen(message); - if(mlen > 123) mlen = 123; - packetLen += mlen; +void AsyncWebSocketClient::close(uint16_t code, const char * message) +{ + if(_status != WS_CONNECTED) + return; + + if(code) + { + uint8_t packetLen = 2; + if (message != NULL) + { + size_t mlen = strlen(message); + if(mlen > 123) mlen = 123; + packetLen += mlen; + } + char * buf = (char*)malloc(packetLen); + if (buf != NULL) + { + buf[0] = (uint8_t)(code >> 8); + buf[1] = (uint8_t)(code & 0xFF); + if(message != NULL){ + memcpy(buf+2, message, packetLen -2); + } + _queueControl(WS_DISCONNECT, (uint8_t*)buf, packetLen); + free(buf); + return; + } } - char * buf = (char*)malloc(packetLen); - if(buf != NULL){ - buf[0] = (uint8_t)(code >> 8); - buf[1] = (uint8_t)(code & 0xFF); - if(message != NULL){ - memcpy(buf+2, message, packetLen -2); - } - _queueControl(WS_DISCONNECT, (uint8_t*)buf, packetLen); - free(buf); - return; - } - } - _queueControl(WS_DISCONNECT); + _queueControl(WS_DISCONNECT); } void AsyncWebSocketClient::ping(const uint8_t *data, size_t len)