socket解包-as3方式

protected function onProgressData(event:ProgressEvent):void
    {
        _buffer.position = _bufferPosition;
        readBytes( _buffer , _buffer.length , bytesAvailable );

        while( _buffer.bytesAvailable > 1 )
        {
            var leftDataLength:int = _buffer.length - _bufferPosition;
            var msgDataLength:int = _buffer.readShort();
            if( leftDataLength >= msgDataLength )
            {
                var byteArray:ByteArray = new ByteArray();
                byteArray.endian = Endian.LITTLE_ENDIAN;
                _buffer.position = _bufferPosition;
                _buffer.readBytes( byteArray , 0 , msgDataLength);
                parseOneMessageBytes( byteArray );
                _bufferPosition += msgDataLength;//指针增加已读数据
            }
            else
            {//如果不够一个消息的长度,等待下个进度事件时再拼装
                break;
            }
        }
    }

    /**
     *      解析原始消息数据,并做分发 
     * @param bytes
     * 
     */     
    protected function parseOneMessageBytes( bytes:ByteArray ):void
    {
        bytes.endian = Endian.LITTLE_ENDIAN;
        var len:int;
        var msgId:int;
        var check:int;
        var rawData:ByteArray;
        try
        {
            len = bytes.readShort();        //2 Bytes
            msgId = bytes.readShort();      //2 Bytes
            check = bytes.readInt();        //4 Bytes
        }
        catch(e:Error)
        {
            throw new IllegalOperationError("wrong protocal header!");
        }
        rawData = new ByteArray();
        rawData.endian = Endian.LITTLE_ENDIAN;
        bytes.readBytes( rawData , 0 , len - 8 );
        rawData.position = 0;

        var decodeInfos:Array = _decodeMessages[ msgId ];
        if( decodeInfos == null )
        {
            dispatchEvent( new NetEvent( NetEvent.NO_REGISTED_DECODE_CLASS_FOR_MSG , msgId) );
        }
        //挨个通知解码类
        for each (var decodeInfo:HttpDecodeClassVo in decodeInfos) 
        {
            var decodeClass:Class = decodeInfo.decodeClass;
            var msg:BaseMsg = new decodeClass();
            try{
                msg.decode( rawData );
            }
            catch( error:Error )
            {
                if( error.errorID == 2030 )
                    throw new IllegalOperationError( "消息" + msgId + "解析时遇到文件尾! ");
            }
            msg.traceBinary( msgId , rawData );
            if( rawData.bytesAvailable )
            {
                dispatchEvent( new NetEvent( NetEvent.RAW_DATA_LEFT , msgId) );
            }
            else
            {
                if( msg.errorId )
                {
                    var msgErrorVo:MsgErrorVo = new MsgErrorVo();
                    msgErrorVo.msgId = msgId;
                    msgErrorVo.errorId = msg.errorId;
                    dispatchEvent( new NetEvent( NetEvent.MSG_ERROR , msgErrorVo ) );
                }
            }
            if(msg.errorId == 0 || decodeInfo.allowError)
            {
                if( decodeInfo.callbackFunction != null )
                {
                    decodeInfo.callbackFunction( msg );
                }
            }
            rawData.position = 0;
        }
    }

标签: none