示例#1
0
 protected void CopyBaseDataFrom(MpExt generic)
 {
     storedOffset  = generic.storedOffset;
     storedLength  = generic.storedLength;
     _settings     = generic._settings;
     typeId        = generic.typeId;
     typeSpecifier = generic.typeSpecifier;
     value         = generic.value;
 }
示例#2
0
        public static DateTime ConvertExt(MpExt ext)
        {
            if (ext.TypeSpecifier != -1)
            {
                throw new MsgPackException(string.Concat("The extension type ", ext.TypeSpecifier, " is not a recognised DatTime or TimeStamp, expected type -1."), -1, ext.TypeId);
            }

            switch (ext.TypeId)
            {
            case MsgPackTypeId.MpFExt4:
                uint seconds = BitConverter.ToUInt32(SwapIfLittleEndian((byte[])ext.Value), 0);
                return(EpochToLocalDateTime(seconds));

            case MsgPackTypeId.MpFExt8:
                byte[] bitVal = (byte[])ext.Value;

                if (BitConverter.IsLittleEndian)
                {
                    SwapIfLittleEndian(bitVal, 0, 4);
                    SwapIfLittleEndian(bitVal, 4, 4);
                }

                ulong bytes = BitConverter.ToUInt64(bitVal, 0);
                ulong nanoSecc;
                ulong sec;
                long  ticks;
                unchecked {
                    nanoSecc = bytes >> 34;
                    ulong mask = 0x3FFFFFFFF;
                    sec   = (bytes & mask);
                    ticks = (long)nanoSecc / 100;
                }

                DateTime ret = Zero.AddSeconds(sec).Add(new TimeSpan(ticks));
                return(ret.ToLocalTime());

            case MsgPackTypeId.MpExt8:
                byte[]   vall    = (byte[])ext.Value;
                uint     nanoSec = BitConverter.ToUInt32(SwapIfLittleEndian(vall, 0, 4), 0);
                long     sc      = BitConverter.ToInt64(SwapIfLittleEndian(vall, 4, 8), 0);
                long     tick    = nanoSec / 100;
                TimeSpan subSec  = TimeSpan.FromTicks(tick);
                if (sc < 0)
                {
                    return(EpochToLocalDateTime(sc) - subSec);
                }
                else
                {
                    return(EpochToLocalDateTime(sc) + subSec);
                }
            }

            throw new MsgPackException(string.Concat("The extension type with base type ", GetOfficialTypeName(ext.TypeId), " is not recognised as a DatTime or TimeStamp. expected ", GetOfficialTypeName(MsgPackTypeId.MpFExt4), " or ", GetOfficialTypeName(MsgPackTypeId.MpFExt8), " or ", GetOfficialTypeName(MsgPackTypeId.MpExt8)), -1, ext.TypeId);
        }
示例#3
0
        public static MsgPackItem Unpack(Stream stream, MsgPackSettings settings)
        {
            int typeByte = stream.ReadByte();

            if (typeByte < 0)
            {
                return(new MpError(settings, stream.Position, MsgPackTypeId.NeverUsed, "Unexpected end of data."));
            }
            MsgPackItem item = null;

            try {
                MsgPackTypeId type = (MsgPackTypeId)typeByte;
                switch (type)
                {
                case MsgPackTypeId.MpNull: item = new MpNull(settings); break;

                case MsgPackTypeId.MpBoolFalse:
                case MsgPackTypeId.MpBoolTrue: item = new MpBool(settings); break;

                //case MsgPackTypes.MpBytePart:
                //case MsgPackTypes.MpSBytePart:
                case MsgPackTypeId.MpSByte:
                case MsgPackTypeId.MpShort:
                case MsgPackTypeId.MpInt:
                case MsgPackTypeId.MpLong:
                case MsgPackTypeId.MpUByte:
                case MsgPackTypeId.MpUShort:
                case MsgPackTypeId.MpUInt:
                case MsgPackTypeId.MpULong: item = new MpInt(settings); break;

                case MsgPackTypeId.MpFloat:
                case MsgPackTypeId.MpDouble: item = new MpFloat(settings); break;

                //case MsgPackTypeId.MpStr5:
                case MsgPackTypeId.MpStr8:
                case MsgPackTypeId.MpStr16:
                case MsgPackTypeId.MpStr32: item = new MpString(settings); break;

                case MsgPackTypeId.MpBin8:
                case MsgPackTypeId.MpBin16:
                case MsgPackTypeId.MpBin32: item = new MpBin(settings); break;

                //case MsgPackTypeId.MpArray4:
                case MsgPackTypeId.MpArray16:
                case MsgPackTypeId.MpArray32: item = new MpArray(settings); break;

                //case MsgPackTypeId.MpMap4:
                case MsgPackTypeId.MpMap16:
                case MsgPackTypeId.MpMap32: item = new MpMap(settings); break;

                case MsgPackTypeId.MpFExt1:
                case MsgPackTypeId.MpFExt2:
                case MsgPackTypeId.MpFExt4:
                case MsgPackTypeId.MpFExt8:
                case MsgPackTypeId.MpFExt16:
                case MsgPackTypeId.MpExt8:
                case MsgPackTypeId.MpExt16:
                case MsgPackTypeId.MpExt32: item = new MpExt(settings); break;

                case MsgPackTypeId.NeverUsed:
                {
                    long pos = stream.Position - 1;
                    if (settings.ContinueProcessingOnBreakingError)
                    {
                        FindNextValidTypeId(stream);
                    }
                    return(new MpError(settings, pos, MsgPackTypeId.NeverUsed, "The specification specifically states that the value 0xC1 should never be used.")
                        {
                            storedLength = (stream.Position - pos)
                        });
                }
                }

                if (ReferenceEquals(item, null))
                {
                    if (((byte)type & 0xE0) == 0xE0 || (((byte)type & 0x80) == 0))
                    {
                        item = new MpInt(settings);
                    }
                    else if (((byte)type & 0xA0) == 0xA0)
                    {
                        item = new MpString(settings);
                    }
                    else if (((byte)type & 0x90) == 0x90)
                    {
                        item = new MpArray(settings);
                    }
                    else if (((byte)type & 0x80) == 0x80)
                    {
                        item = new MpMap(settings);
                    }
                }

                if (!ReferenceEquals(item, null))
                {
                    item.storedOffset = stream.Position - 1;
                    item._settings    = settings; // maybe redundent, but want to be sure
                    MsgPackItem ret = item.Read(type, stream);
                    item.storedLength = stream.Position - item.storedOffset;
                    if (!ReferenceEquals(item, ret))
                    {
                        ret.storedLength = item.storedLength;
                    }
                    return(ret);
                }
                else
                {
                    long pos = stream.Position - 1;
                    if (settings.ContinueProcessingOnBreakingError)
                    {
                        FindNextValidTypeId(stream);
                    }
                    return(new MpError(settings, pos, type, "The type identifier with value 0x", BitConverter.ToString(new byte[] { (byte)type }),
                                       " is either new or invalid. It is not (yet) implemented in this version of LsMsgPack.")
                    {
                        storedLength = (stream.Position - pos)
                    });
                }
            }catch (Exception ex) {
                long pos = stream.Position - 1;
                if (settings.ContinueProcessingOnBreakingError)
                {
                    FindNextValidTypeId(stream);
                }
                return(new MpError(settings, new MsgPackException("Error while reading data.", ex, stream.Position, (MsgPackTypeId)typeByte))
                {
                    storedOffset = pos,
                    storedLength = (stream.Position - pos),
                    PartialItem = item
                });
            }
        }
