/// <summary> /// 生成终极传送指令的方法,所有的数据均通过该方法出来 /// </summary> /// <param name="command">命令头</param> /// <param name="customer">自用自定义</param> /// <param name="token">令牌</param> /// <param name="data">字节数据</param> /// <returns>包装后的数据信息</returns> internal static byte[] CommandBytes(int command, int customer, Guid token, byte[] data) { byte[] _temp = null; int _zipped = ProtocolNoZipped; int _sendLength = 0; if (data == null) { _temp = new byte[HeadByteLength]; } else { // 加密 data = HslSecurity.ByteEncrypt(data); if (data.Length > 102400) { // 100K以上的数据,进行数据压缩 data = SoftZipped.CompressBytes(data); _zipped = ProtocolZipped; } _temp = new byte[HeadByteLength + data.Length]; _sendLength = data.Length; } BitConverter.GetBytes(command).CopyTo(_temp, 0); BitConverter.GetBytes(customer).CopyTo(_temp, 4); BitConverter.GetBytes(_zipped).CopyTo(_temp, 8); token.ToByteArray( ).CopyTo(_temp, 12); BitConverter.GetBytes(_sendLength).CopyTo(_temp, 28); if (_sendLength > 0) { Array.Copy(data, 0, _temp, 32, _sendLength); } return(_temp); }
public void SoftZipped1Test( ) { byte[] b1 = new byte[] { 0x13, 0xA6, 0x15, 0x85, 0x5B, 0x05, 0x12, 0x36, 0xF2, 0x27 }; byte[] b2 = SoftZipped.CompressBytes(b1); byte[] b3 = SoftZipped.Decompress(b2); Assert.IsTrue(SoftBasic.IsTwoBytesEquel(b1, b3)); }
/// <summary> /// 解析接收到数据,先解压缩后进行解密 /// </summary> /// <param name="head">指令头</param> /// <param name="content">指令的内容</param> /// <return>真实的数据内容</return> internal static byte[] CommandAnalysis(byte[] head, byte[] content) { if (content != null) { int _zipped = BitConverter.ToInt32(head, 8); // 先进行解压 if (_zipped == ProtocolZipped) { content = SoftZipped.Decompress(content); } // 进行解密 return(HslSecurity.ByteDecrypt(content)); } else { return(null); } }