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(),
panic("invariant broken")
}
delete(t.halfOpen, addr)
- cl.openNewConns(t)
+ t.openNewConns()
}
// Performs initiator handshakes and returns a connection. Returns nil
}
}
-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
func (t *Torrent) addPeer(p Peer) {
cl := t.cl
- cl.openNewConns(t)
+ t.openNewConns()
if len(t.peers) >= cl.config.TorrentPeersHighWater {
return
}
}
t.peers[key] = p
peersAddedBySource.Add(string(p.Source), 1)
- cl.openNewConns(t)
+ t.openNewConns()
}
}
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 {
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)
+}