h1. Enable DHT
public void StartDht (ClientEngine engine, int port)
{
// Send/receive DHT messages on the specified port
IPEndPoint listenAddress = new IPEndPoint (IPAddress.Any, port);
// Create a listener which will process incoming/outgoing dht messages
DhtListener listener = new UdpListener (listenAddress);
// Create the dht engine
DhtEngine dht = new DhtEngine (listener);
// Connect the Dht engine to the MonoTorrent engine
engine.RegisterDht (dht);
// Start listening for dht messages and activate the DHT engine
listener.Start ();
// If there are existing DHT nodes stored on disk, load them
// into the DHT engine so we can try and avoid a (very slow)
// full bootstrap
byte[] nodes = null;
if (File.Exists (path))
nodes = File.ReadAllBytes (nodes);
dht.Start (nodes);
}
public void StopDht (DhtEngine dht, DhtListener listener)
{
// Stop the listener and dht engine. This does not
// clear internal data so the DHT can be started again
// later without needing a full bootstrap.
listener.Stop ();
dht.Stop ();
// Save all known dht nodes to disk so they can be restored
// later. This is *highly* recommended as it makes startup
// much much faster.
File.WriteAllBytes (path, dht.SaveNodes ());
}