Fix buffer

Will be merged into f0be1a9e
This commit is contained in:
世界 2023-12-08 18:06:50 +08:00
parent 0f7de716ac
commit c7ef05a85b
No known key found for this signature in database
GPG key ID: CD109927C34A63C4
2 changed files with 9 additions and 10 deletions

View file

@ -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]
}

View file

@ -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()