Update for new AsyncWebSocketBuffer

Eliminate the extra indirection and allocate shared buffers directly.
This commit is contained in:
Will Miles 2024-03-16 12:07:26 -04:00
parent a1b0f84444
commit 323c70dcdf

View File

@ -102,7 +102,6 @@ void wsEvent(AsyncWebSocket * server, AsyncWebSocketClient * client, AwsEventTyp
void sendDataWs(AsyncWebSocketClient * client) void sendDataWs(AsyncWebSocketClient * client)
{ {
if (!ws.count()) return; if (!ws.count()) return;
AsyncWebSocketMessageBuffer * buffer;
if (!requestJSONBufferLock(12)) { if (!requestJSONBufferLock(12)) {
if (client) { if (client) {
@ -129,7 +128,7 @@ void sendDataWs(AsyncWebSocketClient * client)
return; return;
} }
#endif #endif
buffer = ws.makeBuffer(len); // will not allocate correct memory sometimes on ESP8266 AsyncWebSocketBuffer buffer(len);
#ifdef ESP8266 #ifdef ESP8266
size_t heap2 = ESP.getFreeHeap(); size_t heap2 = ESP.getFreeHeap();
DEBUG_PRINT(F("heap ")); DEBUG_PRINTLN(ESP.getFreeHeap()); DEBUG_PRINT(F("heap ")); DEBUG_PRINTLN(ESP.getFreeHeap());
@ -141,23 +140,18 @@ void sendDataWs(AsyncWebSocketClient * client)
DEBUG_PRINTLN(F("WS buffer allocation failed.")); DEBUG_PRINTLN(F("WS buffer allocation failed."));
ws.closeAll(1013); //code 1013 = temporary overload, try again later ws.closeAll(1013); //code 1013 = temporary overload, try again later
ws.cleanupClients(0); //disconnect all clients to release memory ws.cleanupClients(0); //disconnect all clients to release memory
ws._cleanBuffers();
return; //out of memory return; //out of memory
} }
serializeJson(*pDoc, (char *)buffer.data(), len);
buffer->lock();
serializeJson(*pDoc, (char *)buffer->get(), len);
DEBUG_PRINT(F("Sending WS data ")); DEBUG_PRINT(F("Sending WS data "));
if (client) { if (client) {
client->text(buffer); client->text(std::move(buffer));
DEBUG_PRINTLN(F("to a single client.")); DEBUG_PRINTLN(F("to a single client."));
} else { } else {
ws.textAll(buffer); ws.textAll(std::move(buffer));
DEBUG_PRINTLN(F("to multiple clients.")); DEBUG_PRINTLN(F("to multiple clients."));
} }
buffer->unlock();
ws._cleanBuffers();
releaseJSONBufferLock(); releaseJSONBufferLock();
} }
@ -187,11 +181,10 @@ bool sendLiveLedsWs(uint32_t wsClient)
#endif #endif
size_t bufSize = pos + (used/n)*3; size_t bufSize = pos + (used/n)*3;
AsyncWebSocketMessageBuffer * wsBuf = ws.makeBuffer(bufSize); AsyncWebSocketBuffer wsBuf(bufSize);
if (!wsBuf) return false; //out of memory if (!wsBuf) return false; //out of memory
uint8_t* buffer = wsBuf->get(); uint8_t* buffer = reinterpret_cast<uint8_t*>(wsBuf.data());
if (!buffer) return false; //out of memory if (!buffer) return false; //out of memory
wsBuf->lock(); // protect buffer from being cleaned by another WS instance
buffer[0] = 'L'; buffer[0] = 'L';
buffer[1] = 1; //version buffer[1] = 1; //version
@ -218,9 +211,7 @@ bool sendLiveLedsWs(uint32_t wsClient)
buffer[pos++] = scale8(qadd8(w, b), strip.getBrightness()); //B buffer[pos++] = scale8(qadd8(w, b), strip.getBrightness()); //B
} }
wsc->binary(wsBuf); wsc->binary(std::move(wsBuf));
wsBuf->unlock(); // un-protect buffer
ws._cleanBuffers();
return true; return true;
} }