数据包的解析和测试
          从周四忙到昨天,整个链路总算测试通过了,周末出差在真实广播环境中测试OK,晚上回宿舍的车上,看着窗外的东方明珠一点感觉都没有,现在知道什么叫审美疲劳了,司机不知趣的跟我讲奥运会,我一下打断说等会把发票给我!当时最想说的一句话就是 FUCK北京奥运!
什么都是迎接奥运,连我们的项目都是!

产生模拟数据测试解包
void DataReceiver::GenerateTrafficEventMessages()


{
    #if defined(TS_USING_RG_MID_COMPONENT)    //----------------------------------------------
        
    #else

        for(int i=0;i<trafficEventMsg->GetCount();i++)
{
            delete trafficEventMsg->GetAt(i);
        }
        CString location =Prefrence::getParameter(Prefrence::LOCATION);
        short locationId =PhiloTrafficLocation::getLocationId(location);
        int count =0;

        if(locationId==PhiloTrafficLocation::BEIJING)
{
            count =5;

        }else if(locationId==PhiloTrafficLocation::SHANGHAI)
{
            count =5;

        }else if(locationId==PhiloTrafficLocation::SHENZHEN)
{
            count =5;

        }else if(locationId==PhiloTrafficLocation::ZHEJIANG)
{
            count =5;
        }
        int bytesOfPerMessage =271;
        unsigned char *pBuffer =new unsigned char[count*bytesOfPerMessage];
        unsigned char *pos =pBuffer;

        for(int i=0;i<count;i++)
{
            //serviceId
            *pos =PhiloMessageType::TRAFFIC_EVENT;
            pos ++;
            //locationId
            *pos =(unsigned char)(0xff & locationId);
            pos ++;
            *pos =(unsigned char)((0xff00 & locationId) >> 8);
            pos ++;
            //eventId
            *pos =(unsigned char)(0xff & i);
            pos ++;
            *pos =(unsigned char)((0xff00 & i) >> 8);
            pos ++;
            //deployer
            TrafficEventMessage::setDeployer(pos,locationId);
            pos+=40;
            
            //type
            *pos =(unsigned char)(1 + (rand() % 5));
            pos ++;

            //maplevel
            *pos =0;
            pos++;

            //x
            int x =100 + (rand() % 250);
            *pos =(unsigned char)(0xff & x);
            pos ++;
            *pos =(unsigned char)((0xff00 & x) >> 8);
            pos ++;
            *pos =(unsigned char)((0xff0000 & x) >> 16);
            pos ++;
            *pos =(unsigned char)((0xff000000 & x) >> 24);
            pos ++;

            //y
            int y =200 + (rand() % 100);
            *pos =(unsigned char)(0xff & y);
            pos ++;
            *pos =(unsigned char)((0xff00 & y) >> 8);
            pos ++;
            *pos =(unsigned char)((0xff0000 & y) >> 16);
            pos ++;
            *pos =(unsigned char)((0xff000000 & y) >> 24);
            pos ++;

            //startDate
            CTime t =CTime::GetCurrentTime();
            //years
            int year =t.GetYear();
            *pos =(unsigned char)(0xff & year);
            pos ++;
            *pos =(unsigned char)((0xff00 & year) >> 8);
            pos ++;

            //month
            *pos =(unsigned char)t.GetMonth();
            pos++;

            //day
            *pos =(unsigned char)t.GetDay();
            pos++;

            //hours
            *pos =(unsigned char)t.GetHour();
            pos++;

            //minutes
            *pos =(unsigned char)t.GetMinute();
            pos++;

            //seconds
            *pos =(unsigned char)t.GetSecond();
            pos++;

            *pos =0;
            pos ++;

            //endDate
            //years
            *pos =(unsigned char)(0xff & year);
            pos ++;
            *pos =(unsigned char)((0xff00 & year) >> 8);
            pos ++;

            //month
            *pos =(unsigned char)t.GetMonth();
            pos++;

            //day
            *pos =(unsigned char)t.GetDay();
            pos++;

            //hours
            *pos =(unsigned char)t.GetHour() + 1;
            pos++;

            //minutes
            *pos =(unsigned char)t.GetMinute();
            pos++;

            //seconds
            *pos =(unsigned char)t.GetSecond();
            pos++;

            *pos =0;
            pos ++;

            //description
            TrafficEventMessage::setDescription(pos,(unsigned char)(1 + (rand() % 5)));
            pos+=200;

            //isActive
            *pos =1;
            pos++;
        }
        DataReceived(pBuffer,count*bytesOfPerMessage);
        delete pBuffer;
        pBuffer =NULL;
        pos =NULL;
    #endif
}

