Example #1
0
        public Peer(string ip, int port, string infohash, string peerClientID, BitFile fl)
        {
            IP   = ip;
            Port = port;
#if DEBUG
            Console.WriteLine("Current InfoHash: {0}", infohash);
#endif
            Infohash     = HexStringToByteArray(infohash);
            PeerClientID = peerClientID;
            CurrentFile  = fl;
        }
Example #2
0
        public bool Start()
        {
            BitFile fs = new BitFile(this.TorrentFile.File.FileName, this.TorrentFile.File.Md5Sum, this.TorrentFile.File.FileSize, (long)Math.Ceiling((double)this.TorrentFile.File.FileSize / (double)this.TorrentFile.PieceSize), this.TorrentFile.PieceSize);

loopStart:

            if (fs.AlmostFinished())
            {
                Console.WriteLine("Flushing buffers!");
                while (!fs.Finished())
                {
                    Thread.Sleep(5);
                }
            }

            if (fs.Finished())
            {
                return(true);
            }

            Console.WriteLine("Announcing...");
            if (!this.TrackerManager.Announce())
            {
                return(false);
            }

            Timer.Reset();

            for (int i = 0; i < TrackerManager.GetPeers().Count; i++)
            {
                var  peerInfo = TrackerManager.GetPeers()[i];
                Peer pr       = new Peer(peerInfo.IP, peerInfo.Port, this.TorrentFile.GetInfoHash(), TrackerManager.GetPeerID(), fs);
                try
                {
                    if (!pr.BeginConnection())
                    {
                        continue;
                    }
                }
                catch (Exception e)
                {
#if DEBUG
                    Console.WriteLine("Exception: {0}", e.Message);
#endif
                    if (pr._ReadThread != null)
                    {
                        pr._ReadThread.Abort();
                    }
                    continue;
                }

                if (fs.AlmostFinished())
                {
                    Console.WriteLine("File successfully downloaded!");
                    goto loopStart;
                }

                if (Timer.GetElapsedSeconds() >= 60.0 * 5)
                {
                    goto loopStart;
                }
            }

            goto loopStart;
        }