Пример #1
0
            public bool DecodeArrayElement(DxArrayValue array, int eleIndex)
            {
                MsgPackCode code;

                if (!ReadCode(out code))
                {
                    return(false);
                }
                if (IsStr(code))
                {
                    string strvalue;
                    if (!DecodeString(code, out strvalue))
                    {
                        return(false);
                    }
                    array.SetString(eleIndex, strvalue);
                }
                else if (IsFixedNum(code))
                {
                    array.SetInt(eleIndex, (int)(sbyte)code);
                }
                else if (IsInt(code))
                {
                    long longvalue;
                    if (!DecodeInt(code, out longvalue))
                    {
                        return(false);
                    }
                    if (longvalue > int.MaxValue || longvalue < int.MinValue)
                    {
                        array.SetInt64(eleIndex, longvalue);
                    }
                    else
                    {
                        array.SetInt(eleIndex, (int)longvalue);
                    }
                }
                else if (IsMap(code))
                {
                    DxRecordValue mapvalue;
                    if (!DecodeMap(code, out mapvalue))
                    {
                        return(false);
                    }
                    array[eleIndex] = mapvalue;
                }
                else if (IsArray(code))
                {
                    DxArrayValue result = new DxArrayValue();
                    if (!Decode2Array(code, result))
                    {
                        return(false);
                    }
                    array[eleIndex] = result;
                }
                else if (IsBin(code))
                {
                    byte[] bytevalue;
                    if (!DecodeBinary(code, out bytevalue))
                    {
                        return(false);
                    }
                    DxBinaryValue binaryvalue = new DxBinaryValue();
                    binaryvalue.Bytes = bytevalue;
                    array[eleIndex]   = binaryvalue;
                }
                else if (IsExt(code))
                {
                    byte[] bytevalue;
                    if (!DecodeExtValue(code, out bytevalue))
                    {
                        return(false);
                    }
                    if (bytevalue.Length == 13 && (sbyte)bytevalue[0] == -1) //96位日期格式
                    {
                        //32位纳秒,64位秒
                        Stream stream = new MemoryStream(bytevalue);
                        stream.Position = 1;
                        uint  nsec = BigEndian.Uint32(stream);
                        ulong sec  = BigEndian.Uint64(stream);
                        stream.Dispose();
                        System.DateTime dateTime = new System.DateTime(1970, 1, 1);
                        double          sec1     = nsec / (1000 * 1000);
                        array[eleIndex] = new DxDateTimeValue(dateTime.AddSeconds(sec + sec1));
                    }
                    else
                    {
                        array[eleIndex] = new DxExtValue(bytevalue);
                    }
                }
                else
                {
                    switch (code)
                    {
                    case MsgPackCode.CodeTrue:
                        array.SetBool(eleIndex, true);
                        return(true);

                    case MsgPackCode.CodeFalse:
                        array.SetBool(eleIndex, false);
                        return(true);

                    case MsgPackCode.CodeNil:
                        array.SetNull(eleIndex);
                        return(true);

                    case MsgPackCode.CodeFloat:
                        uint v = BigEndian.Uint32(buffer);
                        array.SetFloat(eleIndex, (float)System.BitConverter.Int64BitsToDouble((long)v));
                        return(true);

                    case MsgPackCode.CodeDouble:
                        ulong vlong = BigEndian.Uint64(buffer);
                        array.SetDouble(eleIndex, System.BitConverter.Int64BitsToDouble((long)vlong));
                        return(true);

                    case MsgPackCode.CodeFixExt4:
                        if (!ReadCode(out code))
                        {
                            return(false);
                        }
                        if ((sbyte)code == -1)     //时间
                        {
                            uint            seconds  = BigEndian.Uint32(buffer);
                            System.DateTime dateTime = new System.DateTime(1970, 1, 1);
                            array.SetDateTime(eleIndex, dateTime.AddSeconds((double)seconds));
                        }
                        else
                        {
                            byte[] bt = new byte[5];
                            buffer.Read(bt, 1, 4);
                            array[eleIndex] = new DxExtValue(bt);
                        }
                        return(true);

                    case MsgPackCode.CodeFixExt8:
                        if (!ReadCode(out code))
                        {
                            return(false);
                        }
                        if ((sbyte)code == -1)     //时间
                        {
                            ulong seconds = BigEndian.Uint64(buffer);
                            long  nsec    = (long)(seconds >> 34);
                            seconds &= 0x00000003ffffffff;
                            System.DateTime dateTime = new System.DateTime(1970, 1, 1);
                            double          sec1     = nsec / (1000 * 1000);
                            array.SetDateTime(eleIndex, dateTime.AddSeconds(seconds + sec1));
                        }
                        else
                        {
                            byte[] bt = new byte[9];
                            buffer.Read(bt, 1, 8);
                            array[eleIndex] = new DxExtValue(bt);
                        }
                        return(true);

                    default:
                        return(false);
                    }
                }
                return(true);
            }
