1
0
mirror of https://github.com/DCC-EX/CommandStation-EX.git synced 2025-07-30 02:43:45 +02:00

Squashed commit of the following:

commit 04b8e20773
Author: Asbelos <asbelos@btinternet.com>
Date:   Sat Jan 18 10:13:32 2025 +0000

    Fix websocket binary mask issue

    Includes <D WEBSOCKET ON>

commit 5600382ae4
Merge: 5941866 137008c
Author: Asbelos <asbelos@btinternet.com>
Date:   Sat Jan 18 08:58:30 2025 +0000

    Merge branch 'devel' into devel-websockets

commit 59418668e2
Author: Asbelos <asbelos@btinternet.com>
Date:   Fri Jan 17 13:16:29 2025 +0000

    WIfiESP websock fix

commit c4e2146bd1
Author: Asbelos <asbelos@btinternet.com>
Date:   Thu Jan 16 08:37:38 2025 +0000

    websockets broadcast

commit 2591d100ca
Author: Asbelos <asbelos@btinternet.com>
Date:   Wed Jan 15 19:38:41 2025 +0000

    browser barf on \n

commit 67f836e886
Author: Asbelos <asbelos@btinternet.com>
Date:   Wed Jan 15 09:44:04 2025 +0000

    5.5.5 + websockets

    Ethernet not yet included
This commit is contained in:
Asbelos
2025-01-23 10:46:39 +00:00
parent d14aa46d51
commit 07691e3985
14 changed files with 660 additions and 45 deletions

View File

@@ -1,7 +1,7 @@
/*
* © 2021 Fred Decker
* © 2021 Fred Decker
* © 2020-2021 Chris Harlow
* © 2020-2025 Chris Harlow
* © 2020, Chris Harlow. All rights reserved.
* © 2020, Harald Barth.
*
@@ -26,6 +26,7 @@
#include "RingStream.h"
#include "CommandDistributor.h"
#include "DIAG.h"
#include "Websockets.h"
WifiInboundHandler * WifiInboundHandler::singleton;
@@ -67,8 +68,13 @@ void WifiInboundHandler::loop1() {
if (pendingCipsend && millis()-lastCIPSEND > CIPSENDgap) {
if (Diag::WIFI) DIAG( F("WiFi: [[CIPSEND=%d,%d]]"), clientPendingCIPSEND, currentReplySize);
StringFormatter::send(wifiStream, F("AT+CIPSEND=%d,%d\r\n"), clientPendingCIPSEND, currentReplySize);
// add allowances for websockets
bool websocket=clientPendingCIPSEND & Websockets::WEBSOCK_CLIENT_MARKER;
byte realClient=clientPendingCIPSEND & ~Websockets::WEBSOCK_CLIENT_MARKER;
int16_t realSize=currentReplySize;
if (websocket) realSize+=Websockets::getOutboundHeaderSize(currentReplySize);
if (Diag::WIFI) DIAG( F("WiFi: [[CIPSEND=%d,%d]]"), realClient, realSize);
StringFormatter::send(wifiStream, F("AT+CIPSEND=%d,%d\r\n"), realClient,realSize);
pendingCipsend=false;
return;
}
@@ -80,7 +86,9 @@ void WifiInboundHandler::loop1() {
int count=inboundRing->count();
if (Diag::WIFI) DIAG(F("Wifi EXEC: %d %d:"),clientId,count);
byte cmd[count+1];
for (int i=0;i<count;i++) cmd[i]=inboundRing->read();
// Copy raw bytes to avoid websocket masked data being
// confused with a ram-saving flash insert marker.
for (int i=0;i<count;i++) cmd[i]=inboundRing->readRawByte();
cmd[count]=0;
if (Diag::WIFI) DIAG(F("%e"),cmd);
@@ -94,6 +102,9 @@ void WifiInboundHandler::loop1() {
// This is a Finite State Automation (FSA) handling the inbound bytes from an ES AT command processor
WifiInboundHandler::INBOUND_STATE WifiInboundHandler::loop2() {
const char WebSocketKeyName[]="Sec-WebSocket-Key: ";
static byte prescanPoint=0;
while (wifiStream->available()) {
int ch = wifiStream->read();
@@ -112,9 +123,12 @@ WifiInboundHandler::INBOUND_STATE WifiInboundHandler::loop2() {
}
if (ch=='>') {
if (Diag::WIFI) DIAG(F("[XMIT %d]"),currentReplySize);
bool websocket=clientPendingCIPSEND & Websockets::WEBSOCK_CLIENT_MARKER;
if (Diag::WIFI) DIAG(F("[XMIT %d ws=%b]"),currentReplySize,websocket);
if (websocket) Websockets::writeOutboundHeader(wifiStream,currentReplySize);
for (int i=0;i<currentReplySize;i++) {
int cout=outboundRing->read();
if (websocket && (cout=='\n')) cout='\r';
wifiStream->write(cout);
if (Diag::WIFI) StringFormatter::printEscape(cout); // DIAG in disguise
}
@@ -195,14 +209,19 @@ WifiInboundHandler::INBOUND_STATE WifiInboundHandler::loop2() {
break;
}
if (Diag::WIFI) DIAG(F("Wifi inbound data(%d:%d):"),runningClientId,dataLength);
if (inboundRing->freeSpace()<=(dataLength+1)) {
// we normally dont read >100 bytes
// so assume its an HTTP GET or similar
if (dataLength<100 && inboundRing->freeSpace()<=(dataLength+1)) {
// This input would overflow the inbound ring, ignore it
loopState=IPD_IGNORE_DATA;
if (Diag::WIFI) DIAG(F("Wifi OVERFLOW IGNORING:"));
break;
}
inboundRing->mark(runningClientId);
loopState=IPD_DATA;
prescanPoint=0;
loopState=(dataLength>100)? IPD_PRESCAN: IPD_DATA;
break;
}
dataLength = dataLength * 10 + (ch - '0');
@@ -217,6 +236,38 @@ WifiInboundHandler::INBOUND_STATE WifiInboundHandler::loop2() {
}
break;
case IPD_PRESCAN: // prescan reading data
dataLength--;
if (dataLength == 0) {
// Nothing found, this input is lost
DIAG(F("Wifi prescan for websock not found"));
inboundRing->commit();
loopState = ANYTHING;
}
if (ch!=WebSocketKeyName[prescanPoint]) {
prescanPoint=0;
break;
}
// matched the next char of the key
prescanPoint++;
if (WebSocketKeyName[prescanPoint]==0) {
if (Diag::WEBSOCKET) DIAG(F("Wifi prescan found"));
// prescan has detected full key
inboundRing->print(WebSocketKeyName);
loopState=IPD_POSTSCAN; // continmue as normal
}
break;
case IPD_POSTSCAN: // reading data
inboundRing->write(ch);
dataLength--;
if (ch=='\n') {
inboundRing->commit();
loopState = IPD_IGNORE_DATA;
}
break;
case IPD_IGNORE_DATA: // ignoring data that would not fit in inbound ring
dataLength--;
if (dataLength == 0) loopState = ANYTHING;