Ejemplo n.º 1
0
        //[InlineData(1000000)]
        //[InlineData(10000000)]
        //[InlineData(100000000)]
        public void ArrayPoolTest1(int count)
        {
            var arrayPool = ArrayPool <byte> .Create();

            while (count >= 0)
            {
                var    buffer = arrayPool.Rent(4096);
                byte[] bytes  = "7E 7D 02 7D 01 7D 02 7D 01 7E".ToHexBytes();
                var    jT808MessagePackReader = new JT808MessagePackReader(bytes);
                jT808MessagePackReader.Decode(buffer);
                try
                {
                    Assert.Equal(JT808Package.BeginFlag, jT808MessagePackReader.ReadStart());
                    Assert.Equal(0x7E, jT808MessagePackReader.ReadByte());
                    Assert.Equal(0x7D, jT808MessagePackReader.ReadByte());
                    Assert.Equal(0x7E, jT808MessagePackReader.ReadByte());
                    Assert.Equal(0x7D, jT808MessagePackReader.ReadByte());
                    Assert.Equal(JT808Package.EndFlag, jT808MessagePackReader.ReadEnd());
                    Assert.Equal(6, jT808MessagePackReader.ReaderCount);
                }
                catch (Exception)
                {
                }
                finally
                {
                    arrayPool.Return(buffer);
                    count--;
                }
            }
        }
Ejemplo n.º 2
0
        public void ReadStringTest(string str)
        {
            byte[] bytes = "7E736D616C6C636869284B6F696B65297E".ToHexBytes();
            JT808MessagePackReader jT808MessagePackReader = new JT808MessagePackReader(bytes);

            Assert.Equal(JT808Package.BeginFlag, jT808MessagePackReader.ReadStart());
            Assert.Equal(str, jT808MessagePackReader.ReadString(str.Length));
            Assert.Equal(JT808Package.EndFlag, jT808MessagePackReader.ReadEnd());
        }
Ejemplo n.º 3
0
        public void ReadBCDTest2()
        {
            byte[] bytes = "7E 12 34 7E".ToHexBytes();
            JT808MessagePackReader jT808MessagePackReader = new JT808MessagePackReader(bytes);

            Assert.Equal(JT808Package.BeginFlag, jT808MessagePackReader.ReadStart());
            Assert.Equal("1234", jT808MessagePackReader.ReadBCD(5));
            Assert.Equal(JT808Package.EndFlag, jT808MessagePackReader.ReadEnd());
        }
Ejemplo n.º 4
0
        public void ReadArrayTest()
        {
            byte[] bytes = "7E 01 02 03 7E".ToHexBytes();
            JT808MessagePackReader jT808MessagePackReader = new JT808MessagePackReader(bytes);

            Assert.Equal(JT808Package.BeginFlag, jT808MessagePackReader.ReadStart());
            Assert.Equal(new byte[] { 0x01, 0x02, 0x03 }, jT808MessagePackReader.ReadArray(3).ToArray());
            Assert.Equal(JT808Package.EndFlag, jT808MessagePackReader.ReadEnd());
        }
Ejemplo n.º 5
0
        public void ReadHexTest(string hexStr)
        {
            byte[] bytes = "7E0000000000000000000000ABCDEF12347E".ToHexBytes();
            JT808MessagePackReader jT808MessagePackReader = new JT808MessagePackReader(bytes);

            Assert.Equal(JT808Package.BeginFlag, jT808MessagePackReader.ReadStart());
            //0000000000000000000000ABCDEF1234
            Assert.Equal(hexStr, jT808MessagePackReader.ReadHex(16));
            Assert.Equal(JT808Package.EndFlag, jT808MessagePackReader.ReadEnd());
        }
Ejemplo n.º 6
0
        public void ReadDateTimeTest()
        {
            byte[] bytes = "7E 07 E3 06 19 23 23 23 00 7B 19 06 19 23 23 23 7E".ToHexBytes();
            JT808MessagePackReader jT808MessagePackReader = new JT808MessagePackReader(bytes);

            Assert.Equal(JT808Package.BeginFlag, jT808MessagePackReader.ReadStart());
            Assert.Equal(DateTime.Parse("2019-06-19"), jT808MessagePackReader.ReadDateTime4());
            Assert.Equal(new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 23, 23, 23, 123), jT808MessagePackReader.ReadDateTime5());
            Assert.Equal(DateTime.Parse("2019-06-19 23:23:23"), jT808MessagePackReader.ReadDateTime6());
            Assert.Equal(JT808Package.EndFlag, jT808MessagePackReader.ReadEnd());
        }
