12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879 |
- #include "grpcdbclient.h"
- grpcdbclient::grpcdbclient()
- {
- }
- void grpcdbclient::run()
- {
- std::string target_str = mstrserverip+":";
- target_str = target_str + mstrserverport ;//std::to_string()
- auto cargs = grpc::ChannelArguments();
- cargs.SetMaxReceiveMessageSize(1024 * 1024 * 1024); // 1 GB
- cargs.SetMaxSendMessageSize(1024 * 1024 * 1024);
- std::shared_ptr<Channel> channel = grpc::CreateCustomChannel(
- target_str, grpc::InsecureChannelCredentials(),cargs);
- std::unique_ptr<iv::db::dbservice::Stub> stub_ = iv::db::dbservice::NewStub(channel);
- gpr_timespec timespec;
- timespec.tv_sec = 5;//设置阻塞时间为5秒
- timespec.tv_nsec = 0;
- timespec.clock_type = GPR_TIMESPAN;
- while(!QThread::isInterruptionRequested())
- {
- mWaitMutex.lock();
- mwc.wait(&mWaitMutex,100);
- mWaitMutex.unlock();
- if(mblistrequpdate)
- {
- iv::db::listRequest xlistreq;
- iv::db::listReply xlistreply;
- mMutexReq.lock();
- xlistreq.CopyFrom(mlistreq);
- mblistrequpdate = false;
- mMutexReq.unlock();
- ClientContext context ;
- context.set_deadline(timespec);
- Status status = stub_->querylist(&context, xlistreq, &xlistreply);
- if (status.ok()) {
- std::cout<<"get list ok"<<std::endl;
- emit reqres(0);
- } else {
- std::cout << status.error_code() << ": " << status.error_message()
- << std::endl;
- std::cout<<"RPC failed"<<std::endl;
- emit reqres(-1);
- if(status.error_code() == 4)
- {
- std::cout<<" RPC Exceed Time, Create New stub_"<<std::endl;
- channel = grpc::CreateCustomChannel(
- target_str, grpc::InsecureChannelCredentials(),cargs);
- stub_ = iv::db::dbservice::NewStub(channel);
- }
- std::this_thread::sleep_for(std::chrono::milliseconds(900));
- }
- }
- }
- }
- void grpcdbclient::requestlist(qint64 timefrom, qint64 timeto, std::string strvehid)
- {
- mMutexReq.lock();
- mlistreq.set_fromtime(timefrom);
- mlistreq.set_totime(timeto);
- mlistreq.set_strvehid(strvehid);
- mblistrequpdate = true;
- mMutexReq.unlock();
- }
|