protected Protocol Decode(ByteBuf raw) { ByteBuf data = SimpleZlibExt.Unzip(raw); //ByteBuf data = DotNetZlibExt.UnzipUnsafe(raw); int prtclType = data.GetInt(0); Protocol p = ProtocolMapping.Instance.CreatePrtcl(prtclType); if (null != p) { p.Deserialize(data, 4, data.len - 4); } raw.Dispose(); data.Dispose(); return(p); }
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); } }