Skip to content

Commit

Permalink
keepalive api (default 2h,75s,9 not enabled) (#3937)
Browse files Browse the repository at this point in the history
  • Loading branch information
d-a-v authored and devyte committed Jan 5, 2018
1 parent fb7c519 commit 9cfbbc7
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 0 deletions.
26 changes: 26 additions & 0 deletions libraries/ESP8266WiFi/src/WiFiClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -344,3 +344,29 @@ void WiFiClient::stopAllExcept(WiFiClient* except)
}
}
}


void WiFiClient::keepAlive (uint16_t idle_sec, uint16_t intv_sec, uint8_t count)
{
_client->keepAlive(idle_sec, intv_sec, count);
}

bool WiFiClient::isKeepAliveEnabled () const
{
return _client->isKeepAliveEnabled();
}

uint16_t WiFiClient::getKeepAliveIdle () const
{
return _client->getKeepAliveIdle();
}

uint16_t WiFiClient::getKeepAliveInterval () const
{
return _client->getKeepAliveInterval();
}

uint8_t WiFiClient::getKeepAliveCount () const
{
return _client->getKeepAliveCount();
}
11 changes: 11 additions & 0 deletions libraries/ESP8266WiFi/src/WiFiClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@

#define WIFICLIENT_MAX_PACKET_SIZE 1460

#define TCP_DEFAULT_KEEPALIVE_IDLE_SEC 7200 // 2 hours
#define TCP_DEFAULT_KEEPALIVE_INTERVAL_SEC 75 // 75 sec
#define TCP_DEFAULT_KEEPALIVE_COUNT 9 // fault after 9 failures

class ClientContext;
class WiFiServer;

Expand Down Expand Up @@ -85,6 +89,13 @@ class WiFiClient : public Client, public SList<WiFiClient> {
static void stopAll();
static void stopAllExcept(WiFiClient * c);

void keepAlive (uint16_t idle_sec = TCP_DEFAULT_KEEPALIVE_IDLE_SEC, uint16_t intv_sec = TCP_DEFAULT_KEEPALIVE_INTERVAL_SEC, uint8_t count = TCP_DEFAULT_KEEPALIVE_COUNT);
bool isKeepAliveEnabled () const;
uint16_t getKeepAliveIdle () const;
uint16_t getKeepAliveInterval () const;
uint8_t getKeepAliveCount () const;
void disableKeepAlive () { keepAlive(0, 0, 0); }

protected:

static int8_t _s_connected(void* arg, void* tpcb, int8_t err);
Expand Down
35 changes: 35 additions & 0 deletions libraries/ESP8266WiFi/src/include/ClientContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ class ClientContext
tcp_sent(pcb, &_s_sent);
tcp_err(pcb, &_s_error);
tcp_poll(pcb, &_s_poll, 1);

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

err_t abort()
Expand Down Expand Up @@ -341,6 +344,38 @@ class ClientContext
return _write_from_source(new BufferedStreamDataSource<ProgmemStream>(stream, size));
}

void keepAlive (uint16_t idle_sec = TCP_DEFAULT_KEEPALIVE_IDLE_SEC, uint16_t intv_sec = TCP_DEFAULT_KEEPALIVE_INTERVAL_SEC, uint8_t count = TCP_DEFAULT_KEEPALIVE_COUNT)
{
if (idle_sec && intv_sec && count) {
_pcb->so_options |= SOF_KEEPALIVE;
_pcb->keep_idle = (uint32_t)1000 * idle_sec;
_pcb->keep_intvl = (uint32_t)1000 * intv_sec;
_pcb->keep_cnt = count;
}
else
_pcb->so_options &= ~SOF_KEEPALIVE;
}

bool isKeepAliveEnabled () const
{
return !!(_pcb->so_options & SOF_KEEPALIVE);
}

uint16_t getKeepAliveIdle () const
{
return isKeepAliveEnabled()? (_pcb->keep_idle + 500) / 1000: 0;
}

uint16_t getKeepAliveInterval () const
{
return isKeepAliveEnabled()? (_pcb->keep_intvl + 500) / 1000: 0;
}

uint8_t getKeepAliveCount () const
{
return isKeepAliveEnabled()? _pcb->keep_cnt: 0;
}

protected:

bool _is_timeout()
Expand Down

0 comments on commit 9cfbbc7

Please sign in to comment.