Пример #2
0
            public DxBaseValue Parser()
            {
                MsgPackCode code;

                if (!ReadCode(out code))
                {
                    return(null);
                }
                //判断类型
                if (IsStr(code))
                {
                    string value;
                    if (DecodeString(code, out value))
                    {
                        return(new DxStringValue(value));
                    }
                    return(null);
                }
                else if (IsFixedNum(code))
                {
                    return(new DxIntValue((int)code));
                }
                else if (IsInt(code))
                {
                    long v = 0;
                    if (DecodeInt(code, out v))
                    {
                        if (v <= int.MaxValue && v >= int.MinValue)
                        {
                            return(new DxIntValue((int)v));
                        }
                        else
                        {
                            return(new DxInt64Value(v));
                        }
                    }
                }
                else if (IsMap(code))
                {
                    DxRecordValue result = null;
                    if (DecodeMap(code, out result))
                    {
                        return(result);
                    }
                }
                else if (IsArray(code))
                {
                    DxArrayValue result = new DxArrayValue();
                    if (!Decode2Array(code, result))
                    {
                        return(null);
                    }
                    return(result);
                }
                else if (IsBin(code))
                {
                    byte[] binary;
                    if (!DecodeBinary(code, out binary))
                    {
                        return(null);
                    }
                    if (binary != null)
                    {
                        DxBinaryValue binaryValue = new DxBinaryValue();
                        binaryValue.Bytes = binary;
                        return(binaryValue);
                    }
                }
                else if (IsExt(code))
                {
                    byte[] binary;
                    if (!DecodeExtValue(code, out binary))
                    {
                        return(null);
                    }
                    if (binary.Length == 13 && (sbyte)binary[0] == -1) //96位日期格式
                    {
                        //32位纳秒,64位秒
                        Stream stream = new MemoryStream(binary);
                        stream.Position = 1;
                        uint  nsec = BigEndian.Uint32(stream);
                        ulong sec  = BigEndian.Uint64(stream);
                        stream.Dispose();
                        System.DateTime dateTime = new System.DateTime(1970, 1, 1);
                        double          sec1     = nsec / (1000 * 1000);
                        return(new DxDateTimeValue(dateTime.AddSeconds(sec + sec1)));
                    }
                    else
                    {
                        return(new DxExtValue(binary));
                    }
                }
                else
                {
                    switch (code)
                    {
                    case MsgPackCode.CodeTrue:
                        return(new DxBoolValue(true));

                    case MsgPackCode.CodeFalse:
                        return(new DxBoolValue(false));

                    case MsgPackCode.CodeFloat:
                        uint v = BigEndian.Uint32(buffer);
                        return(new DxFloatValue((float)System.BitConverter.Int64BitsToDouble((long)v)));

                    case MsgPackCode.CodeDouble:
                        ulong v1 = BigEndian.Uint64(buffer);
                        return(new DxDoubleValue(System.BitConverter.Int64BitsToDouble((long)v1)));

                    case MsgPackCode.CodeFixExt4:
                        if (!ReadCode(out code))
                        {
                            return(null);
                        }
                        if ((sbyte)code == -1)     //时间
                        {
                            uint            seconds  = BigEndian.Uint32(buffer);
                            System.DateTime dateTime = new System.DateTime(1970, 1, 1);
                            return(new DxDateTimeValue(dateTime.AddSeconds((double)seconds)));
                        }
                        else
                        {
                            byte[] bt = new byte[5];
                            buffer.Read(bt, 1, 4);
                            return(new DxExtValue(bt));
                        }

                    case MsgPackCode.CodeFixExt8:
                        //64位时间格式
                        if (!ReadCode(out code))
                        {
                            return(null);
                        }
                        if ((sbyte)code == -1)     //时间
                        {
                            ulong seconds = BigEndian.Uint64(buffer);
                            long  nsec    = (long)(seconds >> 34);
                            seconds &= 0x00000003ffffffff;
                            System.DateTime dateTime = new System.DateTime(1970, 1, 1);
                            double          sec1     = nsec / (1000 * 1000);
                            return(new DxDateTimeValue(dateTime.AddSeconds(seconds + sec1)));
                        }
                        else
                        {
                            byte[] bt = new byte[9];
                            buffer.Read(bt, 1, 8);
                            return(new DxExtValue(bt));
                        }
                    }
                }

                return(null);
            }
