public JT808_0x8103 Deserialize(ReadOnlySpan<byte> bytes, out int readSize)
 {
     int offset = 0;
     JT808_0x8103 jT808_0x8103 = new JT808_0x8103
     {
         ParamList = new List<JT808_0x8103_BodyBase>(),
         CustomParamList=new List<JT808_0x8103_CustomBodyBase>()
     };
     var paramCount = JT808BinaryExtensions.ReadByteLittle(bytes, ref offset);//参数总数
     for (int i = 0; i < paramCount; i++)
     {
         var paramId = JT808BinaryExtensions.ReadUInt32Little(bytes, ref offset);//参数ID
         int readSubBodySize = 0;
         if (JT808_0x8103_BodyBase.JT808_0x8103Method.TryGetValue(paramId, out Type type))
         {
             jT808_0x8103.ParamList.Add(JT808FormatterResolverExtensions.JT808DynamicDeserialize(JT808FormatterExtensions.GetFormatter(type), bytes.Slice(offset), out readSubBodySize));
         }
         else if(JT808GlobalConfig.Instance.JT808_0X8103_Custom_Factory.ParamMethods.TryGetValue(paramId, out Type customType))
         {
             jT808_0x8103.ParamList.Add(JT808FormatterResolverExtensions.JT808DynamicDeserialize(JT808FormatterExtensions.GetFormatter(customType), bytes.Slice(offset), out readSubBodySize));
         }
         offset = offset + readSubBodySize;
     }
     readSize = offset;
     return jT808_0x8103;
 }
Ejemplo n.º 2
0
        public JT808HeaderPackage Deserialize(ReadOnlySpan <byte> bytes, out int readSize, IJT808Config config)
        {
            int offset = 0;
            JT808HeaderPackage jT808HeaderPackage = new JT808HeaderPackage();
            // 转义还原——>验证校验码——>解析消息
            // 1. 解码(转义还原)
            ReadOnlySpan <byte> buffer = JT808Utils.JT808DeEscape(bytes);
            // 2. 验证校验码
            //  2.1. 获取校验位索引
            int checkIndex = buffer.Length - 2;
            //  2.2. 获取校验码
            int checkCode1 = buffer[checkIndex];
            //  2.3. 从消息头到校验码前一个字节
            byte checkCode2 = buffer.ToXor(1, checkIndex);

            //  2.4. 验证校验码
            if (!JT808GlobalConfig.Instance.SkipCRCCode)
            {
                if (checkCode1 != checkCode2)
                {
                    throw new JT808Exception(JT808ErrorCode.CheckCodeNotEqual, $"{checkCode1}!={checkCode2}");
                }
            }
            offset += 1;
            // 3.初始化消息头
            try
            {
                jT808HeaderPackage.Header = JT808FormatterExtensions.GetFormatter <JT808Header>().Deserialize(buffer.Slice(offset, 13), out readSize, config);
            }
            catch (Exception ex)
            {
                throw new JT808Exception(JT808ErrorCode.HeaderParseError, ex);
            }
            offset = readSize;
            if (jT808HeaderPackage.Header.MessageBodyProperty.DataLength != 0)
            {
                Type jT808BodiesImplType = JT808MsgIdFactory.GetBodiesImplTypeByMsgId(jT808HeaderPackage.Header.MsgId);
                if (jT808BodiesImplType != null)
                {
                    if (jT808HeaderPackage.Header.MessageBodyProperty.IsPackge)
                    {//4.分包消息体 从17位开始  或   未分包消息体 从13位开始
                     //消息总包数2位+包序号2位=4位
                        offset = offset + 2 + 2;
                    }
                    if (jT808HeaderPackage.Header.MessageBodyProperty.DataLength != 0)
                    {
                        try
                        {
                            //5.处理消息体
                            jT808HeaderPackage.Bodies = buffer.Slice(offset + 1, jT808HeaderPackage.Header.MessageBodyProperty.DataLength).ToArray();
                        }
                        catch (Exception ex)
                        {
                            throw new JT808Exception(JT808ErrorCode.BodiesParseError, ex);
                        }
                    }
                }
            }
            readSize = buffer.Length;
            return(jT808HeaderPackage);
        }
