rpcClient.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #include "rpcClient.h"
  2. #include <QDebug>
  3. bool rpcClient::upload(cv::Mat img, std::string str_user, std::string str_id)
  4. {
  5. //创建一个ChunkOneLine*的vector,用于存储图像的数据
  6. std::vector<ChunkOneLine*>chunkonelie;
  7. ChunkOneLine* sedchunk = new ChunkOneLine();
  8. Chunk* chunk = new Chunk();
  9. //编码的前必须的data格式,用一个uchar类型的vector
  10. std::vector<uchar> data_encode;
  11. //直接编码
  12. cv::imencode(".jpg", img, data_encode);
  13. //放到string里面
  14. std::string str_encode(data_encode.begin(), data_encode.end());
  15. std::string _user = "user1";
  16. std::string _id = "1234567898";
  17. //把得到的string放到buff里面,
  18. chunk->set_allocated_buff(&str_encode);
  19. chunk->set_allocated_name(&str_user);
  20. chunk->set_id(1111);
  21. sedchunk->set_allocated_databuf(chunk);
  22. ClientContext context;
  23. //定义一个用来存储返回信息的变量
  24. Reply reply;
  25. //获得远程API(俗称远程方法)的指针
  26. std::unique_ptr<ClientWriter<::namespace_uploadpic::ChunkOneLine>> writer = stu_->Upload(&context, &reply);
  27. //记录当前时间
  28. std::chrono::system_clock::time_point start_time = std::chrono::system_clock::now();
  29. //开始写(发送)
  30. if (!writer->Write(*sedchunk))
  31. {
  32. //break;
  33. qDebug() << "Error!";
  34. }
  35. //写完了
  36. writer->WritesDone();
  37. grpc::Status status = writer->Finish();
  38. if (status.ok() )
  39. {
  40. // 计算服务器解析花了多少时间
  41. m_nServerTime = reply.result().size();
  42. //再次记录当前时间
  43. std::chrono::system_clock::time_point end_time = std::chrono::system_clock::now();
  44. auto sec = std::chrono::duration_cast<std::chrono::milliseconds>(end_time - start_time);
  45. m_nClientTime = sec.count();
  46. }
  47. else
  48. {
  49. m_nClientTime = 888;
  50. qDebug() << "Error";
  51. }
  52. return true;
  53. }
  54. void rpcClient::ListFeatures()
  55. {
  56. //向服务请求的参数
  57. Request request;
  58. request.set_name("11");
  59. //保存服务每次返回的流结果
  60. Response feature;
  61. //上下文
  62. ClientContext context;
  63. //获得远程API(俗称远程方法)的指针
  64. std::unique_ptr<ClientReader<Response> > reader(stu_->ListFeatures(&context, request));
  65. //读取由服务端返回的流数据,知道服务端没有流,才跳出while循环,否则一直读
  66. while (reader->Read(&feature))
  67. {
  68. for (int i = 0; i < feature.result_size(); i++)
  69. {
  70. qDebug() << "flow call received: " << feature.result(i).c_str() ;
  71. }
  72. // std::cout << "flow call received: " << feature.result() << std::endl;
  73. }
  74. Status status = reader->Finish();
  75. if (status.ok())
  76. {
  77. qDebug() << "ListFeatures rpc succeeded." ;
  78. }
  79. else
  80. {
  81. qDebug() << "ListFeatures rpc failed.";
  82. }
  83. qDebug() << "3333333.";
  84. }
  85. int rpcClient:: getClientTime()
  86. {
  87. return m_nClientTime;
  88. }
  89. int rpcClient:: getServerTime()
  90. {
  91. return m_nServerTime;
  92. }