Пример #3
0
            public bool DecodeMapKv(DxRecordValue mapValue, MsgPackCode code)
            {
                string key;

                if (!DecodeString(code, out key))
                {
                    return(false);
                }
                if (!ReadCode(out code))
                {
                    return(false);
                }
                if (IsStr(code))
                {
                    string value;
                    if (!DecodeString(code, out value))
                    {
                        return(false);
                    }
                    mapValue.SetString(key, value);
                }
                else if (IsFixedNum(code))
                {
                    mapValue.SetInt(key, (int)(sbyte)code);
                }
                else if (IsInt(code))
                {
                    long v;
                    if (!DecodeInt(code, out v))
                    {
                        return(false);
                    }
                    if (v > int.MaxValue || v < int.MinValue)
                    {
                        mapValue.SetInt64(key, v);
                    }
                    else
                    {
                        mapValue.SetInt(key, (int)v);
                    }
                }
                else if (IsMap(code))
                {
                    DxRecordValue v;
                    if (!DecodeMap(code, out v))
                    {
                        return(false);
                    }
                    mapValue[key] = v;
                }
                else if (IsArray(code))
                {
                    DxArrayValue arr = new DxArrayValue();
                    if (!Decode2Array(code, arr))
                    {
                        return(false);
                    }
                    mapValue[key] = arr;
                }
                else if (IsBin(code))
                {
                    byte[] binary;
                    if (!DecodeBinary(code, out binary))
                    {
                        return(false);
                    }
                    if (binary != null)
                    {
                        DxBinaryValue binaryValue = new DxBinaryValue();
                        binaryValue.Bytes = binary;
                        mapValue[key]     = binaryValue;
                    }
                }
                else if (IsExt(code))
                {
                    byte[] binary;
                    if (!DecodeExtValue(code, out binary))
                    {
                        return(false);
                    }
                    if (binary.Length == 13 && (sbyte)binary[0] == -1) //96位日期格式
                    {
                        //32位纳秒,64位秒
                        Stream stream = new MemoryStream(binary);
                        stream.Position = 1;
                        uint  nsec = BigEndian.Uint32(stream);
                        ulong sec  = BigEndian.Uint64(stream);
                        stream.Dispose();
                        System.DateTime dateTime = new System.DateTime(1970, 1, 1);
                        double          sec1     = nsec / (1000 * 1000);
                        mapValue.SetDateTime(key, dateTime.AddSeconds(sec + sec1));
                    }
                    else
                    {
                        mapValue[key] = new DxExtValue(binary);
                    }
                }
                else
                {
                    switch (code)
                    {
                    case MsgPackCode.CodeTrue:
                        mapValue.SetBool(key, true);
                        return(true);

                    case MsgPackCode.CodeFalse:
                        mapValue.SetBool(key, false);
                        return(true);

                    case MsgPackCode.CodeNil:
                        mapValue[key] = null;
                        return(true);

                    case MsgPackCode.CodeFloat:
                        uint v = BigEndian.Uint32(buffer);
                        mapValue.SetFloat(key, (float)System.BitConverter.Int64BitsToDouble((long)v));
                        return(true);

                    case MsgPackCode.CodeDouble:
                        ulong v1 = BigEndian.Uint64(buffer);
                        mapValue.SetDouble(key, System.BitConverter.Int64BitsToDouble((long)v1));
                        return(true);

                    case MsgPackCode.CodeFixExt4:
                        if (!ReadCode(out code))
                        {
                            return(false);
                        }
                        if ((sbyte)code == -1)    //时间
                        {
                            uint            seconds  = BigEndian.Uint32(buffer);
                            System.DateTime dateTime = new System.DateTime(1970, 1, 1);
                            mapValue.SetDateTime(key, dateTime.AddSeconds((double)seconds));
                        }
                        else
                        {
                            byte[] bt = new byte[5];
                            buffer.Read(bt, 1, 4);
                            DxExtValue dxExtValue = new DxExtValue(bt);
                            mapValue[key] = dxExtValue;
                        }
                        return(true);

                    case MsgPackCode.CodeFixExt8:
                        //64位时间格式
                        if (!ReadCode(out code))
                        {
                            return(false);
                        }
                        if ((sbyte)code == -1)     //时间
                        {
                            ulong seconds = BigEndian.Uint64(buffer);
                            long  nsec    = (long)(seconds >> 34);
                            seconds &= 0x00000003ffffffff;
                            System.DateTime dateTime = new System.DateTime(1970, 1, 1);
                            double          sec1     = nsec / (1000 * 1000);
                            mapValue.SetDateTime(key, dateTime.AddSeconds(seconds + sec1));
                        }
                        else
                        {
                            byte[] bt = new byte[9];
                            buffer.Read(bt, 1, 8);
                            DxExtValue dxExtValue = new DxExtValue(bt);
                            mapValue[key] = dxExtValue;
                        }
                        return(true);
                    }
                }
                return(true);
            }