mirror of
https://github.com/esp8266/Arduino.git
synced 2025-07-30 16:24:09 +03:00
deprecate and update Stream::send*(Print -> Stream)
(#8874)
* deprecate and update Stream::send(Print -> Stream) in order to benefit from and use output's timeout value
This commit is contained in:
@ -167,25 +167,49 @@ class Stream: public Print {
|
||||
// When result is 0 or less than requested maxLen, Print::getLastSend()
|
||||
// contains an error reason.
|
||||
|
||||
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
|
||||
|
||||
// transfers already buffered / immediately available data (no timeout)
|
||||
// returns number of transferred bytes
|
||||
size_t sendAvailable (Print* to) { return sendGeneric(to, -1, -1, oneShotMs::alwaysExpired); }
|
||||
size_t sendAvailable (Print& to) { return sendAvailable(&to); }
|
||||
[[deprecated]] size_t sendAvailable (Print* to) { return sendGeneric(to, -1, -1, oneShotMs::alwaysExpired); }
|
||||
[[deprecated]] size_t sendAvailable (Print& to) { return sendAvailable(&to); }
|
||||
|
||||
// transfers data until timeout
|
||||
// returns number of transferred bytes
|
||||
size_t sendAll (Print* to, const oneShotMs::timeType timeoutMs = oneShotMs::neverExpires) { return sendGeneric(to, -1, -1, timeoutMs); }
|
||||
size_t sendAll (Print& to, const oneShotMs::timeType timeoutMs = oneShotMs::neverExpires) { return sendAll(&to, timeoutMs); }
|
||||
[[deprecated]] size_t sendAll (Print* to, const oneShotMs::timeType timeoutMs = oneShotMs::neverExpires) { return sendGeneric(to, -1, -1, timeoutMs); }
|
||||
[[deprecated]] size_t sendAll (Print& to, const oneShotMs::timeType timeoutMs = oneShotMs::neverExpires) { return sendAll(&to, timeoutMs); }
|
||||
|
||||
// transfers data until a char is encountered (the char is swallowed but not transferred) with timeout
|
||||
// returns number of transferred bytes
|
||||
size_t sendUntil (Print* to, const int readUntilChar, const oneShotMs::timeType timeoutMs = oneShotMs::neverExpires) { return sendGeneric(to, -1, readUntilChar, timeoutMs); }
|
||||
size_t sendUntil (Print& to, const int readUntilChar, const oneShotMs::timeType timeoutMs = oneShotMs::neverExpires) { return sendUntil(&to, readUntilChar, timeoutMs); }
|
||||
[[deprecated]] size_t sendUntil (Print* to, const int readUntilChar, const oneShotMs::timeType timeoutMs = oneShotMs::neverExpires) { return sendGeneric(to, -1, readUntilChar, timeoutMs); }
|
||||
[[deprecated]] size_t sendUntil (Print& to, const int readUntilChar, const oneShotMs::timeType timeoutMs = oneShotMs::neverExpires) { return sendUntil(&to, readUntilChar, timeoutMs); }
|
||||
|
||||
// transfers data until requested size or timeout
|
||||
// returns number of transferred bytes
|
||||
size_t sendSize (Print* to, const ssize_t maxLen, const oneShotMs::timeType timeoutMs = oneShotMs::neverExpires) { return sendGeneric(to, maxLen, -1, timeoutMs); }
|
||||
size_t sendSize (Print& to, const ssize_t maxLen, const oneShotMs::timeType timeoutMs = oneShotMs::neverExpires) { return sendSize(&to, maxLen, timeoutMs); }
|
||||
[[deprecated]] size_t sendSize (Print* to, const ssize_t maxLen, const oneShotMs::timeType timeoutMs = oneShotMs::neverExpires) { return sendGeneric(to, maxLen, -1, timeoutMs); }
|
||||
[[deprecated]] size_t sendSize (Print& to, const ssize_t maxLen, const oneShotMs::timeType timeoutMs = oneShotMs::neverExpires) { return sendSize(&to, maxLen, timeoutMs); }
|
||||
|
||||
#pragma GCC diagnostic pop
|
||||
|
||||
// transfers already buffered / immediately available data (no timeout)
|
||||
// returns number of transferred bytes
|
||||
size_t sendAvailable (Stream* to) { return sendGeneric(to, -1, -1, oneShotMs::alwaysExpired); }
|
||||
size_t sendAvailable (Stream& to) { return sendAvailable(&to); }
|
||||
|
||||
// transfers data until timeout
|
||||
// returns number of transferred bytes
|
||||
size_t sendAll (Stream* to, const oneShotMs::timeType timeoutMs = oneShotMs::neverExpires) { return sendGeneric(to, -1, -1, timeoutMs); }
|
||||
size_t sendAll (Stream& to, const oneShotMs::timeType timeoutMs = oneShotMs::neverExpires) { return sendAll(&to, timeoutMs); }
|
||||
|
||||
// transfers data until a char is encountered (the char is swallowed but not transferred) with timeout
|
||||
// returns number of transferred bytes
|
||||
size_t sendUntil (Stream* to, const int readUntilChar, const oneShotMs::timeType timeoutMs = oneShotMs::neverExpires) { return sendGeneric(to, -1, readUntilChar, timeoutMs); }
|
||||
size_t sendUntil (Stream& to, const int readUntilChar, const oneShotMs::timeType timeoutMs = oneShotMs::neverExpires) { return sendUntil(&to, readUntilChar, timeoutMs); }
|
||||
|
||||
// transfers data until requested size or timeout
|
||||
// returns number of transferred bytes
|
||||
size_t sendSize (Stream* to, const ssize_t maxLen, const oneShotMs::timeType timeoutMs = oneShotMs::neverExpires) { return sendGeneric(to, maxLen, -1, timeoutMs); }
|
||||
size_t sendSize (Stream& to, const ssize_t maxLen, const oneShotMs::timeType timeoutMs = oneShotMs::neverExpires) { return sendSize(&to, maxLen, timeoutMs); }
|
||||
|
||||
// remaining size (-1 by default = unknown)
|
||||
virtual ssize_t streamRemaining () { return -1; }
|
||||
@ -202,11 +226,17 @@ class Stream: public Print {
|
||||
Report getLastSendReport () const { return _sendReport; }
|
||||
|
||||
protected:
|
||||
[[deprecated]]
|
||||
size_t sendGeneric (Print* to,
|
||||
const ssize_t len = -1,
|
||||
const int readUntilChar = -1,
|
||||
oneShotMs::timeType timeoutMs = oneShotMs::neverExpires /* neverExpires=>getTimeout() */);
|
||||
|
||||
size_t sendGeneric (Stream* to,
|
||||
const ssize_t len = -1,
|
||||
const int readUntilChar = -1,
|
||||
oneShotMs::timeType timeoutMs = oneShotMs::neverExpires /* neverExpires=>getTimeout() */);
|
||||
|
||||
size_t SendGenericPeekBuffer(Print* to, const ssize_t len, const int readUntilChar, const oneShotMs::timeType timeoutMs);
|
||||
size_t SendGenericRegularUntil(Print* to, const ssize_t len, const int readUntilChar, const oneShotMs::timeType timeoutMs);
|
||||
size_t SendGenericRegular(Print* to, const ssize_t len, const oneShotMs::timeType timeoutMs);
|
||||
|
@ -22,9 +22,17 @@
|
||||
#include <Arduino.h>
|
||||
#include <StreamDev.h>
|
||||
|
||||
size_t Stream::sendGeneric(Print* to, const ssize_t len, const int readUntilChar,
|
||||
size_t Stream::sendGeneric(Stream* to, const ssize_t len, const int readUntilChar,
|
||||
const esp8266::polledTimeout::oneShotFastMs::timeType timeoutMs)
|
||||
{
|
||||
// "neverExpires (default, impossible)" is translated to default timeout
|
||||
esp8266::polledTimeout::oneShotFastMs::timeType inputTimeoutMs
|
||||
= timeoutMs >= esp8266::polledTimeout::oneShotFastMs::neverExpires ? getTimeout()
|
||||
: timeoutMs;
|
||||
|
||||
esp8266::polledTimeout::oneShotFastMs::timeType mainTimeoutMs = std::max(
|
||||
inputTimeoutMs, (esp8266::polledTimeout::oneShotFastMs::timeType)to->getTimeout());
|
||||
|
||||
setReport(Report::Success);
|
||||
|
||||
if (len == 0)
|
||||
@ -43,25 +51,60 @@ size_t Stream::sendGeneric(Print* to, const ssize_t len, const int readUntilChar
|
||||
|
||||
if (hasPeekBufferAPI())
|
||||
{
|
||||
return SendGenericPeekBuffer(to, len, readUntilChar, timeoutMs);
|
||||
return SendGenericPeekBuffer(to, len, readUntilChar, mainTimeoutMs);
|
||||
}
|
||||
|
||||
if (readUntilChar >= 0)
|
||||
{
|
||||
return SendGenericRegularUntil(to, len, readUntilChar, timeoutMs);
|
||||
return SendGenericRegularUntil(to, len, readUntilChar, mainTimeoutMs);
|
||||
}
|
||||
|
||||
return SendGenericRegular(to, len, timeoutMs);
|
||||
return SendGenericRegular(to, len, mainTimeoutMs);
|
||||
}
|
||||
|
||||
size_t Stream::sendGeneric(Print* to, const ssize_t len, const int readUntilChar,
|
||||
const esp8266::polledTimeout::oneShotFastMs::timeType timeoutMs)
|
||||
{
|
||||
// "neverExpires (default, impossible)" is translated to default timeout
|
||||
esp8266::polledTimeout::oneShotFastMs::timeType inputTimeoutMs
|
||||
= timeoutMs >= esp8266::polledTimeout::oneShotFastMs::neverExpires ? getTimeout()
|
||||
: timeoutMs;
|
||||
|
||||
setReport(Report::Success);
|
||||
|
||||
if (len == 0)
|
||||
{
|
||||
return 0; // conveniently avoids timeout for no requested data
|
||||
}
|
||||
|
||||
// There are two timeouts:
|
||||
// - read (network, serial, ...)
|
||||
// - write (network, serial, ...)
|
||||
// However
|
||||
// - getTimeout() is for reading only
|
||||
// - there is no getOutputTimeout() api
|
||||
// So we use getTimeout() for both,
|
||||
// (also when inputCanTimeout() is false)
|
||||
|
||||
if (hasPeekBufferAPI())
|
||||
{
|
||||
return SendGenericPeekBuffer(to, len, readUntilChar, inputTimeoutMs);
|
||||
}
|
||||
|
||||
if (readUntilChar >= 0)
|
||||
{
|
||||
return SendGenericRegularUntil(to, len, readUntilChar, inputTimeoutMs);
|
||||
}
|
||||
|
||||
return SendGenericRegular(to, len, inputTimeoutMs);
|
||||
}
|
||||
|
||||
size_t
|
||||
Stream::SendGenericPeekBuffer(Print* to, const ssize_t len, const int readUntilChar,
|
||||
const esp8266::polledTimeout::oneShotFastMs::timeType timeoutMs)
|
||||
{
|
||||
// "neverExpires (default, impossible)" is translated to default timeout
|
||||
esp8266::polledTimeout::oneShotFastMs timedOut(
|
||||
timeoutMs >= esp8266::polledTimeout::oneShotFastMs::neverExpires ? getTimeout()
|
||||
: timeoutMs);
|
||||
esp8266::polledTimeout::oneShotFastMs timedOut(timeoutMs);
|
||||
|
||||
// len==-1 => maxLen=0 <=> until starvation
|
||||
const size_t maxLen = std::max((ssize_t)0, len);
|
||||
size_t written = 0;
|
||||
@ -152,10 +195,8 @@ Stream::SendGenericRegularUntil(Print* to, const ssize_t len, const int readUnti
|
||||
// regular Stream API
|
||||
// no other choice than reading byte by byte
|
||||
|
||||
// "neverExpires (default, impossible)" is translated to default timeout
|
||||
esp8266::polledTimeout::oneShotFastMs timedOut(
|
||||
timeoutMs >= esp8266::polledTimeout::oneShotFastMs::neverExpires ? getTimeout()
|
||||
: timeoutMs);
|
||||
esp8266::polledTimeout::oneShotFastMs timedOut(timeoutMs);
|
||||
|
||||
// len==-1 => maxLen=0 <=> until starvation
|
||||
const size_t maxLen = std::max((ssize_t)0, len);
|
||||
size_t written = 0;
|
||||
@ -231,10 +272,8 @@ size_t Stream::SendGenericRegular(Print* to, const ssize_t len,
|
||||
// regular Stream API
|
||||
// use an intermediary buffer
|
||||
|
||||
// "neverExpires (default, impossible)" is translated to default timeout
|
||||
esp8266::polledTimeout::oneShotFastMs timedOut(
|
||||
timeoutMs >= esp8266::polledTimeout::oneShotFastMs::neverExpires ? getTimeout()
|
||||
: timeoutMs);
|
||||
esp8266::polledTimeout::oneShotFastMs timedOut(timeoutMs);
|
||||
|
||||
// len==-1 => maxLen=0 <=> until starvation
|
||||
const size_t maxLen = std::max((ssize_t)0, len);
|
||||
size_t written = 0;
|
||||
|
Reference in New Issue
Block a user