Ejemplo n.º 3
0
 public int Serialize(IMemoryOwner <byte> memoryOwner, int offset, JT808_0x0201 value)
 {
     offset += JT808BinaryExtensions.WriteUInt16Little(memoryOwner, offset, value.MsgNum);
     offset += JT808FormatterExtensions.GetFormatter <JT808_0x0200>().Serialize(memoryOwner, offset, value.Position);
     return(offset);
 }
Ejemplo n.º 4
0
 public int Serialize(ref byte[] bytes, int offset, JT808_0x0500 value)
 {
     offset += JT808BinaryExtensions.WriteUInt16Little(bytes, offset, value.MsgNum);
     offset += JT808FormatterExtensions.GetFormatter <JT808_0x0200>().Serialize(ref bytes, offset, value.JT808_0x0200);
     return(offset);
 }
Ejemplo n.º 5
0
        public JT808_0x0104 Deserialize(ReadOnlySpan <byte> bytes, out int readSize, IJT808Config config)
        {
            int          offset       = 0;
            JT808_0x0104 jT808_0x0104 = new JT808_0x0104
            {
                MsgNum            = JT808BinaryExtensions.ReadUInt16Little(bytes, ref offset),
                AnswerParamsCount = JT808BinaryExtensions.ReadByteLittle(bytes, ref offset)
            };

            for (int i = 0; i < jT808_0x0104.AnswerParamsCount; i++)
            {
                var paramId         = JT808BinaryExtensions.ReadUInt32Little(bytes, ref offset);//参数ID
                int readSubBodySize = 0;
                if (JT808_0x8103_BodyBase.JT808_0x8103Method.TryGetValue(paramId, out Type type))
                {
                    if (jT808_0x0104.ParamList != null)
                    {
                        jT808_0x0104.ParamList.Add(JT808FormatterResolverExtensions.JT808DynamicDeserialize(JT808FormatterExtensions.GetFormatter(type), bytes.Slice(offset), out readSubBodySize, config));
                    }
                    else
                    {
                        jT808_0x0104.ParamList = new List <JT808_0x8103_BodyBase> {
                            JT808FormatterResolverExtensions.JT808DynamicDeserialize(JT808FormatterExtensions.GetFormatter(type), bytes.Slice(offset), out readSubBodySize, config)
                        };
                    }
                }
                offset += readSubBodySize;
            }
            readSize = offset;
            return(jT808_0x0104);
        }
