本文整理汇总了Golang中github.com/anacrolix/torrent.Torrent.Length方法的典型用法代码示例。如果您正苦于以下问题:Golang Torrent.Length方法的具体用法?Golang Torrent.Length怎么用?Golang Torrent.Length使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/anacrolix/torrent.Torrent
的用法示例。
在下文中一共展示了Torrent.Length方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: Update
func (torrent *Torrent) Update(t *torrent.Torrent) {
torrent.Name = t.Name()
torrent.Loaded = t.Info() != nil
if torrent.Loaded {
torrent.Size = t.Length()
}
totalChunks := 0
totalCompleted := 0
tfiles := t.Files()
if len(tfiles) > 0 && torrent.Files == nil {
torrent.Files = make([]*File, len(tfiles))
}
//merge in files
for i, f := range tfiles {
path := f.Path()
file := torrent.Files[i]
if file == nil {
file = &File{Path: path}
torrent.Files[i] = file
}
chunks := f.State()
file.Size = f.Length()
file.Chunks = len(chunks)
completed := 0
for _, p := range chunks {
if p.Complete {
completed++
}
}
file.Completed = completed
file.Percent = percent(int64(file.Completed), int64(file.Chunks))
file.f = f
totalChunks += file.Chunks
totalCompleted += file.Completed
}
//cacluate rate
now := time.Now()
bytes := t.BytesCompleted()
torrent.Percent = percent(bytes, torrent.Size)
if !torrent.updatedAt.IsZero() {
dt := float32(now.Sub(torrent.updatedAt))
db := float32(bytes - torrent.Downloaded)
rate := db * (float32(time.Second) / dt)
if rate >= 0 {
torrent.DownloadRate = rate
}
}
torrent.Downloaded = bytes
torrent.updatedAt = now
torrent.t = t
}