]> Sergey Matveev's repositories - btrtrc.git/commitdiff
Make the discovery source for a connection more descriptive
authorMatt Joiner <anacrolix@gmail.com>
Wed, 16 Jul 2014 07:06:18 +0000 (17:06 +1000)
committerMatt Joiner <anacrolix@gmail.com>
Wed, 16 Jul 2014 07:06:18 +0000 (17:06 +1000)
client.go
connection.go
torrent.go

index 956c448e68841ef65504d40e1a0c0d02a3fd1432..9808a2aa8ca91954dceded26b5eb19a55e17bd5f 100644 (file)
--- a/client.go
+++ b/client.go
@@ -239,7 +239,7 @@ func (cl *Client) acceptConnections() {
                }
                log.Printf("accepted connection from %s", conn.RemoteAddr())
                go func() {
-                       if err := cl.runConnection(conn, nil); err != nil {
+                       if err := cl.runConnection(conn, nil, peerSourceIncoming); err != nil {
                                log.Print(err)
                        }
                }()
@@ -290,8 +290,8 @@ func (me *Client) initiateConn(peer Peer, torrent *torrent) {
                        log.Printf("error connecting to peer: %s %#v", err, err)
                        return
                }
-               log.Printf("connected to %s", conn.RemoteAddr())
-               err = me.runConnection(conn, torrent)
+               // log.Printf("connected to %s", conn.RemoteAddr())
+               err = me.runConnection(conn, torrent, peer.Source)
                if err != nil {
                        log.Print(err)
                }
@@ -314,9 +314,9 @@ func (cl *Client) incomingPeerPort() int {
        return i
 }
 
-func (me *Client) runConnection(sock net.Conn, torrent *torrent) (err error) {
+func (me *Client) runConnection(sock net.Conn, torrent *torrent, discovery peerSource) (err error) {
        conn := &connection{
-               Incoming:        torrent == nil,
+               Discovery:       discovery,
                Socket:          sock,
                Choked:          true,
                PeerChoked:      true,
@@ -704,8 +704,9 @@ func (me *Client) connectionLoop(t *torrent, c *connection) error {
                                        err := me.AddPeers(t.InfoHash, func() (ret []Peer) {
                                                for _, cp := range pexMsg.Added {
                                                        p := Peer{
-                                                               IP:   make([]byte, 4),
-                                                               Port: int(cp.Port),
+                                                               IP:     make([]byte, 4),
+                                                               Port:   int(cp.Port),
+                                                               Source: peerSourcePEX,
                                                        }
                                                        if n := copy(p.IP, cp.IP[:]); n != 4 {
                                                                panic(n)
@@ -931,8 +932,9 @@ func (cl *Client) announceTorrentDHT(t *torrent) {
                                err = cl.AddPeers(t.InfoHash, func() (ret []Peer) {
                                        for _, cp := range cps {
                                                ret = append(ret, Peer{
-                                                       IP:   cp.IP[:],
-                                                       Port: int(cp.Port),
+                                                       IP:     cp.IP[:],
+                                                       Port:   int(cp.Port),
+                                                       Source: peerSourceDHT,
                                                })
                                                log.Printf("peer from dht: %s", &net.UDPAddr{
                                                        IP:   cp.IP[:],
index 10e84d0259d08fd283ac69161803182fa6deb24f..2832e0cfcc54e379791bb189896e24cca0406a28 100644 (file)
@@ -13,14 +13,22 @@ import (
        "bitbucket.org/anacrolix/go.torrent/peer_protocol"
 )
 
+type peerSource byte
+
+const (
+       peerSourceIncoming = 'I'
+       peerSourceDHT      = 'H'
+       peerSourcePEX      = 'X'
+)
+
 // Maintains the state of a connection with a peer.
 type connection struct {
-       Socket   net.Conn
-       Incoming bool
-       closed   bool
-       mu       sync.Mutex // Only for closing.
-       post     chan peer_protocol.Message
-       write    chan []byte
+       Socket    net.Conn
+       Discovery peerSource
+       closed    bool
+       mu        sync.Mutex // Only for closing.
+       post      chan peer_protocol.Message
+       write     chan []byte
 
        // Stuff controlled by the local peer.
        Interested bool
@@ -86,8 +94,8 @@ func (cn *connection) WriteStatus(w io.Writer) {
        if !cn.Choked && !cn.PeerInterested {
                c('?')
        }
-       if cn.Incoming {
-               c('I')
+       if cn.Discovery != 0 {
+               c(byte(cn.Discovery))
        }
        fmt.Fprintln(w)
 }
index e9361c868dbdad18996f645d0c024e6112ca21cf..8a2124576f7a1c954d91fc3b574c4d99389d9e5b 100644 (file)
@@ -347,9 +347,10 @@ func (t *torrent) pendAllChunkSpecs(index pp.Integer) {
 }
 
 type Peer struct {
-       Id   [20]byte
-       IP   net.IP
-       Port int
+       Id     [20]byte
+       IP     net.IP
+       Port   int
+       Source peerSource
 }
 
 func (t *torrent) PieceLength(piece pp.Integer) (len_ pp.Integer) {