Skip to content

Commit

Permalink
Merge branch 'main' into tstamp4mc
Browse files Browse the repository at this point in the history
  • Loading branch information
mathieucarbou authored Jan 27, 2024
2 parents afbb944 + 4873863 commit d187a7b
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 20 deletions.
27 changes: 22 additions & 5 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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<std::vector<uint8_t>>` 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<std::vector<uint8_t>>` 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) {
Expand Down
2 changes: 1 addition & 1 deletion library.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=ESP Async WebServer
version=2.5.0
version=2.5.1
author=Me-No-Dev
maintainer=Mathieu Carbou <[email protected]>
sentence=Asynchronous HTTP and WebSocket Server Library for ESP32
Expand Down
56 changes: 45 additions & 11 deletions src/AsyncWebSocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,12 +135,21 @@ AsyncWebSocketMessageBuffer::AsyncWebSocketMessageBuffer()
AsyncWebSocketMessageBuffer::AsyncWebSocketMessageBuffer(uint8_t* data, size_t size)
: _buffer(std::make_shared<std::vector<uint8_t>>(size))
{
std::memcpy(_buffer->data(), data, size);
if (_buffer->capacity() < size) {
_buffer.reset();
_buffer = std::make_shared<std::vector<uint8_t>>(0);
} else {
std::memcpy(_buffer->data(), data, size);
}
}

AsyncWebSocketMessageBuffer::AsyncWebSocketMessageBuffer(size_t size)
: _buffer(std::make_shared<std::vector<uint8_t>>(size))
{
if (_buffer->capacity() < size) {
_buffer.reset();
_buffer = std::make_shared<std::vector<uint8_t>>(0);
}
}

AsyncWebSocketMessageBuffer::~AsyncWebSocketMessageBuffer()
Expand Down Expand Up @@ -443,6 +452,9 @@ void AsyncWebSocketClient::_queueMessage(std::shared_ptr<std::vector<uint8_t>> b
if (!_client)
return;

if (buffer->size() == 0)
return;

{
AsyncWebLockGuard l(_lock);
if (_messageQueue.size() >= WS_MAX_QUEUED_MESSAGES)
Expand Down Expand Up @@ -687,8 +699,10 @@ std::shared_ptr<std::vector<uint8_t>> 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<std::vector<uint8_t>> buffer)
Expand Down Expand Up @@ -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<std::vector<uint8_t>> buffer)
Expand Down Expand Up @@ -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<std::vector<uint8_t>> buffer)
Expand Down Expand Up @@ -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<std::vector<uint8_t>> buffer)
Expand Down Expand Up @@ -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;
}
}

/*
Expand Down
4 changes: 2 additions & 2 deletions src/ESPAsyncWebServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit d187a7b

Please sign in to comment.