From 914bc12bb6498a493f1c004cf5613bbe0ab17a5d Mon Sep 17 00:00:00 2001 From: Matt Joiner Date: Wed, 11 Mar 2015 02:39:01 +1100 Subject: [PATCH] Harmless improvements --- client.go | 32 +++++++++----------------------- torrent.go | 14 +++++++------- 2 files changed, 16 insertions(+), 30 deletions(-) diff --git a/client.go b/client.go index e3f37736..ce3289f0 100644 --- a/client.go +++ b/client.go @@ -202,7 +202,7 @@ func (cl *Client) WriteStatus(_w io.Writer) { if addr := cl.ListenAddr(); addr != nil { fmt.Fprintf(w, "Listening on %s\n", cl.ListenAddr()) } else { - fmt.Println(w, "Not listening!") + fmt.Fprintln(w, "Not listening!") } fmt.Fprintf(w, "Peer ID: %q\n", cl.peerID) fmt.Fprintf(w, "Handshaking: %d\n", cl.handshaking) @@ -214,7 +214,6 @@ func (cl *Client) WriteStatus(_w io.Writer) { fmt.Fprintf(w, "DHT announces: %d\n", cl.dHT.NumConfirmedAnnounces) fmt.Fprintf(w, "Outstanding transactions: %d\n", dhtStats.NumOutstandingTransactions) } - cl.downloadStrategy.WriteStatus(w) fmt.Fprintln(w) for _, t := range cl.sortedTorrents() { if t.Name() == "" { @@ -249,7 +248,7 @@ func (cl *Client) torrentReadAt(t *torrent, off int64, p []byte) (n int, err err return } pieceOff := pp.Integer(off % int64(t.usualPieceSize())) - pieceLeft := int(t.PieceLength(pp.Integer(index)) - pieceOff) + pieceLeft := int(t.PieceLength(index) - pieceOff) if pieceLeft <= 0 { err = io.EOF return @@ -491,10 +490,6 @@ func NewClient(cfg *Config) (cl *Client, err error) { } } - if cl.downloadStrategy == nil { - cl.downloadStrategy = &defaultDownloadStrategy{} - } - // Returns the laddr string to listen on for the next Listen call. listenAddr := func() string { if addr := cl.ListenAddr(); addr != nil { @@ -1043,7 +1038,6 @@ func (cl *Client) connCancel(t *torrent, cn *connection, r request) (ok bool) { ok = cn.Cancel(r) if ok { postedCancels.Add(1) - cl.downloadStrategy.DeleteRequest(t, r) } return } @@ -1052,7 +1046,6 @@ func (cl *Client) connDeleteRequest(t *torrent, cn *connection, r request) { if !cn.RequestPending(r) { return } - cl.downloadStrategy.DeleteRequest(t, r) delete(cn.Requests, r) } @@ -1567,7 +1560,6 @@ func (cl *Client) startTorrent(t *torrent) { } }() } - cl.downloadStrategy.TorrentStarted(t) } // Storage cannot be changed once it's set. @@ -1975,7 +1967,6 @@ func (me *Client) dropTorrent(infoHash InfoHash) (err error) { panic(err) } delete(me.torrents, infoHash) - me.downloadStrategy.TorrentStopped(t) return } @@ -2106,7 +2097,6 @@ func (cl *Client) announceTorrentDHT(t *torrent, impliedPort bool) { } ps.Close() log.Printf("finished DHT peer scrape for %s: %d peers", t, len(allAddrs)) - } } @@ -2255,6 +2245,8 @@ func (me *Client) downloadedChunk(t *torrent, c *connection, msg *pp.Message) er defer me.replenishConnRequests(t, c) + piece := t.Pieces[req.Index] + // Do we actually want this chunk? if _, ok := t.Pieces[req.Index].PendingChunkSpecs[req.chunkSpec]; !ok { unusedDownloadedChunksCount.Add(1) @@ -2272,17 +2264,14 @@ func (me *Client) downloadedChunk(t *torrent, c *connection, msg *pp.Message) er } // Record that we have the chunk. - delete(t.Pieces[req.Index].PendingChunkSpecs, req.chunkSpec) - if len(t.Pieces[req.Index].PendingChunkSpecs) == 0 { + delete(piece.PendingChunkSpecs, req.chunkSpec) + if len(piece.PendingChunkSpecs) == 0 { for _, c := range t.Conns { c.pieceRequestOrder.DeletePiece(int(req.Index)) } me.queuePieceCheck(t, req.Index) } - // Unprioritize the chunk. - me.downloadStrategy.TorrentGotChunk(t, req) - // Cancel pending requests for this chunk. for _, c := range t.Conns { if me.connCancel(t, c, req) { @@ -2290,8 +2279,6 @@ func (me *Client) downloadedChunk(t *torrent, c *connection, msg *pp.Message) er } } - me.downloadStrategy.AssertNotRequested(t, req) - return nil } @@ -2316,10 +2303,9 @@ func (me *Client) pieceHashed(t *torrent, piece pp.Integer, correct bool) { p.PendingChunkSpecs = nil p.complete = true p.Event.Broadcast() - me.downloadStrategy.TorrentGotPiece(t, int(piece)) } else { if len(p.PendingChunkSpecs) == 0 { - t.pendAllChunkSpecs(piece) + t.pendAllChunkSpecs(int(piece)) } if p.Priority != piecePriorityNone { me.openNewConns(t) @@ -2333,13 +2319,13 @@ func (me *Client) pieceHashed(t *torrent, piece pp.Integer, correct bool) { }) // TODO: Cancel requests for this piece. for r := range conn.Requests { - if r.Index == piece { + if int(r.Index) == piece { panic("wat") } } conn.pieceRequestOrder.DeletePiece(int(piece)) } - if t.wantPiece(int(piece)) && conn.PeerHasPiece(piece) { + if t.wantPiece(int(piece)) && conn.PeerHasPiece(pp.Integer(piece)) { t.connPendPiece(conn, int(piece)) me.replenishConnRequests(t, conn) } diff --git a/torrent.go b/torrent.go index 711b58c4..751de60b 100644 --- a/torrent.go +++ b/torrent.go @@ -523,14 +523,14 @@ func (t *torrent) bytesLeft() (left int64) { if !t.haveInfo() { return -1 } - for i := pp.Integer(0); i < pp.Integer(t.numPieces()); i++ { + for i := 0; i < t.numPieces(); i++ { left += int64(t.PieceNumPendingBytes(i)) } return } func (t *torrent) piecePartiallyDownloaded(index int) bool { - return t.PieceNumPendingBytes(pp.Integer(index)) != t.PieceLength(pp.Integer(index)) + return t.PieceNumPendingBytes(index) != t.PieceLength(index) } func numChunksForPiece(chunkSize int, pieceSize int) int { @@ -542,7 +542,7 @@ func (t *torrent) usualPieceSize() int { } func (t *torrent) lastPieceSize() int { - return int(t.PieceLength(pp.Integer(t.numPieces() - 1))) + return int(t.PieceLength(t.numPieces() - 1)) } func (t *torrent) numPieces() int { @@ -634,9 +634,9 @@ func (t *torrent) bitfield() (bf []bool) { } func (t *torrent) pieceChunks(piece int) (css []chunkSpec) { - css = make([]chunkSpec, 0, (t.PieceLength(pp.Integer(piece))+chunkSize-1)/chunkSize) + css = make([]chunkSpec, 0, (t.PieceLength(piece)+chunkSize-1)/chunkSize) var cs chunkSpec - for left := t.PieceLength(pp.Integer(piece)); left != 0; left -= cs.Length { + for left := t.PieceLength(piece); left != 0; left -= cs.Length { cs.Length = left if cs.Length > chunkSize { cs.Length = chunkSize @@ -647,7 +647,7 @@ func (t *torrent) pieceChunks(piece int) (css []chunkSpec) { return } -func (t *torrent) pendAllChunkSpecs(index pp.Integer) { +func (t *torrent) pendAllChunkSpecs(index int) { piece := t.Pieces[index] if piece.PendingChunkSpecs == nil { piece.PendingChunkSpecs = make( @@ -668,7 +668,7 @@ type Peer struct { Source peerSource } -func (t *torrent) PieceLength(piece pp.Integer) (len_ pp.Integer) { +func (t *torrent) PieceLength(piece int) (len_ pp.Integer) { if int(piece) == t.numPieces()-1 { len_ = pp.Integer(t.Length() % t.Info.PieceLength) }