From f2911d67b7e62d6d750aa9cbe72b79fc5fa99b7d Mon Sep 17 00:00:00 2001 From: Sebastien Bourdeauducq Date: Thu, 13 Aug 2015 18:32:37 +0800 Subject: [PATCH] Enable TCP keepalive on the core device Automatically runs the idle experiment a few seconds after the master stops responding. Thanks Florent for figuring out TCP_KEEPIDLE_DEFAULT needed to be set in addition to the other options. Closes #31 --- soc/runtime/liblwip/lwipopts.h | 4 ++++ soc/runtime/net_server.c | 17 ++++++++++------- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/soc/runtime/liblwip/lwipopts.h b/soc/runtime/liblwip/lwipopts.h index bca6722a3..147322dd8 100644 --- a/soc/runtime/liblwip/lwipopts.h +++ b/soc/runtime/liblwip/lwipopts.h @@ -105,6 +105,10 @@ a lot of data that needs to be copied, this should be set high. */ /* ---------- TCP options ---------- */ #define LWIP_TCP 1 +#define LWIP_TCP_KEEPALIVE 1 +#define TCP_KEEPIDLE_DEFAULT 1250 +#define TCP_KEEPINTVL_DEFAULT 1000 +#define TCP_KEEPCNT_DEFAULT 3 #define TCP_TTL 255 /* Controls if TCP should queue segments that arrive out of diff --git a/soc/runtime/net_server.c b/soc/runtime/net_server.c index a28b396bd..fa06474b6 100644 --- a/soc/runtime/net_server.c +++ b/soc/runtime/net_server.c @@ -60,14 +60,16 @@ static void net_server_close(struct net_server_connstate *cs, struct tcp_pcb *pc active_pcb = NULL; } - /* lwip loves to call back with broken pointers. Prevent that. */ - tcp_arg(pcb, NULL); - tcp_recv(pcb, NULL); - tcp_sent(pcb, NULL); - tcp_err(pcb, NULL); + if(pcb) { + /* lwip loves to call back with broken pointers. Prevent that. */ + tcp_arg(pcb, NULL); + tcp_recv(pcb, NULL); + tcp_sent(pcb, NULL); + tcp_err(pcb, NULL); + tcp_close(pcb); + } cs_free(cs); - tcp_close(pcb); } static err_t net_server_recv(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err) @@ -155,7 +157,7 @@ static void net_server_err(void *arg, err_t err) struct net_server_connstate *cs; cs = (struct net_server_connstate *)arg; - cs_free(cs); + net_server_close(cs, NULL); } static struct tcp_pcb *listen_pcb; @@ -177,6 +179,7 @@ static err_t net_server_accept(void *arg, struct tcp_pcb *newpcb, err_t err) void net_server_init(void) { listen_pcb = tcp_new(); + listen_pcb->so_options |= SOF_KEEPALIVE; tcp_bind(listen_pcb, IP_ADDR_ANY, 1381); listen_pcb = tcp_listen(listen_pcb); tcp_accept(listen_pcb, net_server_accept);