From 3d115c5db8bd32d02e6b36ee70e39bd3dd1f9c66 Mon Sep 17 00:00:00 2001 From: YouROK <8yourok8@mail.ru> Date: Wed, 16 Dec 2020 22:50:06 +0300 Subject: [PATCH] fix preload size --- src/server/torr/torrent.go | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/src/server/torr/torrent.go b/src/server/torr/torrent.go index 5d49640..30b3737 100644 --- a/src/server/torr/torrent.go +++ b/src/server/torr/torrent.go @@ -236,6 +236,8 @@ func (t *Torrent) Preload(index int, size int64) { return } + t.PreloadSize = size + if t.Stat == state.TorrentGettingInfo { if !t.WaitInfo() { return @@ -263,21 +265,30 @@ func (t *Torrent) Preload(index int, size int64) { if file == nil { file = t.Files()[0] } - - startPiece := file.Offset() / t.Info().PieceLength - endPiece := (file.Offset() + size) / t.Info().PieceLength + pieceLength := t.Info().PieceLength + startPiece := file.Offset() / pieceLength + endPiece := int64(float32(file.Offset()+size)/float32(pieceLength) + 0.5) for i := startPiece; i < endPiece; i++ { t.Torrent.Piece(int(i)).SetPriority(torrent.PiecePriorityNormal) } - endPiece = (file.Offset() + file.Length() - 1024) / t.Info().PieceLength - t.Torrent.Piece(int(endPiece)).SetPriority(torrent.PiecePriorityNormal) + endedPiece := (file.Offset() + file.Length() - 1024) / t.Info().PieceLength + 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.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) 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) }