数据包解析
void DataReceiver::DataReceived(unsigned char *pBuffer, unsigned short usLen)


{


    if(!isDrawTrafficInformation && !isDrawWeather && !isDrawTrafficEvent)
{
        return;
    }
    unsigned char *pos =pBuffer;
    //去掉头部 3 个字节



    #if defined(TS_USING_RG_MID_COMPONENT)    //----------------------------------------------
        pos +=3;
        usLen -=3;
    #else
    #endif
    unsigned int msgtype=0;
    unsigned int msglenth=0;
    CTime time;
    unsigned short bagkey=0;
    unsigned int headlenth=0;
    unsigned int messageCount =0;
    unsigned int messageIndex =0;
    //printf("DataReceived









%d,\n",usLen);
    CString currentLocation =Prefrence::getParameter(Prefrence::LOCATION);
    int currentLocationId =PhiloTrafficLocation::getLocationId(currentLocation);
    
    headlenth =*pos;                            //包头长度
    pos ++;
    bagkey =Util::ByteToShort(pos);                //包序号
    pos +=2;
    time =CTime(Util::ByteToLong(pos));        //时间戳
    pos +=8;
    msgtype=Util::ByteToInt(pos);                //消息类型
    pos +=4;
    msglenth =*pos;                                //每个消息长度
    pos++;
    messageCount =(usLen - headlenth)/msglenth;    //消息个数
   // printf("DataReceived88









%d,\n",messageCount);
    while(messageIndex<messageCount)

    
{

        if(msgtype==PhiloMessageType::PHILO_RTI)
{
                unsigned  int locationId =Util::ByteToInt(pos);   //locationId
                pos +=4;
            

                /**//*    CString str3;
                    str3.Format(L"%d",locationId);
                    AfxMessageBox(str3);*/


                if(locationId == currentLocationId)
{

                    if(previousLocation!=locationId)
{
                        trafficInformationMsg->RemoveAll();
                        previousLocation =locationId;
                    }
                    TrafficInformationMessage msg;
                    unsigned  int IssuanceId =Util::ByteToInt(pos);   //发布段ID
                    pos +=4;
                    msg.setId(IssuanceId);
                    msg.setStatus( *pos);          //交通状况
                    pos ++;
            
                    trafficInformationMsg->SetAtGrow(msg.getId(),msg);
                    
                    pos+=msglenth - PhiloMessageType::PHILO_RTI_MESSAGE_LENGTH;
                    messageIndex ++;

                }else
{
                    pos +=msglenth-4;
                    messageIndex ++;
                }

        }else if(msgtype==PhiloMessageType::PHILO_TRAFFIC_EVENT)
{
                      unsigned int locationId =Util::ByteToInt(pos);
                      pos +=4;

                      if(locationId == currentLocationId)
{
                          if(previousLocation!=locationId)

                              
{
                                for(int i=0;i<trafficEventMsg->GetCount();i++)

                                    
{
                                        delete trafficEventMsg->GetAt(i);
                                    }
                                 trafficEventMsg->RemoveAll();
                                 previousLocation =locationId;
                              }
                     unsigned short eventId =Util::ByteToShort(pos);                 //事件ID
                     pos +=2;

                     TrafficEventMessage *msg =new TrafficEventMessage();         //发布者(40)
                    
                     unsigned char *p =msg->Deployer;
                     for(int i=0;i<10;i++)

                        
{
                            *p =*pos;
                            p++;
                            pos++;
                        }
                     
                    msg->EventType =*pos;                                        //事件类型(1)
                    pos ++;

                    msg->MapLevel =*pos;                                         //映射地图级别
                    pos++;
    
                    unsigned int x =Util::ByteToInt(pos);                       //X(4)
                    pos +=4;
                    msg->X =x;
                    
                    unsigned int y =Util::ByteToInt(pos);                        //Y(4)
                    pos +=4;
                    msg->Y =y;
                    
                    long t =Util::ByteToLong(pos);                               //事件发生时间
                    pos +=8;

                    msg->StartDate =CTime(t);
                    
                    t =Util::ByteToLong(pos);                                    //事件结束时间
                    pos +=8;

                    msg->EndDate =CTime(t);
                    p =msg->Description;                                       //事件描述
                    for(int i=0;i<20;i++)

                    
{
                        *p =*pos;
                        p++;
                        pos++;
                    }
                    msg->isActive =*pos;                                       //活动标志
                    pos++;

                    trafficEventMsg->SetAtGrow(eventId,msg);
                    pos+=msglenth - PhiloMessageType::PHILO_TRAFFIC_EVENT_MESSAGE_LENGTH;
                    messageIndex ++;

                }else
{
                          pos +=msglenth-4;
                          messageIndex ++;       
                      }
            
            

        }else if(msgtype==PhiloMessageType::PHILO_WEATHER)
{
                          unsigned  int locationId =Util::ByteToInt(pos);     //locationId
                          pos +=4;

                          if(locationId == currentLocationId)
{
                
                          weatherMsg->Status  =*pos;                        //天气状况
                          pos ++;
                          weatherMsg->Temperature =*pos;                    //温度
                          pos ++;
                          weatherMsg->Vane =*pos;                           //风向
                          pos ++;
                          weatherMsg->Windpower =*pos;                      //风力
                          pos ++;
                          weatherMsg->Visibility =*pos;                    //能见度
                          pos ++;
                          weatherMsg->Time =*pos;                           //时间
                          pos +=8;
                        //  weatherMsg->toString();
                          pos+=msglenth - PhiloMessageType::PHILO_WEATHER_MESSAGE_LENGTH;
                          messageIndex ++;
                          }

                          else
{
                                 pos +=msglenth-4;
                                 messageIndex ++;    
                              }

        }else
{
            break;
        }
    }

}
         
          
什么都是迎接奥运,连我们的项目都是!
void DataReceiver::GenerateTrafficEventMessages()

