Skip to content

Commit 6d6f83f

Browse files
committed
add DHT and PEX
1 parent bb80269 commit 6d6f83f

12 files changed

Lines changed: 426 additions & 341 deletions

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,9 @@
66

77
- Fix license file error
88
- Fix example error
9+
10+
## 0.1.1
11+
- Add DHT support
12+
- Add PEX support
13+
- Change Tracker
14+
- Fix some bugs

README.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,16 @@ Dart library for implementing BitTorrent client.
33

44
## Support:
55
- [BEP 0003 The BitTorrent Protocol Specification](http://www.bittorrent.org/beps/bep_0003.html)
6-
- [BEP 0006 Fast Extension](http://www.bittorrent.org/beps/bep_0006.html)
7-
- [BEP 0015 UDP Tracker Protocal](http://www.bittorrent.org/beps/bep_0015.html)
86
- [BEP 0005 DHT Protocal](http://www.bittorrent.org/beps/bep_0005.html)
9-
- [BEP 0011 Peer Exchange (PEX)](http://www.bittorrent.org/beps/bep_0011.html)
7+
- [BEP 0006 Fast Extension](http://www.bittorrent.org/beps/bep_0006.html)
108
- [BEP 0010 Extension Protocol](http://www.bittorrent.org/beps/bep_0010.html)
11-
9+
- [BEP 0011 Peer Exchange (PEX)](http://www.bittorrent.org/beps/bep_0011.html)
10+
- [BEP 0015 UDP Tracker Protocal](http://www.bittorrent.org/beps/bep_0015.html)
1211

1312
Developing:
13+
- [BEP 0029 uTorrent transport protocol](https://www.bittorrent.org/beps/bep_0029.html)
14+
- [BEP 0014 Local Service Discovery](https://www.bittorrent.org/beps/bep_0014.html)
15+
- [BEP 0055 Holepunch extension](https://www.bittorrent.org/beps/bep_0055.html)
1416
- [BEP 0009 Extension for Peers to Send Metadata Files](http://www.bittorrent.org/beps/bep_0009.html)
1517

1618
Other support will come soon.

example/torrent_client_example.dart

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,36 @@
11
import 'dart:async';
2+
import 'dart:convert';
3+
import 'dart:io';
24

35
import 'package:torrent_model/torrent_model.dart';
46
import 'package:torrent_task/torrent_task.dart';
57

68
var peers = <Uri>{};
79
void main() async {
8-
var model = await Torrent.parse('example/sample3.torrent');
9-
var task = TorrentTask.newTask(model, 'g:/bttest1/');
10+
// https://newtrackon.com/api/stable
11+
var alist = <Uri>[];
12+
try {
13+
var url = Uri.parse('http://newtrackon.com/api/stable');
14+
var client = HttpClient();
15+
var request = await client.getUrl(url);
16+
var response = await request.close();
17+
print(response.statusCode);
18+
var stream = await utf8.decoder.bind(response);
19+
await stream.forEach((element) {
20+
try {
21+
var r = Uri.parse(element);
22+
alist.add(r);
23+
} catch (e) {
24+
//
25+
}
26+
});
27+
} catch (e) {
28+
print(e);
29+
}
30+
var torrentFile = 'example/test11.torrent';
31+
var savePath = 'g:/bttest/';
32+
var model = await Torrent.parse(torrentFile);
33+
var task = TorrentTask.newTask(model, savePath);
1034
Timer timer;
1135
Timer timer1;
1236
task.onTaskComplete(() {
@@ -18,8 +42,12 @@ void main() async {
1842
task.onStop(() async {
1943
print('Task Stopped');
2044
});
21-
2245
var map = await task.start();
46+
47+
alist.forEach((element) {
48+
task.startAnnounceUrl(element, model.infoHashBuffer);
49+
});
50+
2351
model.nodes?.forEach((element) {
2452
task.addDHTNode(element);
2553
});
@@ -28,7 +56,7 @@ void main() async {
2856

2957
timer = Timer.periodic(Duration(seconds: 2), (timer) async {
3058
print(
31-
'Progress : ${(task.progress * 100).toStringAsFixed(2)}% , Connect num : ${task.peersNumber}. Download speed : ${((task.downloadSpeed) * 1000 / 1024).toStringAsFixed(2)} KB/S , upload speed : ${((task.uploadSpeed) * 1000 / 1024).toStringAsFixed(2)} KB/S');
59+
'Progress : ${(task.progress * 100).toStringAsFixed(2)}% , Peers: ${task.connectedPeersNumber}(${task.seederNumber}/${task.allPeersNumber}). Download speed : ${((task.downloadSpeed) * 1000 / 1024).toStringAsFixed(2)} KB/S , upload speed : ${((task.uploadSpeed) * 1000 / 1024).toStringAsFixed(2)} KB/S');
3260
});
3361
// timer1 = Timer.periodic(Duration(seconds: randomInt(21)), (timer) async {
3462
// task.pause();

example/torrent_task_comin_example.dart

Lines changed: 40 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -5,50 +5,50 @@ import 'package:torrent_task/src/task.dart';
55
import 'package:torrent_task/src/utils.dart';
66

77
Future<void> main() async {
8-
var model = await Torrent.parse('example/test8.torrent');
9-
// 不去获取peers
10-
model.announces.clear();
11-
var task = TorrentTask.newTask(model, 'g:/bttest5/');
12-
Timer timer;
13-
Timer timer1;
14-
// task.addPeer(Uri(host:'127.0.0.1'));
15-
task.onFileComplete((filepath) {
16-
print('$filepath downloaded complete');
17-
});
18-
19-
task.onTaskComplete(() {
20-
print('Complete!');
21-
timer?.cancel();
22-
timer1?.cancel();
23-
task.stop();
24-
});
25-
task.onStop(() async {
26-
print('Task Stopped');
27-
});
28-
await task.start();
8+
// var model = await Torrent.parse('example/test8.torrent');
9+
// // 不去获取peers
10+
// model.announces.clear();
11+
// var task = TorrentTask.newTask(model, 'g:/bttest5/');
12+
// Timer timer;
13+
// Timer timer1;
14+
// // task.addPeer(Uri(host:'127.0.0.1'));
15+
// task.onFileComplete((filepath) {
16+
// print('$filepath downloaded complete');
17+
// });
2918

30-
timer = Timer.periodic(Duration(seconds: 2), (timer) {
31-
try {
32-
print(
33-
'Downloaed: ${task.downloaded / (1024 * 1024)} mb , ${((task.downloaded / model.length) * 100).toStringAsFixed(2)}%');
34-
} finally {}
35-
});
19+
// task.onTaskComplete(() {
20+
// print('Complete!');
21+
// timer?.cancel();
22+
// timer1?.cancel();
23+
// task.stop();
24+
// });
25+
// task.onStop(() async {
26+
// print('Task Stopped');
27+
// });
28+
// await task.start();
3629

37-
timer = Timer.periodic(Duration(seconds: 10), (timer) async {
38-
print(
39-
'download speed : ${(await task.downloadSpeed) * 1000 / 1024} , upload speed : ${task.uploadSpeed * 1000 / 1024}');
40-
});
41-
timer1 = Timer.periodic(Duration(seconds: randomInt(21)), (timer) async {
42-
task.pause();
43-
await Future.delayed(Duration(seconds: randomInt(121)));
44-
task.resume();
45-
});
30+
// timer = Timer.periodic(Duration(seconds: 2), (timer) {
31+
// try {
32+
// print(
33+
// 'Downloaed: ${task.downloaded / (1024 * 1024)} mb , ${((task.downloaded / model.length) * 100).toStringAsFixed(2)}%');
34+
// } finally {}
35+
// });
4636

47-
// Timer(Duration(seconds: 20), () async {
37+
// timer = Timer.periodic(Duration(seconds: 10), (timer) async {
38+
// print(
39+
// 'download speed : ${(await task.downloadSpeed) * 1000 / 1024} , upload speed : ${task.uploadSpeed * 1000 / 1024}');
40+
// });
41+
// timer1 = Timer.periodic(Duration(seconds: randomInt(21)), (timer) async {
4842
// task.pause();
49-
// await Future.delayed(Duration(seconds: 120));
43+
// await Future.delayed(Duration(seconds: randomInt(121)));
5044
// task.resume();
5145
// });
52-
// 自己下载自己
53-
task.addPeer(Uri(host: '127.0.0.1', port: 53191));
46+
47+
// // Timer(Duration(seconds: 20), () async {
48+
// // task.pause();
49+
// // await Future.delayed(Duration(seconds: 120));
50+
// // task.resume();
51+
// // });
52+
// // 自己下载自己
53+
// task.addPeer(Uri(host: '127.0.0.1', port: 53191));
5454
}

0 commit comments

Comments
 (0)