From 3faba5880378a7f4eb85ed529fc89ce4ac93006c Mon Sep 17 00:00:00 2001
From: SmallChi <564952747@qq.com>
Date: Tue, 9 Oct 2018 10:02:17 +0800
Subject: [PATCH] =?UTF-8?q?1.=E5=A2=9E=E5=8A=A0=E8=B7=B3=E8=BF=87=E6=A3=80?=
=?UTF-8?q?=E9=AA=8C=E7=A0=81=E9=85=8D=E7=BD=AE=202.=E6=95=B4=E7=90=86?=
=?UTF-8?q?=E4=BB=A3=E7=A0=81?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../JT809Formatters/JT809PackageFormatter.cs | 7 ++--
src/JT809.Protocol/JT809GlobalConfig.cs | 35 +++++++++++++++++--
src/JT809.Protocol/JT809Serializer.cs | 20 ++---------
3 files changed, 39 insertions(+), 23 deletions(-)
diff --git a/src/JT809.Protocol/JT809Formatters/JT809PackageFormatter.cs b/src/JT809.Protocol/JT809Formatters/JT809PackageFormatter.cs
index 9676adf..ae8f7f4 100644
--- a/src/JT809.Protocol/JT809Formatters/JT809PackageFormatter.cs
+++ b/src/JT809.Protocol/JT809Formatters/JT809PackageFormatter.cs
@@ -29,9 +29,12 @@ namespace JT809.Protocol.JT809Formatters
// 2.3. 从消息头到校验码前一个字节
ushort checkCode = buffer.ToCRC16_CCITT(1, checkIndex);
// 2.4. 验证校验码
- if (jT809Package.CRCCode != checkCode)
+ if (!JT809GlobalConfig.Instance.SkipCRCCode)
{
- throw new JT809Exception(JT809ErrorCode.CRC16CheckInvalid,$"{jT809Package.CRCCode.ToString()}!={checkCode.ToString()}");
+ if (jT809Package.CRCCode != checkCode)
+ {
+ throw new JT809Exception(JT809ErrorCode.CRC16CheckInvalid, $"{jT809Package.CRCCode.ToString()}!={checkCode.ToString()}");
+ }
}
jT809Package.BeginFlag = JT809BinaryExtensions.ReadByteLittle(buffer, ref offset);
// 3.初始化消息头
diff --git a/src/JT809.Protocol/JT809GlobalConfig.cs b/src/JT809.Protocol/JT809GlobalConfig.cs
index a99fe83..d772d0d 100644
--- a/src/JT809.Protocol/JT809GlobalConfig.cs
+++ b/src/JT809.Protocol/JT809GlobalConfig.cs
@@ -14,6 +14,7 @@ namespace JT809.Protocol
{
MsgSNDistributed = new DefaultMsgSNDistributedImpl();
HeaderOptions = new JT809HeaderOptions();
+ SkipCRCCode = false;
}
public static JT809GlobalConfig Instance
@@ -29,23 +30,51 @@ namespace JT809.Protocol
public IMsgSNDistributed MsgSNDistributed {get;private set;}
public JT809HeaderOptions HeaderOptions { get; private set; }
-
+ ///
+ /// 跳过校验码
+ /// 默认:false
+ ///
+ public bool SkipCRCCode { get; private set; }
+ ///
+ /// 设置加密算法实现
+ ///
+ ///
+ ///
public JT809GlobalConfig SetEncrypt(IJT809Encrypt jT809Encrypt)
{
instance.Value.Encrypt = jT809Encrypt;
return instance.Value;
}
-
+ ///
+ /// 设置消息序列号
+ ///
+ ///
+ ///
public JT809GlobalConfig SetMsgSNDistributed(IMsgSNDistributed msgSNDistributed)
{
instance.Value.MsgSNDistributed = msgSNDistributed;
return instance.Value;
}
-
+ ///
+ /// 设置头部选项
+ ///
+ ///
+ ///
public JT809GlobalConfig SetHeaderOptions(JT809HeaderOptions jT809HeaderOptions)
{
instance.Value.HeaderOptions = jT809HeaderOptions;
return instance.Value;
}
+ ///
+ /// 设置跳过校验码
+ /// 场景:测试的时候,可能需要收到改数据,所以测试的时候有用
+ ///
+ ///
+ ///
+ public JT809GlobalConfig SetSkipCRCCode(bool skipCRCCode)
+ {
+ instance.Value.SkipCRCCode = skipCRCCode;
+ return instance.Value;
+ }
}
}
diff --git a/src/JT809.Protocol/JT809Serializer.cs b/src/JT809.Protocol/JT809Serializer.cs
index 4c9457f..4eff7f4 100644
--- a/src/JT809.Protocol/JT809Serializer.cs
+++ b/src/JT809.Protocol/JT809Serializer.cs
@@ -12,28 +12,12 @@ namespace JT809.Protocol
{
public static byte[] Serialize(JT809Package jT809Package, int minBufferSize = 4096)
{
- var formatter = JT809FormatterExtensions.GetFormatter();
- var pool = MemoryPool.Shared;
- IMemoryOwner buffer = pool.Rent(minBufferSize);
- try
- {
- var len = formatter.Serialize(buffer, 0, jT809Package);
- return buffer.Memory.Slice(0, len).ToArray();
- }
- finally
- {
- // 源码:System.Memory.MemoryPool
- // private static readonly MemoryPool s_shared = new ArrayMemoryPool();
- // 单例内存池 不需要手动释放资源
- // buffer.Dispose() 相当于调用ArrayPool.Shared.Return(array)
- buffer.Dispose();
- }
+ return Serialize(jT809Package, minBufferSize);
}
public static JT809Package Deserialize(ReadOnlySpan bytes)
{
- var formatter = JT809FormatterExtensions.GetFormatter();
- return formatter.Deserialize(bytes, out int readSize);
+ return Deserialize(bytes);
}
public static byte[] Serialize(T obj, int minBufferSize = 4096)