Ejemplo n.º 6
0
        public static dynamic Deserialize(ReadOnlySpan <byte> bytes, Type type, IJT808Config config)
        {
            var formatter = JT808FormatterExtensions.GetFormatter(type);

            return(JT808FormatterResolverExtensions.JT808DynamicDeserialize(formatter, bytes, out _, config));
        }
        public JT808_0x0200 Deserialize(ReadOnlySpan <byte> bytes, out int readSize)
        {
            int          offset       = 0;
            JT808_0x0200 jT808_0X0200 = new JT808_0x0200
            {
                AlarmFlag  = JT808BinaryExtensions.ReadUInt32Little(bytes, ref offset),
                StatusFlag = JT808BinaryExtensions.ReadUInt32Little(bytes, ref offset),
                Lat        = JT808BinaryExtensions.ReadInt32Little(bytes, ref offset),
                Lng        = JT808BinaryExtensions.ReadInt32Little(bytes, ref offset)
            };
            JT808StatusProperty jT808StatusProperty = new JT808StatusProperty(Convert.ToString(jT808_0X0200.StatusFlag, 2).PadLeft(32, '0'));

            if (jT808StatusProperty.Bit28 == '1')//西经
            {
                jT808_0X0200.Lng = -jT808_0X0200.Lng;
            }
            if (jT808StatusProperty.Bit29 == '1')//南纬
            {
                jT808_0X0200.Lat = -jT808_0X0200.Lat;
            }
            jT808_0X0200.Altitude  = JT808BinaryExtensions.ReadUInt16Little(bytes, ref offset);
            jT808_0X0200.Speed     = JT808BinaryExtensions.ReadUInt16Little(bytes, ref offset);
            jT808_0X0200.Direction = JT808BinaryExtensions.ReadUInt16Little(bytes, ref offset);
            jT808_0X0200.GPSTime   = JT808BinaryExtensions.ReadDateTime6Little(bytes, ref offset);
            // 位置附加信息
            jT808_0X0200.JT808LocationAttachData = new Dictionary <byte, JT808_0x0200_BodyBase>();
            jT808_0X0200.JT808CustomLocationAttachOriginalData  = new Dictionary <byte, byte[]>();
            jT808_0X0200.JT808UnknownLocationAttachOriginalData = new Dictionary <byte, byte[]>();
            if (bytes.Length > 28)
            {
                int attachOffset = 0;
                ReadOnlySpan <byte> locationAttachMemory = bytes;
                ReadOnlySpan <byte> locationAttachSpan   = locationAttachMemory.Slice(28);
                while (locationAttachSpan.Length > attachOffset)
                {
                    int attachId  = 1;
                    int attachLen = 1;
                    try
                    {
                        if (JT808_0x0200_BodyBase.JT808LocationAttachMethod.TryGetValue(locationAttachSpan[attachOffset], out Type jT808LocationAttachType))
                        {
                            int attachContentLen             = locationAttachSpan[attachOffset + 1];
                            int locationAttachTotalLen       = attachId + attachLen + attachContentLen;
                            ReadOnlySpan <byte> attachBuffer = locationAttachSpan.Slice(attachOffset, locationAttachTotalLen);
                            object  attachImplObj            = JT808FormatterExtensions.GetFormatter(jT808LocationAttachType);
                            dynamic attachImpl = JT808FormatterResolverExtensions.JT808DynamicDeserialize(attachImplObj, attachBuffer, out readSize);
                            attachOffset += locationAttachTotalLen;
                            jT808_0X0200.JT808LocationAttachData.Add(attachImpl.AttachInfoId, attachImpl);
                        }
                        else if (JT808GlobalConfig.Instance.JT808_0X0200_Custom_Factory.AttachIds.Contains(locationAttachSpan[attachOffset]))
                        {
                            int attachContentLen             = locationAttachSpan[attachOffset + 1];
                            int locationAttachTotalLen       = attachId + attachLen + attachContentLen;
                            ReadOnlySpan <byte> attachBuffer = locationAttachSpan.Slice(attachOffset, locationAttachTotalLen);
                            jT808_0X0200.JT808CustomLocationAttachOriginalData.Add(locationAttachSpan[attachOffset], attachBuffer.ToArray());
                            attachOffset += locationAttachTotalLen;
                        }
                        else
                        {
                            int attachContentLen             = locationAttachSpan[attachOffset + 1];
                            int locationAttachTotalLen       = attachId + attachLen + attachContentLen;
                            ReadOnlySpan <byte> attachBuffer = locationAttachSpan.Slice(attachOffset, locationAttachTotalLen);
                            jT808_0X0200.JT808UnknownLocationAttachOriginalData.Add(locationAttachSpan[attachOffset], attachBuffer.ToArray());
                            attachOffset += locationAttachTotalLen;
                        }
                    }
                    catch
                    {
                        int attachContentLen       = locationAttachSpan[attachOffset + 1];
                        int locationAttachTotalLen = attachId + attachLen + attachContentLen;
                        attachOffset += locationAttachTotalLen;
                    }
                }
                offset += attachOffset;
            }
            readSize = offset;
            return(jT808_0X0200);
        }
