Ejemplo n.º 1
0
        /// <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 && (!EnableGzip || (EnableGzip && stackSize <= EnableGzipMinByte)))
            {
                hasGzipHead     = true;
                head.GzipLength = stackSize;
                WriteByte(head.GzipLength);
            }
            //输出总字节长度,公式: stackSize = len(头) + len(内容) + 4(int)
            WriteByte(stackSize);
            WriteHeadBufer(head);
            WriteStackToBuffer();
            //判断是否有启用Gzip
            if (head.HasGzip && EnableGzip && !hasGzipHead && stackSize > EnableGzipMinByte)
            {
                byte[] orgData = PopBuffer();
                head.GzipLength = WriteGzipCompressBuffer(orgData);
            }
            head.PacketLength = Length;
            Reset();
        }
Ejemplo n.º 2
0
 /// <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)
             byte[] deZipData = 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);
     }
 }
Ejemplo n.º 3
0
 private void WriteHeadBufer(MessageHead head)
 {
     WriteByte(head.ErrorCode);
     WriteByte(head.MsgId);
     WriteByte(head.ErrorInfo);
     WriteByte(head.Action);
     WriteByte(head.St);
 }
Ejemplo n.º 4
0
        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);
        }
Ejemplo n.º 5
0
        /// <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);
        }