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

+44 -50
View File
@@ -12,18 +12,16 @@ type Torrent struct {
PeerID [20]byte
MetaInfo *metainfo.MetaInfo
pieces *pieces
peerPool *peerPool
completeCh chan interface{}
Path string
pieceStates *pieceStates
peerPool *peerPool
Path string
}
func NewTorrent(peerID [20]byte, metaInfo *metainfo.MetaInfo) *Torrent {
torrent := &Torrent{
PeerID: peerID,
MetaInfo: metaInfo,
pieces: newPiecesState(len(metaInfo.Info.Pieces)),
PeerID: peerID,
MetaInfo: metaInfo,
pieceStates: newPiecesState(metaInfo),
}
return torrent
}
@@ -33,57 +31,53 @@ func (t *Torrent) Download(path string) {
t.peerPool = newPeerPool(t)
t.peerPool.fetch()
pieceQueueCh := make(chan interface{}, 256)
pieceQueueCh := make(chan interface{}, 128)
defer close(pieceQueueCh)
t.completeCh = make(chan interface{})
defer close(t.completeCh)
for {
downloadedCh := make(chan interface{})
defer close(downloadedCh)
for i := 0; i < len(t.MetaInfo.Info.Pieces); i++ {
// 加入下载队列
pieceQueueCh <- nil
// 获取一个待下载的piece
index := t.pieces.getReady()
// 没有待下载的piece了
if index == -1 {
log.Debugf("no piece to download")
<-t.completeCh
// 下载完成
break
}
go func(index int) {
peer := t.peerPool.get()
if peer == nil {
time.Sleep(time.Second * 10)
t.pieces.setState(index, stateReady)
<-pieceQueueCh
return
}
pc := newPeerConn(t, peer)
err := pc.ready()
if err != nil {
t.pieces.setState(index, stateReady)
t.peerPool.unavailable(peer)
} else {
err = pc.downloadPiece(index)
t.pieceStates.setState(index, stateDownloading)
// 一直下载到成功
for {
peer := t.peerPool.get()
if peer == nil {
time.Sleep(time.Second * 10)
continue
}
pc := newPeerConn(t, peer)
err := pc.ready()
if err != nil {
log.Debugf("piece %d download fail:%s", index, err.Error())
// 下载失败
t.pieces.setState(index, stateReady)
t.peerPool.unavailable(peer)
if errors.Is(err, errPieceCheckFailed) {
// piece校验失败,需要重新下载过一遍,清空block记录
t.pieces.clearBlocks(index)
}
} else {
// 下载完成
t.pieces.setState(index, stateFinished)
t.peerPool.release(peer)
log.Debugf("piece %d download success,total:%d,left:%d", index, t.pieces.size(), t.pieces.getLeft())
// 检查是否所有piece下载完成
if t.pieces.isDone() {
t.completeCh <- nil
err = pc.downloadPiece(index)
if err != nil {
log.Debugf("piece %d download fail:%s", index, err.Error())
// 下载失败
t.peerPool.unavailable(peer)
if errors.Is(err, errPieceCheckFailed) {
// piece校验失败,需要重新下载过一遍,清空block记录
t.pieceStates.clearBlocks(index)
}
continue
} else {
// 下载完成
t.pieceStates.setState(index, stateFinish)
t.peerPool.release(peer)
log.Debugf("piece %d download success,total:%d,left:%d", index, t.pieceStates.size(), t.pieceStates.getLeft())
if t.pieceStates.isDone() {
downloadedCh <- nil
}
break
}
}
}
<-pieceQueueCh
}(index)
}(i)
}
<-downloadedCh
log.Debug("download end")
}