22
基基 基基 Google protobuf Google protobuf webgame webgame 基基基基基基 基基基基基基 赖赖赖 http://laiyonghao.com 2010.8.28

基于 Google protobuf 的 webgame 网络协议设计

  • Upload
    -

  • View
    9.232

  • Download
    12

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: 基于 Google protobuf 的 webgame 网络协议设计

基于 基于 Google protobuf Google protobuf 的 的 wwebgame ebgame 网络协议设计网络协议设计赖勇浩

http://laiyonghao.com2010.8.28

Page 2: 基于 Google protobuf 的 webgame 网络协议设计

Google Protopuf 2.3• http://code.google.com/p/protobuf/• 二进制协议描述语言• C++• Java• Python• other languages

Page 3: 基于 Google protobuf 的 webgame 网络协议设计

人生苦短,我用 人生苦短,我用 PythonPython 。。

Page 4: 基于 Google protobuf 的 webgame 网络协议设计

基本流程• Define message formats in a .proto

file.• Use the protocol buffer compiler.• Use the Python protocol buffer API to

write and read messages.

Page 5: 基于 Google protobuf 的 webgame 网络协议设计

Quick Example• message Person {• required int32 id = 1;• required string name = 2;• optional string email = 3;• }

Page 6: 基于 Google protobuf 的 webgame 网络协议设计

Use the protocol buffer compiler.

• protoc -I=$SRC_DIR --python_out=$DST_DIR $SRC_DIR/example.proto

Page 7: 基于 Google protobuf 的 webgame 网络协议设计

• class Person(message.Message):• __metaclass__ =

reflection.GeneratedProtocolMessageType• DESCRIPTOR = _PERSON

Page 8: 基于 Google protobuf 的 webgame 网络协议设计

Python protocol buffer API• person = Person()• person.id = 10• person.name = 'lai'• person.email = '[email protected]'• with open('person.data', 'wb') as f:• print >>f, person.SerializeToString()

Page 9: 基于 Google protobuf 的 webgame 网络协议设计

Exception• person.no_such_field = 1 # raises

AttributeError• person.id = "1234" # raises

TypeError

Page 10: 基于 Google protobuf 的 webgame 网络协议设计

• with open('example.data', 'rb') as f:• data = f.read()• person = Person()• person.ParseFromString(data)• assert person.id == 10• assert person.name == 'lai'• if person.hasField('email'):• assert person.email == 'mail@laiy...'

Page 11: 基于 Google protobuf 的 webgame 网络协议设计

ActionScript 3.0• http://code.google.com/p/protoc-gen-as3/• It has most protobuf 2.3 features, more than any other protobuf's as3 implementation.• And you can use (as3_bindable) option to generate classes with Bindable metadata tag.• 国人开发(网易杭研杨博)• http://hi.baidu.com/atry/

Page 12: 基于 Google protobuf 的 webgame 网络协议设计

How to use it?• protoc --plugin=protoc-gen-

as3=path/to/protoc-gen-as3[.bat] --as3_out=output-path your.proto

Page 13: 基于 Google protobuf 的 webgame 网络协议设计

目标• as3

– var sock:SdSocket = new SdSocket(...);– var p:Person = new Person();– ...– sock.send(p);

• python– sock = SdSocket(...)– ...– person = sock.recv()– print person.id, person.name

Page 14: 基于 Google protobuf 的 webgame 网络协议设计

问题• 客户端 / 服务器端怎么知道对方发过来的包是 Person ?

Page 15: 基于 Google protobuf 的 webgame 网络协议设计

好问题!• 需要设计一个 header• message Header {• required uint32 length = 1;• required uint32 msg_id = 2;• }

Page 16: 基于 Google protobuf 的 webgame 网络协议设计

我在代码里没有看到 msg_id !• Person 的 msg_id 是什么?• msg_id <=> hash(Person.DESCRIPTOR.full_name)• 需要一个极佳的 hash 函数(完全无冲突)• 额外的好处:防外挂(一点额外的操作)

Page 17: 基于 Google protobuf 的 webgame 网络协议设计

消息分发是一件操蛋的事!消息分发是一件操蛋的事!

Page 18: 基于 Google protobuf 的 webgame 网络协议设计

• svr = TcpServer(Handler(Protocol('full_name_map_msg_id.xml')))• svr.run_forever()

Page 19: 基于 Google protobuf 的 webgame 网络协议设计

更进一步• class Handler extends Object {

– public function _handle_Person(p:Person):void {• trace(p.id.toString());• trace(p.name);• if(p.hasEmail()){

– trace(p.email);• }

– }• }

Page 20: 基于 Google protobuf 的 webgame 网络协议设计

• class Handler(object):– def _handle_Person(self, person):

• print person.id, person.name• if(person.hasField('email')):print person.email

Page 21: 基于 Google protobuf 的 webgame 网络协议设计

开始像 开始像 RPC RPC 了?了?对的,这是我们更高的目标。

Page 22: 基于 Google protobuf 的 webgame 网络协议设计

Thanks all !Thanks all !Q&A

别提性能问题。