{
    #if defined(TS_USING_RG_MID_COMPONENT)    //----------------------------------------------
        
    #else
        for(int i=0;i<trafficEventMsg->GetCount();i++)
{
            delete trafficEventMsg->GetAt(i);
        }
        CString location =Prefrence::getParameter(Prefrence::LOCATION);
        short locationId =PhiloTrafficLocation::getLocationId(location);
        int count =0;
        if(locationId==PhiloTrafficLocation::BEIJING)
{
            count =5;
        }else if(locationId==PhiloTrafficLocation::SHANGHAI)
{
            count =5;
        }else if(locationId==PhiloTrafficLocation::SHENZHEN)
{
            count =5;
        }else if(locationId==PhiloTrafficLocation::ZHEJIANG)
{
            count =5;
        }
        int bytesOfPerMessage =271;
        unsigned char *pBuffer =new unsigned char[count*bytesOfPerMessage];
        unsigned char *pos =pBuffer;
        for(int i=0;i<count;i++)
{
            //serviceId
            *pos =PhiloMessageType::TRAFFIC_EVENT;
            pos ++;
            //locationId
            *pos =(unsigned char)(0xff & locationId);
            pos ++;
            *pos =(unsigned char)((0xff00 & locationId) >> 8);
            pos ++;
            //eventId
            *pos =(unsigned char)(0xff & i);
            pos ++;
            *pos =(unsigned char)((0xff00 & i) >> 8);
            pos ++;
            //deployer
            TrafficEventMessage::setDeployer(pos,locationId);
            pos+=40;
            
            //type
            *pos =(unsigned char)(1 + (rand() % 5));
            pos ++;
            //maplevel
            *pos =0;
            pos++;
            //x
            int x =100 + (rand() % 250);
            *pos =(unsigned char)(0xff & x);
            pos ++;
            *pos =(unsigned char)((0xff00 & x) >> 8);
            pos ++;
            *pos =(unsigned char)((0xff0000 & x) >> 16);
            pos ++;
            *pos =(unsigned char)((0xff000000 & x) >> 24);
            pos ++;
            //y
            int y =200 + (rand() % 100);
            *pos =(unsigned char)(0xff & y);
            pos ++;
            *pos =(unsigned char)((0xff00 & y) >> 8);
            pos ++;
            *pos =(unsigned char)((0xff0000 & y) >> 16);
            pos ++;
            *pos =(unsigned char)((0xff000000 & y) >> 24);
            pos ++;
            //startDate
            CTime t =CTime::GetCurrentTime();
            //years
            int year =t.GetYear();
            *pos =(unsigned char)(0xff & year);
            pos ++;
            *pos =(unsigned char)((0xff00 & year) >> 8);
            pos ++;
            //month
            *pos =(unsigned char)t.GetMonth();
            pos++;
            //day
            *pos =(unsigned char)t.GetDay();
            pos++;
            //hours
            *pos =(unsigned char)t.GetHour();
            pos++;
            //minutes
            *pos =(unsigned char)t.GetMinute();
            pos++;
            //seconds
            *pos =(unsigned char)t.GetSecond();
            pos++;
            *pos =0;
            pos ++;
            //endDate
            //years
            *pos =(unsigned char)(0xff & year);
            pos ++;
            *pos =(unsigned char)((0xff00 & year) >> 8);
            pos ++;
            //month
            *pos =(unsigned char)t.GetMonth();
            pos++;
            //day
            *pos =(unsigned char)t.GetDay();
            pos++;
            //hours
            *pos =(unsigned char)t.GetHour() + 1;
            pos++;
            //minutes
            *pos =(unsigned char)t.GetMinute();
            pos++;
            //seconds
            *pos =(unsigned char)t.GetSecond();
            pos++;
            *pos =0;
            pos ++;
            //description
            TrafficEventMessage::setDescription(pos,(unsigned char)(1 + (rand() % 5)));
            pos+=200;
            //isActive
            *pos =1;
            pos++;
        }
        DataReceived(pBuffer,count*bytesOfPerMessage);
        delete pBuffer;
        pBuffer =NULL;
        pos =NULL;
    #endif
}
void DataReceiver::DataReceived(unsigned char *pBuffer, unsigned short usLen)

