/****************************************************************************** * FunctionName : espconn_server_poll * Description : The poll function is called every 3nd second. * If there has been no data sent (which resets the retries) in 3 seconds, close. * If the last portion of a file has not been sent in 3 seconds, close. * * This could be increased, but we don't want to waste resources for bad connections. * Parameters : arg -- Additional argument to pass to the callback function * pcb -- The connection pcb for which data has been acknowledged * Returns : ERR_OK: try to send some data by calling tcp_output * ERR_ABRT: if you have called tcp_abort from within the function! *******************************************************************************/ static err_t ICACHE_FLASH_ATTR espconn_server_poll(void *arg, struct tcp_pcb *pcb) { espconn_msg *pspoll_cb = arg; /*exception calling abandon the connection for send a RST frame*/ if (arg == NULL) { tcp_abandon(pcb, 0); tcp_poll(pcb, NULL, 0); return ERR_OK; } espconn_printf("espconn_server_poll %d %d\n", pspoll_cb->pcommon.recv_check, pcb->state); pspoll_cb->pcommon.pcb = pcb; if (pcb->state == ESTABLISHED) { pspoll_cb->pcommon.recv_check++; if (pspoll_cb->pcommon.timeout != 0){/*no data sent in one active connection's set timeout, close.*/ if (pspoll_cb->pcommon.recv_check >= pspoll_cb->pcommon.timeout) { pspoll_cb->pcommon.recv_check = 0; espconn_server_close(pspoll_cb, pcb,0); } } else { espconn_msg *ptime_msg = pserver_list; while (ptime_msg != NULL) { if (ptime_msg->pespconn == pspoll_cb->preverse){ if (ptime_msg->pcommon.timeout != 0){/*no data sent in server's set timeout, close.*/ if (pspoll_cb->pcommon.recv_check >= ptime_msg->pcommon.timeout){ pspoll_cb->pcommon.recv_check = 0; espconn_server_close(pspoll_cb, pcb,0); } } else {/*don't close for ever*/ pspoll_cb->pcommon.recv_check = 0; } break; } ptime_msg = ptime_msg->pnext; } } } else { espconn_server_close(pspoll_cb, pcb,0); } return ERR_OK; }
/****************************************************************************** * FunctionName : espconn_close * Description : The connection has been successfully closed. * Parameters : arg -- Additional argument to pass to the callback function * Returns : none *******************************************************************************/ void ICACHE_FLASH_ATTR espconn_tcp_disconnect(espconn_msg *pdiscon) { if (pdiscon != NULL){ if (pdiscon->preverse != NULL) espconn_server_close(pdiscon, pdiscon->pcommon.pcb); else espconn_client_close(pdiscon, pdiscon->pcommon.pcb); } else{ espconn_printf("espconn_server_disconnect err.\n"); } }
/****************************************************************************** * FunctionName : espconn_close * Description : The connection has been successfully closed. * Parameters : arg -- Additional argument to pass to the callback function * Returns : none *******************************************************************************/ void ICACHE_FLASH_ATTR espconn_tcp_disconnect(espconn_msg *pdiscon,uint8 type) { if (pdiscon != NULL){ /*disconnect with the host by send the FIN frame*/ if (pdiscon->preverse != NULL) espconn_server_close(pdiscon, pdiscon->pcommon.pcb,type); else espconn_client_close(pdiscon, pdiscon->pcommon.pcb,type); } else{ espconn_printf("espconn_tcp_disconnect err.\n"); } }
/****************************************************************************** * FunctionName : espconn_server_recv * Description : Data has been received on this pcb. * Parameters : arg -- Additional argument to pass to the callback function * pcb -- The connection pcb which received data * p -- The received data (or NULL when the connection has been closed!) * err -- An error code if there has been an error receiving * Returns : ERR_ABRT: if you have called tcp_abort from within the function! *******************************************************************************/ static err_t ICACHE_FLASH_ATTR espconn_server_recv(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err) { espconn_msg *precv_cb = arg; tcp_arg(pcb, arg); espconn_printf("server has application data received: %d\n", system_get_free_heap_size()); if (p != NULL) { /*To update and advertise a larger window*/ if(precv_cb->recv_hold_flag == 0) tcp_recved(pcb, p->tot_len); else precv_cb->recv_holded_buf_Len += p->tot_len; } if (err == ERR_OK && p != NULL) { u8_t *data_ptr = NULL; u32_t data_cntr = 0; /*clear the count for connection timeout*/ precv_cb->pcommon.recv_check = 0; /*Copy the contents of a packet buffer to an application buffer. *to prevent memory leaks, ensure that each allocated is deleted*/ data_ptr = (u8_t *)os_zalloc(p ->tot_len + 1); data_cntr = pbuf_copy_partial(p, data_ptr, p ->tot_len, 0); pbuf_free(p); if (data_cntr != 0) { /*switch the state of espconn for application process*/ precv_cb->pespconn ->state = ESPCONN_READ; precv_cb->pcommon.pcb = pcb; if (precv_cb->pespconn->recv_callback != NULL) { precv_cb->pespconn->recv_callback(precv_cb->pespconn, data_ptr, data_cntr); } /*switch the state of espconn for next packet copy*/ if (pcb->state == ESTABLISHED) precv_cb->pespconn ->state = ESPCONN_CONNECT; } /*to prevent memory leaks, ensure that each allocated is deleted*/ os_free(data_ptr); data_ptr = NULL; espconn_printf("server's application data has been processed: %d\n", system_get_free_heap_size()); } else { if (p != NULL) { pbuf_free(p); } espconn_server_close(precv_cb, pcb); } return ERR_OK; }
/****************************************************************************** * FunctionName : espconn_server_poll * Description : The poll function is called every 3nd second. * If there has been no data sent (which resets the retries) in 3 seconds, close. * If the last portion of a file has not been sent in 3 seconds, close. * * This could be increased, but we don't want to waste resources for bad connections. * Parameters : arg -- Additional argument to pass to the callback function * pcb -- The connection pcb for which data has been acknowledged * Returns : ERR_OK: try to send some data by calling tcp_output * ERR_ABRT: if you have called tcp_abort from within the function! *******************************************************************************/ static err_t ICACHE_FLASH_ATTR espconn_server_poll(void *arg, struct tcp_pcb *pcb) { espconn_msg *pspoll_cb = arg; if (arg == NULL) { tcp_abandon(pcb, 0); tcp_poll(pcb, NULL, 0); return ERR_OK; } espconn_printf("espconn_server_poll %d %d\n", pspoll_cb->pcommon.recv_check, pcb->state); pspoll_cb->pcommon.pcb = pcb; if (pcb->state == ESTABLISHED) { pspoll_cb->pcommon.recv_check++; if (pspoll_cb->pcommon.timeout != 0){ if (pspoll_cb->pcommon.recv_check == pspoll_cb->pcommon.timeout) { pspoll_cb->pcommon.recv_check = 0; espconn_server_close(pspoll_cb, pcb); } } else if (link_timer != 0){ if (pspoll_cb->pcommon.recv_check == link_timer) { pspoll_cb->pcommon.recv_check = 0; espconn_server_close(pspoll_cb, pcb); } } else { if (pspoll_cb->pcommon.recv_check == 0x0a) { pspoll_cb->pcommon.recv_check = 0; espconn_server_close(pspoll_cb, pcb); } } } else { espconn_server_close(pspoll_cb, pcb); } return ERR_OK; }
/****************************************************************************** * FunctionName : espconn_server_recv * Description : Data has been received on this pcb. * Parameters : arg -- Additional argument to pass to the callback function * pcb -- The connection pcb which received data * p -- The received data (or NULL when the connection has been closed!) * err -- An error code if there has been an error receiving * Returns : ERR_ABRT: if you have called tcp_abort from within the function! *******************************************************************************/ static err_t ICACHE_FLASH_ATTR espconn_server_recv(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err) { espconn_msg *precv_cb = arg; tcp_arg(pcb, arg); espconn_printf("server has application data received: %d\n", system_get_free_heap_size()); if (p != NULL) { tcp_recved(pcb, p->tot_len); } if (err == ERR_OK && p != NULL) { u8_t *data_ptr = NULL; u32_t data_cntr = 0; precv_cb->pcommon.recv_check = 0; data_ptr = (u8_t *)os_zalloc(p ->tot_len + 1); data_cntr = pbuf_copy_partial(p, data_ptr, p ->tot_len, 0); pbuf_free(p); if (data_cntr != 0) { precv_cb->pespconn ->state = ESPCONN_READ; precv_cb->pcommon.pcb = pcb; if (precv_cb->pespconn->recv_callback != NULL) { precv_cb->pespconn->recv_callback(precv_cb->pespconn, data_ptr, data_cntr); } precv_cb->pespconn ->state = ESPCONN_CONNECT; } os_free(data_ptr); data_ptr = NULL; espconn_printf("server's application data has been processed: %d\n", system_get_free_heap_size()); } else { if (p != NULL) { pbuf_free(p); } espconn_server_close(precv_cb, pcb); } return ERR_OK; }