Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
6c64759
wip
d-a-v Aug 22, 2018
746ec81
cc wip
d-a-v Aug 22, 2018
53c9222
cc wip
d-a-v Aug 22, 2018
6de50d4
cc comments
d-a-v Aug 22, 2018
2dd152d
wip cc
d-a-v Aug 24, 2018
f87602a
+sync, get/set default nodelay, sync
d-a-v Aug 27, 2018
6be4b6f
default nodelay=1
d-a-v Aug 28, 2018
8f7fb1e
update flush()
d-a-v Aug 28, 2018
c0bea2b
fix return value
d-a-v Aug 28, 2018
d00ac35
ClientContext: put things together
d-a-v Aug 28, 2018
53931c5
wip
d-a-v Aug 22, 2018
81ec0f4
cc wip
d-a-v Aug 22, 2018
fbf1dd1
cc wip
d-a-v Aug 22, 2018
b4a918f
cc comments
d-a-v Aug 22, 2018
c14eeef
wip cc
d-a-v Aug 24, 2018
238610e
+sync, get/set default nodelay, sync
d-a-v Aug 27, 2018
d0036a1
default nodelay=1
d-a-v Aug 28, 2018
e4c187b
update flush()
d-a-v Aug 28, 2018
d31d6f2
fix return value
d-a-v Aug 28, 2018
5cf60b6
ClientContext: put things together
d-a-v Aug 28, 2018
a058506
Move SSLContext to its own header (#5121)
earlephilhower Sep 17, 2018
a7a5959
Fix connection options and update github pubkey (#5120)
earlephilhower Sep 17, 2018
d06cac2
ClientContext: fix debugging messages
d-a-v Sep 18, 2018
e8c659e
Merge branch 'master' into ClientContext
earlephilhower Sep 18, 2018
f271b2a
WiFiClient: move static members out of the class, add comments
d-a-v Sep 18, 2018
7f57694
Merge branch 'ClientContext' of github.com:d-a-v/Arduino into ClientC…
d-a-v Sep 18, 2018
888bdb8
remove circular dependency
d-a-v Sep 19, 2018
c59a91f
parameter and return value for Client::flush&stop, flush timeout rais…
d-a-v Sep 22, 2018
3c54c9e
Merge branch 'master' into ClientContext
d-a-v Sep 23, 2018
8414e17
tcp flush: restart timer on ack receive
d-a-v Sep 23, 2018
4d62879
OTA protocol needs setNoDelay(true)
d-a-v Sep 23, 2018
826b8b5
fix Ethernet with Client changes
d-a-v Sep 23, 2018
3fa6a97
1 line unredable -> 5 lines readable code
d-a-v Sep 23, 2018
72b59d9
Merge branch 'master' into ClientContext
d-a-v Sep 24, 2018
7752c98
doc
d-a-v Sep 24, 2018
c160d17
doc
d-a-v Sep 24, 2018
db8fe6c
doc
d-a-v Sep 24, 2018
8bb444d
doc
d-a-v Sep 24, 2018
33dee32
Update client-class.rst
devyte Sep 24, 2018
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
Prev Previous commit
Next Next commit
+sync, get/set default nodelay, sync
  • Loading branch information
d-a-v committed Sep 18, 2018
commit 238610ee012caedfba0601ec138e6e3bd70b2fce
25 changes: 23 additions & 2 deletions libraries/ESP8266WiFi/src/WiFiClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ extern "C"
#include "c_types.h"

uint16_t WiFiClient::_localPort = 0;
bool WiFiClient::_defaultNoDelay = false;
bool WiFiClient::_defaultSync = false;

template<>
WiFiClient* SList<WiFiClient>::_s_first = 0;
Expand All @@ -60,6 +62,9 @@ WiFiClient::WiFiClient(ClientContext* client)
_timeout = 5000;
_client->ref();
WiFiClient::_add(this);

setSync(_defaultSync);
setNoDelay(_defaultNoDelay);
}

WiFiClient::~WiFiClient()
Expand Down Expand Up @@ -91,7 +96,6 @@ WiFiClient& WiFiClient::operator=(const WiFiClient& other)
return *this;
}


int WiFiClient::connect(const char* host, uint16_t port)
{
IPAddress remote_addr;
Expand Down Expand Up @@ -147,6 +151,9 @@ int WiFiClient::connect(IPAddress ip, uint16_t port)
return 0;
}

