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

     1  package bufio
     2  
     3  import (
     4  	"github.com/sagernet/sing/common"
     5  	"github.com/sagernet/sing/common/buf"
     6  	N "github.com/sagernet/sing/common/network"
     7  )
     8  
     9  type appendConn struct {
    10  	N.ExtendedConn
    11  	reader N.ExtendedReader
    12  	writer N.ExtendedWriter
    13  }
    14  
    15  func NewAppendConn(conn N.ExtendedConn, reader N.ExtendedReader, writer N.ExtendedWriter) N.ExtendedConn {
    16  	return &appendConn{
    17  		ExtendedConn: conn,
    18  		reader:       reader,
    19  		writer:       writer,
    20  	}
    21  }
    22  
    23  func (c *appendConn) Read(p []byte) (n int, err error) {
    24  	if c.reader == nil {
    25  		return c.ExtendedConn.Read(p)
    26  	} else {
    27  		return c.reader.Read(p)
    28  	}
    29  }
    30  
    31  func (c *appendConn) ReadBuffer(buffer *buf.Buffer) error {
    32  	if c.reader == nil {
    33  		return c.ExtendedConn.ReadBuffer(buffer)
    34  	} else {
    35  		return c.reader.ReadBuffer(buffer)
    36  	}
    37  }
    38  
    39  func (c *appendConn) Write(p []byte) (n int, err error) {
    40  	if c.writer == nil {
    41  		return c.ExtendedConn.Write(p)
    42  	} else {
    43  		return c.writer.Write(p)
    44  	}
    45  }
    46  
    47  func (c *appendConn) WriteBuffer(buffer *buf.Buffer) error {
    48  	if c.writer == nil {
    49  		return c.ExtendedConn.WriteBuffer(buffer)
    50  	} else {
    51  		return c.writer.WriteBuffer(buffer)
    52  	}
    53  }
    54  
    55  func (c *appendConn) Close() error {
    56  	return common.Close(
    57  		c.ExtendedConn,
    58  		c.reader,
    59  		c.writer,
    60  	)
    61  }
    62  
    63  func (c *appendConn) UpstreamReader() any {
    64  	return c.reader
    65  }
    66  
    67  func (c *appendConn) ReaderReplaceable() bool {
    68  	return true
    69  }
    70  
    71  func (c *appendConn) UpstreamWriter() any {
    72  	return c.writer
    73  }
    74  
    75  func (c *appendConn) WriterReplaceable() bool {
    76  	return true
    77  }
    78  
    79  func (c *appendConn) Upstream() any {
    80  	return c.ExtendedConn
    81  }