client.go | 28 +++++++++++++++++++--------- diff --git a/client.go b/client.go index 20957caea2f843b0645a8b3dc6f658453bc042c4..3c0e67805cb4d7badbc67d2a037251e23fcd97ce 100644 --- a/client.go +++ b/client.go @@ -126,6 +126,7 @@ listeners []net.Listener disableTrackers bool downloadStrategy DownloadStrategy dHT *dht.Server + disableUTP bool mu levelmu.LevelMutex event sync.Cond @@ -278,6 +279,7 @@ disableTrackers: cfg.DisableTrackers, downloadStrategy: cfg.DownloadStrategy, halfOpenLimit: 100, dataDir: cfg.DataDir, + disableUTP: cfg.DisableUTP, quit: make(chan struct{}), torrents: make(map[InfoHash]*torrent), @@ -327,10 +329,13 @@ cl.listeners = append(cl.listeners, utpL) go cl.acceptConnections(utpL, true) } if !cfg.NoDHT { - cl.dHT, err = dht.NewServer(&dht.ServerConfig{ + cfg := dht.ServerConfig{ Addr: listenAddr(), - Conn: utpL.RawConn, - }) + } + if utpL != nil { + cfg.Conn = utpL.RawConn + } + cl.dHT, err = dht.NewServer(&cfg) if err != nil { return } @@ -455,16 +460,20 @@ // listen address. // Initiate connections via TCP and UTP simultaneously. Use the first // one that succeeds. - left := 2 + left := 1 + if !me.disableUTP { + left++ + } resCh := make(chan dialResult, left) + if !me.disableUTP { + go doDial(func() (net.Conn, error) { + return (&utp.Dialer{Timeout: dialTimeout}).Dial("utp", addr) + }, resCh, true) + } go doDial(func() (net.Conn, error) { - time.Sleep(time.Second) // Give uTP a bit of a head start. + // time.Sleep(time.Second) // Give uTP a bit of a head start. return net.DialTimeout("tcp", addr, dialTimeout) }, resCh, false) - go doDial(func() (net.Conn, error) { - return (&utp.Dialer{Timeout: dialTimeout}).Dial("utp", addr) - }, resCh, true) - var res dialResult for ; left > 0 && res.Conn == nil; left-- { res = <-resCh @@ -614,6 +623,7 @@ net.Conn } func (pc peerConn) Read(b []byte) (n int, err error) { + // Keep-alives should be received every 2 mins. Give a bit of gracetime. err = pc.Conn.SetReadDeadline(time.Now().Add(150 * time.Second)) if err != nil { return