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); }
public ByteBuf Clone() { ByteBuf clone = ByteCache.Alloc(len); System.Buffer.BlockCopy(buf, 0, clone.buf, 0, len); clone.len = len; return(clone); }
public override ByteBuf Serialize() { string str = JsonStr(); int count = Encoding.UTF8.GetByteCount(str); ByteBuf buf = ByteCache.Alloc(count + 4); buf.Write(type(), 0); Encoding.UTF8.GetBytes(str, 0, str.Length, buf.buf, 4); buf.len = count + 4; return(buf); }
public static ByteBuf ZipUnsafe(ByteBuf toCompress) { Zip(toCompress.buf, 0, toCompress.len); int count = (int)zipOut.Length; ByteBuf buf = ByteCache.Alloc(count); zipOut.Seek(0, SeekOrigin.Begin); zipOut.Read(buf.buf, 0, count); buf.len = count; zipOut.SetLength(0); return(buf); }
public static ByteBuf UnzipUnsafe(ByteBuf data) { Unzip(data.buf, 0, data.len); int count = (int)unzipOut.Length; ByteBuf buf = ByteCache.Alloc(count); unzipOut.Seek(0, SeekOrigin.Begin); unzipOut.Read(buf.buf, 0, count); buf.len = count; unzipOut.SetLength(0); return(buf); }
public void Dispose() { if (disposed) { return; } disposed = true; if (null != ms) { ms = null; } ByteCache.Free(this); }
public static ByteBuf Zip(ByteBuf toCompress) { ByteBuf buf = ByteCache.Alloc(1024); SimpleZlib ds = new SimpleZlib(new MemoryStream(buf.buf), CompressionMode.Compress, true); ds.Write(toCompress.buf, 0, toCompress.len); MemoryStream ms = (MemoryStream)ds.BaseStream; ds.Close(); buf.len = (int)ms.Position; ms.Close(); return(buf); }
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); }
protected void HandleRawData(ByteBuf raw, int offset, int length) { if (state != NetState.CONNECTED) { return; } ByteBuf buf = ByteCache.Alloc(length); buf.Copy(raw, offset, length); Protocol p = Decode(buf); if (null != p) { HandleMessage(p); } }
private void SendAndReceive() { ByteBuf buffer = ByteCache.Alloc(1024); while (state == NetState.CONNECTED) { if (!isSending) { Send(); } if (!isReceiving) { Receive(buffer); } Thread.Sleep(1); } }
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); } }
public TcpClient() { state = NetState.DISCONNECTED; remainder = ByteCache.Alloc(1024); }