2014-04-21 03:49:47 -07:00
|
|
|
package protocol
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"sync/atomic"
|
|
|
|
)
|
|
|
|
|
|
|
|
type countingReader struct {
|
|
|
|
io.Reader
|
|
|
|
tot uint64
|
|
|
|
}
|
|
|
|
|
2014-05-24 12:34:11 -07:00
|
|
|
var (
|
|
|
|
totalIncoming uint64
|
|
|
|
totalOutgoing uint64
|
|
|
|
)
|
|
|
|
|
2014-04-21 03:49:47 -07:00
|
|
|
func (c *countingReader) Read(bs []byte) (int, error) {
|
|
|
|
n, err := c.Reader.Read(bs)
|
|
|
|
atomic.AddUint64(&c.tot, uint64(n))
|
2014-05-24 12:34:11 -07:00
|
|
|
atomic.AddUint64(&totalIncoming, uint64(n))
|
2014-04-21 03:49:47 -07:00
|
|
|
return n, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *countingReader) Tot() uint64 {
|
|
|
|
return atomic.LoadUint64(&c.tot)
|
|
|
|
}
|
|
|
|
|
|
|
|
type countingWriter struct {
|
|
|
|
io.Writer
|
|
|
|
tot uint64
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *countingWriter) Write(bs []byte) (int, error) {
|
|
|
|
n, err := c.Writer.Write(bs)
|
|
|
|
atomic.AddUint64(&c.tot, uint64(n))
|
2014-05-24 12:34:11 -07:00
|
|
|
atomic.AddUint64(&totalOutgoing, uint64(n))
|
2014-04-21 03:49:47 -07:00
|
|
|
return n, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *countingWriter) Tot() uint64 {
|
|
|
|
return atomic.LoadUint64(&c.tot)
|
|
|
|
}
|
2014-05-24 12:34:11 -07:00
|
|
|
|
|
|
|
func TotalInOut() (uint64, uint64) {
|
|
|
|
return atomic.LoadUint64(&totalIncoming), atomic.LoadUint64(&totalOutgoing)
|
|
|
|
}
|