-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ditto.BitTorrent.Client.cs
103 lines (86 loc) · 3.35 KB
/
Ditto.BitTorrent.Client.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
using System;
using System.Net;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Ditto.Common;
namespace Ditto.BitTorrent
{
class Client : IDisposable
{
readonly private IPAddress myIpAddress;
public static readonly Int32 myPort = 6881;
public static readonly byte[] peerId = new byte[20].FillRandom();
public static bool extensionsEnabled = true; // Extensions enabled by default -- option to disable?
private DHT.Client dht;
private static ILogger logger { get; } = GlobalLogger.CreateLogger<Client>();
public Client() {
dht = new DHT.Client();
myIpAddress = IPAddress.Any;
"ditto.to#".ToASCII().CopyTo(peerId, 0);
}
public async Task Example(IPAddress[] bootstrapAddresses, IPEndPoint peer=null)
{
if (peer == null)
{
foreach (var address in bootstrapAddresses)
{
var bootstrapNode = new IPEndPoint(address, 9527);
dht.AddNode(bootstrapNode);
}
var ubuntuPeers = await dht.GetPeers(KnownTorrents.ubuntuUnknownInfohash);
{
logger.LogInformation(LoggingEvents.DHT_PROTOCOL_MSG,
$"Requested peers for Ubuntu {KnownTorrents.ubuntuUnknownInfohash.ToHex()} and got {ubuntuPeers.Count}!");
foreach (var ep in ubuntuPeers)
{
logger.LogInformation(LoggingEvents.ATTEMPT_CONNECTION, $"Attempting to connect to peer at {ep}.");
try
{
var testTorrent = new Ditto.PeerProtocol.TorrentManager(KnownTorrents.ubuntuUnknownInfohash);
testTorrent.AddPeer(peer); // this will need to be managed more systematically
break;
}
catch (Exception ex)
{
logger.LogError(LoggingEvents.DHT_ERROR, "It failed: " + ex);
await Task.Delay(1000);
logger.LogError(LoggingEvents.DHT_ERROR, "Do I have another peer to try?");
continue;
}
}
logger.LogInformation("Done.");
}
}
else
{
try
{
var ubuntuTorrent = new Ditto.PeerProtocol.TorrentManager(KnownTorrents.netBSDInfohash);
ubuntuTorrent.AddPeer(peer); // this will need to be managed more systematically
}
catch (Exception ex)
{
logger.LogError(LoggingEvents.DHT_ERROR, "It failed: " + ex);
}
}
}
#region IDisposable Support
private bool disposedValue = false; // To detect redundant calls
protected virtual void Dispose(bool disposing)
{
if (!disposedValue)
{
dht?.Dispose();
disposedValue = true;
}
}
~Client() {
Dispose(false);
}
public void Dispose()
{
Dispose(true);
}
#endregion
}
}