refactor: piece download logic

This commit is contained in:
liwei committed 2020-03-03 10:08:39 +08:00
1 parent 753830accf
commit b823dfbb36
5 files changed
+119 -142

No files matched your search

+56 -57
View File
@@ -1,6 +1,8 @@
package torrent
import (
"github.com/monkeyWie/gopeed/protocol/bt/metainfo"
"math"
"sync"
"github.com/RoaringBitmap/roaring"
@@ -11,100 +13,97 @@ type state int
const (
stateReady = iota
stateDownloading
stateFinished
stateFinish
)
// 所有piece的下载状态
type pieces struct {
rw *sync.RWMutex
arr []piece
type pieceStates struct {
lock *sync.RWMutex
// 所有piece的block下载进度
states map[int]*pieceState
}
type piece struct {
// piece的下载状态
type pieceState struct {
state state
// piece中block的下载状态
blocks *roaring.Bitmap
// piece中block的个数
blockCount int
// piece中block的下载记录
blockRecord *roaring.Bitmap
}
func newPiecesState(size int) *pieces {
ps := &pieces{
rw: &sync.RWMutex{},
arr: make([]piece, size),
func newPiecesState(meta *metainfo.MetaInfo) *pieceStates {
pieceCount := len(meta.Info.Pieces)
ps := &pieceStates{
lock: &sync.RWMutex{},
states: make(map[int]*pieceState, pieceCount),
}
for i := 0; i < size; i++ {
ps.arr[i].state = stateReady
ps.arr[i].blocks = roaring.NewBitmap()
for i := 0; i < pieceCount; i++ {
ps.states[i] = &pieceState{
state: stateReady,
blockCount: int(math.Ceil(float64(meta.GetPieceLength(i)) / blockSize)),
blockRecord: roaring.New(),
}
}
return ps
}
func (ps *pieces) getState(index int) state {
ps.rw.RLock()
defer ps.rw.RUnlock()
return ps.arr[index].state
func (ps *pieceStates) getState(index int) state {
ps.lock.RLock()
defer ps.lock.RUnlock()
return ps.states[index].state
}
func (ps *pieces) setState(index int, state state) {
ps.rw.Lock()
defer ps.rw.Unlock()
ps.arr[index].state = state
func (ps *pieceStates) setState(index int, state state) {
ps.lock.Lock()
defer ps.lock.Unlock()
ps.states[index].state = state
}
func (ps *pieces) getReady() int {
ps.rw.Lock()
defer ps.rw.Unlock()
for i, s := range ps.arr {
if s.state == stateReady {
ps.arr[i].state = stateDownloading
return i
}
}
return -1
func (ps *pieceStates) size() int {
return len(ps.states)
}
func (ps *pieces) getLeft() int {
ps.rw.RLock()
defer ps.rw.RUnlock()
func (ps *pieceStates) getLeft() int {
ps.lock.RLock()
defer ps.lock.RUnlock()
count := 0
for _, s := range ps.arr {
if s.state != stateFinished {
for _, v := range ps.states {
if v.state == stateReady || v.state == stateDownloading {
count++
}
}
return count
}
func (ps *pieces) size() int {
return len(ps.arr)
}
func (ps *pieces) isDone() bool {
ps.rw.RLock()
defer ps.rw.RUnlock()
for _, s := range ps.arr {
if s.state != stateFinished {
func (ps *pieceStates) isDone() bool {
ps.lock.RLock()
defer ps.lock.RUnlock()
for _, v := range ps.states {
if v.state == stateReady || v.state == stateDownloading {
return false
}
}
return true
}
func (ps *pieceStates) isPieceDone(index int) bool {
ps.lock.RLock()
defer ps.lock.RUnlock()
state := ps.states[index]
return uint64(state.blockCount) == state.blockRecord.GetCardinality()
}
// 指定piece下的某个block是否下载完成
func (ps *pieces) isBlockDownloaded(pieceIndex int, blockIndex int) bool {
return ps.arr[pieceIndex].blocks.ContainsInt(blockIndex)
func (ps *pieceStates) isBlockDone(pieceIndex int, blockIndex int) bool {
return ps.states[pieceIndex].blockRecord.ContainsInt(blockIndex)
}
// 指定piece下的某个block下载完成
func (ps *pieces) setBlockDownloaded(pieceIndex int, blockIndex int) {
ps.arr[pieceIndex].blocks.AddInt(blockIndex)
func (ps *pieceStates) setBlockDone(pieceIndex int, blockIndex int) {
ps.states[pieceIndex].blockRecord.AddInt(blockIndex)
}
// 清空指定piece下所有的block下载记录
func (ps *pieces) clearBlocks(index int) {
ps.arr[index].blocks.Clear()
}
func (ps *pieces) blockSize(index int) int {
return int(ps.arr[index].blocks.GetCardinality())
func (ps *pieceStates) clearBlocks(index int) {
ps.states[index].blockRecord.Clear()
}