Skip to content

Commit

Permalink
Merge pull request #95 from r4sas/announce-fix
Browse files Browse the repository at this point in the history
Announcing fixes
  • Loading branch information
majestrate authored Dec 18, 2021
2 parents 7cd8d05 + e48e050 commit 7f80f01
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
1 change: 1 addition & 0 deletions lib/bittorrent/swarm/announce.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down
15 changes: 14 additions & 1 deletion lib/storage/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
3 changes: 3 additions & 0 deletions lib/storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 7f80f01

Please sign in to comment.