#include "grpcclient.h" grpcclient * ggrpcclient; void ListenData(const char * strdata,const unsigned int nSize,const unsigned int index,const QDateTime * dt,const char * strmemname) { ggrpcclient->UpdateData(strdata,nSize,strmemname); } grpcclient::grpcclient(std::string stryamlpath) { ggrpcclient = this; dec_yaml(stryamlpath.data()); int i; for(i=0;i5)mvectorlatency.erase(mvectorlatency.begin()); qint64 nlatencytotal =0; int nsize = mvectorlatency.size(); int i; for(i=0;i 0) { nlatencytotal = nlatencytotal/nsize; } mnlatency = nlatencytotal; return nlatencytotal; } void grpcclient::threadsend(std::shared_ptr<::grpc::ClientReaderWriter > writer,std::shared_ptr pbrun,std::shared_ptr nlastreftime) { std::cout<<"threadsend start "< nkeeptime) { nkeeptime = mvectormsgunit[i].mnkeeptime; } iv::cloud::cloudunit xcloudunit; xcloudunit.set_msgname(mvectormsgunit[i].mstrmsgname); xcloudunit.set_data(mvectormsgunit[i].mpstrmsgdata.get(),mvectormsgunit[i].mndatasize); iv::cloud::cloudunit * pcu = xmsg.add_xclouddata(); pcu->CopyFrom(xcloudunit); } } gMutexMsg.unlock(); int nbytesize = xmsg.ByteSize(); char * strbuf = new char[nbytesize]; std::shared_ptr pstrbuf; pstrbuf.reset(strbuf); if(xmsg.SerializeToArray(strbuf,nbytesize)) { iv::UploadRequestStream request; qint64 time1 = QDateTime::currentMSecsSinceEpoch(); request.set_id(nid); request.set_ntime(time1); request.set_strquerymd5(gstrqueryMD5); request.set_strctrlmd5(gstrctrlMD5); request.set_strvin(gstrVIN); request.set_xdata(strbuf,nbytesize); request.set_kepptime(nkeeptime); request.set_bimportant(bImportant); request.set_nlatency(mnlatency); ffraterate = 1000.0f/((float)mninterval); request.set_fframerate(ffraterate); request.set_nsendtime(time1); nid++; nlastsend = xTime.elapsed(); QTime xt; xt.start(); ::grpc::WriteOptions wo; // wo.set_write_through(); // wo.clear_buffer_hint(); // writer->Write(request,(void * )2); // bool bsend = true; bool bsend = writer->Write(request,wo); *nlastreftime = QDateTime::currentMSecsSinceEpoch(); // if(xt.elapsed()>10) // { // nok = 0; // if(ninterval < 1000)ninterval = ninterval * 11/10; // mninterval = ninterval; // ffraterate = 1.0f/((float)mninterval); // qDebug("send ela is %d ninterval is %d",xt.elapsed(),ninterval); // } // else // { // nok++; // if((ninterval > nrawinterval)&&(nok>10)) // { // nok = 0; // ninterval = ninterval*10/11; // mninterval = ninterval; // ffraterate = 1.0f/((float)mninterval); // std::cout<<"ninterval is "< > writer, std::shared_ptr pbrun,std::shared_ptr nlastreftime) { std::cout<<"threadrecv start"<Read(&reply)) { *nlastreftime = QDateTime::currentMSecsSinceEpoch(); qint64 nlaten = QDateTime::currentMSecsSinceEpoch() - reply.nreqsendtime(); if(reply.nreqsendtime() == 0)nlaten = 0; else { nlaten = nlaten - reply.npausetime(); } calclatency(nlaten); if(reply.framerate() >0.001) { mninterval = 1000.0/reply.framerate(); } qDebug("latency is %ld",nlaten); // nfail = 0; // std::cout << "接收到回复:" << reply.remsg()<<"--\n" << std::endl; if(reply.nres() == 1) { iv::cloud::cloudmsg xmsg; if(xmsg.ParseFromArray(reply.xdata().data(),reply.xdata().size())) { sharectrlmsg(&xmsg); } } // std::cout<<"read data from server."< pnrpctime,std::shared_ptr pbRun) { std::cout<<"threadrpc start."< channel = grpc::CreateCustomChannel( target_str, grpc::InsecureChannelCredentials(),cargs); std::unique_ptr stub_ = iv::UploadStream::NewStub(channel); int nfail = 0; while(!QThread::isInterruptionRequested()) { ClientContext context ; // std::shared_ptr pcontext(new ClientContext); std::shared_ptr<::grpc::ClientReaderWriter > writerRead(stub_->upload(&context)); // std::shared_ptr pbRun(new bool); *pbRun = true; std::shared_ptr pntime = pnrpctime ; *pntime = QDateTime::currentMSecsSinceEpoch(); std::thread * pthread = new std::thread(&grpcclient::threadsend,this,writerRead,pbRun,pntime); (void )pthread; std::thread * precvthread = new std::thread(&grpcclient::threadrecv,this,writerRead,pbRun,pntime); (void)precvthread; pthread->join(); precvthread->join(); // std::cout<<"threadRPC end"< 100)std::this_thread::sleep_for(std::chrono::milliseconds(3000)); // else std::this_thread::sleep_for(std::chrono::milliseconds(100)); std::cout<<"reconnnect to server. nfail is "< pbRun(new bool); *pbRun = true; std::shared_ptr pntime(new qint64); *pntime = QDateTime::currentMSecsSinceEpoch(); std::thread * pthread = new std::thread(&grpcclient::threadRPC,this,pntime,pbRun); return; // while(!QThread::isInterruptionRequested()) // { // std::shared_ptr pbRun(new bool); // *pbRun = true; // std::shared_ptr pntime(new qint64); // *pntime = QDateTime::currentMSecsSinceEpoch(); // std::thread * pthread = new std::thread(&grpcclient::threadRPC,this,pntime,pbRun); // (void )pthread; // while(((QDateTime::currentMSecsSinceEpoch() - *pntime)<10000)&&(*pbRun)) // { // std::this_thread::sleep_for(std::chrono::milliseconds(1000)); // } // std::cout<<" reconect."< vecmodulename; if(config["server"]) { gstrserverip = config["server"].as(); } if(config["port"]) { gstrserverport = config["port"].as(); } if(config["uploadinterval"]) { gstruploadinterval = config["uploadinterval"].as(); } if(config["VIN"]) { gstrVIN = config["VIN"].as(); } if(config["queryMD5"]) { gstrqueryMD5 = config["queryMD5"].as(); } else { return; } if(config["ctrlMD5"]) { gstrctrlMD5 = config["ctrlMD5"].as(); } std::string strmsgname; if(config["uploadmessage"]) { for(YAML::const_iterator it= config["uploadmessage"].begin(); it != config["uploadmessage"].end();++it) { std::string strtitle = it->first.as(); std::cout<(); strncpy(xmu.mstrmsgname,strmsgname.data(),255); xmu.mnBufferSize = config["uploadmessage"][strtitle]["buffersize"].as(); xmu.mnBufferCount = config["uploadmessage"][strtitle]["buffercount"].as(); if(config["uploadmessage"][strtitle]["bimportant"]) { std::string strimportant = config["uploadmessage"][strtitle]["bimportant"].as(); if(strimportant == "true") { xmu.mbImportant = true; } } if(config["uploadmessage"][strtitle]["keeptime"]) { std::string strkeep = config["uploadmessage"][strtitle]["keeptime"].as(); xmu.mnkeeptime = atoi(strkeep.data()); } mvectormsgunit.push_back(xmu); } } } else { } if(!config["ctrlMD5"]) { return; } if(config["ctrlmessage"]) { std::string strnodename = "ctrlmessage"; for(YAML::const_iterator it= config[strnodename].begin(); it != config[strnodename].end();++it) { std::string strtitle = it->first.as(); std::cout<(); strncpy(xmu.mstrmsgname,strmsgname.data(),255); xmu.mnBufferSize = config[strnodename][strtitle]["buffersize"].as(); xmu.mnBufferCount = config[strnodename][strtitle]["buffercount"].as(); mvectorctrlmsgunit.push_back(xmu); } } } else { } return; } void grpcclient::sharectrlmsg(iv::cloud::cloudmsg * pxmsg) { int i; int nsize = pxmsg->xclouddata_size(); for(i=0;ixclouddata(i).msgname().data(), mvectorctrlmsgunit[j].mstrmsgname,255) == 0) { // qDebug("size is %d ",pxmsg->xclouddata(i).data().size()); iv::modulecomm::ModuleSendMsg(mvectorctrlmsgunit[j].mpa,pxmsg->xclouddata(i).data().data(),pxmsg->xclouddata(i).data().size()); break; } } } } void grpcclient::UpdateData(const char *strdata, const unsigned int nSize, const char *strmemname) { int nsize = mvectormsgunit.size(); int i; for(i=0;i