fix preload size

This commit is contained in:
YouROK
2020-12-16 22:50:06 +03:00
parent c47321056e
commit 3d115c5db8

View File

@@ -236,6 +236,8 @@ func (t *Torrent) Preload(index int, size int64) {
return return
} }
t.PreloadSize = size
if t.Stat == state.TorrentGettingInfo { if t.Stat == state.TorrentGettingInfo {
if !t.WaitInfo() { if !t.WaitInfo() {
return return
@@ -263,21 +265,30 @@ func (t *Torrent) Preload(index int, size int64) {
if file == nil { if file == nil {
file = t.Files()[0] file = t.Files()[0]
} }
pieceLength := t.Info().PieceLength
startPiece := file.Offset() / t.Info().PieceLength startPiece := file.Offset() / pieceLength
endPiece := (file.Offset() + size) / t.Info().PieceLength endPiece := int64(float32(file.Offset()+size)/float32(pieceLength) + 0.5)
for i := startPiece; i < endPiece; i++ { for i := startPiece; i < endPiece; i++ {
t.Torrent.Piece(int(i)).SetPriority(torrent.PiecePriorityNormal) t.Torrent.Piece(int(i)).SetPriority(torrent.PiecePriorityNormal)
} }
endPiece = (file.Offset() + file.Length() - 1024) / t.Info().PieceLength endedPiece := (file.Offset() + file.Length() - 1024) / t.Info().PieceLength
t.Torrent.Piece(int(endPiece)).SetPriority(torrent.PiecePriorityNormal) t.Torrent.Piece(int(endedPiece)).SetPriority(torrent.PiecePriorityNormal)
for t.PreloadedBytes < size { for t.PreloadedBytes < size-pieceLength {
t.muTorrent.Lock()
if t.Torrent == nil {
return
}
t.PreloadedBytes = t.Torrent.BytesCompleted() t.PreloadedBytes = t.Torrent.BytesCompleted()
t.muTorrent.Unlock()
log.TLogln("Preload:", file.Torrent().InfoHash().HexString(), utils2.Format(float64(t.PreloadedBytes)), "/", utils2.Format(float64(t.PreloadSize)), "Speed:", utils2.Format(t.DownloadSpeed), "Peers:[", t.Torrent.Stats().ConnectedSeeders, "]", t.Torrent.Stats().ActivePeers, "/", t.Torrent.Stats().TotalPeers) log.TLogln("Preload:", file.Torrent().InfoHash().HexString(), utils2.Format(float64(t.PreloadedBytes)), "/", utils2.Format(float64(t.PreloadSize)), "Speed:", utils2.Format(t.DownloadSpeed), "Peers:[", t.Torrent.Stats().ConnectedSeeders, "]", t.Torrent.Stats().ActivePeers, "/", t.Torrent.Stats().TotalPeers)
time.Sleep(time.Millisecond * 500) time.Sleep(time.Millisecond * 500)
} }
for i := startPiece; i < endPiece; i++ {
t.Torrent.Piece(int(i)).SetPriority(torrent.PiecePriorityNone)
}
t.Torrent.Piece(int(endedPiece)).SetPriority(torrent.PiecePriorityNone)
log.TLogln("End preload:", file.Torrent().InfoHash().HexString(), "Peers:[", t.Torrent.Stats().ConnectedSeeders, "]", t.Torrent.Stats().ActivePeers, "/", t.Torrent.Stats().TotalPeers) log.TLogln("End preload:", file.Torrent().InfoHash().HexString(), "Peers:[", t.Torrent.Stats().ConnectedSeeders, "]", t.Torrent.Stats().ActivePeers, "/", t.Torrent.Stats().TotalPeers)
} }