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); }
public bool CanAppend(ByteBuf other, int offset = 0) { return(maxSize >= len + other.len - offset); }
public void Copy(ByteBuf src, int offset, int length) { System.Buffer.BlockCopy(src.buf, offset, buf, 0, length); len = length; }
public override void Deserialize(ByteBuf buf, int start, int count) { string str = Encoding.UTF8.GetString(buf.buf, start, count); Load(str); }
public static T DecodeFromByte <T>(ByteBuf buf, int start, int count) where T : JsonProtocol { string str = Encoding.UTF8.GetString(buf.buf, start, count); return(JsonUtility.FromJson <T>(str)); }
public abstract void Deserialize(ByteBuf buf, int start, int count);