Esempio n. 1
0
    /// <summary>
    /// 解析主函数
    /// </summary>
    /// <param name="data">字节流数据</param>
    /// <returns></returns>
    public CxProtobufData Parser(byte[] data)
    {
        CxProtobufData pbData = new CxProtobufData();

        Log("-----------------开始解析-------------------");
        //读取二进制数据
        for (int i = 0; i < data.Length; i++)
        {
            Log(Convert.ToString(data[i], 10).PadLeft(3, ' ') + " : " + Convert.ToString(data[i], 2).PadLeft(8, '0'));
        }

        //构建字节流
        BufferedStream bs = new BufferedStream(new MemoryStream());

        bs.Write(data, 0, data.Length);
        bs.Position = 0;

        int times = 0;

        //解析
        while (bs.Position < bs.Length)
        {
            ParserOne(bs, pbData);
            times++;
            if (times > 100)
            {
                Log("!!!!!!!!解析超过100条数据,查看是否数据错误!!!!!!!!!");
                break;
            }
        }

        Log(string.Format("canRead:{0},position:{1},length:{2}", bs.CanRead, bs.Position, bs.Length));
        Log("-----------------完成解析-------------------");
        return(pbData);
    }
Esempio n. 2
0
    /// <summary>
    /// 添加数据
    /// </summary>
    /// <param name="fieldIndex"></param>
    /// <param name="data"></param>
    public void Add(int fieldIndex, object data)
    {
        //数组repeated处理
        if (_table.ContainsKey(fieldIndex))
        {
            List <CxProtobufData> list;
            if (_table[fieldIndex] is List <CxProtobufData> )
            {
                list = _table[fieldIndex] as List <CxProtobufData>;
            }
            else
            {
                list = new List <CxProtobufData>();
                CxProtobufData lastData = new CxProtobufData();
                lastData.Add(1, _table[fieldIndex]);
                list.Add(lastData);
                _table[fieldIndex] = list;
            }

            CxProtobufData newData = new CxProtobufData();
            newData.Add(1, data);
            list.Add(newData);
        }
        else
        {
            _table.Add(fieldIndex, data);
        }
    }
Esempio n. 3
0
    /// <summary>
    /// 解析一个数据
    /// </summary>
    /// <param name="bs">字节流</param>
    /// <param name="table">存入table</param>
    private void ParserOne(BufferedStream bs, CxProtobufData pbData)
    {
        object obj = null;
        //解析key
        int key      = (int)GetVarint(bs);
        int wireType = GetVarint_WireType(key);
        int filedNum = GetVarint_FieldNum(key);

        Log(String.Format("key:{0} wireType:{1} filedNum:{2}", key, wireType, filedNum));

        /*-------------------------------------------------------------------------*
        *   消息类型对应表
        *   0	Varint	int32, int64, uint32, uint64, sint32, sint64, bool, enum
        *   1	64-bit	fixed64, sfixed64, double
        *   2	Length-delimited	string, bytes, embedded messages, packed repeated fields
        *   3	Start group	groups (deprecated)
        *   4	End group	groups (deprecated)
        *   5	32-bit	fixed32, sfixed32, float
        *-------------------------------------------------------------------------*/
        //解析内容
        switch (wireType)
        {
        case 0:    //Varint:	int32, int64, uint32, uint64, sint32, sint64, bool, enum
            obj = GetVarint(bs);
            Log("》》Varint:" + obj);
            break;

        case 1:    //64-bit:	fixed64, sfixed64, double

            break;

        case 2:    //Length-delimited:	string, bytes, embedded messages, packed repeated fields
            obj = GetBytes(bs);
            Log("》》Length-delimited:" + obj);
            break;

        case 3:    //Start group	groups (deprecated)

            break;

        case 4:    //End group	groups (deprecated)

            break;

        case 5:    //32-bit	fixed32, sfixed32, float
            obj = Get32Bit(bs);
            Log("》》32-bit:" + obj);
            break;
        }
        pbData.Add(filedNum, obj);
    }
Esempio n. 4
0
    /// <summary>
    /// 获得repeated标记数组
    /// </summary>
    /// <param name="fieldIndex"></param>
    /// <returns></returns>
    public List <CxProtobufData> GetRepeated(int fieldIndex)
    {
        List <CxProtobufData> list = new List <CxProtobufData>();

        if (HaveField(fieldIndex))
        {
            //如果只有一个数据不会构建list
            if (_table[fieldIndex] is List <CxProtobufData> )
            {
                list = _table[fieldIndex] as List <CxProtobufData>;
            }
            else
            {
                list = new List <CxProtobufData>();
                CxProtobufData lastData = new CxProtobufData();
                lastData.Add(1, _table[fieldIndex]);
                list.Add(lastData);
                _table[fieldIndex] = list;
            }
        }
        return(list);
    }