コード例 #1
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);
            }
コード例 #2
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);
            }
コード例 #3
0
            public void EncodeExtValue(DxExtValue value)
            {
                byte[] bt    = value.ExtData;
                int    btlen = bt.Length;

                buf[1] = value.ExtType;
                switch (btlen)
                {
                case 1:
                    buf[0] = (byte)MsgPackCode.CodeFixExt1;
                    databuffer.Write(buf, 0, 2);
                    break;

                case 2:
                    buf[0] = (byte)MsgPackCode.CodeFixExt2;
                    databuffer.Write(buf, 0, 2);
                    break;

                case 4:
                    buf[0] = (byte)MsgPackCode.CodeFixExt4;
                    databuffer.Write(buf, 0, 2);
                    break;

                case 8:
                    buf[0] = (byte)MsgPackCode.CodeFixExt8;
                    databuffer.Write(buf, 0, 2);
                    break;

                default:
                    if (btlen <= 16)
                    {
                        buf[0] = (byte)MsgPackCode.CodeFixExt16;
                        databuffer.Write(buf, 0, 2);
                    }
                    else if (btlen <= DxMsgPack.Max_str8_len)
                    {
                        buf[0] = (byte)MsgPackCode.CodeExt8;
                        buf[1] = (byte)btlen;
                        buf[2] = value.ExtType;
                        databuffer.Write(buf, 0, 3);
                    }
                    else if (btlen <= DxMsgPack.Max_str16_len)
                    {
                        databuffer.WriteByte((byte)MsgPackCode.CodeExt16);
                        BigEndian.PutUint16(databuffer, (ushort)btlen);
                        databuffer.WriteByte(value.ExtType);
                    }
                    else
                    {
                        if (btlen > Max_str32_len)
                        {
                            btlen = Max_str32_len;
                        }
                        databuffer.WriteByte((byte)MsgPackCode.CodeExt32);
                        BigEndian.PutUint32(databuffer, (uint)btlen);
                        databuffer.WriteByte(value.ExtType);
                    }
                    break;
                }
                if (btlen > 0)
                {
                    databuffer.Write(bt, 0, btlen);
                }
            }