新闻  |   论坛  |   博客  |   在线研讨会
STM32的LWIP协议栈源码有问题TCP.C中tcp_fasttmr函数会造成死循环
sunke9 | 2016-12-12 08:12:49    阅读:8840   发布文章

tcp_fasttmr函数原来是这样写的:

/**
 * Is called every TCP_FAST_INTERVAL (250 ms) and process data previously
 * "refused" by upper layer (application) and sends delayed ACKs.
 *
 * Automatically called from tcp_tmr().
 */
void
tcp_fasttmr(void)
{
  struct tcp_pcb *pcb;


  ++tcp_timer_ctr;


tcp_fasttmr_start:
  pcb = tcp_active_pcbs;


  while(pcb != NULL) {
    if (pcb->last_timer != tcp_timer_ctr) {
      struct tcp_pcb *next;
      pcb->last_timer = tcp_timer_ctr;
      /* send delayed ACKs */
      if (pcb->flags & TF_ACK_DELAY) {
        LWIP_DEBUGF(TCP_DEBUG, ("tcp_fasttmr: delayed ACK\n"));
        tcp_ack_now(pcb);
        tcp_output(pcb);
        pcb->flags &= ~(TF_ACK_DELAY | TF_ACK_NOW);
      }


      next = pcb->next;


      /* If there is data which was previously "refused" by upper layer */
      if (pcb->refused_data != NULL) {
        tcp_active_pcbs_changed = 0;
        tcp_process_refused_data(pcb);
        if (tcp_active_pcbs_changed) {
          /* application callback has changed the pcb list: restart the loop */
          goto tcp_fasttmr_start;
        }
      }
      pcb = next;
    }
  }
}
程序的某种情况下会死在while循环中。改成下面的写法就好了:

  1. void
  2. tcp_fasttmr(void)
  3. {
  4.   struct tcp_pcb *pcb;
  5.   for(pcb = tcp_active_pcbs; pcb != NULL; pcb = pcb->next) {
  6.     /* If there is data which was previously "refused" by upper layer */
  7.     if (pcb->refused_data != NULL) {
  8.       /* Notify again application with data previously received. */
  9.       err_t err;
  10.       LWIP_DEBUGF(TCP_INPUT_DEBUG, ("tcp_fasttmr: notify kept packet\n"));
  11.       TCP_EVENT_RECV(pcb, pcb->refused_data, ERR_OK, err);
  12.       if (err == ERR_OK) {
  13.         pcb->refused_data = NULL;
  14.       }
  15.     }
  16.     /* send delayed ACKs */ 
  17.     if (pcb->flags & TF_ACK_DELAY) {
  18.       LWIP_DEBUGF(TCP_DEBUG, ("tcp_fasttmr: delayed ACK\n"));
  19.       tcp_ack_now(pcb);
  20.       pcb->flags &= ~(TF_ACK_DELAY | TF_ACK_NOW);
  21.     }
  22.   }
  23. }

*博客内容为网友个人发布,仅代表博主个人观点,如有侵权请联系工作人员删除。

参与讨论
登录后参与讨论
小可哥工作学习的空间
推荐文章
最近访客