?? protocol.cpp
字號:
Delete=TRUE;
}
}
if(!Delete&&!m_CriticalData.IsEmpty()){
if(CurData==m_CriticalData.GetHead()){
delete m_CriticalData.RemoveHead();
Delete=TRUE;
}
}
if(!Delete&&(!m_CriticalReplyData.IsEmpty())){
if(CurData==m_CriticalReplyData.GetHead()){
delete m_CriticalReplyData.RemoveHead();
Delete=TRUE;
}
}
if(!Delete&&(!m_NonCriticalData.IsEmpty())){
if(CurData==m_NonCriticalData.GetHead()){
delete m_NonCriticalData.RemoveHead();
Delete=TRUE;
}
}
if(!Delete){
ASSERT(Delete);
}
}
else{
CCenterData* CurData = (CCenterData*) Data;
BOOL Delete=FALSE;
if(!m_CenCriticalData.IsEmpty()){
if(!strcmp(CurData->sn,m_CenCriticalData.GetHead()->sn)){
delete m_CenCriticalData.RemoveHead();
Delete=TRUE;
}
}
if(!Delete&&(!m_CenCriticalReplyData.IsEmpty())){
if(!strcmp(CurData->sn,m_CenCriticalReplyData.GetHead()->sn)){
delete m_CenCriticalReplyData.RemoveHead();
Delete=TRUE;
}
}
if(!Delete&&(!m_CenNonCriticalData.IsEmpty())){
if(!strcmp(CurData->sn,m_CenNonCriticalData.GetHead()->sn)){
delete m_CenNonCriticalData.RemoveHead();
Delete=TRUE;
}
}
if(!Delete){
ASSERT(Delete);
}
}
}
void CCSEntity::OnReceive(const void *InPacket,WORD InPacketLen)
{
if(m_Flag==1)
{
CCServiceData *tmpServiceData=new CCServiceData;
tmpServiceData->ReceivePDU(InPacket,InPacketLen);
if(m_CurData==NULL){
delete tmpServiceData;
return;
}
if(tmpServiceData->IsDuplicated()){
//not a duplicated reply of current PDU data,discard it
if(!(CCServiceData *)m_CurData->IsReply(tmpServiceData)){
delete tmpServiceData;
return;
}
}
//copy input data
m_CurData->GetInputData(tmpServiceData);
delete tmpServiceData;
//kill the timeout timer
KillTimer(m_SendTimerId);
#ifdef _LOGDATA
char *LogStr=new char[300];
m_CurData->GetReceiveLogString(LogStr);
((CClientApp *)AfxGetApp())->AddLog(LogStr);
delete []LogStr;
#endif
if(m_CurData->IsReply()){
void *User;
if(m_UserList.Lookup(m_CurData->GetUserId(),User)){
((CCSUser *)User)->OnReply(m_CurData->GetSequenceNumber(),m_CurData->GetReplyPDU(),
m_CurData->GetReplyPDULen(),m_CurData->GetTimeSpan());
}
}
char *tmpbuf = (char*)m_CurData->m_OutBuffer;
if((tmpbuf[1]=='A')&&(tmpbuf[2]=='I'))
{
m_State=Entity_File;
if(PackedFile(tmpbuf))
{
m_CurData=NULL;
ActiveSend();
return;
}
}
if((tmpbuf[1]=='A')&&(tmpbuf[2]=='P'))
{
if(tmpbuf[23]=='\x31')
{
RemoveData(m_CurData);
m_CurData=NULL;
if(m_CriticalData.IsEmpty())
m_DiskBuffer->SaveData(&m_CriticalData);
if(m_ImageFileData.IsEmpty())
m_ImageBuffer->SaveData(&m_ImageFileData);
m_State=Entity_Idle;
ActiveSend();
return;
}
else{
RemoveData(m_CurData);
m_CurData=NULL;
m_State=Entity_File;
ActiveSend();
return;
}
}
if(m_CurData->IsReply()&&m_CurData->IsCritical())
{
if((m_State==Entity_File)||(m_State==Entity_File_Timeouting))
{
RemoveData(m_CurData);
m_CurData=NULL;
m_State=Entity_File;
ActiveSend();
return;
}
}
// Remove the data from data buffer,and make the current send buffer pointer NULL
RemoveData(m_CurData);
m_CurData=NULL;
//if there isn't data in the disk buffer, delete the disk buffer file
if(m_CriticalData.IsEmpty())
m_DiskBuffer->SaveData(&m_CriticalData);
m_State=Entity_Idle;
ActiveSend();
}//flag ==1 end
else{
CCenterData *tmpCenterData=new CCenterData;
tmpCenterData->ReceivePDU(InPacket,InPacketLen);
if(m_CenData==NULL){
delete tmpCenterData;
return;
}
if(atoi(tmpCenterData->packettype)==1)
{
if(tmpCenterData->IsDuplicated()){
if(!(CCenterData *)m_CenData->IsReply(tmpCenterData)){
delete tmpCenterData;
return;
}
}
tmpCenterData->packettype[0]='0';
tmpCenterData->packettype[1]='0';
m_CenData->GetInputData(tmpCenterData);
delete tmpCenterData;
KillTimer(m_SendTimerId);
RemoveData(m_CenData);
m_CenData=NULL;
if(m_CenCriticalData.IsEmpty())
m_CentBuffer->SaveData(&m_CenCriticalData);
m_State=Entity_Idle;
ActiveSend();
}
}
}
int CCSEntity::PackedFile(char* buffer)
{
ASSERT(m_ImageFileData.IsEmpty());
char *p = (char*)m_CriticalData.GetHead()->m_OutBuffer;
if(strcmp(buffer,p))
return 0;
char buf[LINELEN],packet[LINELEN*2+50],tmpbuf[3];
char flag;
FILE *fp;
int cc=0;
char filename[50];
char protocol[30];
BYTE b_buf[LINELEN];
memset(filename,'\0',sizeof(filename));
memset(protocol,'\0',sizeof(protocol));
memcpy(filename,buffer+19,strlen(buffer)-19-1);
memcpy(protocol,buffer,17);
protocol[2]='P';
fp=fopen(filename,"r+b");
if(fp==NULL)
return 0;
int count = 0;
do{
memset(buf,'\0',sizeof(buf));
cc=read(fp->_file ,buf,LINELEN);
int bb = strlen(buf);
if(cc==LINELEN)
flag='\x30';
else flag='\x31';
count++;
int i =0;
bufinfo.Empty();
while(i<cc)
{
b_buf[i]=(int)buf[i];
sprintf(tmpbuf,"%02x",b_buf[i]);
bufinfo+=tmpbuf;
i++;
}
cc = bufinfo.GetLength();
sprintf(packet,"%s%03d%03d%1c%s}",protocol,count,cc,flag,bufinfo);
CCServiceData *ServiceData=
new CCServiceData(packet,cc+25,1,TRUE,FALSE,1000);
ServiceData->SetSequenceNumber(m_SequenceNum.Get());
m_ImageFileData.AddTail(ServiceData);
}while(cc/2==LINELEN);
fclose(fp);
m_ImageBuffer->SaveData(&m_ImageFileData);
if(!strcmp(buffer,(char*)m_CriticalData.GetHead()->m_OutBuffer))
delete m_CriticalData.RemoveHead();
return 1;
}
void CCSEntity::ActiveSend()
{
ASSERT(m_State==Entity_Idle||m_State==Entity_Timeouting||m_State==Entity_File||m_State==Entity_File_Timeouting);
if(m_Flag == 1)
{
if(m_State==Entity_File)
{
if(m_ImageFileData.IsEmpty())
{
m_State=Entity_Idle;
ActiveSend();
}
if(!m_CriticalReplyData.IsEmpty())
m_CurData=m_CriticalReplyData.GetHead();
else if(!m_ImageFileData.IsEmpty())
m_CurData=m_ImageFileData.GetHead();
if(m_CurData==NULL)
return;
if(SendToSock(m_CurData)==FALSE){
#ifdef _DEBUG
AfxMessageBox("SendToSock failed!");
#endif
}
m_State=Entity_File_Timeouting;
m_SendTimerId=SetTimer(m_SendTimerEvent,m_CurData->m_TimeOutInterval,NULL);
return;
}
if(m_State==Entity_File_Timeouting)
{
if((m_CurData->IsReply())&&(m_CurData->m_RetryCounter==3)){
m_CurData->m_ReplyTime=CTime::GetCurrentTime();
void *User;
if(m_CurData->IsCritical())
if(m_UserList.Lookup(m_CurData->GetUserId(),User)){
((CCSUser *)User)->OnReply(m_CurData->GetSequenceNumber(),NULL,0,m_CurData->GetTimeSpan(),SENDTIMEOUT);
}
RemoveData(m_CurData);
//start send next data
m_CurData=NULL;
m_State=Entity_File;
ActiveSend();
return;
}
//send current data again
if(!m_CriticalReplyData.IsEmpty()&&(m_CurData!=m_CriticalReplyData.GetHead())){
m_CurData->m_RetryCounter=0;
m_CurData=NULL;
m_State=Entity_File;
ActiveSend();
}
else{
m_CurData->m_RetryCounter++;
if(SendToSock(m_CurData)==FALSE){
#ifdef _DEBUG
AfxMessageBox("SendToSock failed!");
#endif
}
// SendToSock(m_CurData);
m_SendTimerId=SetTimer(m_SendTimerEvent,m_CurData->m_TimeOutInterval,NULL);
}
return;
}
////////////////////////////////////////////////////////
if(m_State==Entity_Idle){
ASSERT(m_CurData==NULL);
if(!m_CriticalReplyData.IsEmpty())
m_CurData=m_CriticalReplyData.GetHead();
else if(!m_CriticalData.IsEmpty())
m_CurData=m_CriticalData.GetHead();
else if(!m_NonCriticalData.IsEmpty())
m_CurData=m_NonCriticalData.GetHead();
if(m_CurData==NULL)
return;
if(SendToSock(m_CurData)==FALSE){
#ifdef _DEBUG
AfxMessageBox("SendToSock failed!");
#endif
}
// SendToSock(m_CurData);
#ifdef _LOGDATA
char *LogStr=new char[300];
m_CurData->GetSendLogString(LogStr);
((CClientApp *)AfxGetApp())->AddLog(LogStr);
delete []LogStr;
#endif
//change the entity's state
m_State=Entity_Sending;
m_SendTimerId=SetTimer(m_SendTimerEvent,m_CurData->m_TimeOutInterval,NULL);
return;
}
if(m_State==Entity_Timeouting){
//if time out, remove all data from noncritical data buffer
if(!m_NonCriticalData.IsEmpty()){
if(m_CurData==m_NonCriticalData.GetHead())
m_CurData=NULL;
while(!m_NonCriticalData.IsEmpty())
delete m_NonCriticalData.RemoveHead();
if(m_CurData==NULL){
m_State=Entity_Idle;
ActiveSend();
return;
}
}
if((m_CurData->IsReply())&&(m_CurData->m_RetryCounter==3)){
m_CurData->m_ReplyTime=CTime::GetCurrentTime();
void *User;
if(m_CurData->IsCritical())
if(m_UserList.Lookup(m_CurData->GetUserId(),User)){
((CCSUser *)User)->OnReply(m_CurData->GetSequenceNumber(),NULL,0,m_CurData->GetTimeSpan(),SENDTIMEOUT);
}
RemoveData(m_CurData);
//start send next data
m_CurData=NULL;
m_State=Entity_Idle;
ActiveSend();
return;
}
//send current data again
if(!m_CriticalReplyData.IsEmpty()&&(m_CurData!=m_CriticalReplyData.GetHead())){
m_CurData->m_RetryCounter=0;
m_CurData=NULL;
m_State=Entity_Idle;
ActiveSend();
}
else{
m_CurData->m_RetryCounter++;
if(SendToSock(m_CurData)==FALSE){
#ifdef _DEBUG
AfxMessageBox("SendToSock failed!");
#endif
}
// SendToSock(m_CurData);
m_SendTimerId=SetTimer(m_SendTimerEvent,m_CurData->m_TimeOutInterval,NULL);
}
}
return;
}
else{
//////////////////////////////////////////////////////// center entity
if(m_State==Entity_Idle){
ASSERT(m_CenData==NULL);
if(!m_CenCriticalReplyData.IsEmpty())
m_CenData=m_CenCriticalReplyData.GetHead();
else if(!m_CenCriticalData.IsEmpty())
m_CenData=m_CenCriticalData.GetHead();
else if(!m_CenNonCriticalData.IsEmpty())
m_CenData=m_CenNonCriticalData.GetHead();
if(m_CenData==NULL)
return;
if(SendToSock(m_CenData)==FALSE){
#ifdef _DEBUG
AfxMessageBox("SendToSock failed!");
#endif
}
//change the entity's state
m_State=Entity_Sending;
m_SendTimerId=SetTimer(m_SendTimerEvent,m_CenData->m_TimeOutInterval,NULL);
return;
}
if(m_State==Entity_Timeouting){
//if time out, remove all data from noncritical data buffer
if(!m_CenNonCriticalData.IsEmpty()){
if(m_CenData==m_CenNonCriticalData.GetHead())
m_CenData=NULL;
while(!m_CenNonCriticalData.IsEmpty())
delete m_CenNonCriticalData.RemoveHead();
if(m_CenData==NULL){
m_State=Entity_Idle;
ActiveSend();
return;
}
}
if((m_CenData->IsReply())&&(m_CenData->m_RetryCounter==3)){
m_CenData->m_ReplyTime=CTime::GetCurrentTime();
void *User;
if(m_CenData->IsCritical())
if(m_UserList.Lookup(m_CenData->GetUserId(),User)){
((CCSUser *)User)->OnReply(m_CenData->GetSequenceNumber(),NULL,0,m_CenData->GetTimeSpan(),SENDTIMEOUT);
}
RemoveData(m_CenData);
//start send next data
m_CenData=NULL;
m_State=Entity_Idle;
ActiveSend();
return;
}
//send current data again
if(!m_CenCriticalReplyData.IsEmpty()&&(m_CenData!=m_CenCriticalReplyData.GetHead())){
//m_CenData->m_RetryCounter=0;
m_CenData=NULL;
m_State=Entity_Idle;
ActiveSend();
}
else{
//m_CenData->m_RetryCounter++;
if(SendToSock(m_CenData)==FALSE){
#ifdef _DEBUG
AfxMessageBox("SendToSock failed!");
#endif
}
// SendToSock(m_CurData);
m_SendTimerId=SetTimer(m_SendTimerEvent,m_CenData->m_TimeOutInterval,NULL);
}
}
return;
}//end of flag == 2
}
void CCSEntity::OnTimer(UINT nIDEvent)
{
if(m_Flag == 1)
{
//for debug only
if(nIDEvent==m_SendTimerEvent){
KillTimer(m_SendTimerId);
if((m_State!=Entity_Sending)&&(m_State!=Entity_Timeouting)&&(m_State!=Entity_File)&&(m_State!=Entity_File_Timeouting))
return;
if(m_State==Entity_Sending){
m_State=Entity_Timeouting;
//if this is the first time timeout, save data to diskbuf
m_DiskBuffer->SaveData(&m_CriticalData);
ActiveSend();
}
else if(m_State==Entity_Timeouting)
{
AdjustInterval(); //adjust the timeout interval
ActiveSend();
}
else if(m_State==Entity_File_Timeouting)
{
AdjustInterval(); //adjust the timeout interval
ActiveSend();
}
else if(m_State == Entity_File){
AdjustInterval(); //adjust the timeout interval
m_State=Entity_File_Timeouting;
ActiveSend();
}
}
}
else{
if(nIDEvent==m_SendTimerEvent){
KillTimer(m_SendTimerId);
if((m_State!=Entity_Sending)&&(m_State!=Entity_Timeouting))
return;
if(m_State==Entity_Sending){
m_State=Entity_Timeouting;
//if this is the first time timeout, save data to diskbuf
m_CentBuffer->SaveData(&m_CenCriticalData);
//InsertToDatabase(&m_DbId,&m_CenCriticalData);
ActiveSend();
}
else if(m_State==Entity_Timeouting)
{
AdjustInterval(); //adjust the timeout interval
ActiveSend();
}
}
}//flag==2
CFrameWnd::OnTimer(nIDEvent);
}
void CCSEntity::AdjustInterval()
{
}
//lane data
void CCSEntity::SetLaneData(int LaneNo,int TollHouse,int flag,const char *Descrip)
{
m_LaneNo=LaneNo;
m_TollHouse=TollHouse;
if(Descrip!=NULL)
m_TollHouseDescripion=Descrip;
ASSERT(m_DiskBuffer);
if(flag==1)
{
m_DiskBuffer->SetFileHeader(LaneNo,TollHouse,m_TollHouseDescripion);
m_ImageBuffer->SetFileHeader(LaneNo,TollHouse,m_TollHouseDescripion);
}
else m_CentBuffer->SetFileHeader(LaneNo,TollHouse,m_TollHouseDescripion);
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -