]> Sergey Matveev's repositories - btrtrc.git/commitdiff
Move requestPendingMetadata onto connection from Client
authorMatt Joiner <anacrolix@gmail.com>
Mon, 16 May 2016 08:46:38 +0000 (18:46 +1000)
committerMatt Joiner <anacrolix@gmail.com>
Mon, 16 May 2016 08:46:38 +0000 (18:46 +1000)
client.go
connection.go
torrent.go

index dfa375dcc5cd27626131ddf305a4429f3077ed28..4b0d0ac43627e71b576ebe6f229b9cc1ed066a58 100644 (file)
--- 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)
index 6e0edf2b62e5fd72f1b4678d97dd67c28dc8ad93..9264c3f7c98b847d3db14aa619b4bc7804ed4379 100644 (file)
@@ -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])
+       }
+}
index 68937544f9740a8d0233cf06c248a82933ab7853..2f76ab9356eb914db75f41236582460bd088662e 100644 (file)
@@ -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()
        }
 
 }