@@ -28,6 +28,7 @@ import (
28
28
"github.com/anacrolix/torrent/metainfo"
29
29
"github.com/anacrolix/torrent/mmap_span"
30
30
"github.com/anacrolix/torrent/storage"
31
+ //lru "github.com/hashicorp/golang-lru"
31
32
//"github.com/anacrolix/dht/v2"
32
33
)
33
34
@@ -111,14 +112,12 @@ func (t *Torrent) ReloadFile(files []string, datas [][]byte, tm *TorrentManager)
111
112
/*for _, tracker := range tm.trackers {
112
113
spec.Trackers = append(spec.Trackers, tracker)
113
114
}*/
114
- torrent , _ , err := tm .client .AddTorrentSpec (spec )
115
- if err != nil {
116
- return
115
+ if torrent , _ , err := tm .client .AddTorrentSpec (spec ); err == nil {
116
+ <- torrent .GotInfo ()
117
+ torrent .VerifyData ()
118
+ t .Torrent = torrent
119
+ t .Pause ()
117
120
}
118
- <- torrent .GotInfo ()
119
- torrent .VerifyData ()
120
- t .Torrent = torrent
121
- t .Pause ()
122
121
}
123
122
124
123
func (t * Torrent ) ReloadTorrent (data []byte , tm * TorrentManager ) {
@@ -146,14 +145,12 @@ func (t *Torrent) ReloadTorrent(data []byte, tm *TorrentManager) {
146
145
spec.Trackers = append(spec.Trackers, tracker)
147
146
}*/
148
147
spec .Trackers = append (spec .Trackers , tm .trackers ... )
149
- torrent , _ , err := tm .client .AddTorrentSpec (spec )
150
- if err != nil {
151
- return
148
+ if torrent , _ , err := tm .client .AddTorrentSpec (spec ); err == nil {
149
+ <- torrent .GotInfo ()
150
+ torrent .VerifyData ()
151
+ t .Torrent = torrent
152
+ t .Pause ()
152
153
}
153
- <- torrent .GotInfo ()
154
- torrent .VerifyData ()
155
- t .Torrent = torrent
156
- t .Pause ()
157
154
}
158
155
159
156
/*func (t *Torrent) GetFile(subpath string) ([]byte, error) {
@@ -181,32 +178,30 @@ func (t *Torrent) IsAvailable() bool {
181
178
return false
182
179
}
183
180
184
- func (t * Torrent ) HasTorrent () bool {
185
- return t .status != torrentPending
186
- }
181
+ // func (t *Torrent) HasTorrent() bool {
182
+ // return t.status != torrentPending
183
+ // }
187
184
188
185
func (t * Torrent ) WriteTorrent () {
189
- f , err := os .Create (path .Join (t .filepath , "torrent" ))
190
- if err != nil {
191
- return
192
- }
193
- defer f .Close ()
194
- log .Debug ("Write torrent file" , "path" , t .filepath )
195
- if err := t .Metainfo ().Write (f ); err != nil {
196
- log .Error ("Error while write torrent file" , "error" , err )
197
- return
198
- }
186
+ if f , err := os .Create (path .Join (t .filepath , "torrent" )); err == nil {
187
+ defer f .Close ()
188
+ log .Debug ("Write torrent file" , "path" , t .filepath )
189
+ if err := t .Metainfo ().Write (f ); err != nil {
190
+ log .Error ("Error while write torrent file" , "error" , err )
191
+ return
192
+ }
199
193
200
- t .Pause ()
194
+ t .Pause ()
195
+ }
201
196
}
202
197
203
198
func (t * Torrent ) SeedInQueue () {
199
+ t .status = torrentSeedingInQueue
204
200
if t .currentConns != 0 {
205
201
t .currentConns = 0
206
202
t .Torrent .SetMaxEstablishedConns (0 )
207
203
}
208
204
t .Torrent .CancelPieces (0 , t .Torrent .NumPieces ())
209
- t .status = torrentSeedingInQueue
210
205
}
211
206
212
207
func (t * Torrent ) BoostOff () {
@@ -217,14 +212,13 @@ func (t *Torrent) Seed() {
217
212
if t .status == torrentSeeding {
218
213
return
219
214
}
220
-
215
+ t . status = torrentSeeding
221
216
if t .currentConns == 0 {
222
217
t .currentConns = t .maxEstablishedConns
223
218
t .Torrent .SetMaxEstablishedConns (t .currentConns )
224
219
}
225
220
226
221
t .Torrent .DownloadAll ()
227
- t .status = torrentSeeding
228
222
log .Info ("Download success, seeding(s)" , "hash" , t .InfoHash (), "size" , common .StorageSize (t .BytesCompleted ()), "files" , len (t .Files ()), "pieces" , t .Torrent .NumPieces (), "seg" , len (t .Torrent .PieceStateRuns ()), "cited" , t .cited )
229
223
}
230
224
@@ -239,9 +233,9 @@ func (t *Torrent) Pause() {
239
233
t .Torrent .SetMaxEstablishedConns (0 )
240
234
}
241
235
if t .status != torrentPaused {
236
+ t .status = torrentPaused
242
237
t .maxPieces = 0
243
238
t .Torrent .CancelPieces (0 , t .Torrent .NumPieces ())
244
- t .status = torrentPaused
245
239
}
246
240
}
247
241
@@ -327,11 +321,11 @@ func (tm *TorrentManager) CreateTorrent(t *torrent.Torrent, requested int64, sta
327
321
func (tm * TorrentManager ) GetTorrent (ih metainfo.Hash ) * Torrent {
328
322
tm .lock .RLock ()
329
323
defer tm .lock .RUnlock ()
330
- torrent , ok := tm .torrents [ih ]
331
- if ! ok {
324
+ if torrent , ok := tm .torrents [ih ]; ! ok {
332
325
return nil
326
+ } else {
327
+ return torrent
333
328
}
334
- return torrent
335
329
}
336
330
337
331
func (tm * TorrentManager ) SetTorrent (ih metainfo.Hash , torrent * Torrent ) {
@@ -389,6 +383,7 @@ func GetMagnetURI(infohash metainfo.Hash) string {
389
383
390
384
func (tm * TorrentManager ) UpdateDynamicTrackers (trackers []string ) {
391
385
tm .lock .Lock ()
386
+ defer tm .lock .Unlock ()
392
387
if len (tm .trackers ) == 0 {
393
388
tm .trackers = append (tm .trackers , trackers )
394
389
} else if len (tm .trackers ) == 1 {
@@ -401,8 +396,6 @@ func (tm *TorrentManager) UpdateDynamicTrackers(trackers []string) {
401
396
for _ , t := range tm .pendingTorrents {
402
397
t .AddTrackers (newTrackers )
403
398
}
404
-
405
- tm .lock .Unlock ()
406
399
}
407
400
408
401
func (tm * TorrentManager ) SetTrackers (trackers []string ) {
@@ -496,36 +489,32 @@ func (tm *TorrentManager) AddTorrent(filePath string, BytesRequested int64) *Tor
496
489
// spec.Trackers = append(spec.Trackers, tracker)
497
490
//}
498
491
spec .Trackers = append (spec .Trackers , tm .trackers ... )
499
- t , _ , err := tm .client .AddTorrentSpec (spec )
500
- if err != nil {
501
- return nil
492
+ if t , _ , err := tm .client .AddTorrentSpec (spec ); err == nil {
493
+ //var ss []string
494
+ //slices.MakeInto(&ss, mi.Nodes)
495
+ //tm.client.AddDHTNodes(ss)
496
+ <- t .GotInfo ()
497
+ t .VerifyData ()
498
+ torrent := tm .CreateTorrent (t , BytesRequested , torrentPending , ih )
499
+ torrent .Pause () //SeedInQueue()
500
+ return torrent
502
501
}
503
- //var ss []string
504
- //slices.MakeInto(&ss, mi.Nodes)
505
- //tm.client.AddDHTNodes(ss)
506
- <- t .GotInfo ()
507
- t .VerifyData ()
508
- torrent := tm .CreateTorrent (t , BytesRequested , torrentPending , ih )
509
- torrent .Pause () //SeedInQueue()
510
- return torrent
511
502
} else {
512
503
spec .Storage = storage .NewFile (TmpDir )
513
504
/*for _, tracker := range tm.trackers {
514
505
spec.Trackers = append(spec.Trackers, tracker)
515
506
}*/
516
507
spec .Trackers = append (spec .Trackers , tm .trackers ... )
517
- t , _ , err := tm .client .AddTorrentSpec (spec )
518
- if err != nil {
519
- return nil
508
+ if t , _ , err := tm .client .AddTorrentSpec (spec ); err == nil {
509
+ //var ss []string
510
+ //slices.MakeInto(&ss, mi.Nodes)
511
+ //tm.client.AddDHTNodes(ss)
512
+ <- t .GotInfo ()
513
+ t .VerifyData ()
514
+ torrent := tm .CreateTorrent (t , BytesRequested , torrentPending , ih )
515
+ torrent .Pause ()
516
+ return torrent
520
517
}
521
- //var ss []string
522
- //slices.MakeInto(&ss, mi.Nodes)
523
- //tm.client.AddDHTNodes(ss)
524
- <- t .GotInfo ()
525
- t .VerifyData ()
526
- torrent := tm .CreateTorrent (t , BytesRequested , torrentPending , ih )
527
- torrent .Pause ()
528
- return torrent
529
518
}
530
519
return nil
531
520
}
@@ -847,7 +836,6 @@ func (tm *TorrentManager) listenTorrentProgress() {
847
836
path .Join (defaultTmpFilePath , t .InfoHash ()),
848
837
path .Join (tm .DataDir , t .InfoHash ()),
849
838
)
850
-
851
839
if err != nil {
852
840
//log.Warn("Seeding path error", "hash", t.Torrent.InfoHash(), "size", t.bytesCompleted, "miss", t.bytesMissing, "loop", log_counter)
853
841
err = os .Remove (
@@ -954,6 +942,7 @@ func (tm *TorrentManager) listenTorrentProgress() {
954
942
955
943
if len (activeTorrents ) <= tm .maxActiveTask {
956
944
for _ , t := range activeTorrents {
945
+ //log.Info("Active torrent", "hash", t.Torrent.InfoHash().String(), "request", t.bytesRequested, "complete", t.bytesCompleted)
957
946
t .Run ()
958
947
active_running += 1
959
948
}
@@ -964,8 +953,13 @@ func (tm *TorrentManager) listenTorrentProgress() {
964
953
active_running += 1
965
954
}
966
955
for i := tm .maxActiveTask ; i < len (activeTorrents ); i ++ {
967
- activeTorrents [i ].Pause ()
968
- active_paused += 1
956
+ if activeTorrents [i ].bytesRequested > activeTorrents [i ].bytesCompleted {
957
+ activeTorrents [i ].Run ()
958
+ active_running += 1
959
+ } else {
960
+ activeTorrents [i ].Pause ()
961
+ active_paused += 1
962
+ }
969
963
}
970
964
}
971
965
0 commit comments