Esempio n. 1
0
        public JT808Package Deserialize(ReadOnlySpan <byte> bytes, int offset, IJT808FormatterResolver formatterResolver, out int readSize)
        {
            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 = formatterResolver.GetFormatter <JT808Header>().Deserialize(buffer, offset, formatterResolver, 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(formatterResolver.GetFormatterDynamic(jT808BodiesTypeAttribute.JT808BodiesType), buffer.Slice(offset, jT808Package.Header.MessageBodyProperty.DataLength).ToArray(), offset, formatterResolver, out readSize);
                        }
                        catch (Exception ex)
                        {
                            throw new JT808Exception($"消息体解析错误,offset:{offset.ToString()}", ex);
                        }
                    }
                }
            }
            jT808Package.End = buffer[bytes.Length - 1];
            readSize         = buffer.Length;
            return(jT808Package);
        }
Esempio n. 2
0
 private static void InitMap()
 {
     foreach (var item in Enum.GetNames(typeof(JT808MsgId)))
     {
         JT808MsgId msgId = item.ToEnum <JT808MsgId>();
         JT808BodiesTypeAttribute jT808BodiesTypeAttribute = msgId.GetAttribute <JT808BodiesTypeAttribute>();
         map.Add((ushort)msgId, jT808BodiesTypeAttribute?.JT808BodiesType);
     }
 }
        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);
        }
Esempio n. 4
0
 private void InitMap()
 {
     foreach (var item in Enum.GetNames(typeof(JT808MsgId)))
     {
         JT808MsgId msgId = item.ToEnum<JT808MsgId>();
         if (!map.ContainsKey((ushort)msgId))
         {
             JT808BodiesTypeAttribute jT808BodiesTypeAttribute = msgId.GetAttribute<JT808BodiesTypeAttribute>();
             if (jT808BodiesTypeAttribute != null)
             {                
                 map.Add((ushort)msgId, jT808BodiesTypeAttribute.JT808BodiesType);
             }
         }
     }
 }
Esempio n. 5
0
        public int Serialize(ref byte[] bytes, int offset, JT808Package value, IJT808FormatterResolver formatterResolver)
        {
            // 1. 先判断是否分包(理论下发不需分包,但为了统一还是加上分包处理)
            // 2. 先序列化数据体,根据数据体的长度赋值给头部,在序列化头部。
            int messageBodyOffset = 0;

            if (value.Header.MessageBodyProperty.IsPackge)
            {   //3. 先写入分包消息总包数、包序号
                messageBodyOffset += JT808BinaryExtensions.WriteUInt16Little(ref bytes, messageBodyOffset, value.Header.MessageBodyProperty.PackgeCount);
                messageBodyOffset += JT808BinaryExtensions.WriteUInt16Little(ref bytes, 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(formatterResolver.GetFormatterDynamic(jT808BodiesTypeAttribute.JT808BodiesType), ref bytes, offset, value.Bodies, formatterResolver);
                }
            }
            byte[] messageBodyBytes = null;
            if (messageBodyOffset != 0)
            {
                messageBodyBytes = bytes.AsSpan().Slice(0, messageBodyOffset).ToArray();
            }
            // ------------------------------------开始组包
            // 1.起始符
            offset += JT808BinaryExtensions.WriteLittle(ref bytes, offset, value.Begin);
            // 2.赋值头数据长度
            value.Header.MessageBodyProperty.DataLength = messageBodyOffset;
            offset = formatterResolver.GetFormatter <JT808Header>().Serialize(ref bytes, offset, value.Header, formatterResolver);
            if (messageBodyOffset != 0)
            {
                Buffer.BlockCopy(messageBodyBytes, 0, bytes, offset, messageBodyOffset);
                offset += messageBodyOffset;
            }
            // 4.校验码
            offset += JT808BinaryExtensions.WriteLittle(ref bytes, offset, bytes.ToXor(1, offset));
            // 5.终止符
            offset += JT808BinaryExtensions.WriteLittle(ref bytes, offset, value.End);
            byte[] temp = JT808Escape(bytes.AsSpan().Slice(0, offset));
            Buffer.BlockCopy(temp, 0, bytes, 0, temp.Length);
            return(temp.Length);
        }