Enable DHT¶
1
2 public void StartDht (ClientEngine engine, int port)
3 {
4 // Send/receive DHT messages on the specified port
5 IPEndPoint listenAddress = new IPEndPoint (IPAddress.Any, port);
6
7 // Create a listener which will process incoming/outgoing dht messages
8 DhtListener listener = new UdpListener (listenAddress);
9
10 // Create the dht engine
11 DhtEngine dht = new DhtEngine (listener);
12
13 // Connect the Dht engine to the MonoTorrent engine
14 engine.RegisterDht (dht);
15
16 // Start listening for dht messages and activate the DHT engine
17 listener.Start ();
18
19 // If there are existing DHT nodes stored on disk, load them
20 // into the DHT engine so we can try and avoid a (very slow)
21 // full bootstrap
22 byte[] nodes = null;
23 if (File.Exists (path))
24 nodes = File.ReadAllBytes (nodes);
25 dht.Start (nodes);
26 }
27
28 public void StopDht (DhtEngine dht, DhtListener listener)
29 {
30 // Stop the listener and dht engine. This does not
31 // clear internal data so the DHT can be started again
32 // later without needing a full bootstrap.
33 listener.Stop ();
34 dht.Stop ();
35
36 // Save all known dht nodes to disk so they can be restored
37 // later. This is *highly* recommended as it makes startup
38 // much much faster.
39 File.WriteAllBytes (path, dht.SaveNodes ());
40 }
Also available in:
HTML
TXT