ºÝºÝߣ

ºÝºÝߣShare a Scribd company logo
»ùÓÚ  Google protobuf  µÄ  webgame  ÍøÂçЭÒéÉè¼Æ ÀµÓ弮 http://laiyonghao.com 2010.8.28
Google Protopuf 2.3 http://code.google.com/p/protobuf/ ¶þ½øÖÆЭÒéÃèÊöÓïÑÔ C++ Java Python other languages
ÈËÉú¿à¶Ì£¬ÎÒÓà  Python ¡£
»ù±¾Á÷³Ì Define message formats in a .proto file. Use the protocol buffer compiler. Use the Python protocol buffer API to write and read messages.
Quick Example message Person { required int32 id = 1; required string name = 2; optional string email = 3; }
Use the protocol buffer compiler. protoc -I=$SRC_DIR --python_out=$DST_DIR $SRC_DIR/example.proto
class Person(message.Message): __metaclass__ = reflection.GeneratedProtocolMessageType DESCRIPTOR = _PERSON
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()
Exception person.no_such_field = 1  # raises AttributeError person.id = "1234"  # raises TypeError
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...'
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./atry/
How to use it? protoc --plugin=protoc-gen-as3=path/to/protoc-gen-as3[.bat] --as3_out=output-path your.proto
Ä¿±ê 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
ÎÊÌâ ¿Í»§¶Ë / ·þÎñÆ÷¶ËÔõô֪µÀ¶Ô·½·¢¹ýÀ´µÄ°üÊÇ  Person £¿
ºÃÎÊÌ⣡ ÐèÒªÉè¼ÆÒ»¸ö  header message Header { required uint32 length = 1; required uint32 msg_id = 2; }
ÎÒÔÚ´úÂëÀïûÓп´µ½  msg_id £¡ Person  µÄ  msg_id  ÊÇʲô£¿ msg_id <=> hash(Person.DESCRIPTOR.full_name) ÐèÒªÒ»¸ö¼«¼ÑµÄ  hash  º¯Êý£¨ÍêÈ«ÎÞ³åÍ»£© ¶îÍâµÄºÃ´¦£º·ÀÍâ¹Ò£¨Ò»µã¶îÍâµÄ²Ù×÷£©
ÏûÏ¢·Ö·¢ÊÇÒ»¼þ²Ùµ°µÄÊ£¡
svr = TcpServer(Handler(Protocol('full_name_map_msg_id.xml'))) svr.run_forever()
¸ü½øÒ»²½ class Handler extends Object { public function _handle_Person(p:Person):void { trace(p.id.toString()); trace(p.name); if(p.hasEmail()){ trace(p.email); } } }
class Handler(object): def _handle_Person(self, person): print person.id, person.name if(person.hasField('email')):print person.email
¿ªÊ¼Ïñ  RPC  ÁË£¿ ¶ÔµÄ£¬ÕâÊÇÎÒÃǸü¸ßµÄÄ¿±ê¡£
Thanks all ! Q&A ±ðÌáÐÔÄÜÎÊÌâ¡£

More Related Content

»ùÓÚ Google protobuf µÄ webgame ÍøÂçЭÒéÉè¼Æ