client.go | 33 +++++++++++++++------------------ connection.go | 8 ++++++++ torrent.go | 17 ++++++++--------- diff --git a/client.go b/client.go index efe5f736776a5a5733ed381106f9412108f7ac47..3072dcd1a6098b8ad58990a142327ab2425ff95a 100644 --- a/client.go +++ b/client.go @@ -714,7 +714,7 @@ return } 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 @@ c.completedHandshake = time.Now() 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 @@ return } 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) diff --git a/connection.go b/connection.go index b04c9c144bf7e4c055ed4026115732df5b1060f8..550d5c10a8956fb76c5ce92acb9adb59a71e8819 100644 --- a/connection.go +++ b/connection.go @@ -1282,3 +1282,11 @@ uploadChunksPosted.Add(1) 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{}{} +} diff --git a/torrent.go b/torrent.go index d5a45f4cdd0ec5fcf29bb60d1c87588b74c12d4e..60119b592c1094aa67224fecf754c95711bd836b 100644 --- a/torrent.go +++ b/torrent.go @@ -1410,6 +1410,13 @@ } 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 @@ } 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 }