Skip to content

Commit

Permalink
MC EAWS: Compile but no websockets
Browse files Browse the repository at this point in the history
  • Loading branch information
ewoudwijma committed Oct 3, 2024
1 parent 9ceedc7 commit daeda41
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 29 deletions.
20 changes: 10 additions & 10 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,21 @@ build_flags =
-D CONFIG_ASYNC_TCP_RUNNING_CORE=1 ;tbd experiment without
;-D CONFIG_ASYNC_TCP_TASK_STACK_SIZE ; 8192*2 here as default !!!
lib_deps =
https://github.com/MoonModules/ESPAsyncWebServer.git @ 3.2.2 ; + queueLength (see https://github.com/esphome/ESPAsyncWebServer/pull/38)
; https://github.com/mathieucarbou/ESPAsyncWebServer.git @ 3.3.5 ; WIP...
https://github.com/MoonModules/ESPAsyncWebServer.git @ 3.2.2 ; + queueLen(gth) (see https://github.com/esphome/ESPAsyncWebServer/pull/38)

; ESPAsyncWebServer AirCoookie v2.0.7 version (2.2.1 is latest)
; ;MC version
; [ESPAsyncWebServer]
; build_flags =
; -D CONFIG_ASYNC_TCP_USE_WDT=0 ;why? tbd: experiment without
; ; -D WS_MAX_QUEUED_MESSAGES=64 ;not supported to set here in AirCoookie version
; -D CONFIG_ASYNC_TCP_STACK_SIZE=CONFIG_ASYNC_TCP_TASK_STACK_SIZE ; ESPHome uses CONFIG_ASYNC_TCP_STACK_SIZE (used in SysModSystem)
; -D WS_MAX_QUEUED_MESSAGES=64
; -D CONFIG_ASYNC_TCP_RUNNING_CORE=1 ;tbd experiment without
; ;-D CONFIG_ASYNC_TCP_TASK_STACK_SIZE ; 8192*2 here as default !!!
; -D STARBASE_USE_MC_AWS ;mathieucarbou/ESPAsyncWebServer
; lib_deps =
; ; https://github.com/lost-hope/ESPAsyncWebServer.git#master ;aircoookie + artifx on <ip>/edit
; ; https://github.com/ewowi/ESPAsyncWebServer.git ;me-no-dev + 64 + queueLength - alternative
; https://github.com/pbolduc/AsyncTCP.git @ 1.2.0 ; WLEDMM this must be first in the list, otherwise Aircoookie/ESPAsyncWebServer pulls in an older version of AsyncTCP !!
; https://github.com/ewoudwijma/ESPAsyncWebServer.git#v2.0.7 ;aircoookie + getClients + 64
; ; https://github.com/ewoudwijma/ESPAsyncWebServer.git#v2.2.1 ;crashes on ws refererence in sendDataWs !!
; https://github.com/mathieucarbou/ESPAsyncWebServer.git @ 3.3.10 ; WIP...




