Skip to content

Commit 04672f4

Browse files
committed
Code formatting plus queue unlocking change
Based on commit f7c5c45 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.
1 parent 1e8d966 commit 04672f4

File tree

2 files changed

+35
-31
lines changed

2 files changed

+35
-31
lines changed

src/AsyncWebSocket.cpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -514,13 +514,16 @@ void AsyncWebSocketClient::_onAck(size_t len, uint32_t time){
514514
_runQueue();
515515
}
516516

517-
void AsyncWebSocketClient::_onPoll(){
518-
if(_client->canSend() && [this](){ AsyncWebLockGuard l(_lock); return !_controlQueue.empty() || !_messageQueue.empty(); }())
517+
void AsyncWebSocketClient::_onPoll()
518+
{
519+
AsyncWebLockGuard l(_lock);
520+
if(_client->canSend() && (!_controlQueue.empty() || !_messageQueue.empty()))
519521
{
522+
l.unlock();
520523
_runQueue();
521-
} else if(_keepAlivePeriod > 0 && (millis() - _lastMessageTime) >= _keepAlivePeriod &&
522-
[this](){ AsyncWebLockGuard l(_lock); return _controlQueue.empty() && _messageQueue.empty(); }())
524+
} else if(_keepAlivePeriod > 0 && (millis() - _lastMessageTime) >= _keepAlivePeriod && (_controlQueue.empty() && _messageQueue.empty()))
523525
{
526+
l.unlock();
524527
ping((uint8_t *)AWSC_PING_PAYLOAD, AWSC_PING_PAYLOAD_LEN);
525528
}
526529
}
@@ -533,12 +536,12 @@ void AsyncWebSocketClient::_runQueue()
533536

534537
if(!_controlQueue.empty() && (_messageQueue.empty() || _messageQueue.front().get().betweenFrames()) && webSocketSendFrameWindow(_client) > (size_t)(_controlQueue.front().len() - 1))
535538
{
536-
l.unlock();
539+
//l.unlock();
537540
_controlQueue.front().send(_client);
538541
}
539542
else if (!_messageQueue.empty() && _messageQueue.front().get().betweenFrames() && webSocketSendFrameWindow(_client))
540543
{
541-
l.unlock();
544+
//l.unlock();
542545
_messageQueue.front().get().send(_client);
543546
}
544547
}

src/AsyncWebSynchronization.h

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -43,37 +43,38 @@ class AsyncPlainLock
4343
class AsyncWebLock
4444
{
4545
private:
46-
SemaphoreHandle_t _lock;
47-
mutable TaskHandle_t _lockedBy{};
46+
SemaphoreHandle_t _lock;
47+
mutable TaskHandle_t _lockedBy{};
4848

4949
public:
50-
AsyncWebLock() {
51-
_lock = xSemaphoreCreateBinary();
52-
// In this fails, the system is likely that much out of memory that
53-
// we should abort anyways. If assertions are disabled, nothing is lost..
54-
assert(_lock);
55-
_lockedBy = NULL;
56-
xSemaphoreGive(_lock);
57-
}
50+
AsyncWebLock()
51+
{
52+
_lock = xSemaphoreCreateBinary();
53+
// In this fails, the system is likely that much out of memory that
54+
// we should abort anyways. If assertions are disabled, nothing is lost..
55+
assert(_lock);
56+
_lockedBy = NULL;
57+
xSemaphoreGive(_lock);
58+
}
5859

59-
~AsyncWebLock() {
60-
vSemaphoreDelete(_lock);
61-
}
60+
~AsyncWebLock() {
61+
vSemaphoreDelete(_lock);
62+
}
6263

63-
bool lock() const {
64-
const auto currentTask = xTaskGetCurrentTaskHandle();
65-
if (_lockedBy != currentTask) {
66-
xSemaphoreTake(_lock, portMAX_DELAY);
67-
_lockedBy = currentTask;
68-
return true;
64+
bool lock() const {
65+
const auto currentTask = xTaskGetCurrentTaskHandle();
66+
if (_lockedBy != currentTask) {
67+
xSemaphoreTake(_lock, portMAX_DELAY);
68+
_lockedBy = currentTask;
69+
return true;
70+
}
71+
return false;
6972
}
70-
return false;
71-
}
7273

73-
void unlock() const {
74-
_lockedBy = NULL;
75-
xSemaphoreGive(_lock);
76-
}
74+
void unlock() const {
75+
_lockedBy = NULL;
76+
xSemaphoreGive(_lock);
77+
}
7778
};
7879

7980
#else

0 commit comments

Comments
 (0)