From: Matt Joiner <anacrolix@gmail.com>
Date: Mon, 16 May 2016 08:46:38 +0000 (+1000)
Subject: Move requestPendingMetadata onto connection from Client
X-Git-Tag: v1.0.0~727
X-Git-Url: http://www.git.stargrave.org/?a=commitdiff_plain;h=d2951c6c4b263c2d5de5c5a14afce3eb4bd19f63;p=btrtrc.git

Move requestPendingMetadata onto connection from Client
---

diff --git a/client.go b/client.go
index dfa375dc..4b0d0ac4 100644
--- a/client.go
+++ b/client.go
@@ -1009,26 +1009,6 @@ func (cl *Client) connDeleteRequest(t *Torrent, cn *connection, r request) bool
 	return true
 }
 
-func (cl *Client) requestPendingMetadata(t *Torrent, c *connection) {
-	if t.haveInfo() {
-		return
-	}
-	if c.PeerExtensionIDs["ut_metadata"] == 0 {
-		// Peer doesn't support this.
-		return
-	}
-	// Request metadata pieces that we don't have in a random order.
-	var pending []int
-	for index := 0; index < t.metadataPieceCount(); index++ {
-		if !t.haveMetadataPiece(index) && !c.requestedMetadataPiece(index) {
-			pending = append(pending, index)
-		}
-	}
-	for _, i := range mathRand.Perm(len(pending)) {
-		c.requestMetadataPiece(pending[i])
-	}
-}
-
 // Process incoming ut_metadata message.
 func (cl *Client) gotMetadataExtensionMsg(payload []byte, t *Torrent, c *connection) (err error) {
 	var d map[string]int
@@ -1268,7 +1248,7 @@ func (cl *Client) connectionLoop(t *Torrent, c *connection) error {
 					}
 				}
 				if _, ok := c.PeerExtensionIDs["ut_metadata"]; ok {
-					cl.requestPendingMetadata(t, c)
+					c.requestPendingMetadata()
 				}
 			case metadataExtendedId:
 				err = cl.gotMetadataExtensionMsg(msg.ExtendedPayload, t, c)
diff --git a/connection.go b/connection.go
index 6e0edf2b..9264c3f7 100644
--- a/connection.go
+++ b/connection.go
@@ -8,6 +8,7 @@ import (
 	"expvar"
 	"fmt"
 	"io"
+	"math/rand"
 	"net"
 	"strconv"
 	"sync"
@@ -617,3 +618,23 @@ func (cn *connection) peerSentHaveNone() error {
 	cn.peerPiecesChanged()
 	return nil
 }
+
+func (c *connection) requestPendingMetadata() {
+	if c.t.haveInfo() {
+		return
+	}
+	if c.PeerExtensionIDs["ut_metadata"] == 0 {
+		// Peer doesn't support this.
+		return
+	}
+	// Request metadata pieces that we don't have in a random order.
+	var pending []int
+	for index := 0; index < c.t.metadataPieceCount(); index++ {
+		if !c.t.haveMetadataPiece(index) && !c.requestedMetadataPiece(index) {
+			pending = append(pending, index)
+		}
+	}
+	for _, i := range rand.Perm(len(pending)) {
+		c.requestMetadataPiece(pending[i])
+	}
+}
diff --git a/torrent.go b/torrent.go
index 68937544..2f76ab93 100644
--- a/torrent.go
+++ b/torrent.go
@@ -290,7 +290,7 @@ func (t *Torrent) setMetadataSize(bytes int64, cl *Client) {
 	t.metadataBytes = make([]byte, bytes)
 	t.metadataCompletedChunks = make([]bool, (bytes+(1<<14)-1)/(1<<14))
 	for _, c := range t.conns {
-		cl.requestPendingMetadata(t, c)
+		c.requestPendingMetadata()
 	}
 
 }