Skip to content

Commit

Permalink
start downloader worker
Browse files Browse the repository at this point in the history
  • Loading branch information
baarayy committed May 17, 2024
1 parent 7e12e01 commit e2fa142
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
2 changes: 1 addition & 1 deletion client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func (c *Client) SendUnchoke() error {
return err
}

func (c *Client) sendHave(index int) error {
func (c *Client) SendHave(index int) error {
msg := message.FormatHave(index)
_, err := c.Conn.Write(msg.Serialize())
return err
Expand Down
25 changes: 23 additions & 2 deletions p2p/p2p.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,29 @@ func (t *Torrent) startDownloadWorker(peer peers.Peer, workQueue chan *pieceWork
}
defer c.Conn.Close()
log.Printf("Completed handshake with %s\n", peer)

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("Error downloading piece:", err)
workQueue <- pw
return
}
err = checkIntegrity(pw, buf)
if err != nil {
log.Println("Error, Integrity check failed piece:", err)
workQueue <- pw
return
}
c.SendHave(pw.index)
}

}

0 comments on commit e2fa142

Please sign in to comment.