Ejemplo n.º 7
0
        public void ReadDateTimeNullTest()
        {
            byte[] bytes = "7E0000000000000000000000000000007E".ToHexBytes();
            JT808MessagePackReader jT808MessagePackReader = new JT808MessagePackReader(bytes);

            Assert.Equal(JT808Package.BeginFlag, jT808MessagePackReader.ReadStart());
            Assert.Null(jT808MessagePackReader.ReadDateTimeNull_YYYYMMDD());
            Assert.Null(jT808MessagePackReader.ReadDateTimeNull_HHmmssfff());
            Assert.Null(jT808MessagePackReader.ReadDateTimeNull_yyMMddHHmmss());
            Assert.Equal(JT808Package.EndFlag, jT808MessagePackReader.ReadEnd());
        }
Ejemplo n.º 8
0
        public void ReadDateTimeTest()
        {
            byte[] bytes = "7E2019061923232301231906192323237E".ToHexBytes();
            JT808MessagePackReader jT808MessagePackReader = new JT808MessagePackReader(bytes);

            Assert.Equal(JT808Package.BeginFlag, jT808MessagePackReader.ReadStart());
            Assert.Equal(DateTime.Parse("2019-06-19"), jT808MessagePackReader.ReadDateTime_YYYYMMDD());
            Assert.Equal(new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 23, 23, 23, 123), jT808MessagePackReader.ReadDateTime_HHmmssfff());
            Assert.Equal(DateTime.Parse("2019-06-19 23:23:23"), jT808MessagePackReader.ReadDateTime_yyMMddHHmmss());
            Assert.Equal(JT808Package.EndFlag, jT808MessagePackReader.ReadEnd());
        }
Ejemplo n.º 9
0
        public void ReadDateTimeNullTest()
        {
            byte[] bytes = "7E0000000000000000000000000000007E".ToHexBytes();
            JT808MessagePackReader jT808MessagePackReader = new JT808MessagePackReader(bytes);

            Assert.Equal(JT808Package.BeginFlag, jT808MessagePackReader.ReadStart());
            Assert.Null(jT808MessagePackReader.ReadDateTimeNull4());
            Assert.Null(jT808MessagePackReader.ReadDateTimeNull5());
            Assert.Null(jT808MessagePackReader.ReadDateTimeNull6());
            Assert.Equal(JT808Package.EndFlag, jT808MessagePackReader.ReadEnd());
        }
Ejemplo n.º 10
0
        public void ReadNumericalTest()
        {
            byte[] bytes = "7E 01 00 10 00 00 00 20 00 00 00 40 7E".ToHexBytes();
            JT808MessagePackReader jT808MessagePackReader = new JT808MessagePackReader(bytes);

            Assert.Equal(JT808Package.BeginFlag, jT808MessagePackReader.ReadStart());
            Assert.Equal(0x01, jT808MessagePackReader.ReadByte());
            Assert.Equal(16, jT808MessagePackReader.ReadUInt16());
            Assert.Equal(32, jT808MessagePackReader.ReadInt32());
            Assert.Equal((uint)64, jT808MessagePackReader.ReadUInt32());
            Assert.Equal(JT808Package.EndFlag, jT808MessagePackReader.ReadEnd());
        }
Ejemplo n.º 11
0
        public void ReadEncodeTest()
        {
            byte[] bytes = "7E 7D 02 7D 01 7D 02 7D 01 7E".ToHexBytes();
            JT808MessagePackReader jT808MessagePackReader = new JT808MessagePackReader(bytes);

            jT808MessagePackReader.Decode(new byte[4096]);
            Assert.Equal(JT808Package.BeginFlag, jT808MessagePackReader.ReadStart());
            Assert.Equal(0x7E, jT808MessagePackReader.ReadByte());
            Assert.Equal(0x7D, jT808MessagePackReader.ReadByte());
            Assert.Equal(0x7E, jT808MessagePackReader.ReadByte());
            Assert.Equal(0x7D, jT808MessagePackReader.ReadByte());
            Assert.Equal(JT808Package.EndFlag, jT808MessagePackReader.ReadEnd());
            Assert.Equal(6, jT808MessagePackReader.ReaderCount);
        }