示例#4
0
        public static MsgPackItem Unpack(Stream stream)
        {
            int typeByte = stream.ReadByte();

            if (typeByte < 0)
            {
                throw new MsgPackException("Unexpected end of data.", stream.Position);
            }
            MsgPackItem item = null;

            try {
                MsgPackTypeId type = (MsgPackTypeId)typeByte;
                switch (type)
                {
                case MsgPackTypeId.MpNull: item = new MpNull(); break;

                case MsgPackTypeId.MpBoolFalse:
                case MsgPackTypeId.MpBoolTrue: item = new MpBool(); break;

                //case MsgPackTypes.MpBytePart:
                //case MsgPackTypes.MpSBytePart:
                case MsgPackTypeId.MpSByte:
                case MsgPackTypeId.MpShort:
                case MsgPackTypeId.MpInt:
                case MsgPackTypeId.MpLong:
                case MsgPackTypeId.MpUByte:
                case MsgPackTypeId.MpUShort:
                case MsgPackTypeId.MpUInt:
                case MsgPackTypeId.MpULong: item = new MpInt(); break;

                case MsgPackTypeId.MpFloat:
                case MsgPackTypeId.MpDouble: item = new MpFloat(); break;

                //case MsgPackTypeId.MpStr5:
                case MsgPackTypeId.MpStr8:
                case MsgPackTypeId.MpStr16:
                case MsgPackTypeId.MpStr32: item = new MpString(); break;

                case MsgPackTypeId.MpBin8:
                case MsgPackTypeId.MpBin16:
                case MsgPackTypeId.MpBin32: item = new MpBin(); break;

                //case MsgPackTypeId.MpArray4:
                case MsgPackTypeId.MpArray16:
                case MsgPackTypeId.MpArray32: item = new MpArray(); break;

                //case MsgPackTypeId.MpMap4:
                case MsgPackTypeId.MpMap16:
                case MsgPackTypeId.MpMap32: item = new MpMap(); break;

                case MsgPackTypeId.MpFExt1:
                case MsgPackTypeId.MpFExt2:
                case MsgPackTypeId.MpFExt4:
                case MsgPackTypeId.MpFExt8:
                case MsgPackTypeId.MpFExt16:
                case MsgPackTypeId.MpExt8:
                case MsgPackTypeId.MpExt16:
                case MsgPackTypeId.MpExt32: item = new MpExt(); break;

                case MsgPackTypeId.NeverUsed: throw new MsgPackException("The specification specifically states that the value 0xC1 should never be used.",
                                                                         stream.Position - 1, MsgPackTypeId.NeverUsed);
                }

                if (ReferenceEquals(item, null))
                {
                    if (((byte)type & 0xE0) == 0xE0 || (((byte)type & 0x80) == 0))
                    {
                        item = new MpInt();
                    }
                    else if (((byte)type & 0xA0) == 0xA0)
                    {
                        item = new MpString();
                    }
                    else if (((byte)type & 0x90) == 0x90)
                    {
                        item = new MpArray();
                    }
                    else if (((byte)type & 0x80) == 0x80)
                    {
                        item = new MpMap();
                    }
                }

                if (!ReferenceEquals(item, null))
                {
                    return(item.Read(type, stream));
                }
                else
                {
                    throw new MsgPackException(string.Concat("The type identifier with value 0x", BitConverter.ToString(new byte[] { (byte)type }),
                                                             " is either new or invalid. It is not (yet) implemented in this version of LsMsgPack."), stream.Position, type);
                }
            }catch (Exception ex) {
                if (!(ex is MsgPackException))
                {
                    MsgPackException mpex = new MsgPackException("Error while reading data.", ex, stream.Position, (MsgPackTypeId)typeByte);
                    if (!ReferenceEquals(item, null))
                    {
                        mpex.Data.Add("PartialMessage", item);
                    }
                    throw mpex;
                }
                else
                {
                    throw;
                }
            }
        }
示例#5
0
 public MpDateTime(MpExt ext) : base()
 {
     CopyBaseDataFrom(ext);
     value = ConvertExt(ext).ToUniversalTime();
 }
示例#6
0
 protected void CopyBaseDataFrom(MpExt generic)
 {
     typeId        = generic.typeId;
     typeSpecifier = generic.typeSpecifier;
     value         = generic.value;
 }