基于 Google protobuf 的 webgame 网络协议设计
Upcoming SlideShare
Loading in...5
×

Like this? Share it with your network

Share

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

  • 9,621 views
Uploaded on

 

More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
No Downloads

Views

Total Views
9,621
On Slideshare
9,619
From Embeds
2
Number of Embeds
1

Actions

Shares
Downloads
142
Comments
1
Likes
23

Embeds 2

http://www.mefeedia.com 2

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. 基于 Google protobuf 的 webgame 网络协议设计 赖勇浩 http://laiyonghao.com 2010.8.28
  • 2. Google Protopuf 2.3
    • http://code.google.com/p/protobuf/
    • 二进制协议描述语言
    • C++
    • Java
    • Python
    • other languages
  • 3. 人生苦短,我用 Python 。
  • 4. 基本流程
    • Define message formats in a .proto file.
    • Use the protocol buffer compiler.
    • Use the Python protocol buffer API to write and read messages.
  • 5. Quick Example
    • message Person {
    • required int32 id = 1;
    • required string name = 2;
    • optional string email = 3;
    • }
  • 6. Use the protocol buffer compiler.
    • protoc -I=$SRC_DIR --python_out=$DST_DIR $SRC_DIR/example.proto
  • 7.
    • class Person(message.Message):
    • __metaclass__ = reflection.GeneratedProtocolMessageType
    • DESCRIPTOR = _PERSON
  • 8. Python protocol buffer API
    • person = Person()
    • person.id = 10
    • person.name = 'lai'
    • person.email = 'mail@laiyonghao.com'
    • with open('person.data', 'wb') as f:
    • print >>f, person.SerializeToString()
  • 9. Exception
    • person.no_such_field = 1 # raises AttributeError
    • person.id = "1234" # raises TypeError
  • 10.
    • 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...'
  • 11. 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/
  • 12. How to use it?
    • protoc --plugin=protoc-gen-as3=path/to/protoc-gen-as3[.bat] --as3_out=output-path your.proto
  • 13. 目标
    • 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
  • 14. 问题
    • 客户端 / 服务器端怎么知道对方发过来的包是 Person ?
  • 15. 好问题!
    • 需要设计一个 header
    • message Header {
    • required uint32 length = 1;
    • required uint32 msg_id = 2;
    • }
  • 16. 我在代码里没有看到 msg_id !
    • Person 的 msg_id 是什么?
    • msg_id <=> hash(Person.DESCRIPTOR.full_name)
    • 需要一个极佳的 hash 函数(完全无冲突)
    • 额外的好处:防外挂(一点额外的操作)
  • 17. 消息分发是一件操蛋的事!
  • 18.
    • svr = TcpServer(Handler(Protocol('full_name_map_msg_id.xml')))
    • svr.run_forever()
  • 19. 更进一步
    • class Handler extends Object {
      • public function _handle_Person(p:Person):void {
        • trace(p.id.toString());
        • trace(p.name);
        • if(p.hasEmail()){
          • trace(p.email);
        • }
      • }
    • }
  • 20.
    • class Handler(object):
      • def _handle_Person(self, person):
        • print person.id, person.name
        • if(person.hasField('email')):print person.email
  • 21. 开始像 RPC 了? 对的,这是我们更高的目标。
  • 22. Thanks all ! Q&A 别提性能问题。