github.com/v2fly/v2ray-core/v5@v5.16.2-0.20240507031116-8191faa6e095/transport/internet/connection.go (about)

     1  package internet
     2  
     3  import (
     4  	"net"
     5  
     6  	"github.com/v2fly/v2ray-core/v5/common"
     7  	"github.com/v2fly/v2ray-core/v5/features/stats"
     8  )
     9  
    10  type Connection interface {
    11  	net.Conn
    12  }
    13  
    14  type AbstractPacketConnReader interface {
    15  	ReadFrom(p []byte) (n int, addr net.Addr, err error)
    16  }
    17  
    18  type AbstractPacketConnWriter interface {
    19  	WriteTo(p []byte, addr net.Addr) (n int, err error)
    20  }
    21  
    22  type AbstractPacketConn interface {
    23  	AbstractPacketConnReader
    24  	AbstractPacketConnWriter
    25  	common.Closable
    26  }
    27  
    28  type PacketConn interface {
    29  	AbstractPacketConn
    30  	net.PacketConn
    31  }
    32  
    33  type StatCouterConnection struct {
    34  	Connection
    35  	ReadCounter  stats.Counter
    36  	WriteCounter stats.Counter
    37  }
    38  
    39  func (c *StatCouterConnection) Read(b []byte) (int, error) {
    40  	nBytes, err := c.Connection.Read(b)
    41  	if c.ReadCounter != nil {
    42  		c.ReadCounter.Add(int64(nBytes))
    43  	}
    44  
    45  	return nBytes, err
    46  }
    47  
    48  func (c *StatCouterConnection) Write(b []byte) (int, error) {
    49  	nBytes, err := c.Connection.Write(b)
    50  	if c.WriteCounter != nil {
    51  		c.WriteCounter.Add(int64(nBytes))
    52  	}
    53  	return nBytes, err
    54  }