1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-07-07 06:01:35 +03:00

BREAKING: Add Print::availableForWrite method (#7658)

* Add Print::availableForWrite method

Adds an availableForWrite() method to the Print class, matching current
ArduinoCore-API commit 398e70f188e2b861c10d9ffe5e2bfcb6a4a4f489 .

Hook availableForWrite into the SDFS filesystem (other FSes don't have
this capability built-in).

Fixes #7650

* WiFiClient::availableForWrite proto matching Print

* Fix Netdump signedness warning

* Clean up Serial availableForWrite

This is evidently a breaking change due to the type difference.
Arduino's `availableForWrite` returns an `int`, while the
(multiply-implemented, non-virtual) core `availableForWrite` returned
`size_t`.
This commit is contained in:
Earle F. Philhower, III
2020-10-27 03:55:42 -07:00
committed by GitHub
parent 95de525af9
commit 7f38e141c7
10 changed files with 28 additions and 9 deletions

View File

@ -46,6 +46,14 @@ int File::available() {
return _p->size() - _p->position();
}
int File::availableForWrite() {
if (!_p)
return false;
return _p->availableForWrite();
}
int File::read() {
if (!_p)
return -1;

View File

@ -57,6 +57,7 @@ public:
// Print methods:
size_t write(uint8_t) override;
size_t write(const uint8_t *buf, size_t size) override;
int availableForWrite() override;
// Stream methods:
int available() override;

View File

@ -35,6 +35,7 @@ public:
virtual bool seek(uint32_t pos, SeekMode mode) = 0;
virtual size_t position() const = 0;
virtual size_t size() const = 0;
virtual int availableForWrite() { return 0; }
virtual bool truncate(uint32_t size) = 0;
virtual void close() = 0;
virtual const char* name() const = 0;

View File

@ -150,7 +150,7 @@ public:
{
return readBytes((char*)buffer, size);
}
int availableForWrite(void)
int availableForWrite(void) override
{
return static_cast<int>(uart_tx_free(_uart));
}

View File

@ -75,6 +75,10 @@ class Print {
inline size_t write(char c) { return write((uint8_t) c); }
inline size_t write(int8_t c) { return write((uint8_t) c); }
// default to zero, meaning "a single write may block"
// should be overriden by subclasses with buffering
virtual int availableForWrite() { return 0; }
size_t printf(const char * format, ...) __attribute__ ((format (printf, 2, 3)));
size_t printf_P(PGM_P format, ...) __attribute__((format(printf, 2, 3)));
size_t print(const __FlashStringHelper *);

View File

@ -173,10 +173,10 @@ void loop() {
// determine maximum output size "fair TCP use"
// client.availableForWrite() returns 0 when !client.connected()
size_t maxToTcp = 0;
int maxToTcp = 0;
for (int i = 0; i < MAX_SRV_CLIENTS; i++)
if (serverClients[i]) {
size_t afw = serverClients[i].availableForWrite();
int afw = serverClients[i].availableForWrite();
if (afw) {
if (!maxToTcp) {
maxToTcp = afw;
@ -190,11 +190,11 @@ void loop() {
}
//check UART for data
size_t len = std::min((size_t)Serial.available(), maxToTcp);
size_t len = std::min(Serial.available(), maxToTcp);
len = std::min(len, (size_t)STACK_PROTECTOR);
if (len) {
uint8_t sbuf[len];
size_t serial_got = Serial.readBytes(sbuf, len);
int serial_got = Serial.readBytes(sbuf, len);
// push UART data to all connected telnet clients
for (int i = 0; i < MAX_SRV_CLIENTS; i++)
// if client.availableForWrite() was 0 (congested)

View File

@ -195,7 +195,7 @@ bool WiFiClient::getSync() const
return _client->getSync();
}
size_t WiFiClient::availableForWrite ()
int WiFiClient::availableForWrite ()
{
return _client? _client->availableForWrite(): 0;
}

View File

@ -86,7 +86,7 @@ public:
static void setLocalPortStart(uint16_t port) { _localPort = port; }
size_t availableForWrite();
int availableForWrite() override;
friend class WiFiServer;

View File

@ -176,7 +176,7 @@ void Netdump::tcpDumpProcess(const Packet& np)
bufferIndex += incl_len;
}
if (bufferIndex && tcpDumpClient && tcpDumpClient.availableForWrite() >= bufferIndex)
if (bufferIndex && tcpDumpClient && tcpDumpClient.availableForWrite() >= (int)bufferIndex)
{
tcpDumpClient.write(packetBuffer, bufferIndex);
bufferIndex = 0;
@ -202,7 +202,7 @@ void Netdump::tcpDumpLoop(WiFiServer &tcpDumpServer, const Filter nf)
{
setCallback(nullptr);
}
if (bufferIndex && tcpDumpClient && tcpDumpClient.availableForWrite() >= bufferIndex)
if (bufferIndex && tcpDumpClient && tcpDumpClient.availableForWrite() >= (int)bufferIndex)
{
tcpDumpClient.write(packetBuffer, bufferIndex);
bufferIndex = 0;

View File

@ -268,6 +268,11 @@ public:
close();
}
int availableForWrite() override
{
return _opened ? _fd->availableForWrite() : 0;
}
size_t write(const uint8_t *buf, size_t size) override
{
return _opened ? _fd->write(buf, size) : -1;