]> Sergey Matveev's repositories - btrtrc.git/commitdiff
Add peers received from received announce_peer DHT messages to the Client
authorMatt Joiner <anacrolix@gmail.com>
Sat, 26 Nov 2016 13:05:19 +0000 (00:05 +1100)
committerMatt Joiner <anacrolix@gmail.com>
Sat, 26 Nov 2016 13:05:19 +0000 (00:05 +1100)
Addresses #133

client.go
connection.go
torrent.go

index 94c02f02441c860e4070c2393eaaa546b7f5af97..2e6ded8b6b213df33febba064b8b0761351133a1 100644 (file)
--- a/client.go
+++ b/client.go
@@ -309,6 +309,9 @@ func NewClient(cfg *Config) (cl *Client, err error) {
                if dhtCfg.Conn == nil && cl.utpSock != nil {
                        dhtCfg.Conn = cl.utpSock
                }
+               if dhtCfg.OnAnnouncePeer == nil {
+                       dhtCfg.OnAnnouncePeer = cl.onDHTAnnouncePeer
+               }
                cl.dHT, err = dht.NewServer(&dhtCfg)
                if err != nil {
                        return
@@ -1341,3 +1344,17 @@ func (cl *Client) newConnection(nc net.Conn) (c *connection) {
        c.r = rateLimitedReader{cl.downloadLimit, c.r}
        return
 }
+
+func (cl *Client) onDHTAnnouncePeer(ih metainfo.Hash, p dht.Peer) {
+       cl.mu.Lock()
+       defer cl.mu.Unlock()
+       t, ok := cl.Torrent(ih)
+       if !ok {
+               return
+       }
+       t.addPeers([]Peer{{
+               IP:     p.IP,
+               Port:   p.Port,
+               Source: peerSourceDHTAnnouncePeer,
+       }})
+}
index 727f97f7846db605da1f43f487d3e8644e56ed77..1d67e9dc197c98c323333155839bd88b1019f9c9 100644 (file)
@@ -28,13 +28,14 @@ import (
 
 var optimizedCancels = expvar.NewInt("optimizedCancels")
 
-type peerSource byte
+type peerSource string
 
 const (
-       peerSourceTracker  = '\x00' // It's the default.
-       peerSourceIncoming = 'I'
-       peerSourceDHT      = 'H'
-       peerSourcePEX      = 'X'
+       peerSourceTracker         = "T" // It's the default.
+       peerSourceIncoming        = "I"
+       peerSourceDHTGetPeers     = "Hg"
+       peerSourceDHTAnnouncePeer = "Ha"
+       peerSourcePEX             = "X"
 )
 
 // Maintains the state of a connection with a peer.
@@ -155,9 +156,7 @@ func (cn *connection) connectionFlags() (ret string) {
        if cn.encrypted {
                c('E')
        }
-       if cn.Discovery != 0 {
-               c(byte(cn.Discovery))
-       }
+       ret += string(cn.Discovery)
        if cn.uTP {
                c('T')
        }
index 8610a5ec99ed7ff95d1c006fd54559efc62ef714..3fd3f0b2b2396780f5f2b0a434c3a8321abf05ab 100644 (file)
@@ -1268,7 +1268,7 @@ func (t *Torrent) consumeDHTAnnounce(pvs <-chan dht.PeersValues) {
                                addPeers = append(addPeers, Peer{
                                        IP:     cp.IP[:],
                                        Port:   cp.Port,
-                                       Source: peerSourceDHT,
+                                       Source: peerSourceDHTGetPeers,
                                })
                                key := (&net.UDPAddr{
                                        IP:   cp.IP[:],