grpcpc.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. #include "grpcpc.h"
  2. #include <memory>
  3. static grpcpc * ggrpcpc;
  4. grpcpc::grpcpc(std::string stryamlpath)
  5. {
  6. mstrpicmsgname[0] = "picfront";
  7. mstrpicmsgname[1] = "picrear";
  8. mstrpicmsgname[2] = "picleft";
  9. mstrpicmsgname[3] = "picright";
  10. ggrpcpc = this;
  11. unsigned int i;
  12. for(i=0;i<NUM_CAM;i++)
  13. {
  14. mnPicUpLatency[i] = 1000;
  15. mnFrameRate[i] = 0;
  16. mnPicDownLatency[i] = 1000;
  17. }
  18. for(i=0;i<NUM_CAM;i++)
  19. {
  20. unsigned int j;
  21. for(j=0;j<NUM_THREAD_PERCAM;j++)
  22. {
  23. mpThread[i*NUM_THREAD_PERCAM + j] = new std::thread(&grpcpc::threadpicdownload,this,i);
  24. }
  25. }
  26. }
  27. void grpcpc::run()
  28. {
  29. int nsize = mvectormsgunit.size();
  30. int nctrlsize = mvectorctrlmsgunit.size();
  31. int i;
  32. qint64 nlasttime = 0;
  33. int ninterval = atoi(gstruploadinterval.data());
  34. if(ninterval<=0)ninterval = 100;
  35. QTime xTime;
  36. xTime.start();
  37. int nlastsend = xTime.elapsed();
  38. std::string target_str = gstrserverip+":";
  39. target_str = target_str + gstrserverport ;//std::to_string()
  40. auto cargs = grpc::ChannelArguments();
  41. cargs.SetMaxReceiveMessageSize(1024 * 1024 * 1024); // 1 GB
  42. cargs.SetMaxSendMessageSize(1024 * 1024 * 1024);
  43. std::shared_ptr<Channel> channel = grpc::CreateCustomChannel(
  44. target_str, grpc::InsecureChannelCredentials(),cargs);
  45. std::unique_ptr<iv::UploadThread::Stub> stub_ = iv::UploadThread::NewStub(channel);
  46. iv::queryReqThread request;
  47. iv::queryReplyThread xreply;
  48. int nid = 0;
  49. int nctrlid = 0;
  50. // Container for the data we expect from the server.
  51. // iv::queryReply reply;
  52. gpr_timespec timespec;
  53. timespec.tv_sec = 30;//设置阻塞时间为2秒
  54. timespec.tv_nsec = 0;
  55. timespec.clock_type = GPR_TIMESPAN;
  56. qint64 nLastCtrlTime = 0;
  57. while(!QThread::isInterruptionRequested())
  58. {
  59. std::this_thread::sleep_for(std::chrono::milliseconds(1));
  60. if(abs(xTime.elapsed()-nlastsend)<ninterval)
  61. {
  62. continue;
  63. }
  64. bool bImportant = false;
  65. int nkeeptime = 0;
  66. iv::cloud::cloudmsg xmsg;
  67. xmsg.set_xtime(QDateTime::currentMSecsSinceEpoch());
  68. nlastsend = xTime.elapsed();
  69. {
  70. ClientContext context ;
  71. context.set_deadline(timespec);
  72. // qint64 time1 = QDateTime::currentMSecsSinceEpoch();
  73. request.set_strquerymd5(gstrqueryMD5);
  74. request.set_strvin(gstrVIN);
  75. request.set_nlasttime(nlasttime);
  76. request.set_id(nctrlid);nctrlid++;
  77. request.set_strctrlmd5(gstrctrlMD5);
  78. request.set_strvin(gstrVIN);
  79. request.set_ntime(QDateTime::currentMSecsSinceEpoch());
  80. request.set_bimportant(bImportant);
  81. request.set_kepptime(nkeeptime);
  82. if(nLastCtrlTime != mnmsgsendupdatetime)
  83. {
  84. mMutexmsgsend.lock();
  85. nLastCtrlTime = mnmsgsendupdatetime;
  86. xmsg.CopyFrom(mmsgsend);
  87. mMutexmsgsend.unlock();
  88. if(xmsg.xclouddata_size()>0)
  89. {
  90. int nbytesize = xmsg.ByteSize();
  91. std::vector<char> pvectordata;
  92. pvectordata.resize(nbytesize);
  93. if(xmsg.SerializeToArray(pvectordata.data(),nbytesize))
  94. {
  95. request.set_xdata(pvectordata.data(),pvectordata.size());
  96. }
  97. }
  98. }
  99. QDateTime xTime;
  100. xTime.fromMSecsSinceEpoch(1607905685318); //1607914763641
  101. // qDebug("time:%s",xTime.toString("yyyy-MM-dd:hh:mm:ss:zzz").toLatin1().data());
  102. // qDebug("nlasttime is %ld",nlasttime);//1607905685318
  103. nid++;
  104. // The actual RPC.
  105. Status status = stub_->queryctrl(&context, request, &xreply);
  106. if (status.ok()) {
  107. // std::cout<<nid<<" query successfully, res is "<<xreply.nres()<<std::endl;
  108. if(xreply.nres() == 1)
  109. {
  110. if(nlasttime != xmsg.xtime())
  111. {
  112. iv::cloud::cloudmsg xmsg;
  113. if(xmsg.ParseFromArray(xreply.xdata().data(),xreply.xdata().size()))
  114. {
  115. mMutexmsgrecv.lock();
  116. mmsgrecv.CopyFrom(xmsg);
  117. mnmsgrecvupdatetime = QDateTime::currentMSecsSinceEpoch();
  118. mMutexmsgrecv.unlock();
  119. }
  120. }
  121. nlasttime = xreply.ntime();
  122. }
  123. else
  124. {
  125. std::this_thread::sleep_for(std::chrono::milliseconds(30));
  126. }
  127. } else {
  128. std::cout << status.error_code() << ": " << status.error_message()
  129. << std::endl;
  130. std::cout<<"RPC failed"<<std::endl;
  131. if(status.error_code() == 4)
  132. {
  133. std::cout<<" RPC Exceed Time, Create New stub_"<<std::endl;
  134. channel = grpc::CreateCustomChannel(
  135. target_str, grpc::InsecureChannelCredentials(),cargs);
  136. stub_ = iv::UploadThread::NewStub(channel);
  137. }
  138. std::this_thread::sleep_for(std::chrono::milliseconds(900));
  139. }
  140. }
  141. }
  142. }
  143. std::string grpcpc::GetVIN()
  144. {
  145. return gstrVIN;
  146. }
  147. //int gnPicNum[NUM_CAM];
  148. //QMutex gMutexPic[NUM_CAM];
  149. //qint64 gnTimeSecond[NUM_CAM];
  150. void grpcpc::threadpicdownload(int nCamPos)
  151. {
  152. std::cout<<"thread cam "<<nCamPos<<"run"<<std::endl;
  153. int nsize = mvectormsgunit.size();
  154. int i;
  155. std::string strcclientid = "civetweb";//ServiceRCIni.GetClientID();
  156. int ninterval = atoi(gstruploadinterval.data());
  157. if(ninterval<=0)ninterval = 100;
  158. QTime xTime;
  159. xTime.start();
  160. int nlastsend = xTime.elapsed();
  161. std::string target_str = gstrserverip+":";
  162. target_str = target_str + gstrserverport ;//std::to_string()
  163. auto cargs = grpc::ChannelArguments();
  164. cargs.SetMaxReceiveMessageSize(1024 * 1024 * 1024); // 1 GB
  165. cargs.SetMaxSendMessageSize(1024 * 1024 * 1024);
  166. std::shared_ptr<Channel> channel = grpc::CreateCustomChannel(
  167. target_str, grpc::InsecureChannelCredentials(),cargs);
  168. std::unique_ptr<iv::UploadThread::Stub> stub_ = iv::UploadThread::NewStub(channel);
  169. iv::PicDownReqThread request;
  170. int nid = 0;
  171. // Container for the data we expect from the server.
  172. iv::PicDownReplyThread reply;
  173. gpr_timespec timespec;
  174. timespec.tv_sec = 30;//设置阻塞时间为2秒
  175. timespec.tv_nsec = 0;
  176. timespec.clock_type = GPR_TIMESPAN;
  177. // ClientContext context;
  178. while(true)
  179. {
  180. std::shared_ptr<char> pstr_ptr;
  181. if((nCamPos<0)||(nCamPos >= NUM_CAM))
  182. {
  183. std::cout<<"Cam Pos Error. "<<"Pos: "<<nCamPos<<" TOTAL:"<<NUM_CAM<<std::endl;
  184. std::this_thread::sleep_for(std::chrono::milliseconds(100));
  185. continue;
  186. }
  187. request.set_strclientid(strcclientid);
  188. request.set_ncampos(nCamPos);
  189. request.set_strquerymd5(gstrqueryMD5);
  190. request.set_strvin(gstrVIN);
  191. ClientContext context ;
  192. context.set_deadline(timespec);
  193. qint64 time1 = QDateTime::currentMSecsSinceEpoch();
  194. //If extend 10 seconds not request picture, pause get picture from server.
  195. if((time1 - mnLastGetPicTime)>10000)
  196. {
  197. std::cout<<"not need updata"<<std::endl;
  198. std::this_thread::sleep_for(std::chrono::milliseconds(50));
  199. continue;
  200. }
  201. nlastsend = xTime.elapsed();
  202. // The actual RPC.
  203. Status status = stub_->querypic(&context, request, &reply);
  204. if (status.ok()) {
  205. if(reply.nres() == 1)
  206. {
  207. std::cout<<nCamPos<<":pic time is "<<reply.npictime()<<std::endl;
  208. mnPicUpLatency[nCamPos] = reply.npicuplatency();
  209. mnFrameRate[nCamPos] = reply.npicframerate();
  210. mnPicDownLatency[nCamPos] = QDateTime::currentMSecsSinceEpoch() - time1;
  211. iv::vision::rawpic xrawpic;
  212. if(xrawpic.ParseFromArray(reply.xdata().data(),reply.xdata().size()))
  213. {
  214. mMutexPic[nCamPos].lock();
  215. mRawPic[nCamPos].CopyFrom(xrawpic);
  216. mnPicUpdateTime[nCamPos] = QDateTime::currentMSecsSinceEpoch();
  217. xrawpic.CopyFrom(mRawPic[nCamPos]);
  218. // iv::modulecomm::ModuleSendMsg(mpaPic[nCamPos],reply.xdata().data(),reply.xdata().size());
  219. mMutexPic[nCamPos].unlock();
  220. }
  221. // iv::cloud::cloudmsg xmsg;
  222. // if(xmsg.ParseFromArray(reply.xdata().data(),reply.xdata().size()))
  223. // {
  224. // sharectrlmsg(&xmsg);
  225. // }
  226. }
  227. else
  228. {
  229. std::this_thread::sleep_for(std::chrono::milliseconds(10*NUM_THREAD_PERCAM));
  230. }
  231. } else {
  232. std::cout << status.error_code() << ": " << status.error_message()
  233. << std::endl;
  234. std::cout<<"camera dowm"<<nCamPos<<" RPC failed"<<std::endl;
  235. if(status.error_code() == 4)
  236. {
  237. std::cout<<nCamPos<<" RPC Exceed Time, Create New stub_"<<std::endl;
  238. channel = grpc::CreateCustomChannel(
  239. target_str, grpc::InsecureChannelCredentials(),cargs);
  240. stub_ = iv::UploadThread::NewStub(channel);
  241. }
  242. std::this_thread::sleep_for(std::chrono::milliseconds(900));
  243. }
  244. }
  245. }
  246. qint64 grpcpc::GetPicLatency(int nCamPos)
  247. {
  248. if((nCamPos < 0)||(nCamPos >= NUM_CAM))return -1;
  249. return mnPicUpLatency[nCamPos];
  250. }
  251. int grpcpc::GetFrameRate(int nCamPos)
  252. {
  253. if((nCamPos < 0)||(nCamPos >= NUM_CAM))return -1;
  254. return mnFrameRate[nCamPos];
  255. }
  256. qint64 grpcpc::GetPicDownLatency(int nCamPos)
  257. {
  258. if((nCamPos < 0)||(nCamPos >= NUM_CAM))return -1;
  259. return mnPicDownLatency[nCamPos];
  260. }
  261. void grpcpc::setserverip(std::string strip)
  262. {
  263. gstrserverip = strip;
  264. }
  265. void grpcpc::setserverport(std::string strport)
  266. {
  267. gstrserverport = strport;
  268. }
  269. void grpcpc::setqueryinterval(std::string strinterval)
  270. {
  271. gstruploadinterval = strinterval;
  272. }
  273. void grpcpc::setVIN(std::string strVIN)
  274. {
  275. gstrVIN = strVIN;
  276. }
  277. void grpcpc::setqueryMD5(std::string strmd5)
  278. {
  279. gstrqueryMD5 = strmd5;
  280. }
  281. void grpcpc::setctrlMD5(std::string strmd5)
  282. {
  283. gstrctrlMD5 = strmd5;
  284. }
  285. int grpcpc::GetRawPic(unsigned int camindex,iv::vision::rawpic & xrawpic)
  286. {
  287. mnLastGetPicTime = QDateTime::currentMSecsSinceEpoch();
  288. if((camindex >= NUM_CAM))return -1;
  289. qint64 now = QDateTime::currentMSecsSinceEpoch();
  290. if((now - mnPicUpdateTime[camindex])>1000)
  291. {
  292. return 0;
  293. }
  294. mMutexPic[camindex].lock();
  295. xrawpic.CopyFrom(mRawPic[camindex]);
  296. mMutexPic[camindex].unlock();
  297. return 1;
  298. }
  299. int grpcpc::GetRecvMsg(iv::cloud::cloudmsg & xmsg)
  300. {
  301. mnLastGetmsgTime = QDateTime::currentMSecsSinceEpoch();
  302. qint64 now = QDateTime::currentMSecsSinceEpoch();
  303. if((now - mnmsgrecvupdatetime) > 1000)
  304. {
  305. return 0;
  306. }
  307. mMutexmsgrecv.lock();
  308. xmsg.CopyFrom(mmsgrecv);
  309. mMutexmsgrecv.unlock();
  310. return 1;
  311. }
  312. int grpcpc::SetSendMsg(iv::cloud::cloudmsg & xmsg)
  313. {
  314. mMutexmsgsend.lock();
  315. mmsgsend.CopyFrom(xmsg);
  316. mnmsgsendupdatetime = QDateTime::currentMSecsSinceEpoch();
  317. mMutexmsgsend.unlock();
  318. return 0;
  319. }