This commit is contained in:
YouROK
2020-11-06 16:49:18 +03:00
parent a1e17b1cf3
commit f92b9eebf3
11 changed files with 83 additions and 60 deletions

View File

@@ -1,10 +1,9 @@
package reader
package torr
import (
"github.com/anacrolix/torrent"
"server/torr"
"io"
"github.com/anacrolix/torrent"
)
type Reader struct {
@@ -14,7 +13,7 @@ type Reader struct {
file *torrent.File
}
func NewReader(torr *torr.Torrent, file *torrent.File, readahead int64) *Reader {
func NewReader(torr *Torrent, file *torrent.File, readahead int64) *Reader {
r := new(Reader)
r.file = file
r.Reader = file.NewReader()

View File

@@ -5,8 +5,8 @@ import (
"sort"
"sync"
"github.com/anacrolix/torrent"
"server/settings"
"server/torr/reader"
"server/torr/utils"
"github.com/anacrolix/torrent/metainfo"
@@ -36,7 +36,7 @@ type Cache struct {
prcLoaded int
readers map[*reader.Reader]struct{}
readers map[torrent.Reader]struct{}
}
func NewCache(capacity int64, storage *Storage) *Cache {
@@ -45,7 +45,7 @@ func NewCache(capacity int64, storage *Storage) *Cache {
filled: 0,
pieces: make(map[int]*Piece),
s: storage,
readers: make(map[*reader.Reader]struct{}),
readers: make(map[torrent.Reader]struct{}),
}
return ret
@@ -173,13 +173,13 @@ func prc(val, of int) int {
return int(float64(val) * 100.0 / float64(of))
}
func (c *Cache) AddReader(r *reader.Reader) {
func (c *Cache) AddReader(r torrent.Reader) {
c.muReader.Lock()
defer c.muReader.Unlock()
c.readers[r] = struct{}{}
}
func (c *Cache) RemReader(r *reader.Reader) {
func (c *Cache) RemReader(r torrent.Reader) {
c.muReader.Lock()
defer c.muReader.Unlock()
delete(c.readers, r)

View File

@@ -10,12 +10,10 @@ import (
"server/torr/utils"
utils2 "server/utils"
"server/torr/reader"
"server/torr/storage/torrstor"
"github.com/anacrolix/torrent"
"github.com/anacrolix/torrent/metainfo"
"github.com/labstack/gommon/bytes"
)
type TorrentStatus int
@@ -227,15 +225,15 @@ func (t *Torrent) Length() int64 {
return t.Torrent.Length()
}
func (t *Torrent) NewReader(file *torrent.File, readahead int64) *reader.Reader {
func (t *Torrent) NewReader(file *torrent.File, readahead int64) *Reader {
if t.status == TorrentClosed {
return nil
}
reader := reader.NewReader(t, file, readahead)
reader := NewReader(t, file, readahead)
return reader
}
func (t *Torrent) CloseReader(reader *reader.Reader) {
func (t *Torrent) CloseReader(reader *Reader) {
reader.Close()
t.cache.RemReader(reader)
t.expiredTime = time.Now().Add(time.Second * time.Duration(settings.BTsets.TorrentDisconnectTimeout))
@@ -317,7 +315,7 @@ func (t *Torrent) Preload(file *torrent.File, size int64) {
for t.status == TorrentPreload {
t.expiredTime = time.Now().Add(time.Minute * 5)
t.PreloadedBytes = t.Torrent.BytesCompleted()
log.Println("Preload:", file.Torrent().InfoHash().HexString(), bytes.Format(t.PreloadedBytes), "/", bytes.Format(t.PreloadSize), "Speed:", utils2.Format(t.DownloadSpeed), "Peers:[", t.Torrent.Stats().ConnectedSeeders, "]", t.Torrent.Stats().ActivePeers, "/", t.Torrent.Stats().TotalPeers)
log.Println("Preload:", 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 t.PreloadedBytes >= t.PreloadSize {
return
}