diff --git a/lib/bittorrent/swarm/announce.go b/lib/bittorrent/swarm/announce.go index a236579..b25cdff 100644 --- a/lib/bittorrent/swarm/announce.go +++ b/lib/bittorrent/swarm/announce.go @@ -31,6 +31,7 @@ func (a *torrentAnnounce) tryAnnounce(ev tracker.Event) (err error) { PeerID: a.t.id, Event: ev, NumWant: DefaultAnnounceNumWant, + Downloaded: a.t.st.DownloadedSize(), Left: a.t.st.DownloadRemaining(), GetNetwork: a.t.Network, } diff --git a/lib/storage/fs.go b/lib/storage/fs.go index ad12557..98bdcb2 100644 --- a/lib/storage/fs.go +++ b/lib/storage/fs.go @@ -238,13 +238,26 @@ func (t *fsTorrent) ensureBitfield() { } } +func (t *fsTorrent) DownloadedSize() (r uint64) { + if t.meta == nil { + return + } + bf := t.Bitfield() + r = uint64(bf.CountSet()) * uint64(t.meta.Info.PieceLength) + return +} + func (t *fsTorrent) DownloadRemaining() (r uint64) { if t.meta == nil { return } bf := t.Bitfield() have := uint64(bf.CountSet()) * uint64(t.meta.Info.PieceLength) - r = t.meta.TotalSize() - have + if have > t.meta.TotalSize() { + r = 0 + } else { + r = t.meta.TotalSize() - have + } return } diff --git a/lib/storage/storage.go b/lib/storage/storage.go index a12b0d8..d7d679d 100644 --- a/lib/storage/storage.go +++ b/lib/storage/storage.go @@ -41,6 +41,9 @@ type Torrent interface { // get bitfield, if cached return cache otherwise compute and cache Bitfield() *bittorrent.Bitfield + // get number of bytes we already downloaded + DownloadedSize() uint64 + // get number of bytes remaining we need to download DownloadRemaining() uint64