Merge pull request #196 from Point-less/master

Added the hold and unhold methods to tcp socket
This commit is contained in:
zeroday 2015-02-09 22:17:13 +08:00
commit a9398b1dd4
8 changed files with 132 additions and 3 deletions

View File

@ -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); 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 #endif

View File

@ -277,6 +277,8 @@ struct tcp_pcb {
/* KEEPALIVE counter */ /* KEEPALIVE counter */
u8_t keep_cnt_sent; u8_t keep_cnt_sent;
u8_t hold;
}; };
struct tcp_pcb_listen { struct tcp_pcb_listen {

View File

@ -718,3 +718,32 @@ espconn_gethostbyname(struct espconn *pespconn, const char *hostname, ip_addr_t
return dns_gethostbyname(hostname, addr, found, pespconn); 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;
}

View File

@ -948,3 +948,19 @@ sint8 ICACHE_FLASH_ATTR espconn_tcp_delete(struct espconn *pdeletecon)
return ESPCONN_ARG; 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;
}

View File

@ -1259,6 +1259,7 @@ tcp_alloc(u8_t prio)
#endif /* LWIP_TCP_KEEPALIVE */ #endif /* LWIP_TCP_KEEPALIVE */
pcb->keep_cnt_sent = 0; //<2F><><EFBFBD>ķ<EFBFBD><C4B7>ʹ<EFBFBD><CDB4><EFBFBD> pcb->keep_cnt_sent = 0; //<2F><><EFBFBD>ķ<EFBFBD><C4B7>ʹ<EFBFBD><CDB4><EFBFBD>
pcb->hold = 0;
} }
return pcb; return pcb;
} }

View File

@ -1137,7 +1137,7 @@ tcp_receive(struct tcp_pcb *pcb)
/* If the incoming segment contains data, we must process it /* If the incoming segment contains data, we must process it
further. */ further. */
if (tcplen > 0) { if ((tcplen > 0) && (!pcb->hold)) {
/* This code basically does three things: /* This code basically does three things:
+) If the incoming segment contains data that is the next +) If the incoming segment contains data that is the next

View File

@ -725,8 +725,15 @@ static int mqtt_socket_connect( lua_State* L )
if(mud == NULL) if(mud == NULL)
return 0; return 0;
if(mud->pesp_conn) if(mud->pesp_conn){ //TODO: should I free tcp struct directly or ask user to call close()???
c_free(mud->pesp_conn); mud->pesp_conn->reverse = NULL;
if(mud->pesp_conn->proto.tcp)
c_free(mud->pesp_conn->proto.tcp);
mud->pesp_conn->proto.tcp = NULL;
c_free(mud->pesp_conn);
mud->pesp_conn = NULL;
}
struct espconn *pesp_conn = NULL; struct espconn *pesp_conn = NULL;
pesp_conn = mud->pesp_conn = (struct espconn *)c_zalloc(sizeof(struct espconn)); pesp_conn = mud->pesp_conn = (struct espconn *)c_zalloc(sizeof(struct espconn));
if(!pesp_conn) if(!pesp_conn)

View File

@ -665,6 +665,10 @@ static int net_start( lua_State* L, const char* mt )
return 0; return 0;
} }
if(nud->pesp_conn == NULL){
NODE_DBG("nud->pesp_conn is NULL.\n");
return 0;
}
pesp_conn = nud->pesp_conn; pesp_conn = nud->pesp_conn;
port = luaL_checkinteger( L, stack ); port = luaL_checkinteger( L, stack );
stack++; stack++;
@ -1074,6 +1078,10 @@ static int net_dns( lua_State* L, const char* mt )
return 0; return 0;
} }
if(nud->pesp_conn == NULL){
NODE_DBG("nud->pesp_conn is NULL.\n");
return 0;
}
pesp_conn = nud->pesp_conn; pesp_conn = nud->pesp_conn;
if(!isserver || pesp_conn->type == ESPCONN_UDP){ // self_ref is only needed by socket userdata, or udp server if(!isserver || pesp_conn->type == ESPCONN_UDP){ // self_ref is only needed by socket userdata, or udp server
@ -1185,6 +1193,54 @@ static int net_socket_send( lua_State* L )
return net_send(L, mt); 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) ) // Lua: socket:dns( string, function(ip) )
static int net_socket_dns( lua_State* L ) static int net_socket_dns( lua_State* L )
{ {
@ -1243,6 +1299,8 @@ static const LUA_REG_TYPE net_socket_map[] =
{ LSTRKEY( "close" ), LFUNCVAL ( net_socket_close ) }, { LSTRKEY( "close" ), LFUNCVAL ( net_socket_close ) },
{ LSTRKEY( "on" ), LFUNCVAL ( net_socket_on ) }, { LSTRKEY( "on" ), LFUNCVAL ( net_socket_on ) },
{ LSTRKEY( "send" ), LFUNCVAL ( net_socket_send ) }, { 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( "dns" ), LFUNCVAL ( net_socket_dns ) },
// { LSTRKEY( "delete" ), LFUNCVAL ( net_socket_delete ) }, // { LSTRKEY( "delete" ), LFUNCVAL ( net_socket_delete ) },
{ LSTRKEY( "__gc" ), LFUNCVAL ( net_socket_delete ) }, { LSTRKEY( "__gc" ), LFUNCVAL ( net_socket_delete ) },