Skip to content

API in WiFiClient to save heap memory #8732

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions libraries/ESP8266WiFi/src/WiFiClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,15 @@ bool WiFiClient::flush(unsigned int maxWaitMs)
return _client->wait_until_acked(maxWaitMs);
}

// Api for heap saving. Must be call just before WiFiClient::stop().
void WiFiClient::abortTimeWait()
{
if (!_client) return;
tcp_pcb* p;
p = _client->getPCB();
if (p) tcp_abort(p);
}

bool WiFiClient::stop(unsigned int maxWaitMs)
{
if (!_client)
Expand Down
11 changes: 11 additions & 0 deletions libraries/ESP8266WiFi/src/WiFiClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,17 @@ class WiFiClient : public Client, public SList<WiFiClient> {
friend class WiFiServer;

using Print::write;

// Api for saving precious heap.
// When Client class is used by a Server: Client = Server.available(), sockets in TIME_WAIT remain after
// issuing Client.stop by the Server application.
// This reduce drastically the heap memory in case of multiple client connections to the server ending
// with a Server shutdown and an ESP8266 reboot after some hours because of insufficient heap memory.
// This API is provided to free heap memory by mean of closing sockets just before issuing a Client.stop
// by the Server.
// The Server must use this API just before calling Client.stop
// Note: none of the proposed methode e.g. tcpCleanup and tcp_kill_timewait works properly.
void abortTimeWait();

static void stopAll();
static void stopAllExcept(WiFiClient * c);
Expand Down