Ejemplo n.º 8
0
        public JT808Package Deserialize(ReadOnlySpan <byte> bytes, out int readSize)
        {
            int          offset       = 0;
            JT808Package jT808Package = new JT808Package();
            // 转义还原——>验证校验码——>解析消息
            // 1. 解码(转义还原)
            ReadOnlySpan <byte> buffer = JT808Utils.JT808DeEscape(bytes);
            // 2. 验证校验码
            //  2.1. 获取校验位索引
            int checkIndex = buffer.Length - 2;

            //  2.2. 获取校验码
            jT808Package.CheckCode = buffer[checkIndex];
            //  2.3. 从消息头到校验码前一个字节
            byte checkCode = buffer.ToXor(1, checkIndex);

            //  2.4. 验证校验码
            if (!JT808GlobalConfig.Instance.SkipCRCCode)
            {
                if (jT808Package.CheckCode != checkCode)
                {
                    throw new JT808Exception(JT808ErrorCode.CheckCodeNotEqual, $"{jT808Package.CheckCode}!={checkCode}");
                }
            }
            jT808Package.Begin = buffer[offset];
            offset             = offset + 1;
            // 3.初始化消息头
            try
            {
                jT808Package.Header = JT808FormatterExtensions.GetFormatter <JT808Header>().Deserialize(buffer.Slice(offset), out readSize);
            }
            catch (Exception ex)
            {
                throw new JT808Exception(JT808ErrorCode.HeaderParseError, ex);
            }
            offset = readSize;
            if (jT808Package.Header.MessageBodyProperty.DataLength != 0)
            {
                Type jT808BodiesImplType = JT808MsgIdFactory.GetBodiesImplTypeByMsgId(jT808Package.Header.MsgId);
                if (jT808BodiesImplType != null)
                {
                    //4.分包消息体 从17位开始  或   未分包消息体 从13位开始
                    if (jT808Package.Header.MessageBodyProperty.IsPackge)
                    {
                        //消息总包数2位+包序号2位=4位
                        offset = offset + 2 + 2;
                        if (jT808Package.Header.MessageBodyProperty.PackageIndex > 1)
                        {
                            try
                            {
                                //5.处理第二包之后的分包数据消息体
                                jT808Package.Bodies = JT808FormatterResolverExtensions.JT808DynamicDeserialize(
                                    JT808FormatterExtensions.GetFormatter(typeof(JT808SplitPackageBodies)),
                                    buffer.Slice(offset + 1, jT808Package.Header.MessageBodyProperty.DataLength),
                                    out readSize);
                            }
                            catch (Exception ex)
                            {
                                throw new JT808Exception(JT808ErrorCode.BodiesParseError, ex);
                            }
                        }
                        else
                        {
                            try
                            {
                                //5.处理消息体
                                jT808Package.Bodies = JT808FormatterResolverExtensions.JT808DynamicDeserialize(
                                    JT808FormatterExtensions.GetFormatter(jT808BodiesImplType),
                                    buffer.Slice(offset + 1, jT808Package.Header.MessageBodyProperty.DataLength),
                                    out readSize);
                            }
                            catch (Exception ex)
                            {
                                throw new JT808Exception(JT808ErrorCode.BodiesParseError, ex);
                            }
                        }
                    }
                    else
                    {
                        try
                        {
                            //5.处理消息体
                            jT808Package.Bodies = JT808FormatterResolverExtensions.JT808DynamicDeserialize(
                                JT808FormatterExtensions.GetFormatter(jT808BodiesImplType),
                                buffer.Slice(offset + 1, jT808Package.Header.MessageBodyProperty.DataLength),
                                out readSize);
                        }
                        catch (Exception ex)
                        {
                            throw new JT808Exception(JT808ErrorCode.BodiesParseError, ex);
                        }
                    }
                }
            }
            jT808Package.End = buffer[buffer.Length - 1];
            readSize         = buffer.Length;
            return(jT808Package);
        }
