FedP2P/conn_stats.go

67 lines
1.2 KiB
Go
Raw Normal View History

2016-07-05 13:52:33 +08:00
package torrent
import (
2016-07-12 14:42:04 +08:00
"io"
"sync"
2016-07-05 13:52:33 +08:00
pp "github.com/anacrolix/torrent/peer_protocol"
)
type ConnStats struct {
2016-07-12 14:42:04 +08:00
// Torrent "piece" messages, or data chunks.
ChunksWritten int64 // Num piece messages sent.
ChunksRead int64
// Total bytes on the wire. Includes handshakes and encryption.
BytesWritten int64
BytesRead int64
// Data bytes, actual torrent data.
DataBytesWritten int64
DataBytesRead int64
2016-07-05 13:52:33 +08:00
}
2016-07-12 14:42:04 +08:00
func (cs *ConnStats) wroteMsg(msg *pp.Message) {
2016-07-05 13:52:33 +08:00
switch msg.Type {
case pp.Piece:
2016-07-12 14:42:04 +08:00
cs.ChunksWritten++
cs.DataBytesWritten += int64(len(msg.Piece))
2016-07-05 13:52:33 +08:00
}
}
2016-07-12 14:42:04 +08:00
func (cs *ConnStats) readMsg(msg *pp.Message) {
switch msg.Type {
case pp.Piece:
cs.ChunksRead++
cs.DataBytesRead += int64(len(msg.Piece))
}
}
func (cs *ConnStats) wroteBytes(n int64) {
cs.BytesWritten += n
}
func (cs *ConnStats) readBytes(n int64) {
cs.BytesRead += n
}
type connStatsReadWriter struct {
rw io.ReadWriter
l sync.Locker
c *connection
}
func (me connStatsReadWriter) Write(b []byte) (n int, err error) {
n, err = me.rw.Write(b)
me.l.Lock()
me.c.wroteBytes(int64(n))
me.l.Unlock()
return
}
func (me connStatsReadWriter) Read(b []byte) (n int, err error) {
n, err = me.rw.Read(b)
me.l.Lock()
me.c.readBytes(int64(n))
me.l.Unlock()
return
2016-07-05 13:52:33 +08:00
}