X-Git-Url: http://www.git.stargrave.org/?a=blobdiff_plain;f=conn_stats.go;h=0c5bfc784e88d4ce142cff68bb552e681cac9e1e;hb=HEAD;hp=67188acb0e1fd10b6c441d0b80b3be8cd411278f;hpb=fefeef4ee9eb72f1d245625eac03c289886121ec;p=btrtrc.git diff --git a/conn_stats.go b/conn_stats.go index 67188acb..0c5bfc78 100644 --- a/conn_stats.go +++ b/conn_stats.go @@ -1,6 +1,7 @@ package torrent import ( + "encoding/json" "fmt" "io" "reflect" @@ -9,31 +10,33 @@ import ( pp "github.com/anacrolix/torrent/peer_protocol" ) -// Various connection-level metrics. At the Torrent level these are -// aggregates. Chunks are messages with data payloads. Data is actual torrent -// content without any overhead. Useful is something we needed locally. -// Unwanted is something we didn't ask for (but may still be useful). Written -// is things sent to the peer, and Read is stuff received from them. +// Various connection-level metrics. At the Torrent level these are aggregates. Chunks are messages +// with data payloads. Data is actual torrent content without any overhead. Useful is something we +// needed locally. Unwanted is something we didn't ask for (but may still be useful). Written is +// things sent to the peer, and Read is stuff received from them. Due to the implementation of +// Count, must be aligned on some platforms: See https://github.com/anacrolix/torrent/issues/262. type ConnStats struct { // Total bytes on the wire. Includes handshakes and encryption. BytesWritten Count BytesWrittenData Count - BytesRead Count - BytesReadData Count - BytesReadUsefulData Count + BytesRead Count + BytesReadData Count + BytesReadUsefulData Count + BytesReadUsefulIntendedData Count ChunksWritten Count - ChunksRead Count - ChunksReadUseful Count - ChunksReadUnwanted Count + ChunksRead Count + ChunksReadUseful Count + ChunksReadWasted Count + + MetadataChunksRead Count // Number of pieces data was written to, that subsequently passed verification. PiecesDirtiedGood Count - // Number of pieces data was written to, that subsequently failed - // verification. Note that a connection may not have been the sole dirtier - // of a piece. + // Number of pieces data was written to, that subsequently failed verification. Note that a + // connection may not have been the sole dirtier of a piece. PiecesDirtiedBad Count } @@ -63,6 +66,10 @@ func (me *Count) String() string { return fmt.Sprintf("%v", me.Int64()) } +func (me *Count) MarshalJSON() ([]byte, error) { + return json.Marshal(me.n) +} + func (cs *ConnStats) wroteMsg(msg *pp.Message) { // TODO: Track messages and not just chunks. switch msg.Type { @@ -72,12 +79,9 @@ func (cs *ConnStats) wroteMsg(msg *pp.Message) { } } -func (cs *ConnStats) readMsg(msg *pp.Message) { - switch msg.Type { - case pp.Piece: - cs.ChunksRead.Add(1) - cs.BytesReadData.Add(int64(len(msg.Piece))) - } +func (cs *ConnStats) receivedChunk(size int64) { + cs.ChunksRead.Add(1) + cs.BytesReadData.Add(size) } func (cs *ConnStats) incrementPiecesDirtiedGood() { @@ -97,7 +101,7 @@ func add(n int64, f func(*ConnStats) *Count) func(*ConnStats) { type connStatsReadWriter struct { rw io.ReadWriter - c *connection + c *PeerConn } func (me connStatsReadWriter) Write(b []byte) (n int, err error) {