⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tcp.lst

📁 基于51单片机和RTL8019以太网控制器的嵌入式以太网控制程序。
💻 LST
📖 第 1 页 / 共 5 页
字号:
 728   5                                              conxn[index].state = STATE_TIME_WAIT;
 729   5      //                                      if (debug) PrintStr("TCP: Entered TIME_WAIT state\r");
 730   5      
 731   5                                              conxn[index].state = STATE_CLOSED;
C51 COMPILER V8.02   TCP                                                                   09/21/2006 20:16:32 PAGE 13  

 732   5                                                      InerClose(index);  // Free up connection
 733   5                                              just_closed = TRUE;
 734   5                                      }
 735   4                                              else
 736   4                                              {
 737   5                                                      // He has not ACK'd my FIN.  This happens when there is a simultaneous
 738   5                                                      // close - I got his FIN but he has not yet ACK'd my FIN
 739   5                                                      conxn[index].state = STATE_CLOSING;
 740   5      //                                              if (debug) PrintStr("TCP: Entered CLOSING state\r");
 741   5                                              }
 742   4                                      }
 743   3                                      else if (packACK == conxn[index].my_sequence)
 744   3                                      {
 745   4                                              // He has ACK'd my FIN but has not sent a FIN yet himself
 746   4                                              conxn[index].state = STATE_FIN_WAIT_2;
 747   4                                      //      if (debug) PrintStr("TCP: Entered FIN_WAIT_2 state\r");
 748   4                                      }
 749   3                                      break;
 750   3                              case STATE_FIN_WAIT_2:
 751   3                                      // He may still be sending me data - should process it
 752   3                                      conxn[index].his_sequence += data_len;
 753   3                                      conxn[index].his_ack = packACK;
 754   3      
 755   3                                      if (pRxdnet->tcpframe.control & FLG_FIN)
 756   3                                      {
 757   4                                              conxn[index].his_sequence++; // For his FIN flag
 758   4                                              tcp_send(&TCPSend,FLG_ACK, 20, index);
 759   4                                              conxn[index].state = STATE_TIME_WAIT;
 760   4      //                                      if (debug) PrintStr("TCP: Entered TIME_WAIT state\r");
 761   4                                      //      conxn[index].state = STATE_CLOSED;
 762   4                                                      InerClose(index); // Free up struct area
 763   4                                              just_closed = TRUE;
 764   4                                      }
 765   3                                      break;
 766   3                              case STATE_TIME_WAIT:
 767   3                                      // With this code, should not get here
 768   3                                              //因为在进入FIN_WAIT_2后这边的连接直接进入CLOSED
 769   3      //                                      if (debug) PrintStr("TCP: Oops! In TIME_WAIT state\r");
 770   3                                      break;
 771   3                              case STATE_CLOSING:
 772   3                                      // Simultaneous close has happened. I have received his FIN
 773   3                                      // but he has not yet ACK'd my FIN.  Waiting for ACK.
 774   3                                      // Will not receive data in this state
 775   3                                      conxn[index].his_ack = packACK;
 776   3      
 777   3                                      if (packACK == conxn[index].my_sequence)
 778   3                                      {
 779   4                                                      conxn[index].state = STATE_TIME_WAIT;
 780   4       //                                     if (debug) PrintStr("TCP: Entered TIME_WAIT state\r");
 781   4      
 782   4                                              // Do not send any response to his ACK
 783   4                              //              conxn[index].state = STATE_CLOSED;
 784   4                                                      InerClose(index);  // Free up struct area
 785   4                                              just_closed = TRUE;
 786   4                                      }
 787   3                                      break;
 788   3                              case STATE_SYN_SENT:
 789   3                                      //这些代码用于当连接为客户端是的状况
 790   3                                      //// If incoming segment contains SYN and  ACK, then handle
 791   3                                      if ((pRxdnet->tcpframe.control & FLG_SYN) && (pRxdnet->tcpframe.control & FLG_ACK))
 792   3                                      {
 793   4                                              conxn[index].my_sequence++;             //for my SYN
C51 COMPILER V8.02   TCP                                                                   09/21/2006 20:16:32 PAGE 14  

 794   4                                              conxn[index].ip.words[0]= pRxdnet->ipframe.sourceip[0];
 795   4                                              conxn[index].ip.words[1]= pRxdnet->ipframe.sourceip[1];
 796   4                                              conxn[index].port = pRxdnet->tcpframe.sourceport;
 797   4                                              conxn[index].localport=LOCAL_PORT;
 798   4                                              conxn[index].state = STATE_ESTABLISHED;
 799   4                                              conxn[index].socket_type = CLIENT;
 800   4                                              conxn[index].his_sequence = 1 + packSeq;
 801   4                                              conxn[index].his_ack = packACK;
 802   4                                              tcp_send(&TCPSend,FLG_ACK, 20, index);          // Send ACK
 803   4                                              //调用OnConnect()函数
 804   4                                              (* conxn[index].connect)();
 805   4                                      }
 806   3                                      break;
 807   3                              default:
 808   3      //                                      if (debug) PrintStr("TCP: Error, no handler\r");
 809   3                                      break;
 810   3                      }
 811   2                      // This is for debug, to see when conxn closes
 812   2                      if (just_closed)
 813   2                      {
 814   3                              just_closed = FALSE;
 815   3                              if (debug) PrintStr("TCP: Closed connection ");
 816   3                      }
 817   2      
 818   2              }
 819   1      }
 820          
 821          
 822          
 823          //------------------------------------------------------------------------
 824          // This runs every 0.5 seconds.  If the other end has not ACK'd
 825          // everyting we have sent, it re-sends it.  To save RAM space, we
 826          // regenerate a segment rather than keeping a bunch of segments
 827          // hanging around eating up RAM.  A connection should not be in an
 828          // opening or closing state when this timer expires, so we simply
 829          // send a reset.
 830          //
 831          //      If a connection is in the ESTABLISHED state when the timer expires
 832          // then we have just sent a web page so re-send the page
 833          //2006-02-13:修改重传的次数及时间
 834          //------------------------------------------------------------------------
 835          void tcp_retransmit()
 836          {
 837   1         unsigned char nr;
 838   1         // Scan through all active connections
 839   1         for (nr = 0; nr < NO_CONNECTION; nr++)
 840   1         {
 841   2            if ((conxn[nr].ip.dwords != 0) && (conxn[nr].timer))
 842   2            {
 843   3               // Decrement the timer and see if it hit 0
 844   3               conxn[nr].timer--;
 845   3               if (conxn[nr].timer == 0)
 846   3               {
 847   4                               
 848   4                  // Socket just timed out. If we are not in ESTABLISHED state
 849   4                  // something is amiss so send reset and close connection
 850   4                  if (conxn[nr].state != STATE_ESTABLISHED)
 851   4                  {
 852   5                                      // Send reset and close connection
 853   5                                      if (debug) PrintStr("TCP: Timeout, sending reset\r");
 854   5                                      tcp_send(&TCPSend,FLG_RST, 20, nr);
 855   5                                      InerClose(nr);
C51 COMPILER V8.02   TCP                                                                   09/21/2006 20:16:32 PAGE 15  

 856   5                                      return;
 857   5                  }
 858   4                  else
 859   4                  {
 860   5                     // Socket is in ESTABLISHED state. First make sure his
 861   5                     // ack number is not bogus.
 862   5                     if (conxn[nr].his_ack > conxn[nr].my_sequence)
 863   5                     {
 864   6                                              // Send reset and close connection
 865   6                                              if (debug) PrintStr("TCP: Timeout, sending reset2\r");
 866   6                                              tcp_send(&TCPSend,FLG_RST, 20, nr);
 867   6                                              InerClose(nr);
 868   6                                              return;
 869   6                     }
 870   5                     // We always increment our sequence number immediately
 871   5                                 // after sending, so the ack number from the other end
 872   5                                 // should be equal to our sequence number.  If it is less,
 873   5                                 // it means he lost some of our data.
 874   5              //       if (conxn[nr].his_ack < conxn[nr].my_sequence)
 875   5                      //         {
 876   5                                 else
 877   5                                 {
 878   6                                         if(conxn[nr].pUnAcked->bUsed)
 879   6                                         {
 880   7                                                      conxn[nr].retries++;
 881   7                                                      if (conxn[nr].retries <= RESENDCOUNT)
 882   7                                                      {
 883   8                                              // The only thing we send is a web page, and it looks
 884   8                                              // like other end did not get it, so resend
 885   8                                              // but do not increase my sequence number
 886   8                                              if (debug) PrintStr("TCP: Timeout, resending data\r");
 887   8                                                              tcp_senddata(&TCPSend,conxn[nr].pUnAcked->pBuf,conxn[nr].pUnAcked->iLen+20,nr,1);
 888   8                                                              //重置保活定时器
 889   8                                                              conxn[nr].aty_timer = INACTIVITY_TIME;
 890   8                                                      }
 891   7                                                      else
 892   7                                                      {
 893   8                                                              conxn[nr].retries = 0;
 894   8                                                              if (debug) PrintStr("TCP: Giving up, sending reset\r");
 895   8                                                              // Send reset and close connection
 896   8                                              tcp_send(&TCPSend,FLG_RST, 20, nr);
 897   8                                                              InerClose(nr);
 898   8                                      }
 899   7                                         }
 900   6                     }
 901   5                              }
 902   4               }
 903   3            }
 904   2              }
 905   1      }
 906          
 907          
 908          
 909          
 910          //------------------------------------------------------------------------
 911          // 该函数每隔2个小时执行,向对方发送一个ARP请求,如果搜到arp echo,则接着发送
 912          //一个ack,该ack序号为正常ack-1

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -