Skip to content

fix #1002 ::Flush() wait for empty send buffer #3967

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

Merged
merged 3 commits into from
Dec 17, 2017
Merged
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
2 changes: 1 addition & 1 deletion libraries/ESP8266WiFi/src/WiFiClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ size_t WiFiClient::peekBytes(uint8_t *buffer, size_t length) {
void WiFiClient::flush()
{
if (_client)
_client->flush();
_client->wait_until_sent();
}

void WiFiClient::stop()
Expand Down
3 changes: 1 addition & 2 deletions libraries/ESP8266WiFi/src/WiFiUdp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -243,8 +243,7 @@ int WiFiUDP::peek()

void WiFiUDP::flush()
{
if (_ctx)
_ctx->flush();
endPacket();
}

IPAddress WiFiUDP::remoteIP()
Expand Down
20 changes: 18 additions & 2 deletions libraries/ESP8266WiFi/src/include/ClientContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ class ClientContext
if(this != 0) {
DEBUGV(":ur %d\r\n", _refcnt);
if(--_refcnt == 0) {
flush();
discard_received();
close();
if(_discard_cb) {
_discard_cb(_discard_cb_arg, this);
Expand Down Expand Up @@ -277,7 +277,7 @@ class ClientContext
return copy_size;
}

void flush()
void discard_received()
{
if(!_rx_buf) {
return;
Expand All @@ -290,6 +290,22 @@ class ClientContext
_rx_buf_offset = 0;
}

void wait_until_sent()
{
// fix option 1 in
// https://github.com/esp8266/Arduino/pull/3967#pullrequestreview-83451496
// TODO: option 2

#define WAIT_TRIES_MS 10 // at most 10ms

int tries = 1+ WAIT_TRIES_MS;

while (state() == ESTABLISHED && tcp_sndbuf(_pcb) != TCP_SND_BUF && --tries) {
_write_some();
delay(1); // esp_ schedule+yield
}
}

uint8_t state() const
{
if(!_pcb) {
Expand Down
9 changes: 7 additions & 2 deletions libraries/Ethernet/src/EthernetUdp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ size_t EthernetUDP::write(const uint8_t *buffer, size_t size)
int EthernetUDP::parsePacket()
{
// discard any remaining bytes in the last packet
flush();
clear_remaining();

if (recvAvailable(_sock) > 0)
{
Expand Down Expand Up @@ -204,7 +204,7 @@ int EthernetUDP::peek()
return b;
}

void EthernetUDP::flush()
void EthernetUDP::clear_remaining()
{
// could this fail (loop endlessly) if _remaining > 0 and recv in read fails?
// should only occur if recv fails after telling us the data is there, lets
Expand All @@ -216,3 +216,8 @@ void EthernetUDP::flush()
}
}

void EthernetUDP::flush()
{
endPacket();
}

3 changes: 3 additions & 0 deletions libraries/Ethernet/src/EthernetUdp.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ class EthernetUDP : public UDP {
uint16_t _remotePort; // remote port for the incoming packet whilst it's being processed
uint16_t _offset; // offset into the packet being sent
uint16_t _remaining; // remaining bytes of incoming packet yet to be processed

protected:
void clear_remaining();

public:
EthernetUDP(); // Constructor
Expand Down