maybe fix crash when setup settings and reconnect bt client

This commit is contained in:
YouROK
2020-12-22 16:23:52 +03:00
parent 29031b935e
commit 6f07e85b5a
2 changed files with 8 additions and 2 deletions

View File

@@ -46,6 +46,7 @@ func (bt *BTServer) Disconnect() {
defer bt.mu.Unlock()
if bt.client != nil {
bt.client.Close()
InitApiHelper(nil)
bt.client = nil
utils.FreeOSMemGC()
}

View File

@@ -1,6 +1,7 @@
package torr
import (
"errors"
"fmt"
"io"
"sort"
@@ -51,6 +52,10 @@ type Torrent struct {
}
func NewTorrent(spec *torrent.TorrentSpec, bt *BTServer) (*Torrent, error) {
// TODO panic when settings sets
if bt == nil {
return nil, errors.New("BT client not connected")
}
switch settings.BTsets.RetrackersMode {
case 1:
spec.Trackers = append(spec.Trackers, [][]string{utils.GetDefTrackers()}...)
@@ -282,12 +287,12 @@ func (t *Torrent) Preload(index int, size int64) {
t.PreloadedBytes = t.Torrent.BytesCompleted()
t.muTorrent.Unlock()
stat := fmt.Sprint(file.Torrent().InfoHash().HexString(), utils2.Format(float64(t.PreloadedBytes)), "/", utils2.Format(float64(t.PreloadSize)), "Speed:", utils2.Format(t.DownloadSpeed), "Peers:[", t.Torrent.Stats().ConnectedSeeders, "]", t.Torrent.Stats().ActivePeers, "/", t.Torrent.Stats().TotalPeers)
stat := fmt.Sprint(file.Torrent().InfoHash().HexString(), " ", utils2.Format(float64(t.PreloadedBytes)), "/", utils2.Format(float64(t.PreloadSize)), " Speed:", utils2.Format(t.DownloadSpeed), " Peers:[", t.Torrent.Stats().ConnectedSeeders, "]", t.Torrent.Stats().ActivePeers, "/", t.Torrent.Stats().TotalPeers)
if stat != lastStat {
log.TLogln("Preload:", stat)
lastStat = stat
}
time.Sleep(time.Millisecond * 500)
time.Sleep(time.Millisecond * 1000)
}
log.TLogln("End preload:", file.Torrent().InfoHash().HexString(), "Peers:[", t.Torrent.Stats().ConnectedSeeders, "]", t.Torrent.Stats().ActivePeers, "/", t.Torrent.Stats().TotalPeers)
}