{

    if(!isDrawTrafficInformation && !isDrawWeather && !isDrawTrafficEvent)
{
        return;
    }
    unsigned char *pos =pBuffer;
    //去掉头部 3 个字节


    #if defined(TS_USING_RG_MID_COMPONENT)    //----------------------------------------------
        pos +=3;
        usLen -=3;
    #else
    #endif
    unsigned int msgtype=0;
    unsigned int msglenth=0;
    CTime time;
    unsigned short bagkey=0;
    unsigned int headlenth=0;
    unsigned int messageCount =0;
    unsigned int messageIndex =0;
    //printf("DataReceived









%d,\n",usLen);
    CString currentLocation =Prefrence::getParameter(Prefrence::LOCATION);
    int currentLocationId =PhiloTrafficLocation::getLocationId(currentLocation);
    
    headlenth =*pos;                            //包头长度
    pos ++;
    bagkey =Util::ByteToShort(pos);                //包序号
    pos +=2;
    time =CTime(Util::ByteToLong(pos));        //时间戳
    pos +=8;
    msgtype=Util::ByteToInt(pos);                //消息类型
    pos +=4;
    msglenth =*pos;                                //每个消息长度
    pos++;
    messageCount =(usLen - headlenth)/msglenth;    //消息个数
   // printf("DataReceived88









%d,\n",messageCount);
    while(messageIndex<messageCount)
    
{
        if(msgtype==PhiloMessageType::PHILO_RTI)
{
                unsigned  int locationId =Util::ByteToInt(pos);   //locationId
                pos +=4;
            
                /**//*    CString str3;
                    str3.Format(L"%d",locationId);
                    AfxMessageBox(str3);*/

                if(locationId == currentLocationId)
{
                    if(previousLocation!=locationId)
{
                        trafficInformationMsg->RemoveAll();
                        previousLocation =locationId;
                    }
                    TrafficInformationMessage msg;
                    unsigned  int IssuanceId =Util::ByteToInt(pos);   //发布段ID
                    pos +=4;
                    msg.setId(IssuanceId);
                    msg.setStatus( *pos);          //交通状况
                    pos ++;
            
                    trafficInformationMsg->SetAtGrow(msg.getId(),msg);
                    
                    pos+=msglenth - PhiloMessageType::PHILO_RTI_MESSAGE_LENGTH;
                    messageIndex ++;
                }else
{
                    pos +=msglenth-4;
                    messageIndex ++;
                }
        }else if(msgtype==PhiloMessageType::PHILO_TRAFFIC_EVENT)
{
                      unsigned int locationId =Util::ByteToInt(pos);
                      pos +=4;
                      if(locationId == currentLocationId)
{
                          if(previousLocation!=locationId)
                              
{
                                for(int i=0;i<trafficEventMsg->GetCount();i++)
                                    
{
                                        delete trafficEventMsg->GetAt(i);
                                    }
                                 trafficEventMsg->RemoveAll();
                                 previousLocation =locationId;
                              }
                     unsigned short eventId =Util::ByteToShort(pos);                 //事件ID
                     pos +=2;
                     TrafficEventMessage *msg =new TrafficEventMessage();         //发布者(40)
                    
                     unsigned char *p =msg->Deployer;
                     for(int i=0;i<10;i++)
                        
{
                            *p =*pos;
                            p++;
                            pos++;
                        }
                     
                    msg->EventType =*pos;                                        //事件类型(1)
                    pos ++;
                    msg->MapLevel =*pos;                                         //映射地图级别
                    pos++;
    
                    unsigned int x =Util::ByteToInt(pos);                       //X(4)
                    pos +=4;
                    msg->X =x;
                    
                    unsigned int y =Util::ByteToInt(pos);                        //Y(4)
                    pos +=4;
                    msg->Y =y;
                    
                    long t =Util::ByteToLong(pos);                               //事件发生时间
                    pos +=8;
                    msg->StartDate =CTime(t);
                    
                    t =Util::ByteToLong(pos);                                    //事件结束时间
                    pos +=8;
                    msg->EndDate =CTime(t);
                    p =msg->Description;                                       //事件描述
                    for(int i=0;i<20;i++)
                    
{
                        *p =*pos;
                        p++;
                        pos++;
                    }
                    msg->isActive =*pos;                                       //活动标志
                    pos++;
                    trafficEventMsg->SetAtGrow(eventId,msg);
                    pos+=msglenth - PhiloMessageType::PHILO_TRAFFIC_EVENT_MESSAGE_LENGTH;
                    messageIndex ++;
                }else
{
                          pos +=msglenth-4;
                          messageIndex ++;       
                      }
            
            
        }else if(msgtype==PhiloMessageType::PHILO_WEATHER)
{
                          unsigned  int locationId =Util::ByteToInt(pos);     //locationId
                          pos +=4;
                          if(locationId == currentLocationId)
{
                
                          weatherMsg->Status  =*pos;                        //天气状况
                          pos ++;
                          weatherMsg->Temperature =*pos;                    //温度
                          pos ++;
                          weatherMsg->Vane =*pos;                           //风向
                          pos ++;
                          weatherMsg->Windpower =*pos;                      //风力
                          pos ++;
                          weatherMsg->Visibility =*pos;                    //能见度
                          pos ++;
                          weatherMsg->Time =*pos;                           //时间
                          pos +=8;
                        //  weatherMsg->toString();
                          pos+=msglenth - PhiloMessageType::PHILO_WEATHER_MESSAGE_LENGTH;
                          messageIndex ++;
                          }
                          else
{
                                 pos +=msglenth-4;
                                 messageIndex ++;    
                              }
        }else
{
            break;
        }
    }
}
                    
                
                
            
        
浙公网安备 33010602011771号