/// <summary> /// 将缓冲队列写入缓冲区,大于10k开始压缩 /// </summary> /// <param name="head"></param> public void WriteBuffer(MessageHead head) { int stackSize = GetHeadByteSize(head); stackSize += GetStackByteSize();//包括了头部长度大小 head.TotalLength = stackSize; bool hasGzipHead = false; if (head.HasGzip && (!Enable7zip || (Enable7zip && stackSize <= Enable7zipMinByte))) { hasGzipHead = true; head.GzipLength = stackSize; WriteByte(head.GzipLength); } //输出总字节长度,公式: stackSize = len(头) + len(内容) + 4(int) WriteByte(stackSize); WriteHeadBufer(head); WriteStackToBuffer(); //判断是否有启用Gzip if (head.HasGzip && Enable7zip && !hasGzipHead && stackSize > Enable7zipMinByte) { byte[] orgData = PopBuffer(); head.GzipLength = WriteGzipCompressBuffer(orgData); } head.PacketLength = Length; Reset(); }
/// <summary> /// 读取消息头,如果有Gzip压缩时自动解压字节流 /// </summary> /// <returns></returns> /// <exception cref="OverflowException"></exception> public MessageHead ReadHeadGzip() { try { MessageHead head = new MessageHead(); head.HasGzip = true; head.PacketLength = Length; if (CheckGzipBuffer()) { byte[] gzipData = PopBuffer(); head.GzipLength = gzipData.Length; //gzip格式500+gzip( 1000+ XXXXXX) //启用7z? byte[] deZipData = LZMA.Decompress(gzipData, 0);// GzipUtils.DeCompress(gzipData, 0, gzipData.Length); WriteByte(deZipData); Reset(); } head.TotalLength = ReadInt(); head.ErrorCode = ReadInt(); head.MsgId = ReadInt(); head.ErrorInfo = ReadString(); head.Action = ReadInt(); head.St = ReadString(); return(head); } catch (Exception ex) { string error = string.Format("read to {0}/{1}pos error,\r\nbytes:{2}", Offset, Length, ToHexString()); throw new OverflowException(error, ex); } }
private void WriteHeadBufer(MessageHead head) { WriteByte(head.ErrorCode); WriteByte(head.MsgId); WriteByte(head.ErrorInfo); WriteByte(head.Action); WriteByte(head.St); }
private int GetHeadByteSize(MessageHead head) { int length = 0; length += GetByteSize(head.ErrorCode); length += GetByteSize(head.MsgId); length += GetByteSize(head.ErrorInfo); length += GetByteSize(head.Action); length += GetByteSize(head.St); return(length); }
/// <summary> /// 读取消息头 /// </summary> /// <returns></returns> public MessageHead ReadHead() { MessageHead head = new MessageHead(); head.PacketLength = Length; head.TotalLength = ReadInt(); head.ErrorCode = ReadInt(); head.MsgId = ReadInt(); head.ErrorInfo = ReadString(); head.Action = ReadInt(); head.St = ReadString(); return(head); }