forked from anacrolix/torrent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
conn_stats.go
66 lines (56 loc) · 1.24 KB
/
conn_stats.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package torrent
import (
"io"
"sync"
pp "github.com/anacrolix/torrent/peer_protocol"
)
type ConnStats struct {
// 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
}
func (cs *ConnStats) wroteMsg(msg *pp.Message) {
switch msg.Type {
case pp.Piece:
cs.ChunksWritten++
cs.DataBytesWritten += int64(len(msg.Piece))
}
}
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
}