}
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
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 {
}
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)
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() {
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
}