name length:消息名长度,占用4字节!大端!(注意大小端只是针对于number类型,而且大小端转换基本无开销)
name: 消息名,utf8编码
seq id: 占用四字节,大端!(一般rpc多路复用都有,因为要并发发送请求么,不能同一个连接 发送接收完 A 再发送接收 B请求, 像Http1.1就是PingPong协议,HTTP2也是有一个seq id ,这东西做的简单点就全局自增一个id即可!)
这里补充下位运算,位运算中 &一般作用就是取值, |一般作用就是Set值!
2. 消息协议二 (旧编码,非严格模式)
这个假如客户端/server端开启了严格模式,则不能兼容次协议
1 2 3 4
Binary protocol Message, old encoding, 9+ bytes: +--------+--------+--------+--------+--------+...+--------+--------+--------+--------+--------+--------+ | name length | name |00000mmm| seq id | +--------+--------+--------+--------+--------+...+--------+--------+--------+--------+--------+--------+
name length(四字节): 这里为了兼容上面协议一,所以高位第一个bit必须为0!也就是name length必须要有符号的正数!
Binary protocol field header and field value: +--------+--------+--------+--------+...+--------+ |tttttttt| field id | field value | +--------+--------+--------+--------+...+--------+
# 字段类型 BOOL, encoded as 2 I8, encoded as 3 DOUBLE, encoded as 4 I16, encoded as 6 I32, encoded as 8 I64, encoded as 10 BINARY, used for binary and string fields, encoded as 11 STRUCT, used for structs and union fields, encoded as 12 MAP, encoded as 13 SET, encoded as 14 LIST, encoded as 15
2. list / set
1 2 3 4
Binary protocol list (5+ bytes) and elements: +--------+--------+--------+--------+--------+--------+...+--------+ |tttttttt| size | elements | +--------+--------+--------+--------+--------+--------+...+--------+
tttttttt表示元素类型,编码为 int8
size 表示全部元素个数,编码为 int32,仅正值
elements 全部元素,顺序排列
3. map
1 2 3 4
Binary protocol map (6+ bytes) and key value pairs: +--------+--------+--------+--------+--------+--------+--------+...+--------+ |kkkkkkkk|vvvvvvvv| size | key value pairs | +--------+--------+--------+--------+--------+--------+--------+...+--------+
kkkkkkkk是关键元素类型,编码为 int8
vvvvvvvv是值元素类型,编码为 int8
size是 map的size,编码为 int32,仅正值
key value pairs是编码的键和值,意思就是先读key,再读value,再读key,再读value
第二个字节: version + type, 其中version是低5bit,type是高3bit , 其中 COMPACT_VERSION = 1
seq id 4字节,var int 编码
name len 为4字节,也是var int 编码
name: 消息名称
1 2 3 4 5
// 消息类型 Call: 1 Reply: 2 Exception: 3 Oneway: 4
2. 类型编码协议
1. struct
1 2 3 4 5 6 7 8 9 10 11 12 13 14
Compact protocol field header (short form) and field value: +--------+--------+...+--------+ |ddddtttt| field value | +--------+--------+...+--------+
Compact protocol field header (1 to 3 bytes, long form) and field value: +--------+--------+...+--------+--------+...+--------+ |0000tttt| field id | field value | +--------+--------+...+--------+--------+...+--------+
# 字段的类型,其中下面的 map/list 类型,也很简单就是可以把BOOLEAN_FALSE当作BOOL类型即可,就不重复写了! BOOLEAN_TRUE, encoded as 1 BOOLEAN_FALSE, encoded as 2 I8, encoded as 3 I16, encoded as 4 I32, encoded as 5 I64, encoded as 6 DOUBLE, encoded as 7 BINARY, used for binary and string fields, encoded as 8 LIST, encoded as 9 SET, encoded as 10 MAP, encoded as 11 STRUCT, used for both structs and union fields, encoded as 12
Compact protocol list header (1 byte, short form) and elements: +--------+--------+...+--------+ |sssstttt| elements | +--------+--------+...+--------+
Compact protocol list header (2+ bytes, long form) and elements: +--------+--------+...+--------+--------+...+--------+ |1111tttt| size | elements | +--------+--------+...+--------+--------+...+--------+
0 1 2 3 4 5 6 7 8 9 a b c d e f 0 1 2 3 4 5 6 7 8 9 a b c d e f +----------------------------------------------------------------+ | 0| LENGTH | +----------------------------------------------------------------+ | 0| HEADER MAGIC | FLAGS | +----------------------------------------------------------------+ | SEQUENCE NUMBER | +----------------------------------------------------------------+ | 0| Header Size(/32) | ... +---------------------------------
Header is of variable size: (and starts at offset 14)
+----------------------------------------------------------------+ | PROTOCOL ID (varint) | NUM TRANSFORMS (varint) | +----------------------------------------------------------------+ | TRANSFORM 0 ID (varint) | TRANSFORM 0 DATA ... +----------------------------------------------------------------+ | ... ... | +----------------------------------------------------------------+ | INFO 0 ID (varint) | INFO 0 DATA ... +----------------------------------------------------------------+ | ... ... | +----------------------------------------------------------------+ | | | PAYLOAD | | | +----------------------------------------------------------------+