Ejemplo n.º 12
0
        public JT808HeaderPackage Deserialize(ref JT808MessagePackReader reader, IJT808Config config)
        {
            // 1. 验证校验和
            if (!config.SkipCRCCode)
            {
                if (reader.CheckXorCodeVali)
                {
                    throw new JT808Exception(JT808ErrorCode.CheckCodeNotEqual, $"{reader.RealCheckXorCode}!={reader.CalculateCheckXorCode}");
                }
            }
            JT808HeaderPackage jT808Package = new JT808HeaderPackage();

            // ---------------开始解包--------------
            // 2.读取起始位置
            jT808Package.Begin = reader.ReadStart();
            // 3.读取头部信息
            jT808Package.Header = new JT808Header();
            //  3.1.读取消息Id
            jT808Package.Header.MsgId = reader.ReadUInt16();
            //  3.2.读取消息体属性
            jT808Package.Header.MessageBodyProperty = new JT808HeaderMessageBodyProperty();
            ushort messageBodyPropertyValue = reader.ReadUInt16();

            //    3.2.1.解包消息体属性
            jT808Package.Header.MessageBodyProperty.Unwrap(messageBodyPropertyValue, config);
            // 3.3.读取终端手机号
            jT808Package.Header.TerminalPhoneNo = reader.ReadBCD(config.TerminalPhoneNoLength);
            // 3.4.读取消息流水号
            jT808Package.Header.MsgNum = reader.ReadUInt16();
            // 3.5.判断有无分包
            if (jT808Package.Header.MessageBodyProperty.IsPackge)
            {
                //3.5.1.读取消息包总数
                jT808Package.Header.MessageBodyProperty.PackgeCount = reader.ReadUInt16();
                //3.5.2.读取消息包序号
                jT808Package.Header.MessageBodyProperty.PackageIndex = reader.ReadUInt16();
            }
            // 4.处理数据体
            //  4.1.判断有无数据体
            if (jT808Package.Header.MessageBodyProperty.DataLength > 0)
            {
                jT808Package.Bodies = reader.ReadContent().ToArray();
            }
            // 5.读取校验码
            jT808Package.CheckCode = reader.ReadByte();
            // 6.读取终止位置
            jT808Package.End = reader.ReadEnd();
            // ---------------解包完成--------------
            return(jT808Package);
        }
Ejemplo n.º 13
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="reader"></param>
        /// <param name="config"></param>
        /// <returns></returns>
        public JT808Package Deserialize(ref JT808MessagePackReader reader, IJT808Config config)
        {
            // 1. 验证校验和
            if (!config.SkipCRCCode)
            {
                if (!reader.CheckXorCodeVali)
                {
                    throw new JT808Exception(JT808ErrorCode.CheckCodeNotEqual, $"{reader.RealCheckXorCode}!={reader.CalculateCheckXorCode}");
                }
            }
            JT808Package jT808Package = new JT808Package();

            // ---------------开始解包--------------
            // 2.读取起始位置
            jT808Package.Begin = reader.ReadStart();
            // 3.读取头部信息
            jT808Package.Header = new JT808Header();
            //  3.1.读取消息Id
            jT808Package.Header.MsgId = reader.ReadUInt16();
            //  3.2.读取消息体属性
            jT808Package.Header.MessageBodyProperty = new JT808HeaderMessageBodyProperty(reader.ReadUInt16());
            if (reader.Version == JT808Version.JTT2013Force)
            {
                jT808Package.Header.TerminalPhoneNo = reader.ReadBCD(config.TerminalPhoneNoLength, config.Trim);
                reader.Version = JT808Version.JTT2013;
            }
            else
            {
                if (reader.Version == JT808Version.JTT2019 || jT808Package.Header.MessageBodyProperty.VersionFlag)
                {
                    //2019版本
                    jT808Package.Header.ProtocolVersion = reader.ReadByte();
                    //  3.4.读取终端手机号
                    jT808Package.Header.TerminalPhoneNo = reader.ReadBCD(20, config.Trim);
                    reader.Version = JT808Version.JTT2019;
                }
                else
                {
                    //2013版本
                    //  3.3.读取终端手机号
                    jT808Package.Header.TerminalPhoneNo = reader.ReadBCD(config.TerminalPhoneNoLength, config.Trim);
                }
            }
            jT808Package.Version = reader.Version;
            //  3.4.读取消息流水号
            jT808Package.Header.MsgNum = reader.ReadUInt16();
            //  3.5.判断有无分包
            if (jT808Package.Header.MessageBodyProperty.IsPackage)
            {
                //3.5.1.读取消息包总数
                jT808Package.Header.PackgeCount = reader.ReadUInt16();
                //3.5.2.读取消息包序号
                jT808Package.Header.PackageIndex = reader.ReadUInt16();
            }
            // 4.处理数据体
            //  4.1.判断有无数据体
            if (jT808Package.Header.MessageBodyProperty.DataLength > 0)
            {
                if (config.MsgIdFactory.TryGetValue(jT808Package.Header.MsgId, out object instance))
                {
                    if (jT808Package.Header.MessageBodyProperty.IsPackage)
                    {
                        //读取分包的数据体
                        try
                        {
                            jT808Package.SubDataBodies = reader.ReadArray(jT808Package.Header.MessageBodyProperty.DataLength).ToArray();
                        }
                        catch (Exception ex)
                        {
                            throw new JT808Exception(JT808ErrorCode.BodiesParseError, ex);
                        }
                    }
                    else
                    {
                        try
                        {
                            //4.2.处理消息体
                            jT808Package.Bodies = JT808MessagePackFormatterResolverExtensions.JT808DynamicDeserialize(
                                instance,
                                ref reader, config);
                        }
                        catch (Exception ex)
                        {
                            throw new JT808Exception(JT808ErrorCode.BodiesParseError, ex);
                        }
                    }
                }
            }
            // 5.读取校验码
            jT808Package.CheckCode = reader.ReadByte();
            // 6.读取终止位置
            jT808Package.End = reader.ReadEnd();
            // ---------------解包完成--------------
            return(jT808Package);
        }
