connection.go 571 B

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