You've already forked torrent-client
mirror of
https://github.com/veggiedefender/torrent-client.git
synced 2025-11-06 09:29:16 +02:00
Fix bug which produced incorrect piece size
This commit is contained in:
20
p2p/p2p.go
20
p2p/p2p.go
@@ -24,6 +24,7 @@ type Torrent struct {
|
|||||||
PeerID [20]byte
|
PeerID [20]byte
|
||||||
InfoHash [20]byte
|
InfoHash [20]byte
|
||||||
PieceHashes [][20]byte
|
PieceHashes [][20]byte
|
||||||
|
PieceLength int
|
||||||
Length int
|
Length int
|
||||||
Name string
|
Name string
|
||||||
}
|
}
|
||||||
@@ -181,13 +182,20 @@ func (t *Torrent) startDownloadWorker(peer peers.Peer, workQueue chan *pieceWork
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func calculateBoundsForPiece(index, numPieces, length int) (begin int, end int) {
|
func (t *Torrent) calculateBoundsForPiece(index int) (begin int, end int) {
|
||||||
pieceLength := length / numPieces
|
begin = index * t.PieceLength
|
||||||
begin = index * pieceLength
|
end = begin + t.PieceLength
|
||||||
end = begin + pieceLength
|
if end > t.Length {
|
||||||
|
end = t.Length
|
||||||
|
}
|
||||||
return begin, end
|
return begin, end
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (t *Torrent) calculatePieceSize(index int) int {
|
||||||
|
begin, end := t.calculateBoundsForPiece(index)
|
||||||
|
return end - begin
|
||||||
|
}
|
||||||
|
|
||||||
// Download downloads the torrent. This stores the entire file in memory.
|
// Download downloads the torrent. This stores the entire file in memory.
|
||||||
func (t *Torrent) Download() ([]byte, error) {
|
func (t *Torrent) Download() ([]byte, error) {
|
||||||
log.Println("Starting download for", t.Name)
|
log.Println("Starting download for", t.Name)
|
||||||
@@ -195,7 +203,7 @@ func (t *Torrent) Download() ([]byte, error) {
|
|||||||
workQueue := make(chan *pieceWork, len(t.PieceHashes))
|
workQueue := make(chan *pieceWork, len(t.PieceHashes))
|
||||||
results := make(chan *pieceResult, len(t.PieceHashes))
|
results := make(chan *pieceResult, len(t.PieceHashes))
|
||||||
for index, hash := range t.PieceHashes {
|
for index, hash := range t.PieceHashes {
|
||||||
length := t.Length / len(t.PieceHashes)
|
length := t.calculatePieceSize(index)
|
||||||
workQueue <- &pieceWork{index, hash, length}
|
workQueue <- &pieceWork{index, hash, length}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -209,7 +217,7 @@ func (t *Torrent) Download() ([]byte, error) {
|
|||||||
donePieces := 0
|
donePieces := 0
|
||||||
for donePieces < len(t.PieceHashes) {
|
for donePieces < len(t.PieceHashes) {
|
||||||
res := <-results
|
res := <-results
|
||||||
begin, end := calculateBoundsForPiece(res.index, len(t.PieceHashes), t.Length)
|
begin, end := t.calculateBoundsForPiece(res.index)
|
||||||
copy(buf[begin:end], res.buf)
|
copy(buf[begin:end], res.buf)
|
||||||
donePieces++
|
donePieces++
|
||||||
|
|
||||||
|
|||||||
@@ -54,6 +54,7 @@ func (t *TorrentFile) Download() ([]byte, error) {
|
|||||||
PeerID: peerID,
|
PeerID: peerID,
|
||||||
InfoHash: t.InfoHash,
|
InfoHash: t.InfoHash,
|
||||||
PieceHashes: t.PieceHashes,
|
PieceHashes: t.PieceHashes,
|
||||||
|
PieceLength: t.PieceLength,
|
||||||
Length: t.Length,
|
Length: t.Length,
|
||||||
Name: t.Name,
|
Name: t.Name,
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user