标签:
在最近参与开发的adobe air项目中,前后端的通信协议从XML、JSON再到protobuf,最后选择protobuf原因,主要是前后端维护protobuf协议就行了,同时还可以利用IDE做一些编译检查。目前我能找到的protobuf as3开源库,都存在一些问题:不支持嵌套类,生成代码无法编译等等。于是花了一点时间,参考google protobuf相关说明,编写protobuf-as3以及protoc-as3,用于支持运行时及代码生成,https://github.com/zhongfq/protobuf-as3。
protobuf-as3库只支持proto3格式,proto3列出的数据类型基本都支持。
以下proto文件将生成5个as3类:Token.as、Token$Type.as、TokenBindingResponseCode.as、TokenBindingRequest.as、TokenBindingResponse.as。我们以‘$’作为分隔符以实现protobuf类的嵌套。
syntax = "proto3"; package user.token; message Token { enum Type { NONE = 0; QQ = 1; WEIBO = 2; WECHAT = 3; } Type type = 1; string value = 2; } enum TokenBindingResponseCode { ERROR = 0; OK = 200; EXIST = 400; } message TokenBindingRequest { Token token = 1; } message TokenBindingResponse { TokenBindingResponseCode responseCode = 1; Token token = 2; }
var token:Token = new Token(); token.type = Token$Type.QQ; token.value = "xxxxxx"; var req:TokenBindingRequest = new TokenBindingRequest(); req.token = token;
protobuf数据不包含任何的元数据,所以对于一段数据而言,你是不能够区别这段数据是属于哪一条协议的,因此对于前后端的交互,我引入12字节的头部(length、session、type)。
package network { import flash.utils.ByteArray; internal class Packet { private var _session:int; private var _type:int; private var _data:ByteArray; public function Packet(session:int, type:int, data:ByteArray) { _session = session; _type = type; _data = data; _data.position = 0; } public function get session():int { return _session; } public function get type():int { return _type; } public function get data():ByteArray { return _data; } } }
package network { import flash.net.Socket; import flash.utils.ByteArray; import flash.utils.Endian; internal class PacketBuffer { //length + session + type private static const HEADER_LENGTH:int = 12; private var _length:int = 0; private var _position:int = 0; private var _packet:Packet = null; public function PacketBuffer() { } public function pack(socket:Socket):Packet { // 消息长度至少为12, 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 >= 12 if (_length < HEADER_LENGTH) { socket.close(); return null; } var session:int = socket.readInt(); var type:int = socket.readInt(); _position = 0; _packet = new Packet(session, type, new ByteArray()); } if (socket.bytesAvailable > 0 || _length == HEADER_LENGTH) { var len:int = Math.min(_length - HEADER_LENGTH - _position, socket.bytesAvailable); socket.readBytes(_packet.data, _position, len); _position += len; if (_position == _length - HEADER_LENGTH) { var ret:Packet = _packet; _packet = null; _position = 0; _length = 0; return ret; } } return null; } } }
syntax = "proto3"; enum CommandCode { // gateway GATEWAY = 0x0000; // login SIGN_IN = 0x0100; CHECK_ACCOUNT = 0x0101; SIGN_UP = 0x0102; QUICK_ENROLL = 0x0103; // user USER_MODIFIER = 0x0200; TOKEN_BINDING = 0x0201; USER_STAT = 0x0202; USER_AVATAR = 0x0203; }
CommandCode作为数据包的类型标识,用于上面所说头部中的类型段。
对于发送一个数据包,方式如下:
private function sendPacket(packet:Packet):void { // length + session + type _socket.endian = Endian.BIG_ENDIAN; _socket.writeInt(packet.data.length + 12); _socket.writeInt(packet.session); _socket.writeInt(packet.type); _socket.writeBytes(packet.data); _socket.flush(); }
对于接收数据包,方式如下:
private function socketDataHandler(event:ProgressEvent):void { var packet:Packet; while ((packet = _packetBuffer.pack(_socket)) != null) { dispatchPacket(packet); } }
session变量由服务器维护,服务器每发送一条用户数据,session就+1,前端断线重连时,发送session与服务作对比,如果一样,就是执行断线重连,如果不一样就强制走登录流程。
标签:
原文地址:http://www.cnblogs.com/zhongfq/p/5663106.html