client.go | 19 +++++++++++++++++++ torrent.go | 3 ++- diff --git a/client.go b/client.go index ae246f9fa72759219c08db43e9ce5fb8083d16f4..5170db9c0c855b693bd530a6b860ad47439fa5bc 100644 --- a/client.go +++ b/client.go @@ -130,6 +130,7 @@ if int(index) >= len(t.Pieces) { err = io.EOF return } + t.lastReadPiece = int(index) piece := t.Pieces[index] if !piece.EverHashed { cl.queuePieceCheck(t, index) @@ -714,7 +715,25 @@ func (ResponsiveDownloadStrategy) FillRequests(t *torrent, c *connection) { for e := t.Priorities.Front(); e != nil; e = e.Next() { if !c.Request(e.Value.(request)) { + return + } + } + for i := t.lastReadPiece; i < t.lastReadPiece+5 && i < t.NumPieces(); i++ { + for cs := range t.Pieces[i].PendingChunkSpecs { + if !c.Request(request{pp.Integer(i), cs}) { + return + } + } + } + // Then finish off incomplete pieces in order of bytes remaining. + for _, index := range t.piecesByPendingBytes() { + if t.PieceNumPendingBytes(index) == t.PieceLength(index) { break + } + for chunkSpec := range t.Pieces[index].PendingChunkSpecs { + if !c.Request(request{index, chunkSpec}) { + return + } } } } diff --git a/torrent.go b/torrent.go index 4f94b89bc0bb842e78c2ba907f0eeba09cab2cf8..39d85e5d7185c11f21219c51169775c2a511e37d 100644 --- a/torrent.go +++ b/torrent.go @@ -34,7 +34,8 @@ Peers []Peer Priorities *list.List // BEP 12 Multitracker Metadata Extension. The tracker.Client instances // mirror their respective URLs from the announce-list key. - Trackers [][]tracker.Client + Trackers [][]tracker.Client + lastReadPiece int } func (t *torrent) String() string {