基于 Google protobuf 的 webgame 网络协议设计
-
Upload
- -
Category
Technology
-
view
9.237 -
download
12
description
Transcript of 基于 Google protobuf 的 webgame 网络协议设计
![Page 1: 基于 Google protobuf 的 webgame 网络协议设计](https://reader031.fdocuments.net/reader031/viewer/2022012309/540686c78d7f72d8088b45eb/html5/thumbnails/1.jpg)
基于 基于 Google protobuf Google protobuf 的 的 wwebgame ebgame 网络协议设计网络协议设计赖勇浩
http://laiyonghao.com2010.8.28
![Page 2: 基于 Google protobuf 的 webgame 网络协议设计](https://reader031.fdocuments.net/reader031/viewer/2022012309/540686c78d7f72d8088b45eb/html5/thumbnails/2.jpg)
Google Protopuf 2.3• http://code.google.com/p/protobuf/• 二进制协议描述语言• C++• Java• Python• other languages
![Page 3: 基于 Google protobuf 的 webgame 网络协议设计](https://reader031.fdocuments.net/reader031/viewer/2022012309/540686c78d7f72d8088b45eb/html5/thumbnails/3.jpg)
人生苦短,我用 人生苦短,我用 PythonPython 。。
![Page 4: 基于 Google protobuf 的 webgame 网络协议设计](https://reader031.fdocuments.net/reader031/viewer/2022012309/540686c78d7f72d8088b45eb/html5/thumbnails/4.jpg)
基本流程• 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 网络协议设计](https://reader031.fdocuments.net/reader031/viewer/2022012309/540686c78d7f72d8088b45eb/html5/thumbnails/5.jpg)
Quick Example• message Person {• required int32 id = 1;• required string name = 2;• optional string email = 3;• }
![Page 6: 基于 Google protobuf 的 webgame 网络协议设计](https://reader031.fdocuments.net/reader031/viewer/2022012309/540686c78d7f72d8088b45eb/html5/thumbnails/6.jpg)
Use the protocol buffer compiler.
• protoc -I=$SRC_DIR --python_out=$DST_DIR $SRC_DIR/example.proto
![Page 7: 基于 Google protobuf 的 webgame 网络协议设计](https://reader031.fdocuments.net/reader031/viewer/2022012309/540686c78d7f72d8088b45eb/html5/thumbnails/7.jpg)
• class Person(message.Message):• __metaclass__ =
reflection.GeneratedProtocolMessageType• DESCRIPTOR = _PERSON
![Page 8: 基于 Google protobuf 的 webgame 网络协议设计](https://reader031.fdocuments.net/reader031/viewer/2022012309/540686c78d7f72d8088b45eb/html5/thumbnails/8.jpg)
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 网络协议设计](https://reader031.fdocuments.net/reader031/viewer/2022012309/540686c78d7f72d8088b45eb/html5/thumbnails/9.jpg)
Exception• person.no_such_field = 1 # raises
AttributeError• person.id = "1234" # raises
TypeError
![Page 10: 基于 Google protobuf 的 webgame 网络协议设计](https://reader031.fdocuments.net/reader031/viewer/2022012309/540686c78d7f72d8088b45eb/html5/thumbnails/10.jpg)
• 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 网络协议设计](https://reader031.fdocuments.net/reader031/viewer/2022012309/540686c78d7f72d8088b45eb/html5/thumbnails/11.jpg)
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 网络协议设计](https://reader031.fdocuments.net/reader031/viewer/2022012309/540686c78d7f72d8088b45eb/html5/thumbnails/12.jpg)
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 网络协议设计](https://reader031.fdocuments.net/reader031/viewer/2022012309/540686c78d7f72d8088b45eb/html5/thumbnails/13.jpg)
目标• 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 网络协议设计](https://reader031.fdocuments.net/reader031/viewer/2022012309/540686c78d7f72d8088b45eb/html5/thumbnails/14.jpg)
问题• 客户端 / 服务器端怎么知道对方发过来的包是 Person ?
![Page 15: 基于 Google protobuf 的 webgame 网络协议设计](https://reader031.fdocuments.net/reader031/viewer/2022012309/540686c78d7f72d8088b45eb/html5/thumbnails/15.jpg)
好问题!• 需要设计一个 header• message Header {• required uint32 length = 1;• required uint32 msg_id = 2;• }
![Page 16: 基于 Google protobuf 的 webgame 网络协议设计](https://reader031.fdocuments.net/reader031/viewer/2022012309/540686c78d7f72d8088b45eb/html5/thumbnails/16.jpg)
我在代码里没有看到 msg_id !• Person 的 msg_id 是什么?• msg_id <=> hash(Person.DESCRIPTOR.full_name)• 需要一个极佳的 hash 函数(完全无冲突)• 额外的好处:防外挂(一点额外的操作)
![Page 17: 基于 Google protobuf 的 webgame 网络协议设计](https://reader031.fdocuments.net/reader031/viewer/2022012309/540686c78d7f72d8088b45eb/html5/thumbnails/17.jpg)
消息分发是一件操蛋的事!消息分发是一件操蛋的事!
![Page 18: 基于 Google protobuf 的 webgame 网络协议设计](https://reader031.fdocuments.net/reader031/viewer/2022012309/540686c78d7f72d8088b45eb/html5/thumbnails/18.jpg)
• svr = TcpServer(Handler(Protocol('full_name_map_msg_id.xml')))• svr.run_forever()
![Page 19: 基于 Google protobuf 的 webgame 网络协议设计](https://reader031.fdocuments.net/reader031/viewer/2022012309/540686c78d7f72d8088b45eb/html5/thumbnails/19.jpg)
更进一步• 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 网络协议设计](https://reader031.fdocuments.net/reader031/viewer/2022012309/540686c78d7f72d8088b45eb/html5/thumbnails/20.jpg)
• 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 网络协议设计](https://reader031.fdocuments.net/reader031/viewer/2022012309/540686c78d7f72d8088b45eb/html5/thumbnails/21.jpg)
开始像 开始像 RPC RPC 了?了?对的,这是我们更高的目标。
![Page 22: 基于 Google protobuf 的 webgame 网络协议设计](https://reader031.fdocuments.net/reader031/viewer/2022012309/540686c78d7f72d8088b45eb/html5/thumbnails/22.jpg)
Thanks all !Thanks all !Q&A
别提性能问题。