main.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. #include <QCoreApplication>
  2. #include <QDateTime>
  3. #include <iostream>
  4. #include <vector>
  5. #include "cumsgbuffer.h"
  6. #include "pcmsgbuffer.h"
  7. #include <iostream>
  8. #include <memory>
  9. #include <string>
  10. #include <grpcpp/grpcpp.h>
  11. #include <grpcpp/health_check_service_interface.h>
  12. #include <grpcpp/ext/proto_server_reflection_plugin.h>
  13. #include "uploadstreammsg.grpc.pb.h"
  14. using grpc::Server;
  15. using grpc::ServerBuilder;
  16. using grpc::ServerContext;
  17. using grpc::Status;
  18. #include <QDateTime>
  19. #include <thread>
  20. static cumsgbuffer gcumsgbuf;
  21. static pcmsgbuffer gpcmsgbuf;
  22. char gstrport[255];
  23. #include <getopt.h>
  24. void print_useage()
  25. {
  26. std::cout<<" -p --port $port : port . eq. -p 50051"<<std::endl;
  27. std::cout<<" -h --help print help"<<std::endl;
  28. }
  29. int GetOptLong(int argc, char *argv[]) {
  30. int nRtn = 0;
  31. int opt; // getopt_long() 的返回值
  32. int digit_optind = 0; // 设置短参数类型及是否需要参数
  33. // 如果option_index非空,它指向的变量将记录当前找到参数符合long_opts里的
  34. // 第几个元素的描述,即是long_opts的下标值
  35. int option_index = 0;
  36. // 设置短参数类型及是否需要参数
  37. const char *optstring = "m:r:x:y:o:p:s:h";
  38. static struct option long_options[] = {
  39. {"port", required_argument, NULL, 'p'},
  40. {"help", no_argument, NULL, 'h'},
  41. // {"optarg", optional_argument, NULL, 'o'},
  42. {0, 0, 0, 0} // 添加 {0, 0, 0, 0} 是为了防止输入空值
  43. };
  44. while ( (opt = getopt_long(argc,
  45. argv,
  46. optstring,
  47. long_options,
  48. &option_index)) != -1) {
  49. switch(opt)
  50. {
  51. case 'p':
  52. strncpy(gstrport,optarg,255);
  53. break;
  54. case 'h':
  55. print_useage();
  56. nRtn = 1; //because use -h
  57. break;
  58. default:
  59. break;
  60. }
  61. }
  62. return nRtn;
  63. }
  64. void uploadsend(::grpc::ServerReaderWriter<iv::UploadReplyStream, iv::UploadRequestStream>* stream,bool * pbrun,
  65. std::string * pstrvin,std::string * pstrmd5,bool *pbUpdatemd4orvin,QMutex * pmutex,int * preqid,qint64 * pnsendtime,qint64 * recvtime,QMutex * pmutexidtime)
  66. {
  67. std::string strvin;
  68. std::string strmd5;
  69. pmutex->lock();
  70. strvin = *pstrmd5;
  71. strmd5 = *pstrvin;
  72. pmutex->unlock();
  73. QTime xTime;
  74. xTime.start();
  75. int nlastsend = xTime.elapsed();
  76. while(*pbrun)
  77. {
  78. if(*pbUpdatemd4orvin)
  79. {
  80. pmutex->lock();
  81. strvin = *pstrvin;
  82. strmd5 = *pstrmd5;
  83. *pbUpdatemd4orvin = false;
  84. pmutex->unlock();
  85. }
  86. int id;
  87. qint64 ntime;
  88. float framerate;
  89. std::vector<char > xvectorctrldata;
  90. int nres = gpcmsgbuf.getmsg(strvin,strmd5,id,ntime,&xvectorctrldata,&framerate);
  91. iv::UploadReplyStream reply;
  92. pmutexidtime->lock();
  93. reply.set_nreqsendtime(*pnsendtime);
  94. reply.set_nreqid(*preqid);
  95. reply.set_npausetime(QDateTime::currentMSecsSinceEpoch() - *recvtime);
  96. pmutexidtime->unlock();
  97. if(nres == 1)
  98. {
  99. reply.set_nres(nres);
  100. reply.set_xdata(xvectorctrldata.data(),xvectorctrldata.size());
  101. reply.set_framerate(framerate);
  102. stream->Write(reply);
  103. nlastsend = xTime.elapsed();
  104. }
  105. else
  106. {
  107. if(abs(xTime.elapsed() - nlastsend)>1000)
  108. {
  109. reply.set_nres(nres);
  110. reply.set_framerate(0);
  111. stream->Write(reply);
  112. nlastsend = xTime.elapsed();
  113. }
  114. }
  115. std::this_thread::sleep_for(std::chrono::milliseconds(1));
  116. }
  117. }
  118. qint64 calcserverlatency(std::vector<qint64> & xvectorserverlatency,qint64 nlatency)
  119. {
  120. xvectorserverlatency.push_back(nlatency);
  121. while(xvectorserverlatency.size()>5)xvectorserverlatency.erase(xvectorserverlatency.begin());
  122. if(xvectorserverlatency.size()<1)return 0;
  123. int nsize = xvectorserverlatency.size();
  124. int i;
  125. qint64 nrtn = 0;
  126. for(i=0;i<nsize;i++)nrtn = nrtn + xvectorserverlatency.at(i);
  127. return nrtn/nsize;
  128. }
  129. void queryctrlsend(::grpc::ServerReaderWriter<iv::queryReplyStream, iv::queryReqStream>* stream,bool * pbrun,
  130. std::string * pstrvin,std::string * pstrmd5,bool *pbUpdatemd4orvin,QMutex * pmutex,qint64 * pnserverlatency)
  131. {
  132. std::string strvin;
  133. std::string strmd5;
  134. pmutex->lock();
  135. strvin = *pstrmd5;
  136. strmd5 = *pstrvin;
  137. pmutex->unlock();
  138. QTime xTime;
  139. xTime.start();
  140. int nlastsend = xTime.elapsed();
  141. qint64 nlastdatatime = 0;
  142. qint64 nculatency;
  143. while(*pbrun)
  144. {
  145. if(*pbUpdatemd4orvin)
  146. {
  147. pmutex->lock();
  148. strvin = *pstrvin;
  149. strmd5 = *pstrmd5;
  150. *pbUpdatemd4orvin = false;
  151. pmutex->unlock();
  152. }
  153. int id;
  154. qint64 ntime;
  155. std::vector<char > xvectorquerydata;
  156. int nres = gcumsgbuf.getmsg(strvin,strmd5,nlastdatatime,id,ntime,&xvectorquerydata,&nculatency);
  157. nlastdatatime = ntime;
  158. iv::queryReplyStream reply;
  159. reply.set_nres(nres);
  160. if(nres > 0)
  161. {
  162. reply.set_xdata(xvectorquerydata.data(),xvectorquerydata.size());
  163. reply.set_id(id);
  164. reply.set_ntime(ntime);
  165. reply.set_nculatency(nculatency);
  166. reply.set_nserverlatency(*pnserverlatency);
  167. reply.set_nsendtime(QDateTime::currentMSecsSinceEpoch());
  168. stream->Write(reply);
  169. nlastsend = xTime.elapsed();
  170. }
  171. else
  172. {
  173. if(abs(xTime.elapsed() - nlastsend)>1000)
  174. {
  175. stream->Write(reply);
  176. nlastsend = xTime.elapsed();
  177. }
  178. }
  179. std::this_thread::sleep_for(std::chrono::milliseconds(1));
  180. }
  181. }
  182. // Logic and data behind the server's behavior.
  183. class UploadServiceImpl final : public iv::UploadStream::Service {
  184. Status upload(ServerContext* context, ::grpc::ServerReaderWriter<iv::UploadReplyStream, iv::UploadRequestStream>* stream) override {
  185. iv::UploadRequestStream request;
  186. bool brun = true;
  187. std::string strctrlmd5 = "md5";
  188. std::string strvin = "aaa";
  189. bool bUpdatemd4orvin = false;
  190. QMutex uploadmutex;
  191. int reqid = -1;
  192. qint64 pnsendtime = 0;
  193. qint64 nrecvtime = 0;
  194. QMutex mutexidtime;
  195. std::thread * pthread = new std::thread(uploadsend,stream,&brun,&strvin,&strctrlmd5,&bUpdatemd4orvin,&uploadmutex,
  196. &reqid,&pnsendtime,&nrecvtime,&mutexidtime);
  197. std::cout<<"new connect."<<std::endl;
  198. while (stream->Read(&request))
  199. {
  200. std::cout<<" rec req."<<std::endl;
  201. std::vector<char> xvectordata;
  202. qDebug("size is %d",request.xdata().size());
  203. if((strctrlmd5 != request.strctrlmd5())||(strvin != request.strvin()))
  204. {
  205. uploadmutex.lock();
  206. strctrlmd5 = request.strctrlmd5();
  207. strvin = request.strvin();
  208. bUpdatemd4orvin = true;
  209. uploadmutex.unlock();
  210. }
  211. if(request.xdata().size()>0)
  212. {
  213. xvectordata.resize(request.xdata().size());
  214. memcpy(xvectordata.data(),request.xdata().data(),request.xdata().size());
  215. }
  216. mutexidtime.lock();
  217. reqid = request.id();
  218. pnsendtime = request.nsendtime();
  219. nrecvtime = QDateTime::currentMSecsSinceEpoch();
  220. mutexidtime.unlock();
  221. gcumsgbuf.addmsg(request.id(),request.ntime(),request.strvin(),request.strquerymd5(),request.strctrlmd5(),
  222. &xvectordata,request.bimportant(),request.kepptime(),request.nlatency());
  223. // std::cout << "收到请求,类型为" << request.askmsg() <<"\n"<<std::endl;
  224. }
  225. std::cout<<" no conn"<<std::endl;
  226. brun = false;
  227. pthread->join();
  228. std::cout<<"dis connect."<<std::endl;
  229. return Status::OK;
  230. }
  231. Status queryctrl(ServerContext* context, ::grpc::ServerReaderWriter<iv::queryReplyStream, iv::queryReqStream>* stream) override {
  232. iv::queryReqStream request;
  233. bool brun = true;
  234. std::string strctrlmd5 = "md5";
  235. std::string strquerymd5 = "md5";
  236. std::string strvin = "aaa";
  237. bool bUpdatemd4orvin = false;
  238. QMutex uploadmutex;
  239. qint64 nreplysendtime;
  240. qint64 npausetime;
  241. std::vector<qint64> xvectorserverlatency;
  242. qint64 nserverlatency = 0;
  243. std::thread * pthread = new std::thread(queryctrlsend,stream,&brun,&strvin,&strquerymd5,&bUpdatemd4orvin,&uploadmutex,&nserverlatency);
  244. std::cout<<"new connect."<<std::endl;
  245. while (stream->Read(&request))
  246. {
  247. std::cout<<" rec req."<<std::endl;
  248. std::vector<char> xvectordata;
  249. qDebug("size is %d",request.xdata().size());
  250. if((strquerymd5 != request.strquerymd5())||(strvin != request.strvin()))
  251. {
  252. uploadmutex.lock();
  253. strquerymd5 = request.strquerymd5();
  254. strvin = request.strvin();
  255. bUpdatemd4orvin = true;
  256. uploadmutex.unlock();
  257. }
  258. if(request.xdata().size()>0)
  259. {
  260. xvectordata.resize(request.xdata().size());
  261. memcpy(xvectordata.data(),request.xdata().data(),request.xdata().size());
  262. }
  263. nreplysendtime = request.nreplysendtime();
  264. npausetime = request.npausetime();
  265. if(nreplysendtime > 0)
  266. {
  267. nserverlatency = calcserverlatency(xvectorserverlatency,QDateTime::currentMSecsSinceEpoch() - nreplysendtime - npausetime);
  268. }
  269. static int tempid = 0;
  270. tempid++;
  271. int nid = gpcmsgbuf.addmsg(tempid,request.ntime(),request.strvin(),request.strctrlmd5(),&xvectordata,
  272. request.bimportant(),request.kepptime(),request.nsuggestframerate());
  273. (void)&nid;
  274. // std::cout << "收到请求,类型为" << request.askmsg() <<"\n"<<std::endl;
  275. }
  276. std::cout<<" no conn"<<std::endl;
  277. brun = false;
  278. pthread->join();
  279. std::cout<<"dis connect."<<std::endl;
  280. return Status::OK;
  281. }
  282. // Status query(ServerContext* context, const iv::queryreq* request,
  283. // iv::queryReply* reply) override {
  284. // int id;
  285. // qint64 ntime;
  286. // std::vector<char > xvectorquerydata;
  287. // int nres = gcumsgbuf.getmsg(request->strvin(),request->strquerymd5(),request->nlasttime(),id,ntime,&xvectorquerydata);
  288. // reply->set_nres(nres);
  289. // if(nres > 0)
  290. // {
  291. // reply->set_data(xvectorquerydata.data(),xvectorquerydata.size());
  292. // reply->set_id(id);
  293. // reply->set_ntime(ntime);
  294. // }
  295. // return Status::OK;
  296. // }
  297. // Status ctrl(ServerContext* context, const iv::ctrlreq* request,
  298. // iv::ctrlReply * reply) override {
  299. // std::vector<char> xvectordata;
  300. // if(request->data().size()>0)
  301. // {
  302. // xvectordata.resize(request->data().size());
  303. // memcpy(xvectordata.data(),request->data().data(),request->data().size());
  304. // }
  305. // int nid = gpcmsgbuf.addmsg(request->id(),request->ntime(),request->strvin(),request->strctrlmd5(),&xvectordata,
  306. // request->bimportant(),request->kepptime());
  307. // reply->set_nsendid(nid);
  308. // return Status::OK;
  309. // }
  310. };
  311. void RunServer() {
  312. std::string server_address("0.0.0.0:");
  313. server_address = server_address.append(gstrport);
  314. UploadServiceImpl service;
  315. grpc::EnableDefaultHealthCheckService(true);
  316. // grpc::reflection::InitProtoReflectionServerBuilderPlugin();
  317. ServerBuilder builder;
  318. // Listen on the given address without any authentication mechanism.
  319. builder.AddListeningPort(server_address, grpc::InsecureServerCredentials());
  320. builder.SetMaxReceiveMessageSize(300000000);
  321. // builder.SetMaxMessageSize(100000000);
  322. // builder.SetMaxSendMessageSize(100000000);
  323. // Register "service" as the instance through which we'll communicate with
  324. // clients. In this case it corresponds to an *synchronous* service.
  325. builder.RegisterService(&service);
  326. // Finally assemble the server.
  327. std::unique_ptr<Server> server(builder.BuildAndStart());
  328. std::cout << "Server listening on " << server_address << std::endl;
  329. // Wait for the server to shutdown. Note that some other thread must be
  330. // responsible for shutting down the server for this call to ever return.
  331. server->Wait();
  332. }
  333. int main(int argc, char *argv[])
  334. {
  335. QCoreApplication a(argc, argv);
  336. snprintf(gstrport,255,"50051");
  337. int nRtn = GetOptLong(argc,argv);
  338. if(nRtn == 1) //show help,so exit.
  339. {
  340. return 0;
  341. }
  342. gpcmsgbuf.start();
  343. RunServer();
  344. return a.exec();
  345. }