📄 upsentqcheck.cpp
字号:
/********************************************************************** FileName : UpSentQCheck.cpp Description : 上端已发送队列检查模块 Version : 1.0 Date : 2003年7月3日 Author : 刘荣辉 Other : 确定是否重发***********************************************************************/#include "UpNode.h"//int CUpNode::UpSentQCheck()//int CUpNode::SentQReset(); //重置已发送队列的等待时间标识int CUpNode::UpSentQCheck(DB_MySQL *MyDB, int Relay_Interval){ int i; int UnitNum; char sSysEvent[SYS_EVENT_LEN]; //SendQUnit Units[WINDOW_SIZE]; //存放数据包指针的指针(为了地址的传递)的数组 time_t nowtime; struct tm *tmptime; int DelNum; unsigned int PackSeq; int QSize; QSize = SentQ.GetSize(); //获得当前的队列大小 SendQUnit *Units=(SendQUnit *)malloc(QSize * sizeof(SendQUnit)); UnitNum = SentQ.GetAllNode(Units, QSize); //获取最多QSize个队列单元 #ifdef DEBUG //printf("\n CheckSentQ: UnitNum=[%d] \n",UnitNum); #endif for(i=0;i<UnitNum;i++) //检查各单元,确定是否重发 { if(ToExit) //系统收到退出命令后,3秒钟内没收到应答包则将请求包进行超时处理 { if(Protocol==0) Units[i].SendTime = Units[i].SendTime - CMPP_SEND_TIMEOUT +3; else if(Protocol==1) Units[i].SendTime = Units[i].SendTime - SEND_TIMEOUT +3; } time(&nowtime); if(nowtime >= Units[i].SendTime) //等待时间已到的数据包 { //从已发送队列中删除 PackSeq = ntohl(*(unsigned int *)(Units[i].Pack + 8)); #ifdef DEBUG //printf("\n CheckSentQ: Sequence=[%d],SendTime=[%d],nowtime=[%d],iResent=[%d]\n",PackSeq,(int)Units[i].SendTime,(int)nowtime,Units[i].iResent); #endif DelNum = SentQ.Delete(PackSeq); if(DelNum!=1) { sprintf(sSysEvent,"Error: %d Units[%d] is deleted from SentQ in one time!",DelNum,PackSeq); WrSystemLog->WriteLog(sSysEvent,SYSTEMLOG); } else { #ifdef DEBUG //printf("\n CheckSentQ: A Unit[%d] is deleted from SentQ.\n",PackSeq); #endif } if(Units[i].iResent || ToExit) //可重发的请求包,或者发送失败的应答包 { //转存到待发送队列 if(SendQ->Put(Units[i])==false) { sprintf(sSysEvent,"Warning: SendQ of UpNode[%s] is full! Cann't transmit a pack[%d] from SentQ.",NodeCode,PackSeq); WrSystemLog->WriteLog(sSysEvent,SYSTEMLOG); continue; } else { #ifdef DEBUG if(Protocol==0) printf("\n CheckSentQ: A CMPP_Unit[%d] is transmited from SentQ to SendQ.\n",PackSeq); else if(Protocol==1) printf("\n CheckSentQ: A SMGP_Unit[%d] is transmited from SentQ to SendQ.\n",PackSeq); #endif } } else //最大发送次数已满,仍未等到应答的请求包 { if(ntohl(*(int *)Units[i].Pack) == CMPP_SUBMIT) //对于CMPP_Submit包,要缓存到数据库 { //-----------构造CMPP_Submit下行转发记录------------- Cmpp_MT_Log MtLog; tmptime = localtime( &Units[i].SrcRecvTime); sprintf( MtLog.SrcRecvTime, "%d%02d%02d%02d%02d%02d", tmptime->tm_year + 1900, tmptime->tm_mon + 1, tmptime->tm_mday, tmptime->tm_hour, tmptime->tm_min, tmptime->tm_sec); MtLog.SrcNode = Units[i].SrcNode; memcpy(&MtLog.MsgId_Echo, Units[i].ID_Relate, sizeof(MtLog.MsgId_Echo)); MtLog.DestNode = NodeID; time_t Schedule = (int)nowtime + Relay_Interval; tmptime = localtime( &Schedule); sprintf( MtLog.SentTime, "%d%02d%02d%02d%02d%02d", tmptime->tm_year + 1900, tmptime->tm_mon + 1, tmptime->tm_mday, tmptime->tm_hour, tmptime->tm_min, tmptime->tm_sec); if(State!=0) (char)MtLog.Result = CONN_ABNORMAL; else (char)MtLog.Result = WAIT_TIMEOUT; MtLog.Submit = (CMPP_Submit *)Units[i].Pack; MtLog.RelayTimes = Units[i].RelayTimes + 1; //--------写入下行转发记录缓存表-------- MyDB->WrMT_Queue(&MtLog, CMPP_PROTOCOL,TAB_MT_Queue); sprintf(sSysEvent,"Warning: A TimeOut CMPP_Submit to UpNode[%d] is saved to MT_Queue temporarily!",NodeID); WrSystemLog->WriteLog(sSysEvent,SYSTEMLOG); } else //非CMPP_Submit包 { #ifdef DEBUG char timebuf[15]; CFunc::TimeToStr(nowtime, timebuf); if(Protocol==0) printf("\n Warning[%s]: An TimeOut CMPP_Pack[%d] is abandoned in CheckSentQ!!!\n",timebuf,PackSeq); else if(Protocol==1) printf("\n Warning[%s]: An TimeOut SMGP_Pack[%d] is abandoned in CheckSentQ!!!\n",timebuf,PackSeq); #endif } free(Units[i].Pack); //删除包,释放内存 } }//if()等待时间已到的数据包 }//for() free(Units); return 0;}//==========================================================================//=========================重置已发送队列的等待时间标识=====================//==========================================================================int CUpNode::SentQReset(){ int QSize; int UnitNum; time_t nowtime; SendQUnit *Units; QSize = SentQ.GetSize(); //获得当前的队列大小 Units=(SendQUnit *)malloc(QSize * sizeof(SendQUnit)); UnitNum = SentQ.GetAllNode(Units, QSize); //获取最多QSize个队列单元 time(&nowtime); for(int i=0;i<UnitNum;i++) //重置各单元的时间标识 { Units[i].iResent = 0; //不再重发 if(Protocol==0) Units[i].SendTime = nowtime + CMPP_SEND_TIMEOUT; //下一次发送时间(重发时间) else if(Protocol==1) Units[i].SendTime = nowtime + SEND_TIMEOUT; //下一次发送时间(重发时间) #ifdef DEBUG printf("\n SentQReset: A pack in SentQ of UpNode[%d] is reseted, which is from Node[%d].\n", NodeID,Units[i].SrcNode); #endif } free(Units); return UnitNum;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -