using HighWayIot.Log4net; using HighWayIot.Repository.domain; using HighWayIot.Repository.service.Impl; using HighWayIot.TouchSocket.Entity; using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using TouchSocket.Core; namespace HighWayIot.TouchSocket { /// /// 服务端接收报文分析 /// public class ServerBufferAnalysis { private static readonly Lazy lazy = new Lazy(() => new ServerBufferAnalysis()); public static ServerBufferAnalysis Instance => lazy.Value; private LogHelper logHelper = LogHelper.Instance; List tempData = new List(); /// /// 基础数据转包 /// public BaseMessagePack BaseUnpackServerBufferAnalysis(byte[] bytes) { BaseMessagePack unpackEntity = new BaseMessagePack(); int index = 1; //报文完整性验证 if (!MessageCheck(bytes)) { tempData.AddRange(bytes.ToList()); bytes = tempData.ToArray(); if (!MessageCheck(bytes)) { return null; } else { tempData.Clear(); } } //功能码 Array.Copy(bytes, index, unpackEntity.FunctionCode, 0, 2); Array.Reverse(unpackEntity.FunctionCode); index += 2; //报文字节长度 Array.Copy(bytes, index, unpackEntity.Length, 0, 2); Array.Reverse(unpackEntity.Length); index += 2; //版本号 unpackEntity.VersionCode = bytes[index]; index++; //报文序列号 Array.Copy(bytes, index, unpackEntity.SerialCode, 0, 4); Array.Reverse(unpackEntity.SerialCode); index += 4; //时间戳 Array.Copy(bytes, index, unpackEntity.TimeStamp, 0, 8); Array.Reverse(unpackEntity.TimeStamp); index += 8; //数据内容 int dataLength = BitConverter.ToUInt16(unpackEntity.Length, 0) - 13; unpackEntity.DataContent = new byte[dataLength]; Array.Copy(bytes, index, unpackEntity.DataContent, 0, dataLength); index += dataLength; //校验值 unpackEntity.CheckSum = bytes[index]; //核对校验值 byte[] checkBytes = new byte[bytes.Length - 4]; Array.Copy(bytes, 1, checkBytes, 0, bytes.Length - 4); if (unpackEntity.CheckSum != CalculateCheckSum(checkBytes)) { logHelper.Error("报文校验失败!"); return null; } //到工厂分类解析 return unpackEntity; } /// /// 基础数据封包 /// public byte[] BasePackedServerBufferAnalysis(BaseMessagePack message) { int length = BitConverter.ToUInt16(message.Length, 0) + 8; byte[] bytes = new byte[length]; int index = 0; //起始字符 bytes[index] = 0x3A; index++; //功能码 Array.Reverse(message.FunctionCode); Array.Copy(message.FunctionCode, 0, bytes, index, 2); index += 2; //报文字节长度 Array.Reverse(message.Length); Array.Copy(message.Length, 0, bytes, index, 2); index += 2; //版本号 bytes[index] = message.VersionCode; index++; //报文序列号 Array.Reverse(message.SerialCode); Array.Copy(message.SerialCode, 0, bytes, index, 4); index += 4; //时间戳 Array.Reverse(message.TimeStamp); Array.Copy(message.TimeStamp, 0, bytes, index, 8); index += 8; //数据内容 Array.Reverse(message.Length); int dataLength = BitConverter.ToUInt16(message.Length, 0) - 13; Array.Copy(message.DataContent, 0, bytes, index, dataLength); index += dataLength; //校验值 byte[] checkBytes = new byte[bytes.Length - 4]; Array.Copy(bytes, 1, checkBytes, 0, bytes.Length - 4); bytes[index] = CalculateCheckSum(checkBytes); //结束字符1 bytes[index] = 0x0D; //结束字符2 bytes[index] = 0x0A; return bytes; } /// /// 报文完整性验证 /// /// /// true为通过 false为失败 private bool MessageCheck(byte[] message) { bool result = true; if (message[0] != 0x3A || message[message.Length - 1] != 0x0A || message[message.Length - 2] != 0x0D) { result = false; } return result; } /// /// 和校验 /// /// /// /// public byte CalculateCheckSum(byte[] bytes) { int length = bytes.Length; int check = 0; for (int i = 0; i < length; i++) { check += bytes[i]; } byte result = (byte)(255 - (check % 255)); return result; } } }