Ejemplo n.º 14
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="reader"></param>
        /// <param name="writer"></param>
        /// <param name="config"></param>
        public void Analyze(ref JT808MessagePackReader reader, Utf8JsonWriter writer, IJT808Config config)
        {
            // ---------------开始解析对象--------------
            writer.WriteStartObject();
            // 1. 验证校验和
            if (!reader.CheckXorCodeVali)
            {
                writer.WriteString("检验和错误", $"{reader.RealCheckXorCode}!={reader.CalculateCheckXorCode}");
            }
            // 2.读取起始位置
            byte start = reader.ReadEnd();

            writer.WriteNumber($"[{start.ReadNumber()}]开始", start);
            var msgid = reader.ReadUInt16();

            writer.WriteNumber($"[{msgid.ReadNumber()}]消息Id", msgid);
            ushort messageBodyPropertyValue  = reader.ReadUInt16();
            var    headerMessageBodyProperty = new JT808HeaderMessageBodyProperty(messageBodyPropertyValue);

            //消息体属性对象 开始
            writer.WriteStartObject("消息体属性对象");
            ReadOnlySpan <char> messageBodyPropertyReadOnlySpan = messageBodyPropertyValue.ReadBinary();

            writer.WriteNumber($"[{messageBodyPropertyReadOnlySpan.ToString()}]消息体属性", messageBodyPropertyValue);
            if (reader.Version == JT808Version.JTT2013Force)
            {
                reader.Version = JT808Version.JTT2013;
                writer.WriteString("版本号", JT808Version.JTT2013.ToString());
                writer.WriteNumber("[bit15]保留", 0);
                writer.WriteNumber("[bit14]保留", 0);
                writer.WriteBoolean("[bit13]是否分包", headerMessageBodyProperty.IsPackage);
                writer.WriteString("[bit10~bit12]数据加密", headerMessageBodyProperty.Encrypt.ToString());
                writer.WriteNumber("[bit0~bit9]消息体长度", headerMessageBodyProperty.DataLength);
                writer.WriteEndObject();
                //2013版本
                //  3.3.读取终端手机号
                var terminalPhoneNo = reader.ReadBCD(config.TerminalPhoneNoLength, false);
                //消息体属性对象 结束
                writer.WriteString($"[{terminalPhoneNo.PadLeft(config.TerminalPhoneNoLength, '0')}]终端手机号", terminalPhoneNo);
            }
            else
            {
                if (reader.Version == JT808Version.JTT2019 || headerMessageBodyProperty.VersionFlag)
                {
                    reader.Version = JT808Version.JTT2019;
                    writer.WriteString("版本号", JT808Version.JTT2019.ToString());
                    writer.WriteNumber("[bit15]保留", 0);
                    writer.WriteBoolean("[bit14]协议版本标识", headerMessageBodyProperty.VersionFlag);
                    writer.WriteBoolean("[bit13]是否分包", headerMessageBodyProperty.IsPackage);
                    writer.WriteString("[bit10~bit12]数据加密", headerMessageBodyProperty.Encrypt.ToString());
                    writer.WriteNumber("[bit0~bit9]消息体长度", headerMessageBodyProperty.DataLength);
                    //消息体属性对象 结束
                    writer.WriteEndObject();
                    //2019版本
                    var protocolVersion = reader.ReadByte();
                    writer.WriteNumber($"[{protocolVersion.ReadNumber()}]协议版本号(2019)", protocolVersion);
                    //  3.4.读取终端手机号
                    var terminalPhoneNo = reader.ReadBCD(20, config.Trim);
                    writer.WriteString($"[{terminalPhoneNo.PadLeft(20, '0')}]终端手机号", terminalPhoneNo);
                }
                else
                {
                    reader.Version = JT808Version.JTT2013;
                    writer.WriteString("版本号", JT808Version.JTT2013.ToString());
                    writer.WriteNumber("[bit15]保留", 0);
                    writer.WriteNumber("[bit14]保留", 0);
                    writer.WriteBoolean("[bit13]是否分包", headerMessageBodyProperty.IsPackage);
                    writer.WriteString("[bit10~bit12]数据加密", headerMessageBodyProperty.Encrypt.ToString());
                    writer.WriteNumber("[bit0~bit9]消息体长度", headerMessageBodyProperty.DataLength);
                    writer.WriteEndObject();
                    //2013版本
                    //  3.3.读取终端手机号
                    var terminalPhoneNo = reader.ReadBCD(config.TerminalPhoneNoLength, false);
                    //消息体属性对象 结束
                    writer.WriteString($"[{terminalPhoneNo.PadLeft(config.TerminalPhoneNoLength, '0')}]终端手机号", terminalPhoneNo);
                }
            }
            //  3.4.读取消息流水号
            var msgNum = reader.ReadUInt16();

            writer.WriteNumber($"[{msgNum.ReadNumber()}]消息流水号", msgNum);
            //  3.5.判断有无分包
            uint packgeCount = 0, packageIndex = 0;

            if (headerMessageBodyProperty.IsPackage)
            {
                //3.5.1.读取消息包总数
                packgeCount = reader.ReadUInt16();
                writer.WriteNumber($"[{packgeCount.ReadNumber()}]消息包总数", packgeCount);
                //3.5.2.读取消息包序号
                packageIndex = reader.ReadUInt16();
                writer.WriteNumber($"[{packageIndex.ReadNumber()}]消息包序号", packageIndex);
            }
            // 4.处理数据体
            //  4.1.判断有无数据体
            if (headerMessageBodyProperty.DataLength > 0)
            {
                //数据体属性对象 开始
                writer.WriteStartObject("数据体对象");
                string description = "数据体";
                if (headerMessageBodyProperty.IsPackage)
                {
                    //读取分包的数据体
                    try
                    {
                        writer.WriteString($"[分包]数据体", reader.ReadArray(reader.ReadCurrentRemainContentLength()).ToArray().ToHexString());
                    }
                    catch (IndexOutOfRangeException ex)
                    {
                        writer.WriteString($"数据体解析异常,无可用数据体进行解析", ex.StackTrace);
                    }
                    catch (ArgumentOutOfRangeException ex)
                    {
                        writer.WriteString($"[分包]数据体解析异常,无可用数据体进行解析", ex.StackTrace);
                    }
                    catch (Exception ex)
                    {
                        writer.WriteString($"[分包]数据体异常", ex.StackTrace);
                    }
                }
                else
                {
                    if (config.MsgIdFactory.TryGetValue(msgid, out object instance))
                    {
                        if (instance is IJT808Description jT808Description)
                        {
                            //4.2.处理消息体
                            description = jT808Description.Description;
                        }
                        try
                        {
                            //数据体长度正常
                            writer.WriteString($"{description}", reader.ReadVirtualArray(reader.ReadCurrentRemainContentLength()).ToArray().ToHexString());
                            if (instance is IJT808Analyze analyze)
                            {
                                //4.2.处理消息体
                                analyze.Analyze(ref reader, writer, config);
                            }
                        }
                        catch (IndexOutOfRangeException ex)
                        {
                            writer.WriteString($"数据体解析异常,无可用数据体进行解析", ex.StackTrace);
                        }
                        catch (ArgumentOutOfRangeException ex)
                        {
                            writer.WriteString($"数据体解析异常,无可用数据体进行解析", ex.StackTrace);
                        }
                        catch (Exception ex)
                        {
                            writer.WriteString($"数据体异常", ex.StackTrace);
                        }
                    }
                    else
                    {
                        writer.WriteString($"[未知]数据体", reader.ReadArray(reader.ReadCurrentRemainContentLength()).ToArray().ToHexString());
                    }
                }
                //数据体属性对象 结束
                writer.WriteEndObject();
            }
            else
            {
                if (config.MsgIdFactory.TryGetValue(msgid, out object instance))
                {
                    //数据体属性对象 开始
                    writer.WriteStartObject("数据体对象");
                    string description = "[Null]数据体";
                    if (instance is IJT808Description jT808Description)
                    {
                        //4.2.处理消息体
                        description = jT808Description.Description;
                    }
                    writer.WriteNull(description);
                    //数据体属性对象 结束
                    writer.WriteEndObject();
                }
                else
                {
                    writer.WriteNull($"[Null]数据体");
                }
            }
            try
            {
                // 5.读取校验码
                reader.ReadByte();
                writer.WriteNumber($"[{reader.RealCheckXorCode.ReadNumber()}]校验码", reader.RealCheckXorCode);
                // 6.读取终止位置
                byte end = reader.ReadEnd();
                writer.WriteNumber($"[{end.ReadNumber()}]结束", end);
            }
            catch (ArgumentOutOfRangeException ex)
            {
                writer.WriteString($"数据解析异常,无可用数据进行解析", ex.StackTrace);
            }
            catch (Exception ex)
            {
                writer.WriteString($"数据解析异常", ex.StackTrace);
            }
            finally
            {
                writer.WriteEndObject();
            }
        }
