从零开始编写一个BitTorrent下载器 (9)

image

func (t *Torrent) startDownloadWorker(peer peers.Peer, workQueue chan *pieceWork, results chan *pieceResult) { c, err := client.New(peer, t.PeerID, t.InfoHash) if err != nil { log.Printf("could not handshake eith %s\ndisconnecting\n", peer.IP) return } defer c.Conn.Close() log.Printf("completed handshake with %s\n", peer.IP) c.SendUnchoke() c.SendInterested() for pw := range workQueue { if !c.Bitfield.HasPiece(pw.index) { workQueue <- pw // 将分块重新放入队列 continue } // 下载分块 buf, err := attemptDownloadPiece(c, pw) if err != nil { log.Println("exiting", err) workQueue <- pw // 将分块重新放入队列 return } err = checkIntegrity(pw, buf) if err != nil { log.Printf("piece #%d failed integrity check\n", pw.index) workQueue <- pw // 将分块重新放入队列 continue } c.SendHave(pw.index) results <- &pieceResult{pw.index, buf} } } 状态(~/p2p/p2p.go)

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zgfffd.html