remove length

This commit is contained in:
YouROK
2021-02-20 12:38:06 +03:00
parent aaaa805183
commit ce5b06d8e9
2 changed files with 26 additions and 15 deletions

View File

@@ -61,7 +61,6 @@ func (c *Cache) Init(info *metainfo.Info, hash metainfo.Hash) {
for i := 0; i < c.pieceCount; i++ {
c.pieces[i] = &Piece{
Id: i,
Length: info.Piece(i).Length(),
Hash: info.Piece(i).Hash().HexString(),
cache: c,
}
@@ -116,11 +115,11 @@ func (c *Cache) GetState() *state.CacheState {
var fill int64 = 0
for _, p := range c.pieces {
if p.Size > 0 {
fill += p.Length
fill += p.Size
piecesState[p.Id] = state.ItemState{
Id: p.Id,
Size: p.Size,
Length: p.Length,
Length: c.pieceLength,
Completed: p.complete,
}
}
@@ -192,16 +191,14 @@ func (c *Cache) getRemPieces() []*Piece {
if p.Size > 0 {
fill += p.Size
}
piece := c.torrent.Piece(id)
state := c.torrent.PieceState(id)
if len(ranges) > 0 {
if !inRanges(ranges, id) {
if p.Size > 0 {
if p.Size > 0 && !c.isIdInFileBE(ranges, id) {
piecesRemove = append(piecesRemove, p)
}
} else {
if state.Priority == torrent.PiecePriorityNone {
piece.SetPriority(torrent.PiecePriorityNormal)
if c.torrent.PieceState(id).Priority == torrent.PiecePriorityNone {
c.torrent.Piece(id).SetPriority(torrent.PiecePriorityNormal)
}
}
}
@@ -214,3 +211,18 @@ func (c *Cache) getRemPieces() []*Piece {
c.filled = fill
return piecesRemove
}
func (c *Cache) isIdInFileBE(ranges []Range, id int) bool {
for _, rng := range ranges {
ss := int(rng.File.Offset() / c.pieceLength)
se := int((5*1024*1024 + rng.File.Offset()) / c.pieceLength)
es := int((rng.File.Offset() + rng.File.Length() - 5*1024*1024) / c.pieceLength)
ee := int((rng.File.Offset() + rng.File.Length()) / c.pieceLength)
if id >= ss && id <= se || id >= es && id <= ee {
return true
}
}
return false
}

View File

@@ -15,7 +15,6 @@ type Piece struct {
Id int
Hash string
Length int64
Size int64
complete bool