Fix deadline reader

This commit is contained in:
世界 2023-05-30 19:42:01 +08:00
parent 49166ac427
commit 221f066dba
No known key found for this signature in database
GPG key ID: CD109927C34A63C4
4 changed files with 10 additions and 16 deletions

View file

@ -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,

View file

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

View file

@ -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,

View file

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