connection.go 568 B

12345678910111213141516171819202122232425262728293031323334
  1. package internet
  2. import (
  3. "net"
  4. "v2ray.com/core/features/stats"
  5. )
  6. type Connection interface {
  7. net.Conn
  8. }
  9. type StatCouterConnection struct {
  10. Connection
  11. Uplink stats.Counter
  12. Downlink stats.Counter
  13. }
  14. func (c *StatCouterConnection) Read(b []byte) (int, error) {
  15. nBytes, err := c.Connection.Read(b)
  16. if c.Uplink != nil {
  17. c.Uplink.Add(int64(nBytes))
  18. }
  19. return nBytes, err
  20. }
  21. func (c *StatCouterConnection) Write(b []byte) (int, error) {
  22. nBytes, err := c.Connection.Write(b)
  23. if c.Downlink != nil {
  24. c.Downlink.Add(int64(nBytes))
  25. }
  26. return nBytes, err
  27. }