Protocol Buffer

Google Protocol Buffer(Protobuf/proto3)

Wireshark protobuf dissector

  1. Message: 在protobuf的术语中,结构化数据被称为 Message
  2. Varints : Varints are a method of serializing integers using one or more bytes/Varints是使用一个或多个字节来序列化整数的一种方法。Varint 是一种紧凑的表示数字的方法。它用一个或多个字节来表示一个数字,值越小的数字使用越少的字节数。这能减少用来表示数字的字节数。
  3. Wire Type : 传输类型/message interchange format 消息交换格式

1 Zigzag Encoding

采用varints的方式编码有符号的整数,效率比较差,因为负数的最高位是1,这样就导致了情况类似于编码一个很大的数。

为了解决这个问题,Protocol Buffers定义了sint32/sint64属性,他们采用了“之字形”(ZigZag)编码的方式,将负数编码成正数,交替进行。看了下表就很好理解了:

Signed Original Encoded As
0 0
-1 1
1 2
-2 3
2 4

Each value n is encoded using

(n << 1) ^ (n >> 31) for sint32s, or

(n << 1) ^ (n >> 63) for the 64-bit version.

This might be easier to visualise in binary (8 bit here):

(-1 << 1) ^ (-1 >> 7) = 11111110 ^ 11111111= 00000001

  1. Syntax: The Proto version (or concept) used.
  2. Field Type: Specifies property types.
  3. Field Name: Specifies the property name. Each filed has a default value
  4. Field Tag: Sequential numbered tag. This specifies the order of the serialization of all the fields for the message Person. Usually ordering of tag will not be altered. For protocol buffer Filed Name can be optional. It is the application that uses Filed Name for getting/setting the values.

References

  1. Protobuf的那些事
  2. protobuf学习资料整理
  3. Protocol Buffers (part 3, advanced)
  4. Github/tcp+protobuf: Protobuf Socket RPC
  5. 详解Google-ProtoBuf中结构化数据的编码
  6. Protocol Buffers: Encoding and Message Structure(Negative Number说明)
  7. Protobuf data encoding for numeric datatypes
  8. Protocol Buffers编码详解,例子,图解
  9. Length-prefix framing for protocol buffers

results matching ""

    No results matching ""