protobuf proto3 参考指引指导(C++)

一.protobuf proto3 指导

链接: https://www.jianshu.com/p/73c9ed3a4877.
链接: https://developers.google.cn/protocol-buffers/docs/proto3.

helloworld.proto

syntax = "proto3";

package helloworld;

// The greeting service definition.
service Greeter {
  // Sends a greeting
  rpc SayHello (HelloRequest) returns (HelloReply) {}
  // Sends another greeting
  rpc SayHelloAgain (HelloRequest) returns (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;
}

上面的rpc 就是远程过程调用(Grpc) request:请求 reply:回答

用在C++ 即 .cpp 源文件中

1.调用命名空间

using grpc::Server;
using grpc::ServerBuilder;
using grpc::ServerContext;
using grpc::Status;
using helloworld::HelloRequest;
using helloworld::HelloReply;
using helloworld::Greeter;

2.调用必要的包

#include <grpcpp/grpcpp.h>
#include <grpcpp/health_check_service_interface.h>
#include <grpcpp/ext/proto_server_reflection_plugin.h>

3.调用helloworld.proto 生成的头文件(.pb.h / .grpc.pc.h)

#include "helloworld.grpc.pb.h"

下面两句使proto生成(.pb.h / .grpc.pc.h / .pb.cc / .grpc.pc.cc 4个文件)
/usr/local/bin/protoc -I ./proto --grpc_out=./grpc --plugin=protoc-gen-grpc=/usr/local/bin/grpc_cpp_plugin ./proto/helloworld.proto

/usr/local/bin/protoc -I ./proto --cpp_out=./grpc ./proto/helloworld.proto

4.函数调用

class GreeterServiceImpl final : public Greeter::Service {
  Status SayHello(ServerContext* context, const HelloRequest* request, HelloReply* reply) override {
    std::string prefix("Hello ");
    reply->set_message(prefix + request->name());
    return Status::OK;
  }

5.函数应用(抽取上面的关键字段)
其中:Greeter::Service
找好对应: rpc SayHello (HelloRequest) returns (HelloReply) {}

关于reply的调用关系:
using helloworld::HelloReply;

message HelloReply {
string message = 1;
}

HelloReply* reply

reply->set_message(reply还有多个函数clear_message/ message() / mutable_message() 等)
注:以上面string message = 1中的message为主体扩展

6.bool型 即true,false 判断是否远程连接成功。

Status::OK

二.proto3书写规则,函数应用说明

proto3书写规则,函数应用说明

1.参数设置

syntax = "proto3";

// message 定义
message Example1 {
int32 int32Val = 1;
bool boolVal = 2;

}

注:上面的1,2个人感觉只是个ID,无实际意义

Example1 example1;
example1.set_int32val(1);
example1.set_boolval(true);

2.枚举

// message 定义
message Example1 {
    enum COLOR {
        YELLOW = 0;
        RED = 1;
        BLACK = 2;
        WHITE = 3;
        BLUE = 4;
    }
    // 枚举常量必须在 32 位整型值的范围
    // 使用 Varints 编码,对负数不够高效,因此不推荐在枚举中使用负数
    COLOR colorVal = 1;
}
Example1 example1;
example1.set_colorval(Example1_COLOR_BLUE);

3.字符串,嵌入的函数,repeated(可重复的,可设置多个)

// message 定义
message Example1 {
    string stringVal = 1;
    bytes bytesVal = 2;
    message EmbeddedMessage {
        int32 int32Val = 1;
        string stringVal = 2;
    }
    EmbeddedMessage embeddedExample1 = 3;
    repeated int32 repeatedInt32Val = 4;
    repeated string repeatedStringVal = 5;
}
Example1 example1;
example1.set_stringval("hello,world");
example1.set_bytesval("are you ok?");

Example1_EmbeddedMessage *embeddedExample2 = new Example1_EmbeddedMessage();

embeddedExample2->set_int32val(1);
embeddedExample2->set_stringval("embeddedInfo");
example1.set_allocated_embeddedexample1(embeddedExample2);

example1.add_repeatedint32val(2);
example1.add_repeatedint32val(3);
example1.add_repeatedstringval("repeated1");
example1.add_repeatedstringval("repeated2");
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值