Skip to content

Commit 08797a9

Browse files
committed
Update for new AsyncWebSocketSharedBuffer
Eliminate the extra indirection and allocate shared buffers directly.
1 parent 32b7612 commit 08797a9

File tree

1 file changed

+6
-15
lines changed

1 file changed

+6
-15
lines changed

wled00/ws.cpp

+6-15
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,6 @@ void wsEvent(AsyncWebSocket * server, AsyncWebSocketClient * client, AwsEventTyp
102102
void sendDataWs(AsyncWebSocketClient * client)
103103
{
104104
if (!ws.count()) return;
105-
AsyncWebSocketMessageBuffer * buffer;
106105

107106
if (!requestJSONBufferLock(12)) {
108107
if (client) {
@@ -129,7 +128,7 @@ void sendDataWs(AsyncWebSocketClient * client)
129128
return;
130129
}
131130
#endif
132-
buffer = ws.makeBuffer(len); // will not allocate correct memory sometimes on ESP8266
131+
AsyncWebSocketSharedBuffer buffer(len); // will not allocate correct memory sometimes on ESP8266
133132
#ifdef ESP8266
134133
size_t heap2 = ESP.getFreeHeap();
135134
DEBUG_PRINT(F("heap ")); DEBUG_PRINTLN(ESP.getFreeHeap());
@@ -141,23 +140,18 @@ void sendDataWs(AsyncWebSocketClient * client)
141140
DEBUG_PRINTLN(F("WS buffer allocation failed."));
142141
ws.closeAll(1013); //code 1013 = temporary overload, try again later
143142
ws.cleanupClients(0); //disconnect all clients to release memory
144-
ws._cleanBuffers();
145143
return; //out of memory
146144
}
147-
148-
buffer->lock();
149-
serializeJson(*pDoc, (char *)buffer->get(), len);
145+
serializeJson(*pDoc, (char *)buffer.data(), len);
150146

151147
DEBUG_PRINT(F("Sending WS data "));
152148
if (client) {
153-
client->text(buffer);
149+
client->text(std::move(buffer));
154150
DEBUG_PRINTLN(F("to a single client."));
155151
} else {
156152
ws.textAll(buffer);
157153
DEBUG_PRINTLN(F("to multiple clients."));
158154
}
159-
buffer->unlock();
160-
ws._cleanBuffers();
161155

162156
releaseJSONBufferLock();
163157
}
@@ -187,11 +181,10 @@ bool sendLiveLedsWs(uint32_t wsClient)
187181
#endif
188182
size_t bufSize = pos + (used/n)*3;
189183

190-
AsyncWebSocketMessageBuffer * wsBuf = ws.makeBuffer(bufSize);
184+
AsyncWebSocketSharedBuffer wsBuf(bufSize);
191185
if (!wsBuf) return false; //out of memory
192-
uint8_t* buffer = wsBuf->get();
186+
uint8_t* buffer = reinterpret_cast<uint8_t*>(wsBuf.data());
193187
if (!buffer) return false; //out of memory
194-
wsBuf->lock(); // protect buffer from being cleaned by another WS instance
195188
buffer[0] = 'L';
196189
buffer[1] = 1; //version
197190

@@ -218,9 +211,7 @@ bool sendLiveLedsWs(uint32_t wsClient)
218211
buffer[pos++] = scale8(qadd8(w, b), strip.getBrightness()); //B
219212
}
220213

221-
wsc->binary(wsBuf);
222-
wsBuf->unlock(); // un-protect buffer
223-
ws._cleanBuffers();
214+
wsc->binary(std::move(wsBuf));
224215
return true;
225216
}
226217

0 commit comments

Comments
 (0)