Skip to content

TCP-server keepalive #3886

Closed
Closed
@Eric-1dev

Description

@Eric-1dev

Hardware

Hardware: ESP-12
Core Version: 2.4.0-rc2

Hello. It may be useful for someone to be able to enable keepalive packets for a TCP server. For example, like this:

In file WifiClient.cpp:

void WiFiClient::keepalive(bool enable, uint32_t idle, uint32_t interval, uint32_t count)
{
	keep_enabled = enable;
	keep_idle = idle;
	keep_interval = interval;
	keep_count = count;
}

int WiFiClient::connect(IPAddress ip, uint16_t port)
{
    ip_addr_t addr;
    addr.addr = ip;

    if (_client)
        stop();

    // if the default interface is down, tcp_connect exits early without
    // ever calling tcp_err
    // http://lists.gnu.org/archive/html/lwip-devel/2010-05/msg00001.html
#if LWIP_VERSION_MAJOR == 1
    netif* interface = ip_route(&addr);
    if (!interface) {
        DEBUGV("no route to host\r\n");
        return 0;
    }
#endif

    tcp_pcb* pcb = tcp_new();
    if (!pcb)
        return 0;

    if (_localPort > 0) {
        pcb->local_port = _localPort++;
    }

> 	if ( keep_enabled )
> 	{
> 		pcb->so_options |= SOF_KEEPALIVE;
> 		pcb->keep_idle = keep_idle;
> 		pcb->keep_intvl = keep_interval;
> 		pcb->keep_cnt = keep_count;
> 	}
> 	else
> 		pcb->so_options &= ~SOF_KEEPALIVE;

    _client = new ClientContext(pcb, nullptr, nullptr);
    _client->ref();
    _client->setTimeout(_timeout);
    int res = _client->connect(&addr, port);
    if (res == 0) {
        _client->unref();
        _client = nullptr;
        return 0;
    }

    return 1;
}

In file WifiClient.h:

public:
 void keepalive(bool enable, uint32_t idle, uint32_t interval, uint32_t count);
private:
  bool keep_enabled;
  uint32_t keep_idle;
  uint32_t keep_interval;
  uint32_t keep_count;

Can it be worth adding this feature to the project? Thank you. Sorry for my english.

Metadata

Metadata

Assignees

Labels

waiting for feedbackWaiting on additional info. If it's not received, the issue may be closed.

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions