1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-07-24 19:42:27 +03:00
Files
esp8266/libraries/ESP8266WiFi/src/WiFiClient.cpp
RobertGnz 3c6db4ed9b WiFiClient::abort() (#8738)
Api for saving heap when Client class is used by a Server (WiFiServer class): Client = Server.available().

Suppose the local end is the server and the remote end is the client, we will deal with heap memory at the local end.

When the local application (server) decides to close an active connection with a remote end it issues an Client.stop.
The stop() function calls the close() function of ClientContext class which in turn calls tcp_close.
The connexion is closed by tcp_close and the protocol control block (pcb) can be put in the following states depending on the requests sent by the remote: CLOSING, FIN_WAIT_1 and FIN_WAIT_2. In theses states pcbs are not freed, then consume some memory heap.
If an acknowledgment from the remote end is received, the pcb enter in TIME_WAIT state for some minutes but pcbs in TIME_WAIT state are not freed. Then consume some heap memory.
TIME_WAIT pcbs are automatically freed after some minutes or can be freed for instance issuing an tcp_kill_timewait()
in the local application which will free the oldest pcb in TIME_WAIT state.

If the connection is first closed from the remote end (the client), the local end (server) receive a connection termination request. It then acknowledge it and enter in CLOSE_WAIT state waiting for a connection termination request from the local application.
It then send a termination request and enter in LAST_ACK state until it receive an acknowledgment from the remote end.
After receiving the acknowledgment it enter in ClOSED state and the local pcb is freed leaving some room in the heap memory.

To summarize, when a connexion termination request is send by one end (remote or local), the local pcb is not freed immediatly.
This pcb can be in the following states: FIN_WAIT_1, FIN_WAIT_2, CLOSING, TIME_WAIT, CLOSE_WAIT, LAST_ACK.
As a consequence, some old pcbs from old closed connections are still consuming heap memory.

The local application can call tcp_kill_timewait hoping it will free some TIME_WAIT state pcbs. But if the server receive frequent connections requests and close them after sending whatever it has to send, there may be zero pcbs in TIME_WAIT state among all previously closed connections.

In case of insufficient memory to accept a new connection, lwip has developped a strategy: it successively tries to kill the oldest pcb in TIME_WAIT state, or in LAST_ACK state or in CLOSING state or the oldest active connection with lower priority than the new one.

As a matter of fact this "urgent" strategy is deployed only when very few heap memory remain available (less than some kb). In case of success, Client.available returns a valid Client but the local application will crash when sending or receiving data from the client (Client.read ou readuntil or available) because this need more heap memory and just some kb were freed in lwip to allocate the new pcb structure ans start the new connection.

The propose API is intended to avoid this drawback by calling the abort function of ClientContext which in turn calls tcp_abort which calls tcp_abandon. The connection is aborted and notified to the client with a RESET and the pcb and ressources associated are immediately released increasing the available heap memory.
2022-12-06 12:40:48 +03:00

466 lines
9.4 KiB
C++

/*
WiFiClient.cpp - TCP/IP client for esp8266, mostly compatible
with Arduino WiFi shield library
Copyright (c) 2014 Ivan Grokhotkov. All rights reserved.
This file is part of the esp8266 core for Arduino environment.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
extern "C"
{
#include "wl_definitions.h"
#include "osapi.h"
#include "ets_sys.h"
}
#include "debug.h"
#include "ESP8266WiFi.h"
#include "WiFiClient.h"
#include "WiFiServer.h"
#include "lwip/opt.h"
#include "lwip/ip.h"
#include "lwip/tcp.h"
#include "lwip/inet.h"
#include "lwip/netif.h"
#include <include/ClientContext.h>
#include "c_types.h"
#include <StreamDev.h>
uint16_t WiFiClient::_localPort = 0;
static bool defaultNoDelay = false; // false == Nagle enabled by default
static bool defaultSync = false;
bool getDefaultPrivateGlobalSyncValue ()
{
return defaultSync;
}
void WiFiClient::setDefaultNoDelay (bool noDelay)
{
defaultNoDelay = noDelay;
}
void WiFiClient::setDefaultSync (bool sync)
{
defaultSync = sync;
}
bool WiFiClient::getDefaultNoDelay ()
{
return defaultNoDelay;
}
bool WiFiClient::getDefaultSync ()
{
return defaultSync;
}
template<>
WiFiClient* SList<WiFiClient>::_s_first = 0;
WiFiClient::WiFiClient()
: _client(0), _owned(0)
{
_timeout = 5000;
WiFiClient::_add(this);
}
WiFiClient::WiFiClient(ClientContext* client)
: _client(client), _owned(0)
{
_timeout = 5000;
_client->ref();
WiFiClient::_add(this);
setSync(defaultSync);
setNoDelay(defaultNoDelay);
}
WiFiClient::~WiFiClient()
{
WiFiClient::_remove(this);
if (_client)
_client->unref();
}
std::unique_ptr<WiFiClient> WiFiClient::clone() const {
return std::make_unique<WiFiClient>(*this);
}
WiFiClient::WiFiClient(const WiFiClient& other)
{
_client = other._client;
_timeout = other._timeout;
_localPort = other._localPort;
_owned = other._owned;
if (_client)
_client->ref();
WiFiClient::_add(this);
}
WiFiClient& WiFiClient::operator=(const WiFiClient& other)
{
if (_client)
_client->unref();
_client = other._client;
_timeout = other._timeout;
_localPort = other._localPort;
_owned = other._owned;
if (_client)
_client->ref();
return *this;
}
int WiFiClient::connect(const char* host, uint16_t port)
{
IPAddress remote_addr;
if (WiFi.hostByName(host, remote_addr, _timeout))
{
return connect(remote_addr, port);
}
return 0;
}
int WiFiClient::connect(const String& host, uint16_t port)
{
return connect(host.c_str(), port);
}
int WiFiClient::connect(IPAddress ip, uint16_t port)
{
if (_client) {
stop();
_client->unref();
_client = nullptr;
}
tcp_pcb* pcb = tcp_new();
if (!pcb)
return 0;
if (_localPort > 0) {
pcb->local_port = _localPort++;
}
_client = new ClientContext(pcb, nullptr, nullptr);
_client->ref();
_client->setTimeout(_timeout);
int res = _client->connect(ip, port);
if (res == 0) {
_client->unref();
_client = nullptr;
return 0;
}
setSync(defaultSync);
setNoDelay(defaultNoDelay);
return 1;
}
void WiFiClient::setNoDelay(bool nodelay) {
if (!_client)
return;
_client->setNoDelay(nodelay);
}
bool WiFiClient::getNoDelay() const {
if (!_client)
return false;
return _client->getNoDelay();
}
void WiFiClient::setSync(bool sync)
{
if (!_client)
return;
_client->setSync(sync);
}
bool WiFiClient::getSync() const
{
if (!_client)
return false;
return _client->getSync();
}
int WiFiClient::availableForWrite ()
{
return _client? _client->availableForWrite(): 0;
}
size_t WiFiClient::write(uint8_t b)
{
return write(&b, 1);
}
size_t WiFiClient::write(const uint8_t *buf, size_t size)
{
if (!_client || !size)
{
return 0;
}
_client->setTimeout(_timeout);
return _client->write((const char*)buf, size);
}
size_t WiFiClient::write(Stream& stream)
{
// (this method is deprecated)
if (!_client || !stream.available())
{
return 0;
}
// core up to 2.7.4 was equivalent to this
return stream.sendAll(this);
}
size_t WiFiClient::write_P(PGM_P buf, size_t size)
{
if (!_client || !size)
{
return 0;
}
_client->setTimeout(_timeout);
StreamConstPtr nopeek(buf, size);
return nopeek.sendAll(this);
}
int WiFiClient::available()
{
if (!_client)
return 0;
int result = _client->getSize();
if (!result) {
optimistic_yield(100);
}
return result;
}
int WiFiClient::read()
{
if (!available())
return -1;
return _client->read();
}
int WiFiClient::read(uint8_t* buf, size_t size)
{
return (int)_client->read((char*)buf, size);
}
int WiFiClient::read(char* buf, size_t size)
{
return (int)_client->read(buf, size);
}
int WiFiClient::peek()
{
if (!available())
return -1;
return _client->peek();
}
size_t WiFiClient::peekBytes(uint8_t *buffer, size_t length) {
size_t count = 0;
if(!_client) {
return 0;
}
_startMillis = millis();
while((available() < (int) length) && ((millis() - _startMillis) < _timeout)) {
yield();
}
if(available() < (int) length) {
count = available();
} else {
count = length;
}
return _client->peekBytes((char *)buffer, count);
}
bool WiFiClient::flush(unsigned int maxWaitMs)
{
if (!_client)
return true;
if (maxWaitMs == 0)
maxWaitMs = WIFICLIENT_MAX_FLUSH_WAIT_MS;
return _client->wait_until_acked(maxWaitMs);
}
bool WiFiClient::stop(unsigned int maxWaitMs)
{
if (!_client)
return true;
bool ret = flush(maxWaitMs); // virtual, may be ssl's
if (_client->close() != ERR_OK)
ret = false;
return ret;
}
uint8_t WiFiClient::connected()
{
if (!_client || _client->state() == CLOSED)
return 0;
return _client->state() == ESTABLISHED || available();
}
uint8_t WiFiClient::status()
{
if (!_client)
return CLOSED;
return _client->state();
}
WiFiClient::operator bool()
{
return available() || connected();
}
IPAddress WiFiClient::remoteIP()
{
if (!_client || !_client->getRemoteAddress())
return IPAddress(0U);
return _client->getRemoteAddress();
}
uint16_t WiFiClient::remotePort()
{
if (!_client)
return 0;
return _client->getRemotePort();
}
IPAddress WiFiClient::localIP()
{
if (!_client || !_client->getLocalAddress())
return IPAddress(0U);
return IPAddress(_client->getLocalAddress());
}
uint16_t WiFiClient::localPort()
{
if (!_client)
return 0;
return _client->getLocalPort();
}
// Api for heap saving. Optional use instead of WiFiClient::stop to systematically retreive some heap memory
// and avoiding server crashes in case of frequent clients connections.
void WiFiClient::abort()
{
if (!_client)
return;
flush(0); // Flush output buffer. Don't make any use of return boolean.
_client->abort(); // Wich in turn calls tcp_abort which calls tcp_abandon().
}
void WiFiClient::stopAll()
{
for (WiFiClient* it = _s_first; it; it = it->_next) {
it->stop();
}
}
void WiFiClient::stopAllExcept(WiFiClient* except)
{
// Stop all will look at the lowest-level wrapper connections only
while (except->_owned) {
except = except->_owned;
}
for (WiFiClient* it = _s_first; it; it = it->_next) {
WiFiClient* conn = it;
// Find the lowest-level owner of the current list entry
while (conn->_owned) {
conn = conn->_owned;
}
if (conn != except) {
conn->stop();
}
}
}
void WiFiClient::keepAlive (uint16_t idle_sec, uint16_t intv_sec, uint8_t count)
{
_client->keepAlive(idle_sec, intv_sec, count);
}
bool WiFiClient::isKeepAliveEnabled () const
{
return _client->isKeepAliveEnabled();
}
uint16_t WiFiClient::getKeepAliveIdle () const
{
return _client->getKeepAliveIdle();
}
uint16_t WiFiClient::getKeepAliveInterval () const
{
return _client->getKeepAliveInterval();
}
uint8_t WiFiClient::getKeepAliveCount () const
{
return _client->getKeepAliveCount();
}
bool WiFiClient::hasPeekBufferAPI () const
{
return true;
}
// return a pointer to available data buffer (size = peekAvailable())
// semantic forbids any kind of read() before calling peekConsume()
const char* WiFiClient::peekBuffer ()
{
return _client? _client->peekBuffer(): nullptr;
}
// return number of byte accessible by peekBuffer()
size_t WiFiClient::peekAvailable ()
{
return _client? _client->peekAvailable(): 0;
}
// consume bytes after use (see peekBuffer)
void WiFiClient::peekConsume (size_t consume)
{
if (_client)
_client->peekConsume(consume);
}