grpcdbclient.cpp 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #include "grpcdbclient.h"
  2. grpcdbclient::grpcdbclient()
  3. {
  4. }
  5. void grpcdbclient::run()
  6. {
  7. std::string target_str = mstrserverip+":";
  8. target_str = target_str + mstrserverport ;//std::to_string()
  9. auto cargs = grpc::ChannelArguments();
  10. cargs.SetMaxReceiveMessageSize(1024 * 1024 * 1024); // 1 GB
  11. cargs.SetMaxSendMessageSize(1024 * 1024 * 1024);
  12. std::shared_ptr<Channel> channel = grpc::CreateCustomChannel(
  13. target_str, grpc::InsecureChannelCredentials(),cargs);
  14. std::unique_ptr<iv::db::dbservice::Stub> stub_ = iv::db::dbservice::NewStub(channel);
  15. gpr_timespec timespec;
  16. timespec.tv_sec = 5;//设置阻塞时间为5秒
  17. timespec.tv_nsec = 0;
  18. timespec.clock_type = GPR_TIMESPAN;
  19. while(!QThread::isInterruptionRequested())
  20. {
  21. mWaitMutex.lock();
  22. mwc.wait(&mWaitMutex,100);
  23. mWaitMutex.unlock();
  24. if(mblistrequpdate)
  25. {
  26. iv::db::listRequest xlistreq;
  27. iv::db::listReply xlistreply;
  28. mMutexReq.lock();
  29. xlistreq.CopyFrom(mlistreq);
  30. mblistrequpdate = false;
  31. mMutexReq.unlock();
  32. ClientContext context ;
  33. context.set_deadline(timespec);
  34. Status status = stub_->querylist(&context, xlistreq, &xlistreply);
  35. if (status.ok()) {
  36. std::cout<<"get list ok"<<std::endl;
  37. emit reqres(0);
  38. } else {
  39. std::cout << status.error_code() << ": " << status.error_message()
  40. << std::endl;
  41. std::cout<<"RPC failed"<<std::endl;
  42. emit reqres(-1);
  43. if(status.error_code() == 4)
  44. {
  45. std::cout<<" RPC Exceed Time, Create New stub_"<<std::endl;
  46. channel = grpc::CreateCustomChannel(
  47. target_str, grpc::InsecureChannelCredentials(),cargs);
  48. stub_ = iv::db::dbservice::NewStub(channel);
  49. }
  50. std::this_thread::sleep_for(std::chrono::milliseconds(900));
  51. }
  52. }
  53. }
  54. }
  55. void grpcdbclient::requestlist(qint64 timefrom, qint64 timeto, std::string strvehid)
  56. {
  57. mMutexReq.lock();
  58. mlistreq.set_fromtime(timefrom);
  59. mlistreq.set_totime(timeto);
  60. mlistreq.set_strvehid(strvehid);
  61. mblistrequpdate = true;
  62. mMutexReq.unlock();
  63. }