123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
- #include "rpcClient.h"
- #include <QDebug>
- bool rpcClient::upload(cv::Mat img, std::string str_user, std::string str_id)
- {
- //创建一个ChunkOneLine*的vector,用于存储图像的数据
- std::vector<ChunkOneLine*>chunkonelie;
- ChunkOneLine* sedchunk = new ChunkOneLine();
- Chunk* chunk = new Chunk();
- //编码的前必须的data格式,用一个uchar类型的vector
- std::vector<uchar> data_encode;
- //直接编码
- cv::imencode(".jpg", img, data_encode);
- //放到string里面
- std::string str_encode(data_encode.begin(), data_encode.end());
- std::string _user = "user1";
- std::string _id = "1234567898";
- //把得到的string放到buff里面,
- chunk->set_allocated_buff(&str_encode);
- chunk->set_allocated_name(&str_user);
- chunk->set_id(1111);
- sedchunk->set_allocated_databuf(chunk);
- ClientContext context;
- //定义一个用来存储返回信息的变量
- Reply reply;
- //获得远程API(俗称远程方法)的指针
- std::unique_ptr<ClientWriter<::namespace_uploadpic::ChunkOneLine>> writer = stu_->Upload(&context, &reply);
- //记录当前时间
- std::chrono::system_clock::time_point start_time = std::chrono::system_clock::now();
- //开始写(发送)
- if (!writer->Write(*sedchunk))
- {
- //break;
- qDebug() << "Error!";
- }
- //写完了
- writer->WritesDone();
- grpc::Status status = writer->Finish();
- if (status.ok() )
- {
- // 计算服务器解析花了多少时间
- m_nServerTime = reply.result().size();
- //再次记录当前时间
- std::chrono::system_clock::time_point end_time = std::chrono::system_clock::now();
- auto sec = std::chrono::duration_cast<std::chrono::milliseconds>(end_time - start_time);
- m_nClientTime = sec.count();
- }
- else
- {
- m_nClientTime = 888;
- qDebug() << "Error";
- }
-
- return true;
- }
- void rpcClient::ListFeatures()
- {
- //向服务请求的参数
- Request request;
- request.set_name("11");
- //保存服务每次返回的流结果
- Response feature;
- //上下文
- ClientContext context;
- //获得远程API(俗称远程方法)的指针
- std::unique_ptr<ClientReader<Response> > reader(stu_->ListFeatures(&context, request));
- //读取由服务端返回的流数据,知道服务端没有流,才跳出while循环,否则一直读
- while (reader->Read(&feature))
- {
- for (int i = 0; i < feature.result_size(); i++)
- {
- qDebug() << "flow call received: " << feature.result(i).c_str() ;
- }
- // std::cout << "flow call received: " << feature.result() << std::endl;
- }
- Status status = reader->Finish();
- if (status.ok())
- {
- qDebug() << "ListFeatures rpc succeeded." ;
- }
- else
- {
- qDebug() << "ListFeatures rpc failed.";
- }
- qDebug() << "3333333.";
- }
- int rpcClient:: getClientTime()
- {
- return m_nClientTime;
- }
- int rpcClient:: getServerTime()
- {
- return m_nServerTime;
- }
|