mirror of
https://github.com/SagerNet/sing.git
synced 2025-04-04 20:37:40 +03:00
Add lazy headroom
This commit is contained in:
parent
c4e9bf11fa
commit
ec72b91c12
2 changed files with 51 additions and 18 deletions
|
@ -88,10 +88,7 @@ type NetVectorisedWriterWrapper struct {
|
|||
|
||||
func (w *NetVectorisedWriterWrapper) WriteVectorised(buffers []*buf.Buffer) error {
|
||||
defer buf.ReleaseMulti(buffers)
|
||||
netBuffers := make(net.Buffers, 0, len(buffers))
|
||||
for _, buffer := range buffers {
|
||||
netBuffers = append(netBuffers, buffer.Bytes())
|
||||
}
|
||||
netBuffers := net.Buffers(buf.ToSliceMulti(buffers))
|
||||
return common.Error(netBuffers.WriteTo(w.upstream))
|
||||
}
|
||||
|
||||
|
|
|
@ -55,6 +55,8 @@ func IsSafePacketReader(reader any) ThreadSafePacketReader {
|
|||
return nil
|
||||
}
|
||||
|
||||
const DefaultHeadroom = 1024
|
||||
|
||||
type FrontHeadroom interface {
|
||||
FrontHeadroom() int
|
||||
}
|
||||
|
@ -63,28 +65,62 @@ type RearHeadroom interface {
|
|||
RearHeadroom() int
|
||||
}
|
||||
|
||||
type LazyHeadroom interface {
|
||||
LazyHeadroom() bool
|
||||
}
|
||||
|
||||
func CalculateFrontHeadroom(writer any) int {
|
||||
var headroom int
|
||||
if headroomWriter, needHeadroom := writer.(FrontHeadroom); needHeadroom {
|
||||
headroom = headroomWriter.FrontHeadroom()
|
||||
}
|
||||
if upstreamWriter, hasUpstreamWriter := writer.(WithUpstreamWriter); hasUpstreamWriter {
|
||||
headroom += CalculateFrontHeadroom(upstreamWriter.UpstreamWriter())
|
||||
} else if upstream, hasUpstream := writer.(common.WithUpstream); hasUpstream {
|
||||
headroom += CalculateFrontHeadroom(upstream.Upstream())
|
||||
for {
|
||||
if lazyRoom, isLazy := writer.(LazyHeadroom); isLazy && lazyRoom.LazyHeadroom() {
|
||||
return DefaultHeadroom
|
||||
}
|
||||
if headroomWriter, needHeadroom := writer.(FrontHeadroom); needHeadroom {
|
||||
headroom += headroomWriter.FrontHeadroom()
|
||||
}
|
||||
if upstreamWriter, hasUpstreamWriter := writer.(WithUpstreamWriter); hasUpstreamWriter {
|
||||
upstream := upstreamWriter.UpstreamWriter()
|
||||
if upstream != nil {
|
||||
writer = upstream
|
||||
continue
|
||||
}
|
||||
}
|
||||
if upstream, hasUpstream := writer.(common.WithUpstream); hasUpstream {
|
||||
upstreamWriter := upstream.Upstream()
|
||||
if upstreamWriter != nil {
|
||||
writer = upstreamWriter
|
||||
continue
|
||||
}
|
||||
}
|
||||
break
|
||||
}
|
||||
return headroom
|
||||
}
|
||||
|
||||
func CalculateRearHeadroom(writer any) int {
|
||||
var headroom int
|
||||
if headroomWriter, needHeadroom := writer.(RearHeadroom); needHeadroom {
|
||||
headroom = headroomWriter.RearHeadroom()
|
||||
}
|
||||
if upstreamWriter, hasUpstreamWriter := writer.(WithUpstreamWriter); hasUpstreamWriter {
|
||||
headroom += CalculateRearHeadroom(upstreamWriter.UpstreamWriter())
|
||||
} else if upstream, hasUpstream := writer.(common.WithUpstream); hasUpstream {
|
||||
headroom += CalculateRearHeadroom(upstream.Upstream())
|
||||
for {
|
||||
if lazyRoom, isLazy := writer.(LazyHeadroom); isLazy && lazyRoom.LazyHeadroom() {
|
||||
return DefaultHeadroom
|
||||
}
|
||||
if headroomWriter, needHeadroom := writer.(RearHeadroom); needHeadroom {
|
||||
headroom += headroomWriter.RearHeadroom()
|
||||
}
|
||||
if upstreamWriter, hasUpstreamWriter := writer.(WithUpstreamWriter); hasUpstreamWriter {
|
||||
upstream := upstreamWriter.UpstreamWriter()
|
||||
if upstream != nil {
|
||||
writer = upstream
|
||||
continue
|
||||
}
|
||||
}
|
||||
if upstream, hasUpstream := writer.(common.WithUpstream); hasUpstream {
|
||||
upstreamWriter := upstream.Upstream()
|
||||
if upstreamWriter != nil {
|
||||
writer = upstreamWriter
|
||||
continue
|
||||
}
|
||||
}
|
||||
break
|
||||
}
|
||||
return headroom
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue