Skip to content

Commit 3e8a713

Browse files
committed
Merge remote-tracking branch 'origin/master' into pr8736
2 parents f2397e7 + 9701d3a commit 3e8a713

File tree

6 files changed

+51
-10
lines changed

6 files changed

+51
-10
lines changed

doc/esp8266wifi/client-class.rst

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,35 @@ Default input value 0 means that effective value is left at the discretion of th
2929

3030
``stop()`` returns ``false`` in case of an issue when closing the client (for instance a timed-out ``flush``). Depending on implementation, its parameter can be passed to ``flush()``.
3131

32+
abort
33+
~~~~~
34+
35+
.. code:: cpp
36+
37+
void abort();
38+
39+
40+
Originally proposed in `#8738 <https://github.com/esp8266/Arduino/pull/8738>`__
41+
Unlike ``stop()``, immediately shuts down internal connection object.
42+
43+
Under usual circumstances, we either enter ``CLOSE_WAIT`` or ``TIME_WAIT`` state. But, the connection object is not freed right away, and requires us to either
44+
* wait until ``malloc()`` returns ``NULL`` when our TCP stack tries to allocate memory for a new connection
45+
* manually call ``tcp_kill_timewait()`` to forcibly stop the 'oldest' connection
46+
47+
This API frees up resources used by the connection. Consider using it instead of ``stop()`` if your application handles a lot of clients and frequently runs out of available heap memory.
48+
49+
*Example:*
50+
51+
.. code:: cpp
52+
# define MIN_HEAP_FREE 20000 // or whatever min available heap memory convienent for your application
53+
auto client = server.accept();
54+
// ... do something with the client object ...
55+
if (ESP.getFreeHeap() >= MIN_HEAP_FREE) {
56+
client.stop();
57+
} else {
58+
client.abort();
59+
}
60+
3261
setNoDelay
3362
~~~~~~~~~~
3463

libraries/ESP8266WiFi/src/WiFiClient.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,17 @@ uint16_t WiFiClient::localPort()
376376
return _client->getLocalPort();
377377
}
378378

379+
// Api for heap saving. Optional use instead of WiFiClient::stop to systematically retreive some heap memory
380+
// and avoiding server crashes in case of frequent clients connections.
381+
void WiFiClient::abort()
382+
{
383+
if (!_client)
384+
return;
385+
386+
flush(0); // Flush output buffer. Don't make any use of return boolean.
387+
_client->abort(); // Wich in turn calls tcp_abort which calls tcp_abandon().
388+
}
389+
379390
void WiFiClient::stopAll()
380391
{
381392
for (WiFiClient* it = _s_first; it; it = it->_next) {

libraries/ESP8266WiFi/src/WiFiClient.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ class WiFiClient : public Client, public SList<WiFiClient> {
103103
friend class WiFiServer;
104104

105105
using Print::write;
106-
106+
107107
static void stopAll();
108108
static void stopAllExcept(WiFiClient * c);
109109

@@ -148,6 +148,10 @@ class WiFiClient : public Client, public SList<WiFiClient> {
148148
virtual bool outputCanTimeout () override { return connected(); }
149149
virtual bool inputCanTimeout () override { return connected(); }
150150

151+
// Immediately stops this client instance.
152+
// Unlike stop(), does not wait to gracefuly shutdown the connection.
153+
void abort();
154+
151155
protected:
152156

153157
static int8_t _s_connected(void* arg, void* tpcb, int8_t err);

libraries/ESP8266httpUpdate/examples/httpUpdate/httpUpdate.ino

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,11 @@ ESP8266WiFiMulti WiFiMulti;
2323
void setup() {
2424

2525
Serial.begin(115200);
26-
// Serial.setDebugOutput(true);
26+
// Serial.setDebugOutput(false);
2727

28-
Serial.println();
29-
Serial.println();
3028
Serial.println();
3129

32-
for (uint8_t t = 4; t > 0; t--) {
33-
Serial.printf("[SETUP] WAIT %d...\n", t);
34-
Serial.flush();
35-
delay(1000);
36-
}
30+
ESPhttpUpdate.setClientTimeout(2000); // default was 8000
3731

3832
WiFi.mode(WIFI_STA);
3933
WiFiMulti.addAP(APSSID, APPSK);

libraries/ESP8266httpUpdate/src/ESP8266httpUpdate.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,9 @@ class ESP8266HTTPUpdate
130130
int getLastError(void);
131131
String getLastErrorString(void);
132132

133+
void setClientTimeout(int timeout) {
134+
_httpClientTimeout = timeout;
135+
}
133136
protected:
134137
t_httpUpdate_return handleUpdate(HTTPClient& http, const String& currentVersion, bool spiffs = false);
135138
bool runUpdate(Stream& in, uint32_t size, const String& md5, int command = U_FLASH);

0 commit comments

Comments
 (0)