]> Sergey Matveev's repositories - btrtrc.git/commitdiff
Move initiateConn and openNewConns onto Torrent
authorMatt Joiner <anacrolix@gmail.com>
Sun, 4 Feb 2018 01:59:23 +0000 (12:59 +1100)
committerMatt Joiner <anacrolix@gmail.com>
Sun, 4 Feb 2018 01:59:23 +0000 (12:59 +1100)
client.go
torrent.go

index 0dd785ab330955b1a794c4c70430f3769a318919..d2dac0fa45949807a2cc93863e297649a9a4fd75 100644 (file)
--- 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
index 46c37a8869ed8213f7ed9087e2d15d1ba5143664..77371b130826b407cc6807e40d236114fc6e9877 100644 (file)
@@ -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)
+}