diff --git a/common/bufio/deadline/packet_reader.go b/common/bufio/deadline/packet_reader.go index 8244b7c..36b4e87 100644 --- a/common/bufio/deadline/packet_reader.go +++ b/common/bufio/deadline/packet_reader.go @@ -107,7 +107,7 @@ func (r *packetReader) ReadPacket(buffer *buf.Buffer) (destination M.Socksaddr, } select { case <-r.done: - go r.pipeReadFromBuffer(buffer.Cap(), buffer.Start()) + go r.pipeReadFromBuffer(buffer.FreeLen()) default: } return r.readPacket(buffer) @@ -123,9 +123,7 @@ func (r *packetReader) readPacket(buffer *buf.Buffer) (destination M.Socksaddr, } func (r *packetReader) pipeReturnFromBuffer(result *packetReadResult, buffer *buf.Buffer) (M.Socksaddr, error) { - buffer.Resize(result.buffer.Start(), 0) - n := copy(buffer.FreeBytes(), result.buffer.Bytes()) - buffer.Truncate(n) + n, _ := buffer.Write(result.buffer.Bytes()) result.buffer.Advance(n) if !result.buffer.IsEmpty() { r.result <- result @@ -136,9 +134,8 @@ func (r *packetReader) pipeReturnFromBuffer(result *packetReadResult, buffer *bu } } -func (r *packetReader) pipeReadFromBuffer(bufLen int, bufStart int) { - buffer := buf.NewSize(bufLen) - buffer.Advance(bufStart) +func (r *packetReader) pipeReadFromBuffer(pLen int) { + buffer := buf.NewSize(pLen) destination, err := r.TimeoutPacketReader.ReadPacket(buffer) r.result <- &packetReadResult{ buffer: buffer, diff --git a/common/bufio/deadline/packet_reader_fallback.go b/common/bufio/deadline/packet_reader_fallback.go index f654511..276b784 100644 --- a/common/bufio/deadline/packet_reader_fallback.go +++ b/common/bufio/deadline/packet_reader_fallback.go @@ -61,7 +61,7 @@ func (r *fallbackPacketReader) ReadPacket(buffer *buf.Buffer) (destination M.Soc destination, err = r.TimeoutPacketReader.ReadPacket(buffer) return } - go r.pipeReadFromBuffer(buffer.Cap(), buffer.Start()) + go r.pipeReadFromBuffer(buffer.FreeLen()) default: } return r.readPacket(buffer) diff --git a/common/bufio/deadline/reader.go b/common/bufio/deadline/reader.go index 366e2a0..b6d3c7d 100644 --- a/common/bufio/deadline/reader.go +++ b/common/bufio/deadline/reader.go @@ -100,7 +100,7 @@ func (r *reader) ReadBuffer(buffer *buf.Buffer) error { } select { case <-r.done: - go r.pipeReadBuffer(buffer.Cap(), buffer.Start()) + go r.pipeReadBuffer(buffer.FreeLen()) default: } return r.readBuffer(buffer) @@ -116,9 +116,7 @@ func (r *reader) readBuffer(buffer *buf.Buffer) error { } func (r *reader) pipeReturnBuffer(result *readResult, buffer *buf.Buffer) error { - buffer.Resize(result.buffer.Start(), 0) - n := copy(buffer.FreeBytes(), result.buffer.Bytes()) - buffer.Truncate(n) + n, _ := buffer.Write(result.buffer.Bytes()) result.buffer.Advance(n) if !result.buffer.IsEmpty() { r.result <- result @@ -129,9 +127,8 @@ func (r *reader) pipeReturnBuffer(result *readResult, buffer *buf.Buffer) error } } -func (r *reader) pipeReadBuffer(bufLen int, bufStart int) { - cacheBuffer := buf.NewSize(bufLen) - cacheBuffer.Advance(bufStart) +func (r *reader) pipeReadBuffer(pLen int) { + cacheBuffer := buf.NewSize(pLen) err := r.ExtendedReader.ReadBuffer(cacheBuffer) r.result <- &readResult{ buffer: cacheBuffer, diff --git a/common/bufio/deadline/reader_fallback.go b/common/bufio/deadline/reader_fallback.go index 96cc068..182ab40 100644 --- a/common/bufio/deadline/reader_fallback.go +++ b/common/bufio/deadline/reader_fallback.go @@ -58,7 +58,7 @@ func (r *fallbackReader) ReadBuffer(buffer *buf.Buffer) error { defer r.inRead.Store(false) return r.ExtendedReader.ReadBuffer(buffer) } - go r.pipeReadBuffer(buffer.Cap(), buffer.Start()) + go r.pipeReadBuffer(buffer.FreeLen()) default: } return r.readBuffer(buffer)