Skip to content

Commit 238610e

Browse files
committed
+sync, get/set default nodelay, sync
1 parent c14eeef commit 238610e

File tree

5 files changed

+78
-27
lines changed

5 files changed

+78
-27
lines changed

libraries/ESP8266WiFi/src/WiFiClient.cpp

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ extern "C"
4242
#include "c_types.h"
4343

4444
uint16_t WiFiClient::_localPort = 0;
45+
bool WiFiClient::_defaultNoDelay = false;
46+
bool WiFiClient::_defaultSync = false;
4547

4648
template<>
4749
WiFiClient* SList<WiFiClient>::_s_first = 0;
@@ -60,6 +62,9 @@ WiFiClient::WiFiClient(ClientContext* client)
6062
_timeout = 5000;
6163
_client->ref();
6264
WiFiClient::_add(this);
65+
66+
setSync(_defaultSync);
67+
setNoDelay(_defaultNoDelay);
6368
}
6469

6570
WiFiClient::~WiFiClient()
@@ -91,7 +96,6 @@ WiFiClient& WiFiClient::operator=(const WiFiClient& other)
9196
return *this;
9297
}
9398

94-
9599
int WiFiClient::connect(const char* host, uint16_t port)
96100
{
97101
IPAddress remote_addr;
@@ -147,6 +151,9 @@ int WiFiClient::connect(IPAddress ip, uint16_t port)
147151
return 0;
148152
}
149153

154+
setSync(_defaultSync);
155+
setNoDelay(_defaultNoDelay);
156+
150157
return 1;
151158
}
152159

@@ -156,12 +163,26 @@ void WiFiClient::setNoDelay(bool nodelay) {
156163
_client->setNoDelay(nodelay);
157164
}
158165