; see also:
; https://github.com/esphome/ESPAsyncWebServer/issues/34 ; Decide which ESPAsyncWebServer fork should be canonical?
Expand Down
6 changes: 5 additions & 1 deletion src/App/LedModFixture.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,11 @@ class LedModFixture:public SysModule {
// ui->setComment(var, "Click to enlarge");
return true;
case onLoop: {
var["interval"] = max(eff->fixture.nrOfLeds * web->ws.count()/200, 16U)*10; //interval in ms * 10, not too fast //from cs to ms
#ifdef STARBASE_USE_MC_AWS
var["interval"] = 25;// max(eff->fixture.nrOfLeds * ws.count()/200, 16U)*10; // error: reference to 'ws' is ambiguous !!!
#else
var["interval"] = max(eff->fixture.nrOfLeds * web->ws.count()/200, 16U)*10; //interval in ms * 10, not too fast //from cs to ms
#endif

web->sendDataWs([this](AsyncWebSocketMessageBuffer * wsBuf) {
byte* buffer;
Expand Down
6 changes: 5 additions & 1 deletion src/Sys/SysModSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,11 @@ void SysModSystem::setup() {

ui->initButton(parentVar, "reboot", false, [](JsonObject var, unsigned8 rowNr, unsigned8 funType) { switch (funType) { //varFun
case onChange:
web->ws.closeAll(1012);
#ifdef STARBASE_USE_MC_AWS
ws.closeAll(1012);
#else
web->ws.closeAll(1012);
#endif

// mdls->reboot(); //not working yet
// long dly = millis();
Expand Down
94 changes: 79 additions & 15 deletions src/Sys/SysModWeb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,15 @@ void SysModWeb::setup() {

ui->initNumber(tableVar, "clNr", UINT16_MAX, 0, 999, true, [this](JsonObject var, unsigned8 rowNr, unsigned8 funType) { switch (funType) { //varFun
case onSetValue: {
unsigned8 rowNr = 0; for (auto &client:ws.getClients())
unsigned8 rowNr = 0;
for (auto &clientx:ws.getClients()) {
#ifdef STARBASE_USE_MC_AWS
AsyncWebSocketClient *client = &clientx;
#else
AsyncWebSocketClient *client = clientx;
#endif
mdl->setValue(var, client->id(), rowNr++);
}
return true; }
case onUI:
ui->setLabel(var, "Nr");
Expand All @@ -70,8 +77,15 @@ void SysModWeb::setup() {

ui->initText(tableVar, "clIp", nullptr, 16, true, [this](JsonObject var, unsigned8 rowNr, unsigned8 funType) { switch (funType) { //varFun
case onSetValue: {
unsigned8 rowNr = 0; for (auto &client:ws.getClients())
unsigned8 rowNr = 0; for (auto &clientx:ws.getClients()) {
#ifdef STARBASE_USE_MC_AWS
AsyncWebSocketClient *client = &clientx;
#else
AsyncWebSocketClient *client = clientx;
#endif

mdl->setValue(var, JsonString(client->remoteIP().toString().c_str(), JsonString::Copied), rowNr++);
}
return true; }
case onUI:
ui->setLabel(var, "IP");
Expand All @@ -81,8 +95,14 @@ void SysModWeb::setup() {

ui->initCheckBox(tableVar, "clIsFull", UINT16_MAX, true, [this](JsonObject var, unsigned8 rowNr, unsigned8 funType) { switch (funType) { //varFun
case onSetValue: {
unsigned8 rowNr = 0; for (auto &client:ws.getClients())
unsigned8 rowNr = 0; for (auto &clientx:ws.getClients()) {
#ifdef STARBASE_USE_MC_AWS
AsyncWebSocketClient *client = &clientx;
#else
AsyncWebSocketClient *client = clientx;
#endif
mdl->setValue(var, client->queueIsFull(), rowNr++);
}
return true; }
case onUI:
ui->setLabel(var, "Is full");
Expand All @@ -92,8 +112,15 @@ void SysModWeb::setup() {

ui->initSelect(tableVar, "clStatus", UINT16_MAX, true, [this](JsonObject var, unsigned8 rowNr, unsigned8 funType) { switch (funType) { //varFun
case onSetValue: {
unsigned8 rowNr = 0; for (auto &client:ws.getClients())
unsigned8 rowNr = 0; for (auto &clientx:ws.getClients()) {
#ifdef STARBASE_USE_MC_AWS
AsyncWebSocketClient *client = &clientx;
#else
AsyncWebSocketClient *client = clientx;
#endif

mdl->setValue(var, client->status(), rowNr++);
}
return true; }
case onUI:
{
Expand All @@ -110,8 +137,14 @@ void SysModWeb::setup() {

ui->initNumber(tableVar, "clLength", UINT16_MAX, 0, WS_MAX_QUEUED_MESSAGES, true, [this](JsonObject var, unsigned8 rowNr, unsigned8 funType) { switch (funType) { //varFun
case onSetValue: {
unsigned8 rowNr = 0; for (auto &client:ws.getClients())
unsigned8 rowNr = 0; for (auto &clientx:ws.getClients()) {
#ifdef STARBASE_USE_MC_AWS
AsyncWebSocketClient *client = &clientx;
#else
AsyncWebSocketClient *client = clientx;
#endif
mdl->setValue(var, client->queueLen(), rowNr++);
}
return true; }
case onUI:
ui->setLabel(var, "Length");
Expand Down Expand Up @@ -424,11 +457,18 @@ void SysModWeb::sendDataWs(std::function<void(AsyncWebSocketMessageBuffer *)> fi
AsyncWebSocketMessageBuffer * wsBuf = ws.makeBuffer(len); //assert failed: block_trim_free heap_tlsf.c:371 (block_is_free(block) && "block must be free"), AsyncWebSocket::makeBuffer(unsigned int)

if (wsBuf) {
wsBuf->lock();
#ifndef STARBASE_USE_MC_AWS
wsBuf->lock();
#endif

fill(wsBuf); //function parameter

for (auto &loopClient:ws.getClients()) {
for (auto &loopClientx:ws.getClients()) {
#ifdef STARBASE_USE_MC_AWS
AsyncWebSocketClient *loopClient = &loopClientx;
#else
AsyncWebSocketClient *loopClient = loopClientx;
#endif
if (!client || client == loopClient) {
if (loopClient->status() == WS_CONNECTED && !loopClient->queueIsFull()) { //WS_MAX_QUEUED_MESSAGES / ws.count() / 2)) { //binary is lossy
if (!isBinary || loopClient->queueLen() <= 3) {
Expand All @@ -441,21 +481,27 @@ void SysModWeb::sendDataWs(std::function<void(AsyncWebSocketMessageBuffer *)> fi
}
}
else {
printClient("sendDataWs client full or not connected", loopClient);
printClient("sendDataWs client full or not connected", (AsyncWebSocketClient *)&loopClient);
// ppf("sendDataWs client full or not connected\n");
ws.cleanupClients(); //only if above threshold
ws._cleanBuffers();
#ifndef STARBASE_USE_MC_AWS
ws._cleanBuffers();
#endif
}
}
}
wsBuf->unlock();
ws._cleanBuffers();
#ifndef STARBASE_USE_MC_AWS
wsBuf->unlock();
ws._cleanBuffers();
#endif
}
else {
ppf("sendDataWs WS buffer allocation failed\n");
ws.closeAll(1013); //code 1013 = temporary overload, try again later
ws.cleanupClients(0); //disconnect ALL clients to release memory
ws._cleanBuffers();
#ifndef STARBASE_USE_MC_AWS
ws._cleanBuffers();
#endif
}
}

Expand All @@ -472,7 +518,11 @@ void SysModWeb::serveIndex(WebRequest *request) {
// if (handleIfNoneMatchCacheHeader(request)) return;

WebResponse *response;
response = request->beginResponse_P(200, "text/html", PAGE_index, PAGE_index_L);
#ifdef STARBASE_USE_MC_AWS
response = request->beginResponse(200, "text/html", PAGE_index, PAGE_index_L);
#else
response = request->beginResponse_P(200, "text/html", PAGE_index, PAGE_index_L);
#endif
response->addHeader("Content-Encoding","gzip");
// setStaticContentCacheHeaders(response);
request->send(response);
Expand All @@ -488,7 +538,12 @@ void SysModWeb::serveNewUI(WebRequest *request) {
// if (handleIfNoneMatchCacheHeader(request)) return;

WebResponse *response;
response = request->beginResponse_P(200, "text/html", PAGE_newui, PAGE_newui_L);
#ifdef STARBASE_USE_MC_AWS
response = request->beginResponse(200, "text/html", PAGE_newui, PAGE_newui_L);
#else
response = request->beginResponse_P(200, "text/html", PAGE_newui, PAGE_newui_L);
#endif

response->addHeader("Content-Encoding","gzip");
// setStaticContentCacheHeaders(response);
request->send(response);
Expand Down Expand Up @@ -625,7 +680,16 @@ void SysModWeb::jsonHandler(WebRequest *request, JsonVariant json) {
}

void SysModWeb::clientsToJson(JsonArray array, bool nameOnly, const char * filter) {
for (auto &client:ws.getClients()) {
for (auto &clientx:ws.getClients()) {
// NM: typedef LinkedList<AsyncWebSocketClient *> AsyncWebSocketClientLinkedList; LinkedList is a class
// MC: std::list<AsyncWebSocketClient>& getClients() { return _clients; }

#ifdef STARBASE_USE_MC_AWS
AsyncWebSocketClient *client = &clientx;
#else
AsyncWebSocketClient *client = clientx;
#endif

if (nameOnly) {
array.add(JsonString(client->remoteIP().toString().c_str(), JsonString::Copied));
} else {
Expand Down
12 changes: 10 additions & 2 deletions src/Sys/SysModWeb.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,23 @@
#define WebResponse AsyncWebServerResponse
#endif

#ifdef STARBASE_USE_MC_AWS
static WebSocket ws("/ws");
static WebServer server(80);
//static otherwise multiple definition of `server';
#endif

class SysModWeb:public SysModule {

public:
#ifdef STARBASE_USE_Psychic
WebSocket ws = WebSocket();
WebServer server = WebServer();
#else
WebSocket ws = WebSocket("/ws");
WebServer server = WebServer(80);
#ifndef STARBASE_USE_MC_AWS
WebSocket ws = WebSocket("/ws");
WebServer server = WebServer(80);
#endif
#endif

SemaphoreHandle_t wsMutex = xSemaphoreCreateMutex();
Expand Down

0 comments on commit daeda41

Please sign in to comment.