Skip to content

TCP keepalive(2h,75s,9) API + status helper + change/disable methods (not enabled by default) #3937

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
Jan 5, 2018
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
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
Copy link
Collaborator

Choose a reason for hiding this comment

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

Do #defines count against available heap, or is it better to make these static const and put them in flash?
(I'm thinking of the free heap project, every byte counts)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

unlike static const, #define count for nothing in ram nor flash.

#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) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

What happens if any 2 of the 3 values are non-zero? Are all combinations allowed?

Copy link
Collaborator

Choose a reason for hiding this comment

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

(mostly thinking of sanity checks, if necessary)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

any 0 is an invalid value for keep-alive api, so any 0 disables keep-alive.

_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