Ejemplo n.º 1
0
        void DownloadLogic(int counter)
        {
            // FIXME: Hardcoded 15kB/sec - is this ok?
            if ((DateTime.Now - manager.StartTime) > TimeSpan.FromSeconds(20))
            {
                foreach (string s in manager.Torrent.GetRightHttpSeeds)
                {
                    string peerId = "-WebSeed-";
                    peerId = peerId + (webseedCount++).ToString().PadLeft(20 - peerId.Length, '0');

                    Uri uri = new Uri(s);
                    // HACK: Don't want to add more than 10 same webseed (sometimes webseeds stuck)
                    if (manager.Peers.ConnectedPeers.FindAll(p => p.Uri.Host.Equals(uri.Host)).Count > 9)
                    {
                        continue;
                    }

                    // Dont' want to add more than 2 same webseed if current webseeds is active (speed more than 5Kbs.
                    if (manager.Peers.ConnectedPeers.FindAll(p => p.Uri.Host.Equals(uri.Host) &&
                                                             p.Monitor.DownloadSpeed > 5 * 1024
                                                             ).Count > 1
                        )
                    {
                        continue;
                    }

                    Peer           peer       = new Peer(peerId, uri);
                    PeerId         id         = new PeerId(peer, manager);
                    HttpConnection connection = new HttpConnection(new Uri(s));
                    connection.Manager = this.manager;
                    peer.IsSeeder      = true;
                    id.BitField.SetAll(true);
                    id.Encryptor    = new PlainTextEncryption();
                    id.Decryptor    = new PlainTextEncryption();
                    id.IsChoking    = false;
                    id.AmInterested = !manager.Complete;
                    id.Connection   = connection;
                    id.ClientApp    = new Software(id.PeerID);
                    manager.Peers.ConnectedPeers.Add(id);
                    manager.RaisePeerConnected(new PeerConnectionEventArgs(manager, id, Direction.Outgoing));
                    PeerIO.EnqueueReceiveMessage(id.Connection, id.Decryptor, Manager.DownloadLimiter, id.Monitor, id.TorrentManager, id.ConnectionManager.messageReceivedCallback, id);
                }
            }

            // Remove inactive peers we haven't heard from if we're downloading
            if (manager.State == TorrentState.Downloading && manager.lastCalledInactivePeerManager + TimeSpan.FromSeconds(5) < DateTime.Now)
            {
                manager.InactivePeerManager.TimePassed();
                manager.lastCalledInactivePeerManager = DateTime.Now;
            }

            // Now choke/unchoke peers; first instantiate the choke/unchoke manager if we haven't done so already
            if (manager.chokeUnchoker == null)
            {
                manager.chokeUnchoker = new ChokeUnchokeManager(manager, manager.Settings.MinimumTimeBetweenReviews, manager.Settings.PercentOfMaxRateToSkipReview);
            }
            manager.chokeUnchoker.UnchokeReview();
        }
Ejemplo n.º 2
0
        void DownloadLogic(int counter)
        {
            // FIXME: Hardcoded 15kB/sec - is this ok?
            if ((DateTime.Now - manager.StartTime) > TimeSpan.FromMinutes(1) && manager.Monitor.DownloadSpeed < 15 * 1024)
            {
                foreach (string s in manager.Torrent.GetRightHttpSeeds)
                {
                    try
                    {
                        string peerId = "-WebSeed-";
                        peerId = peerId + (webseedCount++).ToString().PadLeft(20 - peerId.Length, '0');

                        Uri            uri        = new Uri(s);
                        Peer           peer       = new Peer(peerId, uri);
                        PeerId         id         = new PeerId(peer, manager);
                        HttpConnection connection = new HttpConnection(new Uri(s));
                        connection.Manager = this.manager;
                        peer.IsSeeder      = true;
                        id.BitField.SetAll(true);
                        id.Encryptor    = new PlainTextEncryption();
                        id.Decryptor    = new PlainTextEncryption();
                        id.IsChoking    = false;
                        id.AmInterested = !manager.Complete;
                        id.Connection   = connection;
                        id.ClientApp    = new Software(id.PeerID);
                        manager.Peers.ConnectedPeers.Add(id);
                        manager.RaisePeerConnected(new PeerConnectionEventArgs(manager, id, Direction.Outgoing));
                        PeerIO.EnqueueReceiveMessage(id.Connection, id.Decryptor, Manager.DownloadLimiter, id.Monitor, id.TorrentManager, id.ConnectionManager.messageReceivedCallback, id);
                    }
                    catch (UriFormatException)
                    {
                        logger.Info("Invalid web seed uri '{0}'", s);
                    }
                }

                // FIXME: In future, don't clear out this list. It may be useful to keep the list of HTTP seeds
                // Add a boolean or something so that we don't add them twice.
                manager.Torrent.GetRightHttpSeeds.Clear();
            }

            // Remove inactive peers we haven't heard from if we're downloading
            if (manager.State == TorrentState.Downloading && manager.lastCalledInactivePeerManager + TimeSpan.FromSeconds(5) < DateTime.Now)
            {
                manager.InactivePeerManager.TimePassed();
                manager.lastCalledInactivePeerManager = DateTime.Now;
            }

            // Now choke/unchoke peers; first instantiate the choke/unchoke manager if we haven't done so already
            if (manager.chokeUnchoker == null)
            {
                manager.chokeUnchoker = new ChokeUnchokeManager(manager, manager.Settings.MinimumTimeBetweenReviews, manager.Settings.PercentOfMaxRateToSkipReview);
            }
            manager.chokeUnchoker.UnchokeReview();
        }