]> Sergey Matveev's repositories - btrtrc.git/commitdiff
Rework connection stat reconciliation with Torrent and refactor doppleganger handling
authorMatt Joiner <anacrolix@gmail.com>
Fri, 2 Feb 2018 08:07:20 +0000 (19:07 +1100)
committerMatt Joiner <anacrolix@gmail.com>
Fri, 2 Feb 2018 08:07:20 +0000 (19:07 +1100)
client.go
connection.go
torrent.go

index efe5f736776a5a5733ed381106f9412108f7ac47..3072dcd1a6098b8ad58990a142327ab2425ff95a 100644 (file)
--- a/client.go
+++ b/client.go
@@ -714,7 +714,7 @@ func (cl *Client) outgoingConnection(t *Torrent, addr string, ps peerSource) {
        }
        defer c.Close()
        c.Discovery = ps
-       cl.runInitiatedHandshookConn(c, t)
+       cl.runHandshookConn(c, t, true)
 }
 
 // The port number for incoming peer connections. 0 if the client isn't
@@ -816,16 +816,6 @@ func (cl *Client) connBTHandshake(c *connection, ih *metainfo.Hash) (ret metainf
        return
 }
 
-func (cl *Client) runInitiatedHandshookConn(c *connection, t *Torrent) {
-       if c.PeerID == cl.peerID {
-               connsToSelf.Add(1)
-               addr := c.conn.RemoteAddr().String()
-               cl.dopplegangerAddrs[addr] = struct{}{}
-               return
-       }
-       cl.runHandshookConn(c, t, true)
-}
-
 func (cl *Client) runReceivedConn(c *connection) {
        err := c.conn.SetDeadline(time.Now().Add(cl.config.HandshakesTimeout))
        if err != nil {
@@ -843,17 +833,24 @@ func (cl *Client) runReceivedConn(c *connection) {
        }
        cl.mu.Lock()
        defer cl.mu.Unlock()
-       if c.PeerID == cl.peerID {
-               // Because the remote address is not necessarily the same as its
-               // client's torrent listen address, we won't record the remote address
-               // as a doppleganger. Instead, the initiator can record *us* as the
-               // doppleganger.
-               return
-       }
        cl.runHandshookConn(c, t, false)
 }
 
 func (cl *Client) runHandshookConn(c *connection, t *Torrent, outgoing bool) {
+       t.reconcileHandshakeStats(c)
+       if c.PeerID == cl.peerID {
+               if outgoing {
+                       connsToSelf.Add(1)
+                       addr := c.conn.RemoteAddr().String()
+                       cl.dopplegangerAddrs[addr] = struct{}{}
+               } else {
+                       // Because the remote address is not necessarily the same as its
+                       // client's torrent listen address, we won't record the remote address
+                       // as a doppleganger. Instead, the initiator can record *us* as the
+                       // doppleganger.
+               }
+               return
+       }
        c.conn.SetWriteDeadline(time.Time{})
        c.r = deadlineReader{c.conn, c.r}
        completedHandshakeConnectionFlags.Add(c.connectionFlags(), 1)
index b04c9c144bf7e4c055ed4026115732df5b1060f8..550d5c10a8956fb76c5ce92acb9adb59a71e8819 100644 (file)
@@ -1282,3 +1282,11 @@ func (c *connection) sendChunk(r request, msg func(pp.Message) bool) (more bool,
        c.lastChunkSent = time.Now()
        return
 }
+
+func (c *connection) setTorrent(t *Torrent) {
+       if c.t != nil {
+               panic("connection already associated with a torrent")
+       }
+       c.t = t
+       t.conns[c] = struct{}{}
+}
index d5a45f4cdd0ec5fcf29bb60d1c87588b74c12d4e..60119b592c1094aa67224fecf754c95711bd836b 100644 (file)
@@ -1410,6 +1410,13 @@ func (t *Torrent) numTotalPeers() int {
        return len(peers)
 }
 
+// Reconcile bytes transferred before connection was associated with a
+// torrent.
+func (t *Torrent) reconcileHandshakeStats(c *connection) {
+       t.stats.wroteBytes(c.stats.BytesWritten)
+       t.stats.readBytes(c.stats.BytesRead)
+}
+
 // Returns true if the connection is added.
 func (t *Torrent) addConnection(c *connection, outgoing bool) bool {
        if t.cl.closed.IsSet() {
@@ -1451,15 +1458,7 @@ func (t *Torrent) addConnection(c *connection, outgoing bool) bool {
        if len(t.conns) >= t.maxEstablishedConns {
                panic(len(t.conns))
        }
-       if c.t != nil {
-               panic("connection already associated with a torrent")
-       }
-       // Reconcile bytes transferred before connection was associated with a
-       // torrent.
-       t.stats.wroteBytes(c.stats.BytesWritten)
-       t.stats.readBytes(c.stats.BytesRead)
-       c.t = t
-       t.conns[c] = struct{}{}
+       c.setTorrent(t)
        return true
 }