github.com/sagernet/sing@v0.4.0-beta.19.0.20240518125136-f67a0988a636/common/uot/conn_wait.go (about)

     1  package uot
     2  
     3  import (
     4  	"encoding/binary"
     5  
     6  	"github.com/sagernet/sing/common/buf"
     7  	E "github.com/sagernet/sing/common/exceptions"
     8  	M "github.com/sagernet/sing/common/metadata"
     9  	N "github.com/sagernet/sing/common/network"
    10  )
    11  
    12  func (c *Conn) InitializeReadWaiter(options N.ReadWaitOptions) (needCopy bool) {
    13  	c.readWaitOptions = options
    14  	return false
    15  }
    16  
    17  func (c *Conn) WaitReadPacket() (buffer *buf.Buffer, destination M.Socksaddr, err error) {
    18  	if c.isConnect {
    19  		destination = c.destination
    20  	} else {
    21  		destination, err = AddrParser.ReadAddrPort(c.Conn)
    22  		if err != nil {
    23  			return
    24  		}
    25  	}
    26  	var length uint16
    27  	err = binary.Read(c.Conn, binary.BigEndian, &length)
    28  	if err != nil {
    29  		return
    30  	}
    31  	buffer = c.readWaitOptions.NewPacketBuffer()
    32  	_, err = buffer.ReadFullFrom(c.Conn, int(length))
    33  	if err != nil {
    34  		buffer.Release()
    35  		return nil, M.Socksaddr{}, E.Cause(err, "UoT read")
    36  	}
    37  	c.readWaitOptions.PostReturn(buffer)
    38  	return
    39  }