github.com/sagernet/sing@v0.4.0-beta.19.0.20240518125136-f67a0988a636/common/network/direct.go (about) 1 package network 2 3 import ( 4 "github.com/sagernet/sing/common/buf" 5 M "github.com/sagernet/sing/common/metadata" 6 ) 7 8 type ReadWaitable interface { 9 InitializeReadWaiter(options ReadWaitOptions) (needCopy bool) 10 } 11 12 type ReadWaitOptions struct { 13 FrontHeadroom int 14 RearHeadroom int 15 MTU int 16 } 17 18 func (o ReadWaitOptions) NeedHeadroom() bool { 19 return o.FrontHeadroom > 0 || o.RearHeadroom > 0 20 } 21 22 func (o ReadWaitOptions) NewBuffer() *buf.Buffer { 23 return o.newBuffer(buf.BufferSize) 24 } 25 26 func (o ReadWaitOptions) NewPacketBuffer() *buf.Buffer { 27 return o.newBuffer(buf.UDPBufferSize) 28 } 29 30 func (o ReadWaitOptions) newBuffer(defaultBufferSize int) *buf.Buffer { 31 var bufferSize int 32 if o.MTU > 0 { 33 bufferSize = o.MTU + o.FrontHeadroom + o.RearHeadroom 34 } else { 35 bufferSize = defaultBufferSize 36 } 37 buffer := buf.NewSize(bufferSize) 38 if o.FrontHeadroom > 0 { 39 buffer.Resize(o.FrontHeadroom, 0) 40 } 41 if o.RearHeadroom > 0 { 42 buffer.Reserve(o.RearHeadroom) 43 } 44 return buffer 45 } 46 47 func (o ReadWaitOptions) PostReturn(buffer *buf.Buffer) { 48 if o.RearHeadroom > 0 { 49 buffer.OverCap(o.RearHeadroom) 50 } 51 } 52 53 type ReadWaiter interface { 54 ReadWaitable 55 WaitReadBuffer() (buffer *buf.Buffer, err error) 56 } 57 58 type ReadWaitCreator interface { 59 CreateReadWaiter() (ReadWaiter, bool) 60 } 61 62 type PacketReadWaiter interface { 63 ReadWaitable 64 WaitReadPacket() (buffer *buf.Buffer, destination M.Socksaddr, err error) 65 } 66 67 type PacketReadWaitCreator interface { 68 CreateReadWaiter() (PacketReadWaiter, bool) 69 }