159-
bool WiFiClient::getNoDelay() {
166+
bool WiFiClient::getNoDelay() const {
160167
if (!_client)
161168
return false;
162169
return _client->getNoDelay();
163170
}
164171

172+
void WiFiClient::setSync(bool sync)
173+
{
174+
if(!_client)
175+
return;
176+
_client->setSync(sync);
177+
}
178+
179+
bool WiFiClient::getSync() const
180+
{
181+
if(!_client)
182+
return false;
183+
return _client->getSync();
184+
}
185+
165186
size_t WiFiClient::availableForWrite ()
166187
{
167188
return _client? _client->availableForWrite(): 0;

libraries/ESP8266WiFi/src/WiFiClient.h

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,11 @@ class WiFiClient : public Client, public SList<WiFiClient> {
7676
uint16_t remotePort();
7777
IPAddress localIP();
7878
uint16_t localPort();
79-
bool getNoDelay();
79+
bool getNoDelay() const;
8080
void setNoDelay(bool nodelay);
81+
bool getSync() const;
82+
void setSync(bool sync);
83+
8184
static void setLocalPortStart(uint16_t port) { _localPort = port; }
8285

8386
size_t availableForWrite();
@@ -96,6 +99,11 @@ class WiFiClient : public Client, public SList<WiFiClient> {
9699
uint8_t getKeepAliveCount () const;
97100
void disableKeepAlive () { keepAlive(0, 0, 0); }
98101

102+
static void setDefaultNoDelay (bool noDelay) { _defaultNoDelay = noDelay; }
103+
static void setDefaultSync (bool sync) { _defaultSync = sync; }
104+
static bool getDefaultNoDelay () { return _defaultNoDelay; }
105+
static bool getDefaultSync () { return _defaultSync; }
106+
99107
protected:
100108

101109
static int8_t _s_connected(void* arg, void* tpcb, int8_t err);
@@ -106,6 +114,9 @@ class WiFiClient : public Client, public SList<WiFiClient> {
106114

107115
ClientContext* _client;
108116
static uint16_t _localPort;
117+
118+
static bool _defaultNoDelay;
119+
static bool _defaultSync;
109120
};
110121

111122
#endif

libraries/ESP8266WiFi/src/WiFiServer.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,16 @@ void WiFiServer::begin(uint16_t port) {
8888
}
8989

9090
void WiFiServer::setNoDelay(bool nodelay) {
91-
_noDelay = nodelay;
91+
_noDelay = nodelay? _ndTrue: _ndFalse;
9292
}
9393

9494
bool WiFiServer::getNoDelay() {
95-
return _noDelay;
95+
switch (_noDelay)
96+
{
97+
case _ndFalse: return false;
98+
case _ndTrue: return true;
99+
default: return WiFiClient::getDefaultNoDelay();
100+
}
96101
}
97102

98103
bool WiFiServer::hasClient() {
@@ -106,7 +111,7 @@ WiFiClient WiFiServer::available(byte* status) {
106111
if (_unclaimed) {
107112
WiFiClient result(_unclaimed);
108113
_unclaimed = _unclaimed->next();
109-
result.setNoDelay(_noDelay);
114+
result.setNoDelay(getNoDelay());
110115
DEBUGV("WS:av\r\n");
111116
return result;
112117
}

libraries/ESP8266WiFi/src/WiFiServer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class WiFiServer : public Server {
4343

4444
ClientContext* _unclaimed;
4545
ClientContext* _discarded;
46-
bool _noDelay = false;
46+
enum { _ndDefault, _ndFalse, _ndTrue } _noDelay = _ndDefault;
4747

4848
public:
4949
WiFiServer(IPAddress addr, uint16_t port);

libraries/ESP8266WiFi/src/include/ClientContext.h

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class ClientContext
3535
{
3636
public:
3737
ClientContext(tcp_pcb* pcb, discard_cb_t discard_cb, void* discard_cb_arg) :
38-
_pcb(pcb), _rx_buf(0), _rx_buf_offset(0), _discard_cb(discard_cb), _discard_cb_arg(discard_cb_arg), _refcnt(0), _next(0)
38+
_pcb(pcb), _rx_buf(0), _rx_buf_offset(0), _discard_cb(discard_cb), _discard_cb_arg(discard_cb_arg), _refcnt(0), _next(0), _sync(WiFiClient::getDefaultSync())
3939
{
4040
tcp_setprio(pcb, TCP_PRIO_MIN);
4141
tcp_arg(pcb, this);
@@ -44,7 +44,7 @@ class ClientContext
4444
tcp_err(pcb, &_s_error);
4545
tcp_poll(pcb, &_s_poll, 1);
4646

47-
// not enabled by default for 2.4.0
47+
// keep-alive not enabled by default
4848
//keepAlive();
4949
}
5050

@@ -159,7 +159,7 @@ class ClientContext
159159
}
160160
}
161161

162-
bool getNoDelay()
162+
bool getNoDelay() const
163163
{
164164
if(!_pcb) {
165165
return false;
@@ -172,12 +172,12 @@ class ClientContext
172172
_timeout_ms = timeout_ms;
173173
}
174174

175-
int getTimeout()
175+
int getTimeout() const
176176
{
177177
return _timeout_ms;
178178
}
179179

180-
uint32_t getRemoteAddress()
180+
uint32_t getRemoteAddress() const
181181
{
182182
if(!_pcb) {
183183
return 0;
@@ -186,7 +186,7 @@ class ClientContext
186186
return _pcb->remote_ip.addr;
187187
}
188188

189-
uint16_t getRemotePort()
189+
uint16_t getRemotePort() const
190190
{
191191
if(!_pcb) {
192192
return 0;
@@ -195,7 +195,7 @@ class ClientContext
195195
return _pcb->remote_port;
196196
}
197197

198-
uint32_t getLocalAddress()
198+
uint32_t getLocalAddress() const
199199
{
200200
if(!_pcb) {
201201
return 0;
@@ -204,7 +204,7 @@ class ClientContext
204204
return _pcb->local_ip.addr;
205205
}
206206

207-
uint16_t getLocalPort()
207+
uint16_t getLocalPort() const
208208
{
209209
if(!_pcb) {
210210
return 0;
@@ -257,7 +257,7 @@ class ClientContext
257257
return size_read;
258258
}
259259

260-
char peek()
260+
char peek() const
261261
{
262262
if(!_rx_buf) {
263263
return 0;
@@ -266,7 +266,7 @@ class ClientContext
266266
return reinterpret_cast<char*>(_rx_buf->payload)[_rx_buf_offset];
267267
}
268268

269-
size_t peekBytes(char *dst, size_t size)
269+
size_t peekBytes(char *dst, size_t size) const
270270
{
271271
if(!_rx_buf) {
272272
return 0;
@@ -307,7 +307,7 @@ class ClientContext
307307
int tries = 1+ WAIT_TRIES_MS;
308308

309309
while (state() == ESTABLISHED && tcp_sndbuf(_pcb) != TCP_SND_BUF && --tries) {
310-
_write_some();
310+
//_write_some();
311311
delay(1); // esp_ schedule+yield
312312
}
313313
}
@@ -321,7 +321,6 @@ class ClientContext
321321
return _pcb->state;
322322
}
323323

324-
325324
size_t write(const uint8_t* data, size_t size)
326325
{
327326
if (!_pcb) {
@@ -379,6 +378,16 @@ class ClientContext
379378
return isKeepAliveEnabled()? _pcb->keep_cnt: 0;
380379
}
381380

381+
bool getSync () const
382+
{
383+
return _sync;
384+
}
385+
386+
void setSync (bool sync)
387+
{
388+
_sync = sync;
389+
}
390+
382391
protected:
383392

384393
bool _is_timeout()
@@ -418,6 +427,10 @@ class ClientContext
418427
esp_yield();
419428
} while(true);
420429
_send_waiting = 0;
430+
431+
if (_sync)
432+
wait_until_sent();
433+
421434
return _written;
422435
}
423436

@@ -442,11 +455,9 @@ class ClientContext
442455
// PUSH is for peer, telling to give to user app as soon as received
443456
// PUSH may be set when sender has finished sending a meaningful data block
444457
// Nagle is for delaying local stack, to send less and bigger packets
445-
uint8_t flags = TCP_WRITE_FLAG_COPY;
446-
if (!tcp_nagle_disabled(_pcb))
447-
// nagle enabled, delayed, buffering, bigger packets
448-
// (When should we use PUSH?)
449-
flags |= TCP_WRITE_FLAG_MORE;
458+
uint8_t flags = TCP_WRITE_FLAG_MORE; // do not tcp-PuSH (XXX always?)
459+
if (!_sync)
460+
flags |= TCP_WRITE_FLAG_COPY;
450461
err_t err = tcp_write(_pcb, buf, next_chunk_size, flags);
451462
DEBUGV(":wrc %d %d %d\r\n", next_chunk_size, will_send, (int)err);
452463
if (err == ERR_OK) {
@@ -460,10 +471,12 @@ class ClientContext
460471
}
461472
}
462473

463-
if (has_written && tcp_nagle_disabled(_pcb))
474+
if (has_written && (_sync || tcp_nagle_disabled(_pcb)))
475+
{
464476
// handle nagle manually because of TCP_WRITE_FLAG_MORE
465477
// lwIP's tcp_output: "Find out what we can send and send it"
466478
tcp_output(_pcb);
479+
}
467480

468481
return has_written;
469482
}
@@ -472,7 +485,7 @@ class ClientContext
472485
{
473486
// lwIP needs feeding
474487
_write_some();
475-
488+
476489
if (_send_waiting == 1) {
477490
_send_waiting--;
478491
}
@@ -596,14 +609,15 @@ class ClientContext
596609

597610
DataSource* _datasource = nullptr;
598611
size_t _written = 0;
599-
//size_t _write_chunk_size = 256;
600612
uint32_t _timeout_ms = 5000;
601613
uint32_t _op_start_time = 0;
602614
uint8_t _send_waiting = 0;
603615
uint8_t _connect_pending = 0;
604616

605617
int8_t _refcnt;
606618
ClientContext* _next;
619+
620+
bool _sync;
607621
};
608622

609623
#endif//CLIENTCONTEXT_H

0 commit comments

Comments
 (0)