示例#1
0
 internal void Stop()
 {
     if (listener.Status != ListenerStatus.NotListening)
     {
         listener.Stop();
     }
 }
示例#2
0
 public void Stop()
 {
     if (listener.Status != ListenerStatus.NotListening)
     {
         listener.Stop();
         handleThread.Start();
     }
 }
        public void Dispose ()
        {
            if (Disposed)
                return;

            Disposed = true;
            MainLoop.QueueWait (() => {
                PeerListener.Stop ();
                listenManager.SetListener (new NullPeerListener ());

                DhtListener.Stop ();
                DhtEngine.Dispose ();

                DiskManager.Dispose ();
                LocalPeerDiscovery.Stop ();
            });
        }
示例#4
0
        async Task UpdateSettingsAsync(EngineSettings oldSettings, EngineSettings newSettings)
        {
            await DiskManager.UpdateSettingsAsync(newSettings);

            if (newSettings.DiskCacheBytes != oldSettings.DiskCacheBytes)
            {
                await Task.WhenAll(Torrents.Select(t => DiskManager.FlushAsync(t)));
            }

            ConnectionManager.Settings = newSettings;

            if (oldSettings.UsePartialFiles != newSettings.UsePartialFiles)
            {
                foreach (var manager in Torrents)
                {
                    await manager.UpdateUsePartialFiles(newSettings.UsePartialFiles);
                }
            }
            if (oldSettings.AllowPortForwarding != newSettings.AllowPortForwarding)
            {
                if (newSettings.AllowPortForwarding)
                {
                    await PortForwarder.StartAsync(CancellationToken.None);
                }
                else
                {
                    await PortForwarder.StopAsync(removeExistingMappings : true, CancellationToken.None);
                }
            }

            if (oldSettings.DhtEndPoint != newSettings.DhtEndPoint)
            {
                if (DhtListener.LocalEndPoint != null)
                {
                    await PortForwarder.UnregisterMappingAsync(new Mapping (Protocol.Udp, DhtListener.LocalEndPoint.Port), CancellationToken.None);
                }
                DhtListener.Stop();

                if (newSettings.DhtEndPoint == null)
                {
                    DhtListener = new NullDhtListener();
                    await RegisterDht(new NullDhtEngine());
                }
                else
                {
                    DhtListener = Factories.CreateDhtListener(Settings.DhtEndPoint) ?? new NullDhtListener();
                    if (IsRunning)
                    {
                        DhtListener.Start();
                    }

                    if (oldSettings.DhtEndPoint == null)
                    {
                        var dht = Factories.CreateDht();
                        await dht.SetListenerAsync(DhtListener);
                        await RegisterDht(dht);
                    }
                    else
                    {
                        await DhtEngine.SetListenerAsync(DhtListener);
                    }
                }

                if (DhtListener.LocalEndPoint != null)
                {
                    await PortForwarder.RegisterMappingAsync(new Mapping (Protocol.Udp, DhtListener.LocalEndPoint.Port));
                }
            }

            if (!Equals(oldSettings.ListenEndPoint, newSettings.ListenEndPoint))
            {
                if (PeerListener.LocalEndPoint != null)
                {
                    await PortForwarder.UnregisterMappingAsync(new Mapping (Protocol.Tcp, PeerListener.LocalEndPoint.Port), CancellationToken.None);
                }

                PeerListener.Stop();
                PeerListener = (newSettings.ListenEndPoint == null ? null : Factories.CreatePeerConnectionListener(newSettings.ListenEndPoint)) ?? new NullPeerListener();
                listenManager.SetListener(PeerListener);

                if (IsRunning)
                {
                    PeerListener.Start();
                    // The settings could say to listen at port 0, which means 'choose one dynamically'
                    if (PeerListener.LocalEndPoint != null)
                    {
                        await PortForwarder.RegisterMappingAsync(new Mapping (Protocol.Tcp, PeerListener.LocalEndPoint.Port));
                    }
                }
            }

            if (oldSettings.AllowLocalPeerDiscovery != newSettings.AllowLocalPeerDiscovery)
            {
                RegisterLocalPeerDiscovery(!newSettings.AllowLocalPeerDiscovery ? null : Factories.CreateLocalPeerDiscovery());
            }
        }
示例#5
0
        public static void DownloadTorrent(string url, string downloadDirectory, int maxUploadSpeed)
        {
            var torrentFileName = string.Format("{0}.torrent", url.GetHashCode().ToString(CultureInfo.InvariantCulture));

            var torrentManager =
                new TorrentManager(
                    Torrent.Load(new Uri(url), torrentFileName),
                    downloadDirectory,
                    new TorrentSettings());

            // find an unused port
            var listener = new TcpListener(IPAddress.Any, 0);

            listener.Start();
            var port = ((IPEndPoint)listener.LocalEndpoint).Port;

            listener.Stop();

            var engineSettings = new EngineSettings(downloadDirectory, port)
            {
                PreferEncryption     = false,
                AllowedEncryption    = EncryptionTypes.All,
                GlobalMaxUploadSpeed = maxUploadSpeed
            };

            var dhtListner = new DhtListener(new IPEndPoint(IPAddress.Any, port));
            var dht        = new DhtEngine(dhtListner);

            var engine = new ClientEngine(engineSettings);

            engine.Register(torrentManager);
            engine.RegisterDht(dht);
            dhtListner.Start();
            engine.DhtEngine.Start();

            engine.StartAll();

            while (true)
            {
                var status = string.Format("{0:0.00}% - {1}\nDL:{2:0.00} kB/s    \nUL:{3:0.00} kB/s    ",
                                           torrentManager.Progress,
                                           torrentManager.Torrent.Name,
                                           torrentManager.Monitor.DownloadSpeed / 1024.0,
                                           torrentManager.Monitor.UploadSpeed / 1024.0);

                var cursorPosition = new Tuple <int, int>(Console.CursorLeft, Console.CursorTop);
                Console.WriteLine(status);

                if (torrentManager.State == TorrentState.Seeding)
                {
                    Console.WriteLine();
                    break;
                }

                Console.SetCursorPosition(cursorPosition.Item1, cursorPosition.Item2);

                Thread.Sleep(500);
            }

            dhtListner.Stop();
            engine.DhtEngine.Stop();
            engine.StopAll();
            engine.Dispose();

            File.Delete(torrentFileName);
        }