github.com/xtls/xray-core@v1.8.12-0.20240518155711-3168d27b0bdb/transport/internet/stat/connection.go (about)

     1  package stat
     2  
     3  import (
     4  	"net"
     5  
     6  	"github.com/xtls/xray-core/features/stats"
     7  )
     8  
     9  type Connection interface {
    10  	net.Conn
    11  }
    12  
    13  type CounterConnection struct {
    14  	Connection
    15  	ReadCounter  stats.Counter
    16  	WriteCounter stats.Counter
    17  }
    18  
    19  func (c *CounterConnection) Read(b []byte) (int, error) {
    20  	nBytes, err := c.Connection.Read(b)
    21  	if c.ReadCounter != nil {
    22  		c.ReadCounter.Add(int64(nBytes))
    23  	}
    24  
    25  	return nBytes, err
    26  }
    27  
    28  func (c *CounterConnection) Write(b []byte) (int, error) {
    29  	nBytes, err := c.Connection.Write(b)
    30  	if c.WriteCounter != nil {
    31  		c.WriteCounter.Add(int64(nBytes))
    32  	}
    33  	return nBytes, err
    34  }