protobuf基础概念

1、 Field Types:域类型指的是:数据类型,例如int32、string以及组合类型(enumerations)及other message types

field rule := required | optional | repeated,

filed type := int32 | int64 | uint32 | uint64 | double  | bool | string | bytes

2、protobuf一共有三个字段修饰符: 
  - required:该必须要设置的; 
  - optional :该字段可以有0个或1个值(不超过1个);意味着该字段的值可以被设置也可以不设置。 
  - repeated:该字段可以重复任意多次(包括0次),类似于C++中的list;

3、Multiple message types can be defined in a single .proto file. This is useful if you are defining multiple related messages – so, for example, if you wanted to define the reply message format that corresponds to your SearchResponse message type, you could add it to the same .proto

4、Optional Fields And Default Values(可选域的缺省值)

5、枚举常量

  • 你可以通过将不同的枚举常量指定为相同的值。如果这样做你需要将allow_alias设定位true,否则编译器会在别名的地方产生一个错误信息。
  • enum EnumAllowingAlias {
      option allow_alias = true;
      UNKNOWN = 0;
      STARTED = 1;
      RUNNING = 1;
    }
    enum EnumNotAllowingAlias {
      UNKNOWN = 0;
      STARTED = 1;
      // RUNNING = 1;  // Uncommenting this line will cause a compile error inside Google and a warning message outside.
    }

6、嵌套类型,Nested Types

7、Defining Services

如果想要将消息类型用在RPC(远程方法调用)系统中,可以在.proto文件中定义一个RPC服务接口,protocol buffer编译器将会根据所选择的不同语言生成服务接口代码及存根。如,想要定义一个RPC服务并具有一个方法,该方法能够接收 SearchRequest并返回一个SearchResponse,此时可以在.proto文件中进行如下定义:

service SearchService {
  rpc Search (SearchRequest) returns (SearchResponse);
}

8、Options选项

在定义.proto文件时能够标注一系列的options。Options并不改变整个文件声明的含义,但却能够影响特定环境下处理方式。完整的可用选项可以在google/protobuf/descriptor.proto找到。

一些选项是文件级别的,意味着它可以作用于最外范围,不包含在任何消息内部、enum或服务定义中。一些选项是消息级别的,意味着它可以用在消息定义的内部。当然有些选项可以作用在域、enum类型、enum值、服务类型及服务方法中。到目前为止,并没有一种有效的选项能作用于所有的类型。

9、cc_generic_servicesjava_generic_servicespy_generic_services (file options):在C++、java、python中protocol buffer编译器是否应该基于服务定义产生抽象服务代码(是否根据services定义生成service以及stub,当为false时是不生成的)。由于历史遗留问题,该值默认是true。但是自2.3.0版本以来,它被认为通过提供代码生成 器插件来对RPC实现更可取,而不是依赖于“抽象”服务。

10、生成访问类

可以通过定义好的.proto文件来生成Java、Python、C++代码,需要基于.proto文件运行protocol buffer编译器protoc。运行的命令如下所示:

protoc --proto_path=IMPORT_PATH --cpp_out=DST_DIR --java_out=DST_DIR --python_out=DST_DIR path/to/file.proto

  • IMPORT_PATH specifies a directory in which to look for .proto files when resolving import directives. If omitted, the current directory is used. Multiple import directories can be specified by passing the --proto_path option multiple times; they will be searched in order. -I=IMPORT_PATH can be used as a short form of --proto_path.   IMPORT_PATH声明了一个.proto文件所在的具体目录。如果忽略该值,则使用当前目录。如果有多个目录则可以 对--proto_path 写多次,它们将会顺序的被访问并执行导入。-I=IMPORT_PATH是它的简化形式。
  • You can provide one or more output directives:
    • --cpp_out generates C++ code in DST_DIR. See the C++ generated code reference for more.
    • --java_out generates Java code in DST_DIR. See the Java generated code reference for more.
    • --python_out generates Python code in DST_DIR. See the Python generated code reference for more.
    当然也可以提供一个或多个输出路径:--cpp_out 在目标目录DST_DIR中产生C++代码,--java_out 在目标目录DST_DIR中产生Java代码, --python_out 在目标目录 DST_DIR 中产生Python代码
  • As an extra convenience, if the DST_DIR ends in .zip or .jar, the compiler will write the output to a single ZIP-format archive file with the given name. .jar outputs will also be given a manifest file as required by the Java JAR specification. Note that if the output archive already exists, it will be overwritten; the compiler is not smart enough to add files to an existing archive.
  • You must provide one or more .proto files as input. Multiple .proto files can be specified at once. Although the files are named relative to the current directory, each file must reside in one of the IMPORT_PATHs so that the compiler can determine its canonical name.

11、

protoc [OPTION] PROTO_FILES 
protoc --proto_path=IMPORT_PATH --cpp_out=DST_DIR --java_out=DST_DIR --python_out=DST_DIR path/to/file.proto
实例:protoc --proto_path=./pb --cpp_out=./GenFileCpp --python_out=./GenFilePython ./pb/server.proto
这里将给出上述命令的参数解释。

  • protoc为ProtocolBuffer提供的命令行编译工具。
  •  --proto_path等同于-I选项,主要用于指定待编译的.proto消息定义文件所在的目录,该选项可以被同时指定多个。若某个.proto文件引入了别的.proto文件,则需要设置此选项。例如:command.proto: File not found.

PATH/server.proto: Import "command.proto" was not found or had errors.

protoc --proto_path=./pb --cpp_out=./GenFileCpp ./pb/server.proto
<<==>>protoc -I=./pb --cpp_out=./GenFileCpp ./pb/server.proto
<<==>>protoc -I./pb --cpp_out=./GenFileCpp ./pb/server.proto
<<==>>protoc.exe -I./pb --cpp_out=./GenFileCpp ./pb/server.proto

  •  --cpp_out选项表示生成C++代码,--java_out表示生成Java代码,--python_out则表示生成Python代码,其后的目录为生成后的代码所存放的目录。通过命令protoc -h可得知支持哪些语言,若生成js,lua的这需要其他的一些配置。
  •  path/to/file.proto表示待编译的消息定义文件。

注:对于C++而言,通过Protocol Buffer编译工具,可以将每个.proto文件生成出一对.h和.cc的C++代码文件。生成后的文件可以直接加载到应用程序所在的工程项目中。如:MyMessage.proto生成的文件为MyMessage.pb.h和MyMessage.pb.cc。

 

 

 

你可能感兴趣的:(c++,protobuf)