diff --git a/common/buf/buffer.go b/common/buf/buffer.go index 02448e3..3315f45 100644 --- a/common/buf/buffer.go +++ b/common/buf/buffer.go @@ -331,6 +331,10 @@ func (b *Buffer) Len() int { } func (b *Buffer) Cap() int { + return b.capacity +} + +func (b *Buffer) RawCap() int { return len(b.data) } @@ -338,10 +342,6 @@ func (b *Buffer) Bytes() []byte { return b.data[b.start:b.end] } -func (b *Buffer) Slice() []byte { - return b.data[:b.capacity] -} - func (b *Buffer) From(n int) []byte { return b.data[b.start+n : b.end] } diff --git a/common/bufio/copy.go b/common/bufio/copy.go index 914865a..f8e63cd 100644 --- a/common/bufio/copy.go +++ b/common/bufio/copy.go @@ -295,17 +295,16 @@ func WritePacketWithPool(originSource N.PacketReader, destinationConn N.PacketWr var notFirstTime bool for _, packetBuffer := range packetBuffers { buffer := buf.NewPacket() - readBufferRaw := buffer.Slice() - readBuffer := buf.With(readBufferRaw[:len(readBufferRaw)-rearHeadroom]) - readBuffer.Resize(frontHeadroom, 0) - _, err = readBuffer.Write(packetBuffer.Buffer.Bytes()) + buffer.Resize(frontHeadroom, 0) + buffer.Reserve(rearHeadroom) + _, err = buffer.Write(packetBuffer.Buffer.Bytes()) packetBuffer.Buffer.Release() if err != nil { buffer.Release() continue } - dataLen := readBuffer.Len() - buffer.Resize(readBuffer.Start(), dataLen) + dataLen := buffer.Len() + buffer.OverCap(rearHeadroom) err = destinationConn.WritePacket(buffer, packetBuffer.Destination) if err != nil { buffer.Leak()