From 65fa317244aa9f35cb37ae8c4543fa089909a955 Mon Sep 17 00:00:00 2001 From: Matt Joiner Date: Fri, 23 May 2014 00:35:24 +1000 Subject: [PATCH] Some minor fiddling to announcing in the client --- client.go | 21 ++++++++++++--------- torrent.go | 7 +++++++ 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/client.go b/client.go index 5170db9c..00f65a0a 100644 --- 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) } } diff --git a/torrent.go b/torrent.go index 39d85e5d..bd56665e 100644 --- a/torrent.go +++ b/torrent.go @@ -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() } -- 2.48.1