Ejemplo n.º 9
0
        public int Serialize(IMemoryOwner <byte> memoryOwner, int offset, JT808Package value)
        {
            // 1. 先判断是否分包(理论下发不需分包,但为了统一还是加上分包处理)
            // 2. 先序列化数据体,根据数据体的长度赋值给头部,在序列化头部。
            int messageBodyOffset = 0;

            if (value.Header.MessageBodyProperty.IsPackge)
            {   //3. 先写入分包消息总包数、包序号
                messageBodyOffset += JT808BinaryExtensions.WriteUInt16Little(memoryOwner, messageBodyOffset, value.Header.MessageBodyProperty.PackgeCount);
                messageBodyOffset += JT808BinaryExtensions.WriteUInt16Little(memoryOwner, messageBodyOffset, value.Header.MessageBodyProperty.PackageIndex);
            }
            // 4. 数据体
            //JT808.Protocol.Enums.JT808MsgId 映射对应消息特性
            JT808BodiesTypeAttribute jT808BodiesTypeAttribute = value.Header.MsgId.GetAttribute <JT808BodiesTypeAttribute>();

            if (jT808BodiesTypeAttribute != null)
            {
                if (value.Bodies != null)
                {
                    // 4.1 处理数据体
                    messageBodyOffset = JT808FormatterResolverExtensions.JT808DynamicSerialize(JT808FormatterExtensions.GetFormatter(jT808BodiesTypeAttribute.JT808BodiesType), memoryOwner, offset, value.Bodies);
                }
            }
            Memory <byte> messageBodyBytes = null;

            if (messageBodyOffset != 0)
            {
                messageBodyBytes = new Memory <byte>(new byte[messageBodyOffset]);
                memoryOwner.Memory.Slice(0, messageBodyOffset).CopyTo(messageBodyBytes);
            }
            // ------------------------------------开始组包
            // 1.起始符
            offset += JT808BinaryExtensions.WriteByteLittle(memoryOwner, offset, value.Begin);
            // 2.赋值头数据长度
            value.Header.MessageBodyProperty.DataLength = messageBodyOffset;
            offset = JT808FormatterExtensions.GetFormatter <JT808Header>().Serialize(memoryOwner, offset, value.Header);
            if (messageBodyOffset != 0)
            {
                JT808BinaryExtensions.CopyTo(messageBodyBytes.Span, memoryOwner.Memory.Span, offset);
                offset          += messageBodyOffset;
                messageBodyBytes = null;
            }
            // 4.校验码
            offset += JT808BinaryExtensions.WriteByteLittle(memoryOwner, offset, memoryOwner.Memory.Span.ToXor(1, offset));
            // 5.终止符
            offset += JT808BinaryExtensions.WriteByteLittle(memoryOwner, offset, value.End);
            byte[] temp = JT808Escape(memoryOwner.Memory.Slice(0, offset).Span);
            memoryOwner.Memory.Span.Clear();
            JT808BinaryExtensions.CopyTo(temp, memoryOwner.Memory.Span, 0);
            return(temp.Length);
        }
Ejemplo n.º 10
0
        public JT808_0x0701 Deserialize(ReadOnlySpan <byte> bytes, out int readSize)
        {
            if (JT808_0x0701.JT808_0x0701Body.BodyImpl == null)
            {
                throw new JT808Exception(JT808ErrorCode.NotImplType, $"Not Impl {nameof(JT808_0x0701.JT808_0x0701Body)} class");
            }
            int          offset       = 0;
            JT808_0x0701 jT808_0X0701 = new JT808_0x0701
            {
                ElectronicWaybillLength = JT808BinaryExtensions.ReadUInt32Little(bytes, ref offset)
            };

            jT808_0X0701.ElectronicContent = JT808FormatterResolverExtensions.JT808DynamicDeserialize(JT808FormatterExtensions.GetFormatter(JT808_0x0701.JT808_0x0701Body.BodyImpl), bytes.Slice(offset, (int)jT808_0X0701.ElectronicWaybillLength), out int readSubBodySize);
            readSize = readSubBodySize;
            return(jT808_0X0701);
        }
Ejemplo n.º 11
0
        public JT808Package Deserialize(ReadOnlySpan <byte> bytes, out int readSize)
        {
            int          offset       = 0;
            JT808Package jT808Package = new JT808Package();
            // 转义还原——>验证校验码——>解析消息
            // 1. 解码(转义还原)
            ReadOnlySpan <byte> buffer = JT808DeEscape(bytes, 0, bytes.Length);
            // 2. 验证校验码
            //  2.1. 获取校验位索引
            int checkIndex = buffer.Length - 2;

            //  2.2. 获取校验码
            jT808Package.CheckCode = buffer[checkIndex];
            //  2.3. 从消息头到校验码前一个字节
            byte checkCode = buffer.ToXor(1, checkIndex);

            //  2.4. 验证校验码
            if (jT808Package.CheckCode != checkCode)
            {
                throw new JT808Exception($"{jT808Package.CheckCode}!={checkCode}");
            }
            jT808Package.Begin = buffer[offset];
            offset             = offset + 1;
            // 3.初始化消息头
            try
            {
                jT808Package.Header = JT808FormatterExtensions.GetFormatter <JT808Header>().Deserialize(buffer.Slice(offset), out readSize);
            }
            catch (Exception ex)
            {
                throw new JT808Exception($"消息头解析错误,offset:{offset.ToString()}", ex);
            }
            offset = readSize;
            if (jT808Package.Header.MessageBodyProperty.DataLength != 0)
            {
                JT808BodiesTypeAttribute jT808BodiesTypeAttribute = jT808Package.Header.MsgId.GetAttribute <JT808BodiesTypeAttribute>();
                if (jT808BodiesTypeAttribute != null)
                {
                    if (jT808Package.Header.MessageBodyProperty.IsPackge)
                    {//4.分包消息体 从17位开始  或   未分包消息体 从13位开始
                     //消息总包数2位+包序号2位=4位
                        offset = offset + 2 + 2;
                    }
                    if (jT808Package.Header.MessageBodyProperty.DataLength != 0)
                    {
                        try
                        {
                            //5.处理消息体
                            jT808Package.Bodies = JT808FormatterResolverExtensions.JT808DynamicDeserialize(JT808FormatterExtensions.GetFormatter(jT808BodiesTypeAttribute.JT808BodiesType), buffer.Slice(offset + 1, jT808Package.Header.MessageBodyProperty.DataLength), out readSize);
                        }
                        catch (Exception ex)
                        {
                            throw new JT808Exception($"消息体解析错误,offset:{offset.ToString()}", ex);
                        }
                    }
                }
            }
            jT808Package.End = buffer[buffer.Length - 1];
            readSize         = buffer.Length;
            return(jT808Package);
        }
Ejemplo n.º 12
0
        public JT808_0x8900 Deserialize(ReadOnlySpan <byte> bytes, out int readSize)
        {
            int          offset       = 0;
            JT808_0x8900 jT808_0X8900 = new JT808_0x8900();

            jT808_0X8900.PassthroughType = JT808BinaryExtensions.ReadByteLittle(bytes, ref offset);
            if (JT808_0x8900_BodyBase.JT808_0x8900Method.TryGetValue(jT808_0X8900.PassthroughType, out Type type))
            {
                jT808_0X8900.JT808_0X8900_BodyBase = JT808FormatterResolverExtensions.JT808DynamicDeserialize(JT808FormatterExtensions.GetFormatter(type), bytes.Slice(offset), out readSize);
            }
            readSize = offset;
            return(jT808_0X8900);
        }
Ejemplo n.º 13
0
 public static JT808Package Deserialize(ReadOnlySpan <byte> bytes)
 {
     return(JT808FormatterExtensions.GetFormatter <JT808Package>().Deserialize(bytes, out _));
 }
Ejemplo n.º 14
0
        public JT808_0x8103 Deserialize(ReadOnlySpan <byte> bytes, out int readSize)
        {
            int          offset       = 0;
            JT808_0x8103 jT808_0x8103 = new JT808_0x8103();
            var          paramCount   = JT808BinaryExtensions.ReadByteLittle(bytes, ref offset);//参数总数

            for (int i = 0; i < paramCount; i++)
            {
                var paramId         = JT808BinaryExtensions.ReadUInt32Little(bytes, ref offset);//参数ID
                int readSubBodySize = 0;
                if (JT808_0x8103_BodyBase.JT808_0x8103Method.TryGetValue(paramId, out Type type))
                {
                    if (jT808_0x8103.ParamList != null)
                    {
                        jT808_0x8103.ParamList.Add(JT808FormatterResolverExtensions.JT808DynamicDeserialize(JT808FormatterExtensions.GetFormatter(type), bytes.Slice(offset), out readSubBodySize));
                    }
                    else
                    {
                        jT808_0x8103.ParamList = new List <JT808_0x8103_BodyBase> {
                            JT808FormatterResolverExtensions.JT808DynamicDeserialize(JT808FormatterExtensions.GetFormatter(type), bytes.Slice(offset), out readSubBodySize)
                        };
                    }
                }
                offset = offset + readSubBodySize;
            }
            readSize = offset;
            return(jT808_0x8103);
        }
