github.com/metacubex/sing-shadowsocks2@v0.2.0/shadowaead_2022/method_wait.go (about)

     1  package shadowaead_2022
     2  
     3  import (
     4  	"github.com/sagernet/sing/common/buf"
     5  	"github.com/sagernet/sing/common/bufio"
     6  	M "github.com/sagernet/sing/common/metadata"
     7  	N "github.com/sagernet/sing/common/network"
     8  )
     9  
    10  var _ N.ReadWaiter = (*clientConn)(nil)
    11  
    12  func (c *clientConn) InitializeReadWaiter(options N.ReadWaitOptions) (needCopy bool) {
    13  	if c.reader == nil {
    14  		c.readWaitOptions = options
    15  		return options.NeedHeadroom()
    16  	}
    17  	return c.reader.InitializeReadWaiter(options)
    18  }
    19  
    20  func (c *clientConn) WaitReadBuffer() (buffer *buf.Buffer, err error) {
    21  	if c.reader == nil {
    22  		err = c.readResponse()
    23  		if err != nil {
    24  			return
    25  		}
    26  	}
    27  	return c.reader.WaitReadBuffer()
    28  }
    29  
    30  var _ N.PacketReadWaitCreator = (*clientPacketConn)(nil)
    31  
    32  func (c *clientPacketConn) CreateReadWaiter() (N.PacketReadWaiter, bool) {
    33  	readWaiter, isReadWaiter := bufio.CreateReadWaiter(c.reader)
    34  	if !isReadWaiter {
    35  		return nil, false
    36  	}
    37  	return &clientPacketReadWaiter{c, readWaiter}, true
    38  }
    39  
    40  var _ N.PacketReadWaiter = (*clientPacketReadWaiter)(nil)
    41  
    42  type clientPacketReadWaiter struct {
    43  	*clientPacketConn
    44  	readWaiter N.ReadWaiter
    45  }
    46  
    47  func (w *clientPacketReadWaiter) InitializeReadWaiter(options N.ReadWaitOptions) (needCopy bool) {
    48  	return w.readWaiter.InitializeReadWaiter(options)
    49  }
    50  
    51  func (w *clientPacketReadWaiter) WaitReadPacket() (buffer *buf.Buffer, destination M.Socksaddr, err error) {
    52  	buffer, err = w.readWaiter.WaitReadBuffer()
    53  	if err != nil {
    54  		return
    55  	}
    56  	destination, err = w.readPacket(buffer)
    57  	if err != nil {
    58  		buffer.Release()
    59  		return nil, M.Socksaddr{}, err
    60  	}
    61  	return
    62  }