From 929d4aed5cdd60bbacf355eccb19458e636f9273 Mon Sep 17 00:00:00 2001 From: Santiago Date: Mon, 9 Feb 2015 03:00:18 -0300 Subject: [PATCH] Added the hold and unhold methods to tcp socket --- app/include/lwip/app/espconn.h | 16 +++++++++++ app/include/lwip/tcp.h | 2 ++ app/lwip/app/espconn.c | 29 ++++++++++++++++++++ app/lwip/app/espconn_tcp.c | 16 +++++++++++ app/lwip/core/tcp.c | 1 + app/lwip/core/tcp_in.c | 2 +- app/modules/net.c | 50 ++++++++++++++++++++++++++++++++++ 7 files changed, 115 insertions(+), 1 deletion(-) diff --git a/app/include/lwip/app/espconn.h b/app/include/lwip/app/espconn.h index e6e60fd1..6eda9e3a 100644 --- a/app/include/lwip/app/espconn.h +++ b/app/include/lwip/app/espconn.h @@ -394,5 +394,21 @@ extern sint8 espconn_igmp_join(ip_addr_t *host_ip, ip_addr_t *multicast_ip); *******************************************************************************/ extern sint8 espconn_igmp_leave(ip_addr_t *host_ip, ip_addr_t *multicast_ip); +/****************************************************************************** + * FunctionName : espconn_recv_hold + * Description : hold tcp receive + * Parameters : espconn -- espconn to hold + * Returns : none +*******************************************************************************/ +extern sint8 espconn_recv_hold(struct espconn *pespconn); + +/****************************************************************************** + * FunctionName : espconn_recv_unhold + * Description : unhold tcp receive + * Parameters : espconn -- espconn to unhold + * Returns : none +*******************************************************************************/ +extern sint8 espconn_recv_unhold(struct espconn *pespconn); + #endif diff --git a/app/include/lwip/tcp.h b/app/include/lwip/tcp.h index 909ff9b7..7856a92d 100644 --- a/app/include/lwip/tcp.h +++ b/app/include/lwip/tcp.h @@ -277,6 +277,8 @@ struct tcp_pcb { /* KEEPALIVE counter */ u8_t keep_cnt_sent; + + u8_t hold; }; struct tcp_pcb_listen { diff --git a/app/lwip/app/espconn.c b/app/lwip/app/espconn.c index bd81e4d6..114f1b59 100644 --- a/app/lwip/app/espconn.c +++ b/app/lwip/app/espconn.c @@ -718,3 +718,32 @@ espconn_gethostbyname(struct espconn *pespconn, const char *hostname, ip_addr_t return dns_gethostbyname(hostname, addr, found, pespconn); } +sint8 espconn_recv_hold(struct espconn *pespconn) { + espconn_msg *pnode = NULL; + + if (pespconn == NULL) { + return ESPCONN_ARG; + } + pespconn->state = ESPCONN_WRITE; + if (!espconn_find_connection(pespconn, &pnode)) { + return ESPCONN_ARG; + } + + espconn_tcp_hold(pnode); + return ESPCONN_OK; +} + +sint8 espconn_recv_unhold(struct espconn *pespconn) { + espconn_msg *pnode = NULL; + + if (pespconn == NULL) { + return ESPCONN_ARG; + } + pespconn->state = ESPCONN_WRITE; + if (!espconn_find_connection(pespconn, &pnode)) { + return ESPCONN_ARG; + } + + espconn_tcp_unhold(pnode); + return ESPCONN_OK; +} diff --git a/app/lwip/app/espconn_tcp.c b/app/lwip/app/espconn_tcp.c index 91843887..da5c441a 100644 --- a/app/lwip/app/espconn_tcp.c +++ b/app/lwip/app/espconn_tcp.c @@ -948,3 +948,19 @@ sint8 ICACHE_FLASH_ATTR espconn_tcp_delete(struct espconn *pdeletecon) return ESPCONN_ARG; } } + +void espconn_tcp_hold(void *arg) { + espconn_msg *ptcp_sent = arg; + struct tcp_pcb *pcb = NULL; + pcb = ptcp_sent->pcommon.pcb; + + pcb->hold = 1; +} + +void espconn_tcp_unhold(void *arg) { + espconn_msg *ptcp_sent = arg; + struct tcp_pcb *pcb = NULL; + pcb = ptcp_sent->pcommon.pcb; + + pcb->hold = 0; +} diff --git a/app/lwip/core/tcp.c b/app/lwip/core/tcp.c index e3193cc7..fe0bb913 100644 --- a/app/lwip/core/tcp.c +++ b/app/lwip/core/tcp.c @@ -1259,6 +1259,7 @@ tcp_alloc(u8_t prio) #endif /* LWIP_TCP_KEEPALIVE */ pcb->keep_cnt_sent = 0; //���ķ��ʹ��� + pcb->hold = 0; } return pcb; } diff --git a/app/lwip/core/tcp_in.c b/app/lwip/core/tcp_in.c index 2f4a1c42..2aca07d4 100644 --- a/app/lwip/core/tcp_in.c +++ b/app/lwip/core/tcp_in.c @@ -1137,7 +1137,7 @@ tcp_receive(struct tcp_pcb *pcb) /* If the incoming segment contains data, we must process it further. */ - if (tcplen > 0) { + if ((tcplen > 0) && (!pcb->hold)) { /* This code basically does three things: +) If the incoming segment contains data that is the next diff --git a/app/modules/net.c b/app/modules/net.c index 817ad670..51639b54 100644 --- a/app/modules/net.c +++ b/app/modules/net.c @@ -1193,6 +1193,54 @@ static int net_socket_send( lua_State* L ) return net_send(L, mt); } +static int net_socket_hold( lua_State* L ) +{ + const char *mt = "net.socket"; + struct espconn *pesp_conn = NULL; + lnet_userdata *nud; + size_t l; + + nud = (lnet_userdata *)luaL_checkudata(L, 1, mt); + luaL_argcheck(L, nud, 1, "Server/Socket expected"); + if(nud==NULL){ + NODE_DBG("userdata is nil.\n"); + return 0; + } + + if(nud->pesp_conn == NULL){ + NODE_DBG("nud->pesp_conn is NULL.\n"); + return 0; + } + pesp_conn = nud->pesp_conn; + espconn_recv_hold(pesp_conn); + + return 0; +} + +static int net_socket_unhold( lua_State* L ) +{ + const char *mt = "net.socket"; + struct espconn *pesp_conn = NULL; + lnet_userdata *nud; + size_t l; + + nud = (lnet_userdata *)luaL_checkudata(L, 1, mt); + luaL_argcheck(L, nud, 1, "Server/Socket expected"); + if(nud==NULL){ + NODE_DBG("userdata is nil.\n"); + return 0; + } + + if(nud->pesp_conn == NULL){ + NODE_DBG("nud->pesp_conn is NULL.\n"); + return 0; + } + pesp_conn = nud->pesp_conn; + espconn_recv_unhold(pesp_conn); + + return 0; +} + // Lua: socket:dns( string, function(ip) ) static int net_socket_dns( lua_State* L ) { @@ -1251,6 +1299,8 @@ static const LUA_REG_TYPE net_socket_map[] = { LSTRKEY( "close" ), LFUNCVAL ( net_socket_close ) }, { LSTRKEY( "on" ), LFUNCVAL ( net_socket_on ) }, { LSTRKEY( "send" ), LFUNCVAL ( net_socket_send ) }, + { LSTRKEY( "hold" ), LFUNCVAL ( net_socket_hold ) }, + { LSTRKEY( "unhold" ), LFUNCVAL ( net_socket_unhold ) }, { LSTRKEY( "dns" ), LFUNCVAL ( net_socket_dns ) }, // { LSTRKEY( "delete" ), LFUNCVAL ( net_socket_delete ) }, { LSTRKEY( "__gc" ), LFUNCVAL ( net_socket_delete ) },