示例#1
0
 public static ByteBuf Unzip(ByteBuf data)
 {
     try
     {
         SimpleZlib ds  = new SimpleZlib(new MemoryStream(data.buf, 0, data.len), CompressionMode.Decompress);
         ByteBuf    ret = null;
         ByteBuf    buf = ByteCache.Alloc(1024);
         int        len = ds.Read(buf.buf, 0, 1024);
         while (len > 0)
         {
             buf.len = len;
             if (ret == null)
             {
                 ret = ByteCache.Alloc(len);
             }
             else if (ret.maxSize < len + ret.len)
             {
                 ret = ByteCache.Reserve(ret, len + ret.len);
             }
             ret.Append(buf);
             len = ds.Read(buf.buf, 0, 1024);
         }
         ds.Close();
         return(ret);
     }
     catch (Exception e)
     {
         Debugger.LogError(e);
     }
     return(null);
 }
示例#2
0
        protected ByteBuf EncodeAndPackage(Protocol p)
        {
            ByteBuf buf = p.Serialize();
            ByteBuf zip = SimpleZlibExt.Zip(buf);
            //ByteBuf zip = DotNetZlibExt.ZipUnsafe(buf);
            ByteBuf ret = ByteCache.Alloc(zip.len + 4);

            ret.Write(zip.len, 0);
            ret.len = 4;
            ret.Append(zip);
            buf.Dispose();
            zip.Dispose();
            return(ret);
        }
示例#3
0
        private void HandleReceive(ByteBuf data)
        {
            if (!remainder.IsEmpty)
            {
                if (!remainder.CanAppend(data))
                {
                    remainder = ByteCache.Reserve(remainder, remainder.len + data.len);
                }
                remainder.Append(data);
                data      = remainder;
                remainder = ByteCache.Alloc(1024);
            }
            int cursor = 0;

            while (data.len - cursor > 4)
            {
                int length = data.GetInt(cursor);
                if (data.len - cursor >= 4 + length)
                {
                    cursor += 4;
                    HandleRawData(data, cursor, length);
                    cursor += length;
                }
                else
                {
                    break;
                }
            }
            if (data.len > cursor)
            {
                if (!remainder.CanAppend(data, cursor))
                {
                    remainder = ByteCache.Reserve(remainder, remainder.len + data.len - cursor);
                }
                remainder.Append(data, cursor);
            }
        }