Skip to content

Commit

Permalink
WIFI RAM save websockets
Browse files Browse the repository at this point in the history
  • Loading branch information
Asbelos committed Dec 5, 2023
1 parent 79a982b commit 2426538
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 5 deletions.
1 change: 0 additions & 1 deletion Websockets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
44 changes: 42 additions & 2 deletions WifiInboundHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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');
Expand All @@ -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;
Expand Down
5 changes: 3 additions & 2 deletions WifiInboundHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
};
Expand All @@ -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.
Expand Down

0 comments on commit 2426538

Please sign in to comment.