mirror of
https://github.com/SagerNet/sing.git
synced 2025-04-03 20:07:38 +03:00
38 lines
846 B
Go
38 lines
846 B
Go
package udpnat
|
|
|
|
import (
|
|
"io"
|
|
|
|
"github.com/sagernet/sing/common/buf"
|
|
M "github.com/sagernet/sing/common/metadata"
|
|
N "github.com/sagernet/sing/common/network"
|
|
)
|
|
|
|
var _ N.PacketReadWaiter = (*conn)(nil)
|
|
|
|
func (c *conn) InitializeReadWaiter(options N.ReadWaitOptions) (needCopy bool) {
|
|
c.readWaitOptions = options
|
|
return false
|
|
}
|
|
|
|
func (c *conn) WaitReadPacket() (buffer *buf.Buffer, destination M.Socksaddr, err error) {
|
|
select {
|
|
case p := <-c.data:
|
|
if c.readWaitOptions.NeedHeadroom() {
|
|
buffer = c.readWaitOptions.NewPacketBuffer()
|
|
_, err = buffer.Write(p.data.Bytes())
|
|
if err != nil {
|
|
buffer.Release()
|
|
return
|
|
}
|
|
c.readWaitOptions.PostReturn(buffer)
|
|
p.data.Release()
|
|
} else {
|
|
buffer = p.data
|
|
}
|
|
destination = p.destination
|
|
return
|
|
case <-c.ctx.Done():
|
|
return nil, M.Socksaddr{}, io.ErrClosedPipe
|
|
}
|
|
}
|