Ejemplo n.º 15
0
        public JT808Package Deserialize(ref JT808MessagePackReader reader, IJT808Config config)
        {
            // 1. 验证校验和
            if (!config.SkipCRCCode)
            {
                if (!reader.CheckXorCodeVali)
                {
                    throw new JT808Exception(JT808ErrorCode.CheckCodeNotEqual, $"{reader.RealCheckXorCode}!={reader.CalculateCheckXorCode}");
                }
            }
            JT808Package jT808Package = new JT808Package();

            // ---------------开始解包--------------
            // 2.读取起始位置
            jT808Package.Begin = reader.ReadStart();
            // 3.读取头部信息
            jT808Package.Header = new JT808Header();
            //  3.1.读取消息Id
            jT808Package.Header.MsgId = reader.ReadUInt16();
            //  3.2.读取消息体属性
            jT808Package.Header.MessageBodyProperty = new JT808HeaderMessageBodyProperty();
            ushort messageBodyPropertyValue = reader.ReadUInt16();

            //    3.2.1.解包消息体属性
            jT808Package.Header.MessageBodyProperty.Unwrap(messageBodyPropertyValue, config);
            // 3.3.读取终端手机号
            jT808Package.Header.TerminalPhoneNo = reader.ReadBCD(config.TerminalPhoneNoLength);
            // 3.4.读取消息流水号
            jT808Package.Header.MsgNum = reader.ReadUInt16();
            // 3.5.判断有无分包
            if (jT808Package.Header.MessageBodyProperty.IsPackge)
            {
                //3.5.1.读取消息包总数
                jT808Package.Header.MessageBodyProperty.PackgeCount = reader.ReadUInt16();
                //3.5.2.读取消息包序号
                jT808Package.Header.MessageBodyProperty.PackageIndex = reader.ReadUInt16();
            }
            // 4.处理数据体
            //  4.1.判断有无数据体
            if (jT808Package.Header.MessageBodyProperty.DataLength > 0)
            {
                Type jT808BodiesImplType = config.MsgIdFactory.GetBodiesImplTypeByMsgId(jT808Package.Header.MsgId, jT808Package.Header.TerminalPhoneNo);
                if (jT808BodiesImplType != null)
                {
                    if (jT808Package.Header.MessageBodyProperty.IsPackge)
                    {
                        if (jT808Package.Header.MessageBodyProperty.PackageIndex > 1)
                        {
                            try
                            {
                                //4.2处理第二包之后的分包数据消息体
                                jT808Package.Bodies = JT808SplitPackageBodiesFormatter.Instance.Deserialize(ref reader, config);
                            }
                            catch (Exception ex)
                            {
                                throw new JT808Exception(JT808ErrorCode.BodiesParseError, ex);
                            }
                        }
                        else
                        {
                            try
                            {
                                //4.2.处理消息体
                                jT808Package.Bodies = JT808MessagePackFormatterResolverExtensions.JT808DynamicDeserialize(
                                    config.GetMessagePackFormatterByType(jT808BodiesImplType),
                                    ref reader, config);
                            }
                            catch (Exception ex)
                            {
                                throw new JT808Exception(JT808ErrorCode.BodiesParseError, ex);
                            }
                        }
                    }
                    else
                    {
                        try
                        {
                            //4.2.处理消息体
                            jT808Package.Bodies = JT808MessagePackFormatterResolverExtensions.JT808DynamicDeserialize(
                                config.GetMessagePackFormatterByType(jT808BodiesImplType),
                                ref reader, config);
                        }
                        catch (Exception ex)
                        {
                            throw new JT808Exception(JT808ErrorCode.BodiesParseError, ex);
                        }
                    }
                }
            }
            // 5.读取校验码
            jT808Package.CheckCode = reader.ReadByte();
            // 6.读取终止位置
            jT808Package.End = reader.ReadEnd();
            // ---------------解包完成--------------
            return(jT808Package);
        }
