Skip to content

Commit

Permalink
Add UoT read waiter support
Browse files Browse the repository at this point in the history
  • Loading branch information
H1JK committed Nov 18, 2023
1 parent 256fafc commit 0457413
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
1 change: 0 additions & 1 deletion common/bufio/copy_direct_posix.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,6 @@ func (w *syscallPacketReadWaiter) InitializeReadWaiter(newBuffer func() *buf.Buf
buffer.Truncate(readN)
} else {
buffer.Release()
buffer = nil
}
if w.readErr == syscall.EAGAIN {
return false
Expand Down
33 changes: 33 additions & 0 deletions common/uot/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,17 @@ import (
N "github.com/sagernet/sing/common/network"
)

var (
_ N.NetPacketConn = (*Conn)(nil)
_ N.PacketReadWaiter = (*Conn)(nil)
)

type Conn struct {
net.Conn
isConnect bool
destination M.Socksaddr
writer N.VectorisedWriter
newBuffer func() *buf.Buffer
}

func NewConn(conn net.Conn, request Request) *Conn {
Expand Down Expand Up @@ -141,6 +147,33 @@ func (c *Conn) WritePacket(buffer *buf.Buffer, destination M.Socksaddr) error {
return c.writer.WriteVectorised([]*buf.Buffer{header, buffer})
}

func (c *Conn) InitializeReadWaiter(newBuffer func() *buf.Buffer) {
c.newBuffer = newBuffer
}

func (c *Conn) WaitReadPacket() (destination M.Socksaddr, err error) {
if c.isConnect {
destination = c.destination
} else {
destination, err = AddrParser.ReadAddrPort(c.Conn)
if err != nil {
return
}
}
var length uint16
err = binary.Read(c.Conn, binary.BigEndian, &length)
if err != nil {
return
}
buffer := c.newBuffer()
_, err = buffer.ReadFullFrom(c.Conn, int(length))
if err != nil {
buffer.Release()
return M.Socksaddr{}, E.Cause(err, "UoT read")
}
return
}

func (c *Conn) NeedAdditionalReadDeadline() bool {
return true
}
Expand Down

0 comments on commit 0457413

Please sign in to comment.