`

c# - Google Protocol Buffer, write proto files

    博客分类:
  • C#
c# 
阅读更多

You specify how you want the information you're serializing to be structured by defining protocol buffer message types in .proto files. Each protocol buffer message is a small logical record of information, containing a series of name-value pairs. The detailed language guide can be found 

 


As a walk-through, we create an Echo message which has three fields.

content: type is strnig and multiplicity is [1..1]
value: type is int32 and multiplicity is [0..1]
value_array: type is int32 and multiplicity is [0..*]

so with this in mind, you may come up with the following definition.

    package sample;
    
    message Echo {
      required string content = 1;
      optional int32 value = 2;
      repeated int32 value_array = 3;
    }
 

and one word on the union approach.


A key difference between GPB and XML is that GPB messages are not self describing. This means that the process receiving the message has to know what type it is before parsing. While there are cases where there is only one type of message being sent on a connection, the majority of applications send and receive multiple types of messages. So how do you make sure that the recipient application knows how to decode your message? One solution is to create a wrapper message that has one optional field for each possible message type. For example, if you have message types Foo, Bar, and Baz, you can combine them with a type like:

 message OneMessage {
      optional Foo foo = 1;
      optional Bar bar = 2;
      optional Echo echo = 3;
    }
 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics