+
+// Returns true if connection is removed from torrent.Conns.
+func (t *Torrent) deleteConnection(c *connection) bool {
+ for i0, _c := range t.conns {
+ if _c != c {
+ continue
+ }
+ i1 := len(t.conns) - 1
+ if i0 != i1 {
+ t.conns[i0] = t.conns[i1]
+ }
+ t.conns = t.conns[:i1]
+ return true
+ }
+ return false
+}
+
+func (t *Torrent) dropConnection(c *connection) {
+ t.cl.event.Broadcast()
+ c.Close()
+ if t.deleteConnection(c) {
+ t.openNewConns()
+ }
+}
+
+// Returns true when peers are required, or false if the torrent is closing.
+func (t *Torrent) waitWantPeers() bool {
+ t.cl.mu.Lock()
+ defer t.cl.mu.Unlock()
+ for {
+ if t.closed.IsSet() {
+ return false
+ }
+ if len(t.peers) > torrentPeersLowWater {
+ goto wait
+ }
+ if t.needData() || t.seeding() {
+ return true
+ }
+ wait:
+ t.wantPeers.Wait()
+ }
+}
+
+// Returns whether the client should make effort to seed the torrent.
+func (t *Torrent) seeding() bool {
+ cl := t.cl
+ if cl.config.NoUpload {
+ return false
+ }
+ if !cl.config.Seed {
+ return false
+ }
+ if t.needData() {
+ return false
+ }
+ return true
+}