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.
This commit is contained in:
Gregg 2024-05-11 07:43:11 -05:00
parent 747b8c3244
commit e0ec162938
3 changed files with 8 additions and 5 deletions

View File

@ -1217,6 +1217,7 @@ void HAPClient::getStatusURL(HAPClient *hapClient, void (*callBack)(const char *
if(hapClient){
hapClient->client.stop();
delay(1);
LOG2("------------ SENT! --------------\n");
}
}

View File

@ -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);
}
}
}

View File

@ -30,7 +30,7 @@
void setup() {
Serial.begin(115200);
homeSpan.setLogLevel(2);
homeSpan.enableWebLog();