From e48e050e7f063677a978c6d7ae481d4bd3493aff Mon Sep 17 00:00:00 2001 From: R4SAS Date: Sat, 18 Dec 2021 18:31:22 +0300 Subject: [PATCH] fix announcing of incorrect left size, correctly announce downloaded size Signed-off-by: R4SAS --- lib/bittorrent/swarm/announce.go | 1 + lib/storage/fs.go | 15 ++++++++++++++- lib/storage/storage.go | 3 +++ 3 files changed, 18 insertions(+), 1 deletion(-) 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