metainfo/info.go | 3 --- piece.go | 10 +++++----- torrent.go | 23 +++++++++++------------ diff --git a/metainfo/info.go b/metainfo/info.go index 7fbc88e9546bfa79a2918dd6a76c683406139006..a7e23a7f07843360a2f92eff81bd278d86305ca4 100644 --- a/metainfo/info.go +++ b/metainfo/info.go @@ -126,9 +126,6 @@ return } func (info *Info) NumPieces() int { - if len(info.Pieces)%20 != 0 { - panic(len(info.Pieces)) - } return len(info.Pieces) / 20 } diff --git a/piece.go b/piece.go index 7a0c2f1789f4b169c694724a9e981db478717b06..e09e00c8da5160dbcd14ffb92e53b7b88ce8c6fd 100644 --- a/piece.go +++ b/piece.go @@ -128,11 +128,11 @@ return chunkIndexSpec(chunk, p.length(), p.chunkSize()) } func (p *Piece) numDirtyBytes() (ret pp.Integer) { - defer func() { - if ret > p.length() { - panic("too many dirty bytes") - } - }() + // defer func() { + // if ret > p.length() { + // panic("too many dirty bytes") + // } + // }() numRegularDirtyChunks := p.numDirtyChunks() if p.chunkIndexDirty(p.numChunks() - 1) { numRegularDirtyChunks-- diff --git a/torrent.go b/torrent.go index 84691af65ca0a8ee10699981269841566cc53427..3374778b2c12bedd0b362fdc691faf14a896a846 100644 --- a/torrent.go +++ b/torrent.go @@ -558,9 +558,11 @@ return t.bytesLeft() } func (t *Torrent) bytesLeft() (left int64) { - for i := 0; i < t.numPieces(); i++ { - left += int64(t.pieces[i].bytesLeft()) - } + bitmap.Flip(t.completedPieces, 0, t.numPieces()).IterTyped(func(piece int) bool { + p := t.pieces[piece] + left += int64(p.length() - p.numDirtyBytes()) + return true + }) return } @@ -658,17 +660,14 @@ // Peer is known to support encryption. SupportsEncryption bool } -func (t *Torrent) pieceLength(piece int) (len_ pp.Integer) { - if piece < 0 || piece >= t.info.NumPieces() { - return - } +func (t *Torrent) pieceLength(piece int) pp.Integer { if piece == t.numPieces()-1 { - len_ = pp.Integer(t.length % t.info.PieceLength) + ret := pp.Integer(t.length % t.info.PieceLength) + if ret != 0 { + return ret + } } - if len_ == 0 { - len_ = pp.Integer(t.info.PieceLength) - } - return + return pp.Integer(t.info.PieceLength) } func (t *Torrent) hashPiece(piece int) (ret metainfo.Hash) {