1
0
mirror of https://github.com/DCC-EX/CommandStation-EX.git synced 2025-02-16 22:19:14 +01:00

WIFI RAM save websockets

This commit is contained in:
Asbelos 2023-12-05 17:58:36 +00:00
parent 79a982bed9
commit 2426538e61
3 changed files with 45 additions and 5 deletions

View File

@ -84,7 +84,6 @@ bool Websockets::checkConnectionString(byte clientId,byte * cmd, RingStream * ou
*/
// check contents to find Sec-WebSocket-Key: and get key up to \n
if (strlen((char*)cmd)<200) return false;
auto keyPos=strstr_P((char*)cmd,(char*)F("Sec-WebSocket-Key: "));
if (!keyPos) return false;
keyPos+=19; // length of Sec-Websocket-Key:

View File

@ -99,6 +99,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();
@ -203,14 +206,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');
@ -225,6 +233,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 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) {
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;

View File

@ -55,7 +55,8 @@ class WifiInboundHandler {
IPD6_LENGTH, // got +IPD,c, reading length
IPD_DATA, // got +IPD,c,ll,: collecting data
IPD_IGNORE_DATA, // got +IPD,c,ll,: ignoring the data that won't fit inblound Ring
IPD_PRESCAN, // prescanning data for websocket keys
IPD_POSTSCAN, // copyimg data for websocket keys
GOT_CLIENT_ID, // clientid prefix to CONNECTED / CLOSED
GOT_CLIENT_ID2 // clientid prefix to CONNECTED / CLOSED
};
@ -67,7 +68,7 @@ class WifiInboundHandler {
void purgeCurrentCIPSEND();
Stream * wifiStream;
static const int INBOUND_RING = 1024;
static const int INBOUND_RING = 128;
static const int OUTBOUND_RING = sizeof(void*)==2?2048:8192;
static const int CIPSENDgap=100; // millis() between retries of cipsend.