Using Protobuf in AS3

Source: Internet
Author: User

In the recently developed Adobe Air Project, the front and back end of the communication protocol from XML, JSON to PROTOBUF, and finally choose the protobuf reason, mainly the front and back end maintenance PROTOBUF protocol on the line, but also can use the IDE to do some compile check. Now I can find the Protobuf AS3 Open Source Library, there are some problems: nested classes are not supported, generated code can not compile and so on. It took a little time, referring to Google protobuf instructions, to write PROTOBUF-AS3 and PROTOC-AS3 to support runtime and code generation, HTTPS://GITHUB.COM/ZHONGFQ/PROTOBUF-AS3.

The PROTOBUF-AS3 library supports only the PROTO3 format, which is basically supported by the data types listed Proto3.

The following proto files will generate 5 AS3 classes: token.as, token$type.as, tokenbindingresponsecode.as, tokenbindingrequest.as, Tokenbindingresponse.as. We use ' $ ' as a delimiter to implement the nesting of the Protobuf class.

Syntax ="Proto3";p ackage User.token;message Token {enumType {NONE=0; QQ=1; WEIBO=2; WECHAT=3; } Type Type=1; stringValue =2;}enumTokenbindingresponsecode {ERROR=0; OK= $; EXIST= -;} Message Tokenbindingrequest {Token token=1;} Message tokenbindingresponse {Tokenbindingresponsecode Responsecode=1; Token token=2;}
var New  ="xxxxxx"; var New  = token;

PROTOBUF data does not contain any metadata, so for a piece of data, you cannot tell which protocol this data belongs to, so for the front-end interaction, I introduce a 12-byte header (length, session, type).

Package Network {Importflash.utils.bytearray;internalclassPacket {Private var_session:int; Private var_type:int; Private var_data:bytearray;  Public functionPacket (Session:int, Type:int, Data:bytearray) {_session=session; _type=type; _data=data; _data.position= 0; }     Public function Getsession (): int {return_session; }     Public function Gettype (): int {return_type; }     Public function Getdata (): ByteArray {return_data; }}}
Package Network {ImportFlash.net.Socket;ImportFlash.utils.ByteArray;Importflash.utils.endian;internalclassPacketbuffer {//length + Session + type    Private StaticConst HEADER_LENGTH:INT = 12; Private var_length:int = 0; Private var_position:int = 0; Private var_packet:packet =NULL;  Public functionPacketbuffer () {} Public functionPack (socket:socket):P acket {//message length of at least one, length + Session + type        if(socket.bytesavailable <= 0 | | (_length = = 0 && socket.bytesavailable <header_length)) {            return NULL; } Socket.endian=Endian.big_endian; if(_length = = 0) {_length=Socket.readint (); //valid Packet:length + Session + type >=            if(_length <header_length)                {Socket.close (); return NULL; }            varSession:int =Socket.readint (); varType:int =Socket.readint (); _position= 0; _packet=NewPacket (Session, type,NewByteArray ()); }        if(socket.bytesavailable > 0 | | _length = =header_length) {            varLen:int = Math.min (_length-header_length-_position, socket.bytesavailable);            Socket.readbytes (_packet.data, _position, Len); _position+=Len; if(_position = = _length-header_length) {                varRet:packet =_packet; _packet=NULL; _position= 0; _length= 0; returnret; }        }        return NULL; }}}
Syntax ="Proto3"; enum Commandcode {//GatewayGATEWAY = 0x0000; //Loginsign_in = 0x0100; Check_account= 0x0101; Sign_up= 0x0102; Quick_enroll= 0x0103; //UserUser_modifier = 0x0200; Token_binding= 0x0201; User_stat= 0x0202; User_avatar= 0x0203;}

The Commandcode is used as the type identifier for the packet, and for the type segment in the header above.

For sending a packet, the method is as follows:

Private function sendpacket (packet:packet):void  {    //  length + Session + type    _socket.endian = Endian.big_endian;     +);    _socket.writeint (packet.session);    _socket.writeint (packet.type);    _socket.writebytes (packet.data);    _socket.flush ();}

For receiving packets, the method is as follows:

Private function Socketdatahandler (event:progressevent):void  {    var  packet: Packet;      while NULL ) {        dispatchpacket (packet);    }}

Session variables are maintained by the server, the server each send a user data, session + 1, the front end of the disconnection, send session and service to compare, if the same, is to perform the disconnection, if not the same forced to walk the login process.

Using Protobuf in AS3

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.