From 7bc8524d2428d12e6cfca0530a28b6ff53d3c2c6 Mon Sep 17 00:00:00 2001 From: Matt Joiner Date: Wed, 2 Oct 2013 17:57:59 +1000 Subject: [PATCH] Mostly handle Piece messages, and kick off a piece check when all the chunks are done --- client.go | 34 +++++++++++++++++++++++++++------- 1 file changed, 27 insertions(+), 7 deletions(-) diff --git a/client.go b/client.go index fddefa37..4ed7dfb3 100644 --- a/client.go +++ b/client.go @@ -465,7 +465,7 @@ func (me *client) runConnection(torrent *torrent, conn *connection) error { continue } go me.withContext(func() { - log.Print(msg) + // log.Print(msg) var err error switch msg.Type { case peer_protocol.Choke: @@ -499,6 +499,24 @@ func (me *client) runConnection(torrent *torrent, conn *connection) error { me.peerGotPiece(torrent, conn, index) } } + case peer_protocol.Piece: + request := request{msg.Index, chunkSpec{msg.Begin, peer_protocol.Integer(len(msg.Piece))}} + if _, ok := conn.Requests[request]; !ok { + err = errors.New("unexpected piece") + break + } + delete(conn.Requests, request) + me.replenishConnRequests(torrent, conn) + if _, ok := torrent.Pieces[request.Index].PendingChunkSpecs[request.chunkSpec]; !ok { + log.Printf("got unnecessary chunk: %s", request) + break + } + log.Printf("need to implement write of chunk: %s", request) + delete(torrent.Pieces[request.Index].PendingChunkSpecs, request.chunkSpec) + if len(torrent.Pieces[request.Index].PendingChunkSpecs) == 0 { + torrent.Pieces[request.Index].State = pieceStateUnknown + go me.verifyPiece(torrent, int(request.Index)) + } default: log.Printf("received unknown message type: %#v", msg.Type) } @@ -652,7 +670,13 @@ func (me *client) pieceHashed(ih InfoHash, piece int, correct bool) { } } } +} +func (me *client) verifyPiece(torrent *torrent, index int) { + sum := torrent.HashPiece(index) + me.withContext(func() { + me.pieceHashed(torrent.InfoHash, index, sum == torrent.Pieces[index].Hash) + }) } func (me *client) run() { @@ -669,12 +693,8 @@ func (me *client) run() { } me.torrents[torrent.InfoHash] = torrent go func() { - for _piece := range torrent.Pieces { - piece := _piece - sum := torrent.HashPiece(piece) - me.withContext(func() { - me.pieceHashed(torrent.InfoHash, piece, sum == torrent.Pieces[piece].Hash) - }) + for index := range torrent.Pieces { + me.verifyPiece(torrent, index) } }() case infoHash := <-me.torrentFinished: