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
}
}
if _, ok := c.PeerExtensionIDs["ut_metadata"]; ok {
- cl.requestPendingMetadata(t, c)
+ c.requestPendingMetadata()
}
case metadataExtendedId:
err = cl.gotMetadataExtensionMsg(msg.ExtendedPayload, t, c)
"expvar"
"fmt"
"io"
+ "math/rand"
"net"
"strconv"
"sync"
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])
+ }
+}
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()
}
}