mirror of
https://github.com/alexhopeoconnor/firmware.git
synced 2026-10-17 17:11:30 +10:00
Improve resource cleanup on connection close (and make server API a unique pointer) (#9799)
* Improve resource cleanup on connection close * Copilot had some good feedback. Let's just make the api a unique pointer * Update src/mesh/api/ServerAPI.cpp Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Copilot stupidly suggesting we call protected methods * Gotta do it in the superclasses as well * Fix moar * Refactor MQTT unit test to ensure proper subscription handling and clear side effects --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
This commit is contained in:
@@ -31,6 +31,7 @@ template <class T> int32_t ServerAPI<T>::runOnce()
|
||||
return StreamAPI::runOncePart();
|
||||
} else {
|
||||
LOG_INFO("Client dropped connection, suspend API service");
|
||||
close();
|
||||
enabled = false; // we no longer need to run
|
||||
return 0;
|
||||
}
|
||||
@@ -45,6 +46,11 @@ template <class T, class U> void APIServerPort<T, U>::init()
|
||||
|
||||
template <class T, class U> int32_t APIServerPort<T, U>::runOnce()
|
||||
{
|
||||
// Clean up previous connection if its client already disconnected
|
||||
if (openAPI && !openAPI->checkIsConnected()) {
|
||||
openAPI.reset();
|
||||
}
|
||||
|
||||
#ifdef ARCH_ESP32
|
||||
#if ESP_ARDUINO_VERSION >= ESP_ARDUINO_VERSION_VAL(3, 0, 0)
|
||||
auto client = U::accept();
|
||||
@@ -70,10 +76,10 @@ template <class T, class U> int32_t APIServerPort<T, U>::runOnce()
|
||||
}
|
||||
#endif
|
||||
LOG_INFO("Force close previous TCP connection");
|
||||
delete openAPI;
|
||||
openAPI.reset();
|
||||
}
|
||||
|
||||
openAPI = new T(client);
|
||||
openAPI.reset(new T(client));
|
||||
}
|
||||
|
||||
#if RAK_4631
|
||||
|
||||
Reference in New Issue
Block a user