From e0ec162938678c0ad66101bf25d24e4511df6bb7 Mon Sep 17 00:00:00 2001 From: Gregg Date: Sat, 11 May 2024 07:43:11 -0500 Subject: [PATCH] Added logic to check for hap[i] disconnect and issue stop() when found This cures memory leakage when using HomeSpan without a Home Hub. Sockets are now properly closed when the Home App abruptly disconnects. --- src/HAP.cpp | 1 + src/HomeSpan.cpp | 10 ++++++---- src/src.ino | 2 +- 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/HAP.cpp b/src/HAP.cpp index fb92a05..e02ca74 100644 --- a/src/HAP.cpp +++ b/src/HAP.cpp @@ -1217,6 +1217,7 @@ void HAPClient::getStatusURL(HAPClient *hapClient, void (*callBack)(const char * if(hapClient){ hapClient->client.stop(); + delay(1); LOG2("------------ SENT! --------------\n"); } } diff --git a/src/HomeSpan.cpp b/src/HomeSpan.cpp index 0683012..780c430 100644 --- a/src/HomeSpan.cpp +++ b/src/HomeSpan.cpp @@ -229,10 +229,10 @@ void Span::pollTask() { processSerialCommand(cBuf); } - if(hapServer->hasClient()){ // found new client + WiFiClient newClient; + + if(newClient=hapServer->available()){ // found new client - WiFiClient newClient=hapServer->available(); // get new client - int socket=newClient.fd()-LWIP_SOCKET_OFFSET; // get socket number (starting at zero) if(hap[socket]==NULL) // create HAPClient at that socket if it does not alreay exist @@ -262,7 +262,9 @@ void Span::pollTask() { } else if(hap[i]->isConnected){ // if client is not connected, but HAPClient thinks it is LOG1("** Client #%d DISCONNECTED (%lu sec)\n",i,millis()/1000); - hap[i]->isConnected=false; + hap[i]->isConnected=false; + hap[i]->client.stop(); + delay(1); } } } diff --git a/src/src.ino b/src/src.ino index c518e7d..e371724 100644 --- a/src/src.ino +++ b/src/src.ino @@ -30,7 +30,7 @@ void setup() { Serial.begin(115200); - + homeSpan.setLogLevel(2); homeSpan.enableWebLog();