main.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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. void uploadsend(::grpc::ServerReaderWriter<iv::UploadReplyStream, iv::UploadRequestStream>* stream,bool * pbrun,
  23. std::string * pstrvin,std::string * pstrmd5,bool *pbUpdatemd4orvin,QMutex * pmutex,int * preqid,qint64 * pnsendtime,qint64 * recvtime,QMutex * pmutexidtime)
  24. {
  25. std::string strvin;
  26. std::string strmd5;
  27. pmutex->lock();
  28. strvin = *pstrmd5;
  29. strmd5 = *pstrvin;
  30. pmutex->unlock();
  31. QTime xTime;
  32. xTime.start();
  33. int nlastsend = xTime.elapsed();
  34. while(*pbrun)
  35. {
  36. if(*pbUpdatemd4orvin)
  37. {
  38. pmutex->lock();
  39. strvin = *pstrvin;
  40. strmd5 = *pstrmd5;
  41. *pbUpdatemd4orvin = false;
  42. pmutex->unlock();
  43. }
  44. int id;
  45. qint64 ntime;
  46. float framerate;
  47. std::vector<char > xvectorctrldata;
  48. int nres = gpcmsgbuf.getmsg(strvin,strmd5,id,ntime,&xvectorctrldata,&framerate);
  49. iv::UploadReplyStream reply;
  50. pmutexidtime->lock();
  51. reply.set_nreqsendtime(*pnsendtime);
  52. reply.set_nreqid(*preqid);
  53. reply.set_npausetime(QDateTime::currentMSecsSinceEpoch() - *recvtime);
  54. pmutexidtime->unlock();
  55. if(nres == 1)
  56. {
  57. reply.set_nres(nres);
  58. reply.set_xdata(xvectorctrldata.data(),xvectorctrldata.size());
  59. reply.set_framerate(framerate);
  60. stream->Write(reply);
  61. nlastsend = xTime.elapsed();
  62. }
  63. else
  64. {
  65. if(abs(xTime.elapsed() - nlastsend)>1000)
  66. {
  67. reply.set_nres(nres);
  68. reply.set_framerate(0);
  69. stream->Write(reply);
  70. nlastsend = xTime.elapsed();
  71. }
  72. }
  73. std::this_thread::sleep_for(std::chrono::milliseconds(1));
  74. }
  75. }
  76. qint64 calcserverlatency(std::vector<qint64> & xvectorserverlatency,qint64 nlatency)
  77. {
  78. xvectorserverlatency.push_back(nlatency);
  79. while(xvectorserverlatency.size()>5)xvectorserverlatency.erase(xvectorserverlatency.begin());
  80. if(xvectorserverlatency.size()<1)return 0;
  81. int nsize = xvectorserverlatency.size();
  82. int i;
  83. qint64 nrtn = 0;
  84. for(i=0;i<nsize;i++)nrtn = nrtn + xvectorserverlatency.at(i);
  85. return nrtn/nsize;
  86. }
  87. void queryctrlsend(::grpc::ServerReaderWriter<iv::queryReplyStream, iv::queryReqStream>* stream,bool * pbrun,
  88. std::string * pstrvin,std::string * pstrmd5,bool *pbUpdatemd4orvin,QMutex * pmutex,qint64 * pnserverlatency)
  89. {
  90. std::string strvin;
  91. std::string strmd5;
  92. pmutex->lock();
  93. strvin = *pstrmd5;
  94. strmd5 = *pstrvin;
  95. pmutex->unlock();
  96. QTime xTime;
  97. xTime.start();
  98. int nlastsend = xTime.elapsed();
  99. qint64 nlastdatatime = 0;
  100. qint64 nculatency;
  101. while(*pbrun)
  102. {
  103. if(*pbUpdatemd4orvin)
  104. {
  105. pmutex->lock();
  106. strvin = *pstrvin;
  107. strmd5 = *pstrmd5;
  108. *pbUpdatemd4orvin = false;
  109. pmutex->unlock();
  110. }
  111. int id;
  112. qint64 ntime;
  113. std::vector<char > xvectorquerydata;
  114. int nres = gcumsgbuf.getmsg(strvin,strmd5,nlastdatatime,id,ntime,&xvectorquerydata,&nculatency);
  115. nlastdatatime = ntime;
  116. iv::queryReplyStream reply;
  117. reply.set_nres(nres);
  118. if(nres > 0)
  119. {
  120. reply.set_xdata(xvectorquerydata.data(),xvectorquerydata.size());
  121. reply.set_id(id);
  122. reply.set_ntime(ntime);
  123. reply.set_nculatency(nculatency);
  124. reply.set_nserverlatency(*pnserverlatency);
  125. reply.set_nsendtime(QDateTime::currentMSecsSinceEpoch());
  126. stream->Write(reply);
  127. nlastsend = xTime.elapsed();
  128. }
  129. else
  130. {
  131. if(abs(xTime.elapsed() - nlastsend)>1000)
  132. {
  133. stream->Write(reply);
  134. nlastsend = xTime.elapsed();
  135. }
  136. }
  137. std::this_thread::sleep_for(std::chrono::milliseconds(1));
  138. }
  139. }
  140. // Logic and data behind the server's behavior.
  141. class UploadServiceImpl final : public iv::UploadStream::Service {
  142. Status upload(ServerContext* context, ::grpc::ServerReaderWriter<iv::UploadReplyStream, iv::UploadRequestStream>* stream) override {
  143. iv::UploadRequestStream request;
  144. bool brun = true;
  145. std::string strctrlmd5 = "md5";
  146. std::string strvin = "aaa";
  147. bool bUpdatemd4orvin = false;
  148. QMutex uploadmutex;
  149. int reqid = -1;
  150. qint64 pnsendtime = 0;
  151. qint64 nrecvtime = 0;
  152. QMutex mutexidtime;
  153. std::thread * pthread = new std::thread(uploadsend,stream,&brun,&strvin,&strctrlmd5,&bUpdatemd4orvin,&uploadmutex,
  154. &reqid,&pnsendtime,&nrecvtime,&mutexidtime);
  155. std::cout<<"new connect."<<std::endl;
  156. while (stream->Read(&request))
  157. {
  158. std::cout<<" rec req."<<std::endl;
  159. std::vector<char> xvectordata;
  160. qDebug("size is %d",request.xdata().size());
  161. if((strctrlmd5 != request.strctrlmd5())||(strvin != request.strvin()))
  162. {
  163. uploadmutex.lock();
  164. strctrlmd5 = request.strctrlmd5();
  165. strvin = request.strvin();
  166. bUpdatemd4orvin = true;
  167. uploadmutex.unlock();
  168. }
  169. if(request.xdata().size()>0)
  170. {
  171. xvectordata.resize(request.xdata().size());
  172. memcpy(xvectordata.data(),request.xdata().data(),request.xdata().size());
  173. }
  174. mutexidtime.lock();
  175. reqid = request.id();
  176. pnsendtime = request.nsendtime();
  177. nrecvtime = QDateTime::currentMSecsSinceEpoch();
  178. mutexidtime.unlock();
  179. gcumsgbuf.addmsg(request.id(),request.ntime(),request.strvin(),request.strquerymd5(),request.strctrlmd5(),
  180. &xvectordata,request.bimportant(),request.kepptime(),request.nlatency());
  181. // std::cout << "收到请求,类型为" << request.askmsg() <<"\n"<<std::endl;
  182. }
  183. std::cout<<" no conn"<<std::endl;
  184. brun = false;
  185. pthread->join();
  186. std::cout<<"dis connect."<<std::endl;
  187. return Status::OK;
  188. }
  189. Status queryctrl(ServerContext* context, ::grpc::ServerReaderWriter<iv::queryReplyStream, iv::queryReqStream>* stream) override {
  190. iv::queryReqStream request;
  191. bool brun = true;
  192. std::string strctrlmd5 = "md5";
  193. std::string strquerymd5 = "md5";
  194. std::string strvin = "aaa";
  195. bool bUpdatemd4orvin = false;
  196. QMutex uploadmutex;
  197. qint64 nreplysendtime;
  198. qint64 npausetime;
  199. std::vector<qint64> xvectorserverlatency;
  200. qint64 nserverlatency = 0;
  201. std::thread * pthread = new std::thread(queryctrlsend,stream,&brun,&strvin,&strquerymd5,&bUpdatemd4orvin,&uploadmutex,&nserverlatency);
  202. std::cout<<"new connect."<<std::endl;
  203. while (stream->Read(&request))
  204. {
  205. std::cout<<" rec req."<<std::endl;
  206. std::vector<char> xvectordata;
  207. qDebug("size is %d",request.xdata().size());
  208. if((strquerymd5 != request.strquerymd5())||(strvin != request.strvin()))
  209. {
  210. uploadmutex.lock();
  211. strquerymd5 = request.strquerymd5();
  212. strvin = request.strvin();
  213. bUpdatemd4orvin = true;
  214. uploadmutex.unlock();
  215. }
  216. if(request.xdata().size()>0)
  217. {
  218. xvectordata.resize(request.xdata().size());
  219. memcpy(xvectordata.data(),request.xdata().data(),request.xdata().size());
  220. }
  221. nreplysendtime = request.nreplysendtime();
  222. npausetime = request.npausetime();
  223. if(nreplysendtime > 0)
  224. {
  225. nserverlatency = calcserverlatency(xvectorserverlatency,QDateTime::currentMSecsSinceEpoch() - nreplysendtime - npausetime);
  226. }
  227. static int tempid = 0;
  228. tempid++;
  229. int nid = gpcmsgbuf.addmsg(tempid,request.ntime(),request.strvin(),request.strctrlmd5(),&xvectordata,
  230. request.bimportant(),request.kepptime(),request.nsuggestframerate());
  231. (void)&nid;
  232. // std::cout << "收到请求,类型为" << request.askmsg() <<"\n"<<std::endl;
  233. }
  234. std::cout<<" no conn"<<std::endl;
  235. brun = false;
  236. pthread->join();
  237. std::cout<<"dis connect."<<std::endl;
  238. return Status::OK;
  239. }
  240. // Status query(ServerContext* context, const iv::queryreq* request,
  241. // iv::queryReply* reply) override {
  242. // int id;
  243. // qint64 ntime;
  244. // std::vector<char > xvectorquerydata;
  245. // int nres = gcumsgbuf.getmsg(request->strvin(),request->strquerymd5(),request->nlasttime(),id,ntime,&xvectorquerydata);
  246. // reply->set_nres(nres);
  247. // if(nres > 0)
  248. // {
  249. // reply->set_data(xvectorquerydata.data(),xvectorquerydata.size());
  250. // reply->set_id(id);
  251. // reply->set_ntime(ntime);
  252. // }
  253. // return Status::OK;
  254. // }
  255. // Status ctrl(ServerContext* context, const iv::ctrlreq* request,
  256. // iv::ctrlReply * reply) override {
  257. // std::vector<char> xvectordata;
  258. // if(request->data().size()>0)
  259. // {
  260. // xvectordata.resize(request->data().size());
  261. // memcpy(xvectordata.data(),request->data().data(),request->data().size());
  262. // }
  263. // int nid = gpcmsgbuf.addmsg(request->id(),request->ntime(),request->strvin(),request->strctrlmd5(),&xvectordata,
  264. // request->bimportant(),request->kepptime());
  265. // reply->set_nsendid(nid);
  266. // return Status::OK;
  267. // }
  268. };
  269. void RunServer() {
  270. std::string server_address("0.0.0.0:50051");
  271. UploadServiceImpl service;
  272. grpc::EnableDefaultHealthCheckService(true);
  273. // grpc::reflection::InitProtoReflectionServerBuilderPlugin();
  274. ServerBuilder builder;
  275. // Listen on the given address without any authentication mechanism.
  276. builder.AddListeningPort(server_address, grpc::InsecureServerCredentials());
  277. builder.SetMaxReceiveMessageSize(300000000);
  278. // builder.SetMaxMessageSize(100000000);
  279. // builder.SetMaxSendMessageSize(100000000);
  280. // Register "service" as the instance through which we'll communicate with
  281. // clients. In this case it corresponds to an *synchronous* service.
  282. builder.RegisterService(&service);
  283. // Finally assemble the server.
  284. std::unique_ptr<Server> server(builder.BuildAndStart());
  285. std::cout << "Server listening on " << server_address << std::endl;
  286. // Wait for the server to shutdown. Note that some other thread must be
  287. // responsible for shutting down the server for this call to ever return.
  288. server->Wait();
  289. }
  290. int main(int argc, char *argv[])
  291. {
  292. QCoreApplication a(argc, argv);
  293. gpcmsgbuf.start();
  294. RunServer();
  295. return a.exec();
  296. }