Ejemplo n.º 16
0
        public JT808HeaderPackage(ref JT808MessagePackReader reader, IJT808Config config)
        {
            // 1. 验证校验和
            if (!config.SkipCRCCode)
            {
                if (!reader.CheckXorCodeVali)
                {
                    throw new JT808Exception(JT808ErrorCode.CheckCodeNotEqual, $"{reader.RealCheckXorCode}!={reader.CalculateCheckXorCode}");
                }
            }
            // ---------------开始解包--------------
            // 2.读取起始位置
            this.Begin = reader.ReadStart();
            // 3.读取头部信息
            this.Header = new JT808Header();
            //  3.1.读取消息Id
            this.Header.MsgId = reader.ReadUInt16();
            //  3.2.读取消息体属性
            ushort messageBodyPropertyValue = reader.ReadUInt16();

            //    3.2.1.解包消息体属性
            this.Header.MessageBodyProperty = new JT808HeaderMessageBodyProperty(messageBodyPropertyValue);
            if (this.Header.MessageBodyProperty.VersionFlag)
            {
                //2019版本
                //  3.3.读取协议版本号
                this.Header.ProtocolVersion = reader.ReadByte();
                //  3.4.读取终端手机号
                this.Header.TerminalPhoneNo = reader.ReadBCD(20, config.Trim);
                reader.Version = JT808Version.JTT2019;
            }
            else
            {
                //2013版本
                //  3.3.读取终端手机号
                this.Header.TerminalPhoneNo = reader.ReadBCD(config.TerminalPhoneNoLength, config.Trim);
            }
            // 3.4.读取消息流水号
            this.Header.MsgNum = reader.ReadUInt16();
            // 3.5.判断有无分包
            if (this.Header.MessageBodyProperty.IsPackage)
            {
                //3.5.1.读取消息包总数
                this.Header.PackgeCount = reader.ReadUInt16();
                //3.5.2.读取消息包序号
                this.Header.PackageIndex = reader.ReadUInt16();
            }
            // 4.处理数据体
            //  4.1.判断有无数据体
            if (this.Header.MessageBodyProperty.DataLength > 0)
            {
                this.Bodies = reader.ReadContent();
            }
            else
            {
                this.Bodies = ReadOnlySpan <byte> .Empty;
            }
            // 5.读取校验码
            this.CheckCode = reader.ReadByte();
            // 6.读取终止位置
            this.End = reader.ReadEnd();
            // ---------------解包完成--------------
            this.OriginalData = reader.SrcBuffer;
        }