4. Google Protocol Buffers
¨ Protocol Buffers are a way of encoding structured
data in an efficient yet extensible format. Google
uses Protocol Buffers for almost all of its internal
RPC protocols and file formats.
5. Google Protocol Buffers
example.proto
message Person {
required string name = 1;
required int32 id = 2;
optional string email = 3;
enum PhoneType {
MOBILE = 0;
HOME = 1;
WORK = 2;
};
message PhoneNumber {
required string number = 1;
optional PhoneType type = 2 [default = HOME];
};
repeated PhoneNumber phone = 4;
}
11. Google Protocol Buffers
¨ 内存使用 - toByteArray()
public byte[] toByteArray() {
try {
final byte[] result = new byte[getSerializedSize()];
final CodedOutputStream output = CodedOutputStream.newInstance(result);
writeTo(output);
output.checkNoSpaceLeft();
return result;
} catch (IOException e) {
throw new RuntimeException(
"Serializing to a byte array threw an IOException " +
"(should never happen).", e);
}
}
19. Phone Management Protocol
¨ 普通服务
Respo
n
Head se
er
Token
t :
Reques Conte 12345**
r
Heade 2345*** vnd.p nt-Type: *
1 mp
Token: pmp://sms/ Conte .item/int
: n
Ser vice unt Statu t-Length:1
SCo
getSM DA_PC_XXX
X Body sCode:20
0
Client: 1 (pr
otobu
Body f seri
alize
d)
20. Phone Management Protocol
¨ 支持 Content-Range 的服务
Respo
n
Head se
er
t
Reques Token
:
r
Heade 2345*** Conte 12345**
1
Token: pmp://file/w
rite vnd.p nt-Type: *
mp
:
Ser vice _PC_XXXX /1400 Conte .item/int
n
DA
Client: Range: 0-10
23 Statu t-Length:4
Conten
t- Body sCode:20
tr> 0
B ody .pmp.item/s yte> 1024
(prot
<pa th, vnd .pmp.dir/b obuf
t, vnd seria
< conten lized
)
21. Phone Management Protocol
¨ 支持 Partial-Return 的服务 Respo
n
Head se
er
Token
:
t Conte 12345**
Reques app nt-Type: v *
r nd.pm
Heade 2345*** Conte p.dir/
1 list
Token: pmp://app/ Statu
nt-Le
n
:
Ser vice _PC_XXXX sCod gth:10204
Conte e:2
Client:
DA nt-Ra 06 X 30
int> Body nge:
Body .pm p.item/ 0-9/
y pe, vnd apps 300
<appT (prot
obuf
seria
lized
Conte )
nt-Ty
Conte pe
nt-Le : vnd.pmp
Statu n .dir/i
Body sCod gth:4 nt
e:200
Body
300
(prot
obuf
seria
lized
22. Phone Management Protocol
¨ 支持 Keep-Alive 的服务 Respo
n
Head se
er
Token
:
Conte 12345**
Reques
t notifi nt-Type: v *
r cat nd.pm
Heade 2345*** ait Conte ion p.dir/
1 c ation/w nt-Le
Token: pmp://notifi Statu n
: sCod gth:1024
Ser vice _PC_XXXX Conte e:2
DA nt-Ra 06 X ttl
Client: Body nge:
nt> 0-1/
Body .pmp.item/i notifi
catio
ttl
< ttl, vnd n (pro
tobuf
seria
Conte ..
Conte nt-Type: v
n n
Statu t-Length:4 d.pmp.dir
Body sCode:20 /int
0
ttl (p
rotob
uf se
rializ
ed