diff --git a/engine/engine.go b/engine/engine.go index b7ca8339..f4e9c859 100644 --- a/engine/engine.go +++ b/engine/engine.go @@ -355,7 +355,7 @@ func (e *Engine) StartTorrent(infohash string) error { defer t.Unlock() if t.Started { - return fmt.Errorf("Already started") + return fmt.Errorf("already started") } t.Started = true t.StartedAt = time.Now() @@ -365,12 +365,7 @@ func (e *Engine) StartTorrent(infohash string) error { } } if t.t.Info() != nil { - t.t.AllowDataUpload() - // start all files by setting the priority to normal - for _, f := range t.t.Files() { - f.SetPriority(torrent.PiecePriorityNormal) - } - t.t.AllowDataDownload() + t.t.DownloadAll() } return nil } @@ -387,24 +382,17 @@ func (e *Engine) StopTorrent(infohash string) error { defer t.Unlock() if !t.Started { - return fmt.Errorf("Already stopped") + return fmt.Errorf("already stopped") } if t.t.Info() != nil { - t.t.DisallowDataDownload() - // stop all files by setting the priority to None - for _, f := range t.t.Files() { - f.SetPriority(torrent.PiecePriorityNone) - } - t.t.DisallowDataUpload() + t.t.CancelPieces(0, t.t.NumPieces()) } t.Started = false t.StoppedAt = time.Now() for _, f := range t.Files { - if f != nil { - f.Started = false - } + f.Started = false } return nil @@ -445,7 +433,9 @@ func (e *Engine) StartFile(infohash, filepath string) error { if f.Started { return fmt.Errorf("already started") } - t.Started = true + if !t.Started { + t.Started = true + } f.Started = true f.f.SetPriority(torrent.PiecePriorityNormal) return nil @@ -466,7 +456,7 @@ func (e *Engine) StopFile(infohash, filepath string) error { } } if f == nil { - return fmt.Errorf("Missing file %s", filepath) + return fmt.Errorf("missing file %s", filepath) } if !f.Started { return fmt.Errorf("already stopped") @@ -483,7 +473,8 @@ func (e *Engine) StopFile(infohash, filepath string) error { } if allStopped { - go e.StopTorrent(infohash) + t.Started = false + t.StoppedAt = time.Now() } return nil diff --git a/engine/torrent.go b/engine/torrent.go index 44d469e7..cc9748b7 100644 --- a/engine/torrent.go +++ b/engine/torrent.go @@ -85,10 +85,11 @@ func (torrent *Torrent) updateOnGotInfo(t *torrent.Torrent) { } func (torrent *Torrent) updateConnStat() { + lastStat := torrent.Stats torrent.Stats = torrent.t.Stats() // calculate ratio - bRead := torrent.Stats.BytesReadData.Int64() + bRead := torrent.Stats.BytesReadUsefulData.Int64() bWrite := torrent.Stats.BytesWrittenData.Int64() if bRead > 0 { torrent.SeedRatio = float32(bWrite) / float32(bRead) @@ -97,22 +98,19 @@ func (torrent *Torrent) updateConnStat() { } now := time.Now() - bytes := torrent.t.BytesCompleted() - ulbytes := torrent.Stats.BytesWrittenData.Int64() - if !torrent.updatedAt.IsZero() { // calculate rate dtinv := float32(time.Second) / float32(now.Sub(torrent.updatedAt)) - dldb := float32(bytes - torrent.Downloaded) + dldb := float32(bRead - lastStat.BytesReadUsefulData.Int64()) torrent.DownloadRate = dldb * dtinv - uldb := float32(ulbytes - torrent.Uploaded) + uldb := float32(bWrite - lastStat.BytesWrittenData.Int64()) torrent.UploadRate = uldb * dtinv } - torrent.Downloaded = bytes - torrent.Uploaded = ulbytes + torrent.Downloaded = torrent.t.BytesCompleted() + torrent.Uploaded = bWrite torrent.updatedAt = now } diff --git a/go.sum b/go.sum index 13953ba4..644da975 100644 --- a/go.sum +++ b/go.sum @@ -15,6 +15,7 @@ cloud.google.com/go/pubsub v1.0.1/go.mod h1:R0Gpsv3s54REJCy4fxDixWD93lHJMoZTyQ2k cloud.google.com/go/storage v1.0.0/go.mod h1:IhtSnM/ZTZV8YYJWCY8RULGVqBDmpoyjwiyrjsg+URw= crawshaw.io/iox v0.0.0-20181124134642-c51c3df30797 h1:yDf7ARQc637HoxDho7xjqdvO5ZA2Yb+xzv/fOnnvZzw= crawshaw.io/iox v0.0.0-20181124134642-c51c3df30797/go.mod h1:sXBiorCo8c46JlQV3oXPKINnZ8mcqnye1EkVkqsectk= +crawshaw.io/sqlite v0.3.2/go.mod h1:igAO5JulrQ1DbdZdtVq48mnZUBAPOeFzer7VhDWNtW4= crawshaw.io/sqlite v0.3.3-0.20210127221821-98b1f83c5508 h1:fILCBBFnjnrQ0whVJlGhfv1E/QiaFDNtGFBObEVRnYg= crawshaw.io/sqlite v0.3.3-0.20210127221821-98b1f83c5508/go.mod h1:igAO5JulrQ1DbdZdtVq48mnZUBAPOeFzer7VhDWNtW4= dmitri.shuralyov.com/app/changes v0.0.0-20180602232624-0a106ad413e3/go.mod h1:Yl+fi1br7+Rr3LqpNJf1/uxUdtRUV+Tnj0o93V2B9MU= @@ -56,6 +57,8 @@ github.com/alecthomas/units v0.0.0-20190717042225-c3de453c63f4/go.mod h1:ybxpYRF github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d/go.mod h1:rBZYJk541a8SKzHPHnH3zbiI+7dagKZ0cgpgrD7Fyho= github.com/alexflint/go-arg v1.3.0/go.mod h1:9iRbDxne7LcR/GSvEr7ma++GLpdIU1zrghf2y2768kM= github.com/alexflint/go-scalar v1.0.0/go.mod h1:GpHzbCOZXEKMEcygYQ5n/aa4Aq84zbxjy3MxYW0gjYw= +github.com/anacrolix/chansync v0.0.0-20210524073341-a336ebc2de92 h1:WGk37RyXPWcIALJxTkTNrXN3yLQp7hSFa3x5GkrK/Rs= +github.com/anacrolix/chansync v0.0.0-20210524073341-a336ebc2de92/go.mod h1:DZsatdsdXxD0WiwcGl0nJVwyjCKMDv+knl1q2iBjA2k= github.com/anacrolix/confluence v1.7.1-0.20210221225853-90405640e928/go.mod h1:NoLcfoRet+kYttjLXJRmh4qBVrylJsfIItik5GGj21A= github.com/anacrolix/confluence v1.7.1-0.20210311004351-d642adb8546c h1:HfbeiZS/0hwdotwtQhllrd3PagmuLgCN9O8CHJgzPGQ= github.com/anacrolix/confluence v1.7.1-0.20210311004351-d642adb8546c/go.mod h1:KCZ3eObqKECNeZg0ekAoJVakHMP3gAdR8i0bQ26IkzM= @@ -77,11 +80,16 @@ github.com/anacrolix/missinggo v1.1.0/go.mod h1:MBJu3Sk/k3ZfGYcS7z18gwfu72Ey/xop github.com/anacrolix/missinggo v1.1.2-0.20190815015349-b888af804467/go.mod h1:MBJu3Sk/k3ZfGYcS7z18gwfu72Ey/xopPFJJbTi5yIo= github.com/anacrolix/missinggo v1.2.1 h1:0IE3TqX5y5D0IxeMwTyIgqdDew4QrzcXaaEnJQyjHvw= github.com/anacrolix/missinggo v1.2.1/go.mod h1:J5cMhif8jPmFoC3+Uvob3OXXNIhOUikzMt+uUjeM21Y= +github.com/anacrolix/missinggo v1.3.0 h1:06HlMsudotL7BAELRZs0yDZ4yVXsHXGi323QBjAVASw= +github.com/anacrolix/missinggo v1.3.0/go.mod h1:bqHm8cE8xr+15uVfMG3BFui/TxyB6//H5fwlq/TeqMc= github.com/anacrolix/missinggo/perf v1.0.0 h1:7ZOGYziGEBytW49+KmYGTaNfnwUqP1HBsy6BqESAJVw= github.com/anacrolix/missinggo/perf v1.0.0/go.mod h1:ljAFWkBuzkO12MQclXzZrosP5urunoLS0Cbvb4V0uMQ= github.com/anacrolix/missinggo/v2 v2.2.0/go.mod h1:o0jgJoYOyaoYQ4E2ZMISVa9c88BbUBVQQW4QeRkNCGY= github.com/anacrolix/missinggo/v2 v2.5.0 h1:75aciOVrzVV1bTH9rl8tYLbXO9A7HXFtHexTChawe/U= github.com/anacrolix/missinggo/v2 v2.5.0/go.mod h1:HYuCbwvJXY3XbcmcIcTgZXHleoDXawxPWx/YiPzFzV0= +github.com/anacrolix/missinggo/v2 v2.5.1/go.mod h1:WEjqh2rmKECd0t1VhQkLGTdIWXO6f6NLjp5GlMZ+6FA= +github.com/anacrolix/missinggo/v2 v2.5.2-0.20210623112532-e21e4ddc477d h1:Z0oOzAPar3HzEwU00HnbK5JRe701kyaa+bnBJOaQ5zU= +github.com/anacrolix/missinggo/v2 v2.5.2-0.20210623112532-e21e4ddc477d/go.mod h1:WEjqh2rmKECd0t1VhQkLGTdIWXO6f6NLjp5GlMZ+6FA= github.com/anacrolix/mmsg v0.0.0-20180515031531-a4a3ba1fc8bb/go.mod h1:x2/ErsYUmT77kezS63+wzZp8E3byYB0gzirM/WMBLfw= github.com/anacrolix/mmsg v1.0.0 h1:btC7YLjOn29aTUAExJiVUhQOuf/8rhm+/nWCMAnL3Hg= github.com/anacrolix/mmsg v1.0.0/go.mod h1:x8kRaJY/dCrY9Al0PEcj1mb/uFHwP6GCJ9fLl4thEPc= @@ -89,12 +97,16 @@ github.com/anacrolix/multiless v0.0.0-20200413040533-acfd16f65d5d/go.mod h1:TrCL github.com/anacrolix/multiless v0.0.0-20210222022749-ef43011a77ec/go.mod h1:TrCLEZfIDbMVfLoQt5tOoiBS/uq4y8+ojuEVVvTNPX4= github.com/anacrolix/multiless v0.1.0 h1:gjR3SdJ+E0avnmEoAV/7K7n2kILZhVu/M6aQEtz8H3s= github.com/anacrolix/multiless v0.1.0/go.mod h1:TrCLEZfIDbMVfLoQt5tOoiBS/uq4y8+ojuEVVvTNPX4= +github.com/anacrolix/multiless v0.1.1-0.20210529082330-de2f6cf29619 h1:ZkusP2EHxvxm+IymiKJ8DBVE/E6fJkb8K/2+GXZpjAY= +github.com/anacrolix/multiless v0.1.1-0.20210529082330-de2f6cf29619/go.mod h1:TrCLEZfIDbMVfLoQt5tOoiBS/uq4y8+ojuEVVvTNPX4= github.com/anacrolix/stm v0.2.0/go.mod h1:zoVQRvSiGjGoTmbM0vSLIiaKjWtNPeTvXUSdJQA4hsg= github.com/anacrolix/stm v0.3.0-alpha h1:yhOHk1NPkpGKqCAOB4XkgFXwB5Eh2KU/WVMksNnxr2Y= github.com/anacrolix/stm v0.3.0-alpha/go.mod h1:spImf/rXwiAUoYYJK1YCZeWkpaHZ3kzjGFjwK5OStfU= github.com/anacrolix/sync v0.0.0-20180808010631-44578de4e778/go.mod h1:s735Etp3joe/voe2sdaXLcqDdJSay1O0OPnM0ystjqk= github.com/anacrolix/sync v0.2.0 h1:oRe22/ZB+v7v/5Mbc4d2zE0AXEZy0trKyKLjqYOt6tY= github.com/anacrolix/sync v0.2.0/go.mod h1:BbecHL6jDSExojhNtgTFSBcdGerzNc64tz3DCOj/I0g= +github.com/anacrolix/sync v0.3.0 h1:ZPjTrkqQWEfnYVGTQHh5qNjokWaXnjsyXTJSMsKY0TA= +github.com/anacrolix/sync v0.3.0/go.mod h1:BbecHL6jDSExojhNtgTFSBcdGerzNc64tz3DCOj/I0g= github.com/anacrolix/tagflag v0.0.0-20180109131632-2146c8d41bf0/go.mod h1:1m2U/K6ZT+JZG0+bdMK6qauP49QT4wE5pmhJXOKKCHw= github.com/anacrolix/tagflag v1.0.0/go.mod h1:1m2U/K6ZT+JZG0+bdMK6qauP49QT4wE5pmhJXOKKCHw= github.com/anacrolix/tagflag v1.1.0/go.mod h1:Scxs9CV10NQatSmbyjqmqmeQNwGzlNe0CMUMIxqHIG8= @@ -105,6 +117,8 @@ github.com/anacrolix/torrent v1.25.1-0.20210221061757-051093ca31f5/go.mod h1:737 github.com/anacrolix/torrent v1.25.1-0.20210224024805-693c30dd889e/go.mod h1:d4V6QqkInfQidWVk8b8hMv8mtciswNitI1A2BiRSQV0= github.com/anacrolix/torrent v1.28.0 h1:zPT5Q4tGVYfIXubGXiD6r6ljHvLT0C65R2++JRhnM3o= github.com/anacrolix/torrent v1.28.0/go.mod h1:FvRiYw56BypG8LHgbyyJoGMVTsjqFvvKR+8agy4bFwM= +github.com/anacrolix/torrent v1.30.1 h1:Y8Sz8V6BEALhafgc7s1a1pJKYzy6X5gfu6ocF5F7nog= +github.com/anacrolix/torrent v1.30.1/go.mod h1:40Hf2bWxFqTbTWbrdig57JnmYLCjShbWWjdbB3VN5n4= github.com/anacrolix/upnp v0.1.2-0.20200416075019-5e9378ed1425 h1:/Wi6l2ONI1FUFWN4cBwHOO90V4ylp4ud/eov6GUcVFk= github.com/anacrolix/upnp v0.1.2-0.20200416075019-5e9378ed1425/go.mod h1:Pz94W3kl8rf+wxH3IbCa9Sq+DTJr8OSbV2Q3/y51vYs= github.com/anacrolix/utp v0.1.0 h1:FOpQOmIwYsnENnz7tAGohA+r6iXpRjrq8ssKSre2Cp4= diff --git a/server/server.go b/server/server.go index 08870f1e..186c7dec 100644 --- a/server/server.go +++ b/server/server.go @@ -154,6 +154,7 @@ func (s *Server) Run(version string) error { if err != nil { return err } + c.EngineDebug = s.DebugTorrent // write cloud-torrent.yaml at the same dir with -c conf and exit if s.ConvYAML {