]> Sergey Matveev's repositories - btrtrc.git/commitdiff
Fix webseeds when info isn't available immediately
authorMatt Joiner <anacrolix@gmail.com>
Tue, 2 Jun 2020 06:18:25 +0000 (16:18 +1000)
committerMatt Joiner <anacrolix@gmail.com>
Tue, 2 Jun 2020 06:18:25 +0000 (16:18 +1000)
peerconn.go
torrent.go
web_seed.go

index 0c328c2273dca71032836f2e65bc24e6868310e9..af98660fa9fa8a875a61e4624ea7fd29112283e7 100644 (file)
@@ -18,6 +18,7 @@ import (
        "github.com/anacrolix/missinggo/v2/bitmap"
        "github.com/anacrolix/missinggo/v2/prioritybitmap"
        "github.com/anacrolix/multiless"
+       "github.com/anacrolix/torrent/metainfo"
        "github.com/pkg/errors"
 
        "github.com/anacrolix/torrent/bencode"
@@ -44,6 +45,7 @@ type PeerImpl interface {
        ConnectionFlags() string
        Close()
        PostCancel(request)
+       onGotInfo(*metainfo.Info)
        Drop()
 }
 
@@ -233,13 +235,15 @@ func (cn *peer) completedString() string {
        return fmt.Sprintf("%d/%d", have, cn.bestPeerNumPieces())
 }
 
-// Correct the PeerPieces slice length. Return false if the existing slice is
-// invalid, such as by receiving badly sized BITFIELD, or invalid HAVE
-// messages.
-func (cn *PeerConn) setNumPieces(num pieceIndex) error {
+func (cn *PeerConn) onGotInfo(info *metainfo.Info) {
+       cn.setNumPieces(info.NumPieces())
+}
+
+// Correct the PeerPieces slice length. Return false if the existing slice is invalid, such as by
+// receiving badly sized BITFIELD, or invalid HAVE messages.
+func (cn *PeerConn) setNumPieces(num pieceIndex) {
        cn._peerPieces.RemoveRange(bitmap.BitIndex(num), bitmap.ToEnd)
        cn.peerPiecesChanged()
-       return nil
 }
 
 func eventAgeString(t time.Time) string {
index 21859af37905a1619647afab2102396c6bc0f99e..a5225518e628dc28265279771911d35f6260fb52 100644 (file)
@@ -406,13 +406,11 @@ func (t *Torrent) setInfo(info *metainfo.Info) error {
        return nil
 }
 
+// This seems to be all the follow-up tasks after info is set, that can't fail.
 func (t *Torrent) onSetInfo() {
-       for conn := range t.conns {
-               if err := conn.setNumPieces(t.numPieces()); err != nil {
-                       t.logger.Printf("closing connection: %s", err)
-                       conn.close()
-               }
-       }
+       t.iterPeers(func(p *peer) {
+               p.onGotInfo(t.info)
+       })
        for i := range t.pieces {
                t.updatePieceCompletion(pieceIndex(i))
                p := &t.pieces[i]
@@ -2026,12 +2024,13 @@ func (t *Torrent) addWebSeed(url string) {
                client: webseed.Client{
                        HttpClient: http.DefaultClient,
                        Url:        url,
-                       FileIndex:  t.fileIndex,
-                       Info:       t.info,
                },
                requests: make(map[request]webseed.Request, maxRequests),
        }
        ws.peer.PeerImpl = &ws
+       if t.haveInfo() {
+               ws.onGotInfo(t.info)
+       }
        t.webSeeds[url] = &ws.peer
 }
 
index 13acc41dd0d76bd636f3724523679c1e8b1d0700..3163ac4dc94927d0d60e9cdb15ac0c09dd9ba5c4 100644 (file)
@@ -3,6 +3,8 @@ package torrent
 import (
        "net/http"
 
+       "github.com/anacrolix/torrent/common"
+       "github.com/anacrolix/torrent/metainfo"
        pp "github.com/anacrolix/torrent/peer_protocol"
        "github.com/anacrolix/torrent/segments"
        "github.com/anacrolix/torrent/webseed"
@@ -31,6 +33,11 @@ type webSeed struct {
 
 var _ PeerImpl = (*webSeed)(nil)
 
+func (ws *webSeed) onGotInfo(info *metainfo.Info) {
+       ws.client.FileIndex = segments.NewIndex(common.LengthIterFromUpvertedFiles(info.UpvertedFiles()))
+       ws.client.Info = info
+}
+
 func (ws *webSeed) PostCancel(r request) {
        ws.Cancel(r)
 }