]> Sergey Matveev's repositories - btrtrc.git/commitdiff
Some minor fiddling to announcing in the client
authorMatt Joiner <anacrolix@gmail.com>
Thu, 22 May 2014 14:35:24 +0000 (00:35 +1000)
committerMatt Joiner <anacrolix@gmail.com>
Thu, 22 May 2014 14:35:24 +0000 (00:35 +1000)
client.go
torrent.go

index 5170db9c0c855b693bd530a6b860ad47439fa5bc..00f65a0a69bf7eee32c015583e0636b11978b21f 100644 (file)
--- a/client.go
+++ b/client.go
@@ -607,14 +607,15 @@ func (cl *Client) listenerAnnouncePort() (port int16) {
 
 func (cl *Client) announceTorrent(t *torrent) {
        req := tracker.AnnounceRequest{
-               Event:   tracker.Started,
-               NumWant: -1,
-               Port:    cl.listenerAnnouncePort(),
+               Event:    tracker.Started,
+               NumWant:  -1,
+               Port:     cl.listenerAnnouncePort(),
+               PeerId:   cl.PeerId,
+               InfoHash: t.InfoHash,
        }
-       req.PeerId = cl.PeerId
-       req.InfoHash = t.InfoHash
 newAnnounce:
        for {
+               req.Left = t.BytesLeft()
                for _, tier := range t.Trackers {
                        for trIndex, tr := range tier {
                                if err := tr.Connect(); err != nil {
@@ -633,17 +634,19 @@ newAnnounce:
                                                Port: peer.Port,
                                        })
                                }
-                               if err := cl.AddPeers(t.InfoHash, peers); err != nil {
+                               err = cl.AddPeers(t.InfoHash, peers)
+                               if err != nil {
                                        log.Print(err)
-                                       return
+                               } else {
+                                       log.Printf("%s: %d new peers from %s", t, len(peers), tr)
                                }
-                               log.Printf("%s: %d new peers from %s", t, len(peers), tr)
                                tier[0], tier[trIndex] = tier[trIndex], tier[0]
                                time.Sleep(time.Second * time.Duration(resp.Interval))
+                               req.Event = tracker.None
                                continue newAnnounce
                        }
                }
-               time.Sleep(time.Second)
+               time.Sleep(5 * time.Second)
        }
 }
 
index 39d85e5d7185c11f21219c51169775c2a511e37d..bd56665e8f142d921d88ee5cf6b9c4d47278e9da 100644 (file)
@@ -42,6 +42,13 @@ func (t *torrent) String() string {
        return t.MetaInfo.Name
 }
 
+func (t *torrent) BytesLeft() (left int64) {
+       for i := peer_protocol.Integer(0); i < peer_protocol.Integer(t.NumPieces()); i++ {
+               left += int64(t.PieceNumPendingBytes(i))
+       }
+       return
+}
+
 func (t *torrent) NumPieces() int {
        return len(t.MetaInfo.Pieces) / pieceHash.Size()
 }