Support disabling UTP

This commit is contained in:
Matt Joiner 2014-11-19 20:02:20 -06:00
parent 2a8a86734b
commit c57ea76b88
1 changed files with 19 additions and 9 deletions

View File

@ -126,6 +126,7 @@ type Client struct {
disableTrackers bool disableTrackers bool
downloadStrategy DownloadStrategy downloadStrategy DownloadStrategy
dHT *dht.Server dHT *dht.Server
disableUTP bool
mu levelmu.LevelMutex mu levelmu.LevelMutex
event sync.Cond event sync.Cond
@ -278,6 +279,7 @@ func NewClient(cfg *Config) (cl *Client, err error) {
downloadStrategy: cfg.DownloadStrategy, downloadStrategy: cfg.DownloadStrategy,
halfOpenLimit: 100, halfOpenLimit: 100,
dataDir: cfg.DataDir, dataDir: cfg.DataDir,
disableUTP: cfg.DisableUTP,
quit: make(chan struct{}), quit: make(chan struct{}),
torrents: make(map[InfoHash]*torrent), torrents: make(map[InfoHash]*torrent),
@ -327,10 +329,13 @@ func NewClient(cfg *Config) (cl *Client, err error) {
go cl.acceptConnections(utpL, true) go cl.acceptConnections(utpL, true)
} }
if !cfg.NoDHT { if !cfg.NoDHT {
cl.dHT, err = dht.NewServer(&dht.ServerConfig{ cfg := dht.ServerConfig{
Addr: listenAddr(), Addr: listenAddr(),
Conn: utpL.RawConn, }
}) if utpL != nil {
cfg.Conn = utpL.RawConn
}
cl.dHT, err = dht.NewServer(&cfg)
if err != nil { if err != nil {
return return
} }
@ -455,16 +460,20 @@ func (me *Client) initiateConn(peer Peer, t *torrent) {
// Initiate connections via TCP and UTP simultaneously. Use the first // Initiate connections via TCP and UTP simultaneously. Use the first
// one that succeeds. // one that succeeds.
left := 2 left := 1
if !me.disableUTP {
left++
}
resCh := make(chan dialResult, left) resCh := make(chan dialResult, left)
go doDial(func() (net.Conn, error) { if !me.disableUTP {
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) { go doDial(func() (net.Conn, error) {
return (&utp.Dialer{Timeout: dialTimeout}).Dial("utp", addr) return (&utp.Dialer{Timeout: dialTimeout}).Dial("utp", addr)
}, resCh, true) }, resCh, true)
}
go doDial(func() (net.Conn, error) {
// time.Sleep(time.Second) // Give uTP a bit of a head start.
return net.DialTimeout("tcp", addr, dialTimeout)
}, resCh, false)
var res dialResult var res dialResult
for ; left > 0 && res.Conn == nil; left-- { for ; left > 0 && res.Conn == nil; left-- {
res = <-resCh res = <-resCh
@ -614,6 +623,7 @@ type peerConn struct {
} }
func (pc peerConn) Read(b []byte) (n int, err error) { 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)) err = pc.Conn.SetReadDeadline(time.Now().Add(150 * time.Second))
if err != nil { if err != nil {
return return