Ejemplo n.º 15
0
        /// <summary>
        /// 用于负载或者分布式的时候,在网关只需要解到头部。
        /// 根据头部的消息Id进行分发处理,可以防止小部分性能损耗。
        /// </summary>
        /// <param name="bytes"></param>
        /// <returns></returns>
        public static JT808HeaderPackage HeaderDeserialize(ReadOnlySpan <byte> bytes)
        {
            var formatter = JT808FormatterExtensions.GetFormatter <JT808HeaderPackage>();

            return(formatter.Deserialize(bytes, out int readSize));
        }
Ejemplo n.º 16
0
        public int Serialize(ref byte[] bytes, int offset, JT808Package value)
        {
            // 1. 先判断是否分包(理论下发不需分包,但为了统一还是加上分包处理)
            // 2. 先序列化数据体,根据数据体的长度赋值给头部,在序列化头部。
            int messageBodyOffset = 0;

            if (value.Header.MessageBodyProperty.IsPackge)
            {   //3. 先写入分包消息总包数、包序号
                messageBodyOffset += JT808BinaryExtensions.WriteUInt16Little(bytes, messageBodyOffset, value.Header.MessageBodyProperty.PackgeCount);
                messageBodyOffset += JT808BinaryExtensions.WriteUInt16Little(bytes, messageBodyOffset, value.Header.MessageBodyProperty.PackageIndex);
            }
            // 4. 数据体
            Type jT808BodiesImplType = JT808MsgIdFactory.GetBodiesImplTypeByMsgId(value.Header.MsgId);

            if (jT808BodiesImplType != null)
            {
                if (value.Bodies != null)
                {
                    // 4.1 处理数据体
                    messageBodyOffset = JT808FormatterResolverExtensions.JT808DynamicSerialize(JT808FormatterExtensions.GetFormatter(jT808BodiesImplType), ref bytes, offset, value.Bodies);
                }
            }
            byte[] messageBodyBytes = null;
            if (messageBodyOffset != 0)
            {
                messageBodyBytes = new byte[messageBodyOffset];
                Array.Copy(bytes, 0, messageBodyBytes, 0, messageBodyOffset);
            }
            // ------------------------------------开始组包
            // 1.起始符
            offset += JT808BinaryExtensions.WriteByteLittle(bytes, offset, value.Begin);
            // 2.赋值头数据长度
            value.Header.MessageBodyProperty.DataLength = messageBodyOffset;
            offset = JT808FormatterExtensions.GetFormatter <JT808Header>().Serialize(ref bytes, offset, value.Header);
            if (messageBodyOffset != 0)
            {
                Array.Copy(messageBodyBytes, 0, bytes, offset, messageBodyOffset);
                offset          += messageBodyOffset;
                messageBodyBytes = null;
            }
            // 4.校验码
            offset += JT808BinaryExtensions.WriteByteLittle(bytes, offset, bytes.ToXor(1, offset));
            // 5.终止符
            offset += JT808BinaryExtensions.WriteByteLittle(bytes, offset, value.End);
            return(JT808Utils.JT808Escape(ref bytes, offset));
        }
Ejemplo n.º 17
0
        /// <summary>
        /// 用于负载或者分布式的时候,在网关只需要解到头部。
        /// 根据头部的消息Id进行分发处理,可以防止小部分性能损耗。
        /// </summary>
        /// <param name="bytes"></param>
        /// <returns></returns>
        public static JT808HeaderPackage HeaderDeserialize(ReadOnlySpan <byte> bytes, IJT808Config config)
        {
            var formatter = JT808FormatterExtensions.GetFormatter <JT808HeaderPackage>();

            return(formatter.Deserialize(bytes, out _, config));
        }
Ejemplo n.º 18
0
        public static T Deserialize <T>(ReadOnlySpan <byte> bytes)
        {
            var formatter = JT808FormatterExtensions.GetFormatter <T>();

            return(formatter.Deserialize(bytes, out _));
        }
Ejemplo n.º 19
0
 public int Serialize(ref byte[] bytes, int offset, JT808_0x0201 value, IJT808Config config)
 {
     offset += JT808BinaryExtensions.WriteUInt16Little(bytes, offset, value.MsgNum);
     offset  = JT808FormatterExtensions.GetFormatter <JT808_0x0200>().Serialize(ref bytes, offset, value.Position, config);
     return(offset);
 }