diff --git a/docs/index.md b/docs/index.md index 9cf1e0e03..8b213e81d 100644 --- a/docs/index.md +++ b/docs/index.md @@ -15,6 +15,7 @@ This fork is based on https://github.com/yubox-node-org/ESPAsyncWebServer and in - Deployed in PlatformIO registry and Arduino IDE library manager - CI - Only supports ESP32 +- Resurrected `AsyncWebSocketMessageBuffer` and `makeBuffer()` in order to make the fork API-compatible with the original library from me-no-dev regarding WebSocket. ## Documentation @@ -23,13 +24,29 @@ Please look at the original libraries for more examples and documentation. [https://github.com/yubox-node-org/ESPAsyncWebServer](https://github.com/yubox-node-org/ESPAsyncWebServer) -## Pitfalls +## `AsyncWebSocketMessageBuffer` and `makeBuffer()` -The fork from yubox introduces some breaking API changes compared to the original library, especially regarding the use of `std::shared_ptr>` for WebSocket. -Thanks to this fork, you can handle them by using `ASYNCWEBSERVER_FORK_mathieucarbou`. +The fork from `yubox-node-org` introduces some breaking API changes compared to the original library, especially regarding the use of `std::shared_ptr>` for WebSocket. -Here is an example for serializing a Json document in a websocket message buffer directly. -This code is compatible with both forks. +This fork is compatible with the original library from `me-no-dev` regarding WebSocket, and wraps the optimizations done by `yubox-node-org` in the `AsyncWebSocketMessageBuffer` class. +So you have the choice of which API to use. +I strongly suggest to use the optimized API from `yubox-node-org` as it is much more efficient. + +Here is an example for serializing a Json document in a websocket message buffer. This code is compatible with any forks, but not optimized: + +```cpp +void send(JsonDocument& doc) { + const size_t len = measureJson(doc); + + // original API from me-no-dev + AsyncWebSocketMessageBuffer* buffer = _ws->makeBuffer(len); + assert(buffer); // up to you to keep or remove this + serializeJson(doc, buffer->get(), len); + _ws->textAll(buffer); +} +``` + +Here is an example for serializing a Json document in a more optimized way, and compatible with both forks: ```cpp void send(JsonDocument& doc) { diff --git a/library.json b/library.json index b918a4630..1730ad578 100644 --- a/library.json +++ b/library.json @@ -1,6 +1,6 @@ { "name": "ESP Async WebServer", - "version": "2.5.0", + "version": "2.5.1", "description": "Asynchronous HTTP and WebSocket Server Library for ESP32. Supports: WebSocket, SSE, Authentication, Arduino Json 7, File Upload, Static File serving, URL Rewrite, URL Redirect, etc.", "keywords": "http,async,websocket,webserver", "homepage": "https://github.com/mathieucarbou/ESPAsyncWebServer", diff --git a/library.properties b/library.properties index 0e81e4c5f..2e5699fe7 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=ESP Async WebServer -version=2.5.0 +version=2.5.1 author=Me-No-Dev maintainer=Mathieu Carbou sentence=Asynchronous HTTP and WebSocket Server Library for ESP32 diff --git a/src/AsyncWebSocket.cpp b/src/AsyncWebSocket.cpp index 443b5f1e0..12a89cbf2 100644 --- a/src/AsyncWebSocket.cpp +++ b/src/AsyncWebSocket.cpp @@ -135,12 +135,21 @@ AsyncWebSocketMessageBuffer::AsyncWebSocketMessageBuffer() AsyncWebSocketMessageBuffer::AsyncWebSocketMessageBuffer(uint8_t* data, size_t size) : _buffer(std::make_shared>(size)) { - std::memcpy(_buffer->data(), data, size); + if (_buffer->capacity() < size) { + _buffer.reset(); + _buffer = std::make_shared>(0); + } else { + std::memcpy(_buffer->data(), data, size); + } } AsyncWebSocketMessageBuffer::AsyncWebSocketMessageBuffer(size_t size) : _buffer(std::make_shared>(size)) { + if (_buffer->capacity() < size) { + _buffer.reset(); + _buffer = std::make_shared>(0); + } } AsyncWebSocketMessageBuffer::~AsyncWebSocketMessageBuffer() @@ -443,6 +452,9 @@ void AsyncWebSocketClient::_queueMessage(std::shared_ptr> b if (!_client) return; + if (buffer->size() == 0) + return; + { AsyncWebLockGuard l(_lock); if (_messageQueue.size() >= WS_MAX_QUEUED_MESSAGES) @@ -687,8 +699,10 @@ std::shared_ptr> makeSharedBuffer(const uint8_t *message, s void AsyncWebSocketClient::text(AsyncWebSocketMessageBuffer * buffer) { - text(std::move(buffer->_buffer)); - delete buffer; + if (buffer) { + text(std::move(buffer->_buffer)); + delete buffer; + } } void AsyncWebSocketClient::text(std::shared_ptr> buffer) @@ -739,8 +753,10 @@ void AsyncWebSocketClient::text(const __FlashStringHelper *data) void AsyncWebSocketClient::binary(AsyncWebSocketMessageBuffer * buffer) { - binary(std::move(buffer->_buffer)); - delete buffer; + if (buffer) { + binary(std::move(buffer->_buffer)); + delete buffer; + } } void AsyncWebSocketClient::binary(std::shared_ptr> buffer) @@ -936,8 +952,10 @@ void AsyncWebSocket::text(uint32_t id, const __FlashStringHelper *data) void AsyncWebSocket::textAll(AsyncWebSocketMessageBuffer * buffer) { - textAll(std::move(buffer->_buffer)); - delete buffer; + if (buffer) { + textAll(std::move(buffer->_buffer)); + delete buffer; + } } void AsyncWebSocket::textAll(std::shared_ptr> buffer) @@ -1014,8 +1032,10 @@ void AsyncWebSocket::binary(uint32_t id, const __FlashStringHelper *data, size_t void AsyncWebSocket::binaryAll(AsyncWebSocketMessageBuffer * buffer) { - binaryAll(std::move(buffer->_buffer)); - delete buffer; + if (buffer) { + binaryAll(std::move(buffer->_buffer)); + delete buffer; + } } void AsyncWebSocket::binaryAll(std::shared_ptr> buffer) @@ -1200,12 +1220,26 @@ void AsyncWebSocket::handleRequest(AsyncWebServerRequest *request) AsyncWebSocketMessageBuffer * AsyncWebSocket::makeBuffer(size_t size) { - return new AsyncWebSocketMessageBuffer(size); + AsyncWebSocketMessageBuffer * buffer = new AsyncWebSocketMessageBuffer(size); + if (buffer->length() != size) + { + delete buffer; + return nullptr; + } else { + return buffer; + } } AsyncWebSocketMessageBuffer * AsyncWebSocket::makeBuffer(uint8_t * data, size_t size) { - return new AsyncWebSocketMessageBuffer(data, size); + AsyncWebSocketMessageBuffer * buffer = new AsyncWebSocketMessageBuffer(data, size); + if (buffer->length() != size) + { + delete buffer; + return nullptr; + } else { + return buffer; + } } /* diff --git a/src/ESPAsyncWebServer.h b/src/ESPAsyncWebServer.h index 0babe00f5..2c88bda35 100644 --- a/src/ESPAsyncWebServer.h +++ b/src/ESPAsyncWebServer.h @@ -40,10 +40,10 @@ #error Platform not supported #endif -#define ASYNCWEBSERVER_VERSION "2.5.0" +#define ASYNCWEBSERVER_VERSION "2.5.1" #define ASYNCWEBSERVER_VERSION_MAJOR 2 #define ASYNCWEBSERVER_VERSION_MINOR 5 -#define ASYNCWEBSERVER_VERSION_REVISION 0 +#define ASYNCWEBSERVER_VERSION_REVISION 1 #define ASYNCWEBSERVER_FORK_mathieucarbou #ifdef ASYNCWEBSERVER_REGEX