diff --git a/client.go b/client.go index 0dd785ab..d2dac0fa 100644 --- a/client.go +++ b/client.go @@ -520,23 +520,6 @@ func (cl *Client) dopplegangerAddr(addr string) bool { return ok } -// Start the process of connecting to the given peer for the given torrent if -// appropriate. -func (cl *Client) initiateConn(peer Peer, t *Torrent) { - if peer.Id == cl.peerID { - return - } - if cl.badPeerIPPort(peer.IP, peer.Port) { - return - } - addr := net.JoinHostPort(peer.IP.String(), fmt.Sprintf("%d", peer.Port)) - if t.addrActive(addr) { - return - } - t.halfOpen[addr] = peer - go cl.outgoingConnection(t, addr, peer.Source) -} - func (cl *Client) dialTCP(ctx context.Context, addr string) (c net.Conn, err error) { d := net.Dialer{ // LocalAddr: cl.tcpListener.Addr(), @@ -615,7 +598,7 @@ func (cl *Client) noLongerHalfOpen(t *Torrent, addr string) { panic("invariant broken") } delete(t.halfOpen, addr) - cl.openNewConns(t) + t.openNewConns() } // Performs initiator handshakes and returns a connection. Returns nil @@ -967,27 +950,6 @@ func (cl *Client) gotMetadataExtensionMsg(payload []byte, t *Torrent, c *connect } } -func (cl *Client) openNewConns(t *Torrent) { - defer t.updateWantPeersEvent() - for len(t.peers) != 0 { - if !t.wantConns() { - return - } - if len(t.halfOpen) >= cl.halfOpenLimit { - return - } - var ( - k peersKey - p Peer - ) - for k, p = range t.peers { - break - } - delete(t.peers, k) - cl.initiateConn(p, t) - } -} - func (cl *Client) badPeerIPPort(ip net.IP, port int) bool { if port == 0 { return true diff --git a/torrent.go b/torrent.go index 46c37a88..77371b13 100644 --- a/torrent.go +++ b/torrent.go @@ -240,7 +240,7 @@ func (t *Torrent) unclosedConnsAsSlice() (ret []*connection) { func (t *Torrent) addPeer(p Peer) { cl := t.cl - cl.openNewConns(t) + t.openNewConns() if len(t.peers) >= cl.config.TorrentPeersHighWater { return } @@ -250,7 +250,7 @@ func (t *Torrent) addPeer(p Peer) { } t.peers[key] = p peersAddedBySource.Add(string(p.Source), 1) - cl.openNewConns(t) + t.openNewConns() } @@ -1027,7 +1027,24 @@ func (t *Torrent) pieceCompletionChanged(piece int) { } func (t *Torrent) openNewConns() { - t.cl.openNewConns(t) + defer t.updateWantPeersEvent() + for len(t.peers) != 0 { + if !t.wantConns() { + return + } + if len(t.halfOpen) >= t.cl.halfOpenLimit { + return + } + var ( + k peersKey + p Peer + ) + for k, p = range t.peers { + break + } + delete(t.peers, k) + t.initiateConn(p) + } } func (t *Torrent) getConnPieceInclination() []int { @@ -1656,3 +1673,20 @@ func (t *Torrent) VerifyData() { t.Piece(i).VerifyData() } } + +// Start the process of connecting to the given peer for the given torrent if +// appropriate. +func (t *Torrent) initiateConn(peer Peer) { + if peer.Id == t.cl.peerID { + return + } + if t.cl.badPeerIPPort(peer.IP, peer.Port) { + return + } + addr := net.JoinHostPort(peer.IP.String(), fmt.Sprintf("%d", peer.Port)) + if t.addrActive(addr) { + return + } + t.halfOpen[addr] = peer + go t.cl.outgoingConnection(t, addr, peer.Source) +}