Skip to content

Commit 5cf60b6

Browse files
committed
ClientContext: put things together
1 parent d31d6f2 commit 5cf60b6

File tree

3 files changed

+28
-35
lines changed

3 files changed

+28
-35
lines changed

libraries/ESP8266WiFi/src/WiFiClient.cpp

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -171,14 +171,14 @@ bool WiFiClient::getNoDelay() const {
171171

172172
void WiFiClient::setSync(bool sync)
173173
{
174-
if(!_client)
174+
if (!_client)
175175
return;
176176
_client->setSync(sync);
177177
}
178178

179179
bool WiFiClient::getSync() const
180180
{
181-
if(!_client)
181+
if (!_client)
182182
return false;
183183
return _client->getSync();
184184
}
@@ -285,21 +285,19 @@ size_t WiFiClient::peekBytes(uint8_t *buffer, size_t length) {
285285
return _client->peekBytes((char *)buffer, count);
286286
}
287287

288-
bool WiFiClient::flush(int maxWaitMs)
288+
void WiFiClient::flush()
289289
{
290290
if (_client)
291-
return !_client || _client->wait_until_sent(maxWaitMs);
292-
return true;
291+
_client->wait_until_sent();
293292
}
294293

295-
bool WiFiClient::stop(int maxWaitMs)
294+
void WiFiClient::stop()
296295
{
297296
if (!_client)
298-
return true;
297+
return;
299298

300-
bool ok = _client->wait_until_sent(maxWaitMs);
301-
ok &= _client->close() == ERR_OK;
302-
return ok;
299+
_client->wait_until_sent();
300+
_client->close();
303301
}
304302

305303
uint8_t WiFiClient::connected()

libraries/ESP8266WiFi/src/WiFiClient.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,8 @@ class WiFiClient : public Client, public SList<WiFiClient> {
7272
size_t peekBytes(char *buffer, size_t length) {
7373
return peekBytes((uint8_t *) buffer, length);
7474
}
75-
bool flush(int maxWaitMs);
76-
bool stop(int maxWaitMs);
77-
virtual void flush() { flush(WIFICLIENT_MAX_FLUSH_WAIT_MS); }
78-
virtual void stop() { stop(WIFICLIENT_MAX_FLUSH_WAIT_MS); }
75+
virtual void flush();
76+
virtual void stop();
7977
virtual uint8_t connected();
8078
virtual operator bool();
8179

libraries/ESP8266WiFi/src/include/ClientContext.h

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -296,31 +296,29 @@ class ClientContext
296296
_rx_buf_offset = 0;
297297
}
298298

299-
bool wait_until_sent(int max_wait_ms = WIFICLIENT_MAX_FLUSH_WAIT_MS)
299+
void wait_until_sent()
300300
{
301-
if (!_pcb)
302-
return true;
303-
304-
tcp_output(_pcb);
305-
306301
// https://github.com/esp8266/Arduino/pull/3967#pullrequestreview-83451496
307302
// option 1 done
308303
// option 2 / _write_some() not necessary since _datasource is always nullptr here
309304

310-
max_wait_ms++;
305+
if (!_pcb)
306+
return;
307+
308+
tcp_output(_pcb);
309+
310+
int max_wait_ms = WIFICLIENT_MAX_FLUSH_WAIT_MS + 1;
311311

312312
// wait for peer's acks flushing lwIP's output buffer
313313
while (state() == ESTABLISHED && tcp_sndbuf(_pcb) != TCP_SND_BUF && --max_wait_ms)
314-
delay(1); // esp_ schedule+yield
314+
delay(1); // yields
315315

316316
#ifdef DEBUGV
317317
if (max_wait_ms == 0) {
318318
// wait until sent: timeout
319319
DEBUGV(":wustmo\n");
320320
}
321321
#endif
322-
323-
return max_wait_ms > 0;
324322
}
325323

326324
uint8_t state() const
@@ -461,13 +459,15 @@ class ClientContext
461459
if (!next_chunk_size)
462460
break;
463461
const uint8_t* buf = _datasource->get_buffer(next_chunk_size);
464-
// TCP_WRITE_FLAG_MORE to remove PUSH flag from packet (lwIP's doc), implicitely disables nagle (see lwIP's tcp_out.c)
462+
// use TCP_WRITE_FLAG_MORE to remove PUSH flag from packet (lwIP's doc),
463+
// because PUSH implicitely disables nagle (see lwIP's tcp_out.c)
465464
// Notes:
466-
// PUSH is for peer, telling to give to user app as soon as received
467-
// PUSH may be set when sender has finished sending a meaningful data block
468-
// Nagle is for delaying local stack, to send less and bigger packets
469-
uint8_t flags = TCP_WRITE_FLAG_MORE; // do not tcp-PuSH (XXX always?)
465+
// PUSH is for peer, telling to give data to user app as soon as received
466+
// PUSH "may be set" when sender has finished sending a meaningful data block
467+
// Nagle is for delaying local data, to send less/bigger packets
468+
uint8_t flags = TCP_WRITE_FLAG_MORE; // do not tcp-PuSH
470469
if (!_sync)
470+
// user data will not stay in place when data are sent but not acknowledged
471471
flags |= TCP_WRITE_FLAG_COPY;
472472
err_t err = tcp_write(_pcb, buf, next_chunk_size, flags);
473473
DEBUGV(":wrc %d %d %d\r\n", next_chunk_size, will_send, (int)err);
@@ -484,8 +484,8 @@ class ClientContext
484484

485485
if (has_written && (_sync || tcp_nagle_disabled(_pcb)))
486486
{
487-
// handle nagle manually because of TCP_WRITE_FLAG_MORE
488-
// lwIP's tcp_output: "Find out what we can send and send it"
487+
// handle no-Nagle manually because of TCP_WRITE_FLAG_MORE
488+
// lwIP's tcp_output doc: "Find out what we can send and send it"
489489
tcp_output(_pcb);
490490
}
491491

@@ -494,13 +494,10 @@ class ClientContext
494494

495495
void _write_some_from_cb()
496496
{
497-
// lwIP needs feeding
498-
_write_some();
499-
500497
if (_send_waiting == 1) {
501498
_send_waiting--;
499+
esp_schedule();
502500
}
503-
esp_schedule();
504501
}
505502

506503
err_t _acked(tcp_pcb* pcb, uint16_t len)

0 commit comments

Comments
 (0)