赞
踩
基于之前的文章所说, Qt6.7之后才开始支持客户端、服务端、及双向流,恰好电脑需要重装,看到Qt6.8版本就直接安装了,内容也是使用Qt6.8的版本进行编译的
Qt 6.8安装目录下包含这两个组件就可以将.proto生成Qt库支持了
基于 Qt6.8 GRPC功能使用(1)标准GRPC C++ exmple编译环境搭建
新建一个Qt grpc客户端应用, 将 helloworld.proto 拷贝到该应用中,.pro加上grpc支持 .pro += grpc
.proto文件内容, 来自xxx\grpc-1.55.0\examples\protos\helloworld.proto
syntax = "proto3"; option java_multiple_files = true; option java_package = "io.grpc.examples.helloworld"; option java_outer_classname = "HelloWorldProto"; option objc_class_prefix = "HLW"; package helloworld; // The greeting service definition. service Greeter { // Sends a greeting rpc SayHello (HelloRequest) returns (HelloReply) {} rpc SayHelloStreamReply (HelloRequest) returns (stream HelloReply) {} } // The request message containing the user's name. message HelloRequest { string name = 1; } // The response message containing the greetings message HelloReply { string message = 1; }
D:\Softwares\Paths\msys64\mingw64\bin\protoc.exe --plugin=protoc-gen-qtprotobuf=D:\Softwares\IDEs\Qt\6.8.0\mingw_64\bin\qtprotobufgen.exe -I E:/Workspace/Qt/greeter_client_qt/grpc --qtprotobuf_out=“E:/Workspace/Qt/greeter_client_qt/grpc” “E:/Workspace/Qt/greeter_client_qt/grpc/helloworld.proto”
D:\Softwares\Paths\msys64\mingw64\bin\protoc.exe --plugin=protoc-gen-qtgrpc=D:\Softwares\IDEs\Qt\6.8.0\mingw_64\bin\qtgrpcgen.exe -I E:/Workspace/Qt/greeter_client_qt/grpc --qtgrpc_out=“E:/Workspace/Qt/greeter_client_qt/grpc” “E:/Workspace/Qt/greeter_client_qt/grpc/helloworld.proto”
#include <QCoreApplication> #include <QGrpcChannelOptions> #include <QGrpcServerStream> #include "helloworld_client.grpc.qpb.h" #include <QGrpcHttp2Channel> #include <QDebug> int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); helloworld::Greeter::Client client; QAbstractSocket::connect(&client, &QAbstractGrpcClient::errorOccurred, [=](const QGrpcStatus &status) { qDebug().noquote() << "errorOccurred : " << status.code() << status.message(); }); QAbstractSocket::connect(&client, &QAbstractGrpcClient::channelChanged, [=]() { qDebug().noquote() << "channelChanged!"; }); QUrl url ("http://localhost:50051"); QGrpcChannelOptions channelOptions(url); std::shared_ptr<QAbstractGrpcChannel> channel = std::make_shared<QGrpcHttp2Channel>( channelOptions); client.attachChannel(channel); helloworld::HelloRequest req; req.setName("GrayHsu"); std::shared_ptr<QGrpcCallReply> grpcReply = client.SayHello(req); QAbstractSocket::connect(grpcReply.get(), &QGrpcCallReply::errorOccurred, [=](const QGrpcStatus &status) { qDebug().noquote() << "sayHello errorOccurred : " << status.code() << status.message(); }); QAbstractSocket::connect(grpcReply.get(), &QGrpcCallReply::finished, [=]() { helloworld::HelloReply rep; grpcReply->read(&rep); qDebug().noquote() << "finished: " << rep.message(); }); return a.exec(); }
执行结果:
Server 没开
Server开了
QGrpcCallReply用于接收返回信息, 提供了两个信号, 用于监控是否出错及是否结束(流时则为关闭)
std::shared_ptr grpcReply = client.SayHello(req);
例如:
如下指令直接生成grpc_out的grpc支持,-cpp_out输出protobuffer支持
D:\Softwares\Paths\msys64\mingw64\bin\protoc.exe --grpc_out E:/test/grpc-1.55.0/examples/cpp/helloworld/cmake/build --cpp_out E:/test/grpc-1.55.0/examples/cpp/helloworld/cmake/build -I E:/test/grpc-1.55.0/examples/protos --plugin=protoc-gen-grpc="D:/Softwares/Paths/msys64/mingw64/bin/grpc_cpp_plugin.exe" E:/test/grpc-1.55.0/examples/protos/helloworld.proto
Qt6.8 GRPC功能使用(1)标准GRPC C++ exmple编译环境搭建
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。