setSync(_defaultSync);
setNoDelay(_defaultNoDelay);

return 1;
}

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

bool WiFiClient::getNoDelay() {
bool WiFiClient::getNoDelay() const {
if (!_client)
return false;
return _client->getNoDelay();
}

void WiFiClient::setSync(bool sync)
{
if(!_client)
return;
_client->setSync(sync);
}

bool WiFiClient::getSync() const
{
if(!_client)
return false;
return _client->getSync();
}

size_t WiFiClient::availableForWrite ()
{
return _client? _client->availableForWrite(): 0;
Expand Down
13 changes: 12 additions & 1 deletion libraries/ESP8266WiFi/src/WiFiClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,11 @@ class WiFiClient : public Client, public SList<WiFiClient> {
uint16_t remotePort();
IPAddress localIP();
uint16_t localPort();
bool getNoDelay();
bool getNoDelay() const;
void setNoDelay(bool nodelay);
bool getSync() const;
void setSync(bool sync);

static void setLocalPortStart(uint16_t port) { _localPort = port; }

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

static void setDefaultNoDelay (bool noDelay) { _defaultNoDelay = noDelay; }
static void setDefaultSync (bool sync) { _defaultSync = sync; }
static bool getDefaultNoDelay () { return _defaultNoDelay; }
static bool getDefaultSync () { return _defaultSync; }

protected:

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

ClientContext* _client;
static uint16_t _localPort;

static bool _defaultNoDelay;
static bool _defaultSync;
};

#endif
11 changes: 8 additions & 3 deletions libraries/ESP8266WiFi/src/WiFiServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,16 @@ void WiFiServer::begin(uint16_t port) {
}

void WiFiServer::setNoDelay(bool nodelay) {
_noDelay = nodelay;
_noDelay = nodelay? _ndTrue: _ndFalse;
}

bool WiFiServer::getNoDelay() {
return _noDelay;
switch (_noDelay)
{
case _ndFalse: return false;
case _ndTrue: return true;
default: return WiFiClient::getDefaultNoDelay();
}
}

bool WiFiServer::hasClient() {
Expand All @@ -106,7 +111,7 @@ WiFiClient WiFiServer::available(byte* status) {
if (_unclaimed) {
WiFiClient result(_unclaimed);
_unclaimed = _unclaimed->next();
result.setNoDelay(_noDelay);
result.setNoDelay(getNoDelay());
DEBUGV("WS:av\r\n");
return result;
}
Expand Down
2 changes: 1 addition & 1 deletion libraries/ESP8266WiFi/src/WiFiServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class WiFiServer : public Server {

ClientContext* _unclaimed;
ClientContext* _discarded;
bool _noDelay = false;
enum { _ndDefault, _ndFalse, _ndTrue } _noDelay = _ndDefault;

public:
WiFiServer(IPAddress addr, uint16_t port);
Expand Down
54 changes: 34 additions & 20 deletions libraries/ESP8266WiFi/src/include/ClientContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class ClientContext
{
public:
ClientContext(tcp_pcb* pcb, discard_cb_t discard_cb, void* discard_cb_arg) :
_pcb(pcb), _rx_buf(0), _rx_buf_offset(0), _discard_cb(discard_cb), _discard_cb_arg(discard_cb_arg), _refcnt(0), _next(0)
_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())
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a bad idea, because it needlessly creates a circular dependency: ClientContext depends on WiFiClient, and WiFiClient depends on ClientContext. I suggest either factoring the sync into its own .h/.cpp, or pushing its declaration/definition into ClientContext.h/.cpp for reuse by WiFiClient class.

{
tcp_setprio(pcb, TCP_PRIO_MIN);
tcp_arg(pcb, this);
Expand All @@ -44,7 +44,7 @@ class ClientContext
tcp_err(pcb, &_s_error);
tcp_poll(pcb, &_s_poll, 1);

// not enabled by default for 2.4.0
// keep-alive not enabled by default
//keepAlive();
}

Expand Down Expand Up @@ -159,7 +159,7 @@ class ClientContext
}
}

bool getNoDelay()
bool getNoDelay() const
{
if(!_pcb) {
return false;
Expand All @@ -172,12 +172,12 @@ class ClientContext
_timeout_ms = timeout_ms;
}

int getTimeout()
int getTimeout() const
{
return _timeout_ms;
}

uint32_t getRemoteAddress()
uint32_t getRemoteAddress() const
{
if(!_pcb) {
return 0;
Expand All @@ -186,7 +186,7 @@ class ClientContext
return _pcb->remote_ip.addr;
}

uint16_t getRemotePort()
uint16_t getRemotePort() const
{
if(!_pcb) {
return 0;
Expand All @@ -195,7 +195,7 @@ class ClientContext
return _pcb->remote_port;
}

uint32_t getLocalAddress()
uint32_t getLocalAddress() const
{
if(!_pcb) {
return 0;
Expand All @@ -204,7 +204,7 @@ class ClientContext
return _pcb->local_ip.addr;
}

uint16_t getLocalPort()
uint16_t getLocalPort() const
{
if(!_pcb) {
return 0;
Expand Down Expand Up @@ -257,7 +257,7 @@ class ClientContext
return size_read;
}

char peek()
char peek() const
{
if(!_rx_buf) {
return 0;
Expand All @@ -266,7 +266,7 @@ class ClientContext
return reinterpret_cast<char*>(_rx_buf->payload)[_rx_buf_offset];
}

size_t peekBytes(char *dst, size_t size)
size_t peekBytes(char *dst, size_t size) const
{
if(!_rx_buf) {
return 0;
Expand Down Expand Up @@ -307,7 +307,7 @@ class ClientContext
int tries = 1+ WAIT_TRIES_MS;

while (state() == ESTABLISHED && tcp_sndbuf(_pcb) != TCP_SND_BUF && --tries) {
_write_some();
//_write_some();
delay(1); // esp_ schedule+yield
}
}
Expand All @@ -321,7 +321,6 @@ class ClientContext
return _pcb->state;
}


size_t write(const uint8_t* data, size_t size)
{
if (!_pcb) {
Expand Down Expand Up @@ -379,6 +378,16 @@ class ClientContext
return isKeepAliveEnabled()? _pcb->keep_cnt: 0;
}

bool getSync () const
{
return _sync;
}

void setSync (bool sync)
{
_sync = sync;
}

protected:

bool _is_timeout()
Expand Down Expand Up @@ -418,6 +427,10 @@ class ClientContext
esp_yield();
} while(true);
_send_waiting = 0;

if (_sync)
wait_until_sent();

return _written;
}

Expand All @@ -442,11 +455,9 @@ class ClientContext
// PUSH is for peer, telling to give to user app as soon as received
// PUSH may be set when sender has finished sending a meaningful data block
// Nagle is for delaying local stack, to send less and bigger packets
uint8_t flags = TCP_WRITE_FLAG_COPY;
if (!tcp_nagle_disabled(_pcb))
// nagle enabled, delayed, buffering, bigger packets
// (When should we use PUSH?)
flags |= TCP_WRITE_FLAG_MORE;
uint8_t flags = TCP_WRITE_FLAG_MORE; // do not tcp-PuSH (XXX always?)
if (!_sync)
flags |= TCP_WRITE_FLAG_COPY;
err_t err = tcp_write(_pcb, buf, next_chunk_size, flags);
DEBUGV(":wrc %d %d %d\r\n", next_chunk_size, will_send, (int)err);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will_send undefined, too

if (err == ERR_OK) {
Expand All @@ -460,10 +471,12 @@ class ClientContext
}
}

if (has_written && tcp_nagle_disabled(_pcb))
if (has_written && (_sync || tcp_nagle_disabled(_pcb)))
{
// handle nagle manually because of TCP_WRITE_FLAG_MORE
// lwIP's tcp_output: "Find out what we can send and send it"
tcp_output(_pcb);
}

return has_written;
}
Expand All @@ -472,7 +485,7 @@ class ClientContext
{
// lwIP needs feeding
_write_some();

if (_send_waiting == 1) {
_send_waiting--;
}
Expand Down Expand Up @@ -596,14 +609,15 @@ class ClientContext

DataSource* _datasource = nullptr;
size_t _written = 0;
//size_t _write_chunk_size = 256;
uint32_t _timeout_ms = 5000;
uint32_t _op_start_time = 0;
uint8_t _send_waiting = 0;
uint8_t _connect_pending = 0;

int8_t _refcnt;
ClientContext* _next;

bool _sync;
};

#endif//CLIENTCONTEXT_H