123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- #include "cumsgbuffer.h"
- cumsgbuffer::cumsgbuffer()
- {
- }
- void cumsgbuffer::addmsg(int id, qint64 ntime, std::string strVIN, std::string strqueryMD5, std::string strctrlMD5,
- std::vector<char> *pxdata,bool bImportant,int nkeeptime,qint64 nculatency)
- {
- qDebug("ntime is %ld",ntime);
- mMutex.lock();
- iv::cumsg * pmsg = 0;
- int nsize = mvectormsg.size();
- int i;
- for(i=0;i<nsize;i++)
- {
- if(strncmp(mvectormsg[i].strVIN.data(),strVIN.data(),255) == 0)
- {
- pmsg = &mvectormsg[i];
- break;
- }
- }
- if(pmsg == 0)
- {
- iv::cumsg cmsg;
- cmsg.id = id;
- cmsg.ntime = ntime;
- cmsg.strVIN = strVIN;
- cmsg.strqueryMD5 = strqueryMD5;
- cmsg.strctrlMD5 = strctrlMD5;
- cmsg.mnlatency = nculatency;
- if(pxdata->size() > 0)
- {
- cmsg.xdata.resize(pxdata->size());
- memcpy(cmsg.xdata.data(),pxdata->data(),pxdata->size());
- }
- cmsg.mlastuptime = QDateTime::currentMSecsSinceEpoch();
- cmsg.mbImportant = bImportant;
- cmsg.mkeeptime = nkeeptime;
- cmsg.mbhavequery = false;
- mvectormsg.push_back(cmsg);
- mMutex.unlock();
- return;
- }
- if((pmsg->mbImportant != true)||(pmsg->mbhavequery)||((QDateTime::currentMSecsSinceEpoch() - pmsg->mlastuptime)>=pmsg->mkeeptime))
- {
- pmsg->id = id;
- pmsg->ntime = ntime;
- pmsg->mbImportant = bImportant;
- pmsg->mbhavequery = false;
- pmsg->mlastuptime = QDateTime::currentMSecsSinceEpoch();
- pmsg->mkeeptime = nkeeptime;
- pmsg->strqueryMD5 = strqueryMD5;
- pmsg->strctrlMD5 = strctrlMD5;
- pmsg->xdata.clear();
- pmsg->mnlatency = nculatency;
- if(pxdata->size()>0)
- {
- pmsg->xdata.resize(pxdata->size());
- memcpy(pmsg->xdata.data(),pxdata->data(),pxdata->size());
- }
- }
- mMutex.unlock();
- }
- int cumsgbuffer::getmsg(std::string strVIN,std::string strqueryMD5, qint64 nlasttime, int &id, qint64 &ntime,
- std::vector<char> *pxdata,qint64 * pnculatency)
- {
- mMutex.lock();
- iv::cumsg * pmsg = 0;
- int nsize = mvectormsg.size();
- int i;
- std::vector<iv::cumsg> * pvectormsg = &mvectormsg;
- for(i=0;i<nsize;i++)
- {
- if(strncmp(mvectormsg[i].strVIN.data(),strVIN.data(),255) == 0)
- {
- pmsg = &mvectormsg[i];
- break;
- }
- }
- if(pmsg == 0)
- {
- std::cout<<" no this vin data vin is "<<strVIN<<std::endl;;
- mMutex.unlock();
- return -1;
- }
- if(strqueryMD5 != pmsg->strqueryMD5)
- {
- std::cout<<" query error."<<std::endl;
- mMutex.unlock();
- return -2;
- }
- pmsg->mbhavequery = true;
- if(nlasttime == pmsg->ntime)
- {
- mMutex.unlock();
- return 0;
- }
- qint64 now = QDateTime::currentMSecsSinceEpoch();
- if(abs(now - pmsg->mlastuptime)>60000) //Data is old not use.
- {
- mMutex.unlock();
- return 0;
- }
- id = pmsg->id;
- ntime = pmsg->ntime;
- pxdata->clear();
- *pnculatency = pmsg->mnlatency;
- int ndatasize = pmsg->xdata.size();
- pxdata->resize(ndatasize);
- memcpy(pxdata->data(),pmsg->xdata.data(),ndatasize);
- mMutex.unlock();
- return 1;
- }
|