示例#1
0
 /// <summary>
 /// 构造函数
 /// </summary>
 /// <param name="downloader">下载器</param>
 /// <param name="choker">阻塞器</param>
 /// <param name="piecesNumber">下载文件的片断数量</param>
 /// <param name="isEverythingPending"></param>
 /// <param name="totalUp">参数类</param>
 /// <param name="maxUploadRate">最大上传速率</param>
 /// <param name="scheduleFunction"></param>
 public Connecter(Downloader downloader, IChoker choker, int piecesNumber, PendingDelegate isEverythingPending,
                  Measure totalUp, int maxUploadRate, SchedulerDelegate scheduleFunction)
 {
     this.downloader           = downloader;
     this.choker               = choker;
     this.piecesNumber         = piecesNumber;
     this.isEverythingPending  = isEverythingPending;
     this.maxUploadRate        = maxUploadRate;
     this.scheduleFunction     = scheduleFunction;
     this.totalUp              = totalUp;
     this.rateCapped           = false;
     this.connectionDictionary = new Dictionary <IEncryptedConnection, IConnection>();
     this.endgame              = false;
     this.CheckEndgame();
 }
示例#2
0
 public DownloaderFeedback(IChoker choker, SchedulerDelegate addTaskFunction, StatusDelegate statusFunction, MeasureRateDelegate uploadFunction,
                           MeasureRateDelegate downloadFunction, MeasureRateDelegate remainingFunction, AmountDelegate leftFunction, long fileLength, Flag finishFlag,
                           double interval, bool sp)
 {
     this.choker            = choker;
     this.addTaskFunction   = addTaskFunction;
     this.statusFunction    = statusFunction;
     this.uploadFunction    = uploadFunction;
     this.downloadFunction  = downloadFunction;
     this.remainingFunction = remainingFunction;
     this.leftFunction      = leftFunction;
     this.fileLength        = fileLength;
     this.finishFlag        = finishFlag;
     this.interval          = interval;
     this.sp      = sp;
     this.lastIDs = new List <byte[]>();
     this.Display();
 }
示例#3
0
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="connection">连接类</param>
        /// <param name="choker">阻塞类</param>
        /// <param name="storageWrapper">磁盘封装类</param>
        /// <param name="maxSliceLength">最大子片断长度</param>
        /// <param name="maxRatePeriod">最大速率更新时间</param>
        /// <param name="fudge"></param>
        public Upload(IConnection connection, IChoker choker, IStorageWrapper storageWrapper, int maxSliceLength,
                      double maxRatePeriod, double fudge)
        {
            this.connection     = connection;
            this.choker         = choker;
            this.storageWrapper = storageWrapper;
            this.maxSliceLength = maxSliceLength;
            this.maxRatePeriod  = maxRatePeriod;
            this.choked         = true;
            this.interested     = false;
            this.buffer         = new LinkedList <ActiveRequest>();
            this.measure        = new Measure(maxRatePeriod, fudge);

            //如果已经获取的某些片断,则向所有节点发送BitField信息
            if (storageWrapper.DoIHaveAnything())
            {
                connection.SendBitfield(storageWrapper.GetHaveList());
            }
        }
示例#4
0
 /// <summary>
 /// 构造函数
 /// </summary>
 /// <param name="downloader">下载器</param>
 /// <param name="choker">阻塞器</param>
 /// <param name="piecesNumber">下载文件的片断数量</param>
 /// <param name="isEverythingPending"></param>
 /// <param name="totalUp">参数类</param>
 /// <param name="maxUploadRate">最大上传速率</param>
 /// <param name="scheduleFunction"></param>
 public DummyConnecter(Downloader downloader, IChoker choker, int piecesNumber, PendingDelegate isEverythingPending,
                       Measure totalUp, int maxUploadRate, SchedulerDelegate scheduleFunction)
     : base(downloader, choker, piecesNumber, isEverythingPending, totalUp, maxUploadRate, scheduleFunction)
 {
 }
示例#5
0
        public static void StartDownload(Parameters parameters, Flag doneFlag, StatusDelegate statusFunction, ErrorDelegate errorFunction, FinishedDelegate finishedFunction)
        {
            if (parameters.ResponseFile.Length == 0 && parameters.Url.Length == 0)
            {
                throw new BitTorrentException("需要Response file 或者 Url");
            }

            Parameters = parameters;
            Stream stream = null;

            byte[] response;
            long   length = 0;

            try
            {
                if (parameters.ResponseFile.Length != 0)
                {
                    stream = File.OpenRead(parameters.ResponseFile);
                    length = stream.Length;
                }

                else
                {
                    WebRequest  webRequest  = WebRequest.Create(parameters.Url);
                    WebResponse webResponse = webRequest.GetResponse();
                    stream = webResponse.GetResponseStream();
                    length = webResponse.ContentLength;
                }

                response = new byte[length];
                stream.Read(response, 0, (int)length);
            }

            catch
            {
                throw new BitTorrentException("Problem getting response info");
            }

            finally
            {
                if (stream != null)
                {
                    stream.Close();
                }
            }

            DictNode rootNode;

            try
            {
                rootNode = BEncodingFactory.Decode(response) as DictNode;
                //BTFormat.CheckMessage(rootNode);
            }
            catch
            {
                throw new BitTorrentException("got bad file");
            }

            DictNode       infoNode = rootNode["info"] as DictNode;
            List <BitFile> files    = new List <BitFile>();
            string         file;
            long           fileLength;

            try
            {
                if (infoNode.ContainsKey("length"))
                {
                    fileLength = (infoNode["length"] as IntNode).Value;
                    BytesNode nameNode = (infoNode["name"] as BytesNode);
                    if (nameNode == null)
                    {
                        return;
                    }
                    file = @"k:\torrent\" + nameNode.StringText;
                    Make(file, false);
                    files.Add(new BitFile(file, fileLength));
                }

                else
                {
                    fileLength = 0L;
                    ListNode filesNode = infoNode["files"] as ListNode;
                    foreach (BEncodedNode handler in filesNode)
                    {
                        DictNode fileNode = infoNode["files"] as DictNode;
                        fileLength += (fileNode["length"] as IntNode).Value;
                    }
                    //访问文件夹
                    BytesNode nameNode = infoNode["name"] as BytesNode;
                    if (nameNode == null)
                    {
                        return;
                    }
                    file = @"C:\torrent\" + nameNode.StringText;
                    // if this path exists, and no files from the info dict exist, we assume it's a new download and
                    // the user wants to create a new directory with the default name
                    bool existed = false;
                    if (Directory.Exists(file))
                    {
                        foreach (BEncodedNode handler in filesNode)
                        {
                            DictNode fileNode = handler as DictNode;
                            ListNode pathNode = fileNode["path"] as ListNode;
                            if (File.Exists(Path.Combine(file, (pathNode[0] as BytesNode).StringText)))
                            {
                                existed = true;
                                break;
                            }
                        }

                        if (!existed)
                        {
                            file = Path.Combine(file, (infoNode["name"] as BytesNode).StringText);
                        }
                    }
                    Make(file, true);

                    // alert the UI to any possible change in path
                    //TODO: if (pathFunc != null)
                    // pathFunc(file)

                    foreach (BEncodedNode handler in filesNode)
                    {
                        DictNode fileNode = handler as DictNode;
                        ListNode pathNode = fileNode["path"] as ListNode;
                        string   n        = file;
                        foreach (BEncodedNode stringHandler in pathNode)
                        {
                            n = Path.Combine(n, (stringHandler as BytesNode).StringText);
                        }
                        files.Add(new BitFile(n, (fileNode["length"] as IntNode).Value));
                        Make(n, false);
                    }
                }
            }
            catch
            {
                throw new BitTorrentException("Couldn't allocate directory...");
            }

            Flag           finishFlag     = new Flag();
            FinishedHelper finishedHelper = new FinishedHelper();

            finishedHelper.ErrorFunction    = errorFunction;
            finishedHelper.FinishedFunction = finishedFunction;
            finishedHelper.DoneFlag         = finishFlag;

            string sID = DateTime.Now.ToLongDateString() + "www.wallywood.co.uk";

            byte[] myID = Globals.GetSha1Hash(Encoding.ASCII.GetBytes(sID));//Globals.Sha1.ComputeHash(Encoding.Default.GetBytes(sID));

            byte[]        piece  = (infoNode["pieces"] as BytesNode).ByteArray;
            List <byte[]> pieces = new List <byte[]>();

            for (int i = 0; i < piece.Length; i += 20)
            {
                byte[] temp = new byte[20];
                Buffer.BlockCopy(piece, i, temp, 0, 20);
                pieces.Add(temp);
            }

            Storage _storage = null;

            try
            {
                try
                {
                    //_storage = new Storage(files, parameters.AllocatePause, statusFunction);
                    finishedHelper.Storage = _storage;
                }
                catch (Exception ex)
                {
                    errorFunction("trouble accessing files - " + ex.Message);
                }
                IntNode pieceLengthNode = infoNode["piece length"] as IntNode;
                StorageWrapper = new StorageWrapper(_storage, parameters.DownloadSliceSize, pieces, (int)pieceLengthNode.Value,
                                                    finishedHelper.Finished, finishedHelper.Failed, statusFunction, finishFlag, parameters.CheckHashes,
                                                    finishedHelper.DataFlunked);
            }
            // Catch ValueError
            // failed("bad data")
            // catch IO Error
            catch (Exception ex)
            {
                finishedHelper.Failed("Problem - " + ex.Message);
            }

            if (finishFlag.IsSet)
            {
                return;
            }

            RawServer rawServer = new RawServer(finishFlag, parameters.TimeoutCheckInterval, parameters.Timeout, false);

            if (parameters.MaxPort < parameters.MinPort)
            {
                int temp = parameters.MinPort;
                parameters.MinPort = parameters.MaxPort;
                parameters.MaxPort = parameters.MinPort;
            }

            ushort listenPort;

            for (listenPort = parameters.MinPort; listenPort <= parameters.MaxPort; listenPort++)
            {
                try
                {
                    rawServer.Bind(listenPort, parameters.Bind, false);
                    break;
                }
                catch (SocketException)
                {
                    //TODO: Error Code
                }
            }

            //TODO: Check whether nothing bound
            Choker = new Choker(parameters.MaxUploads, rawServer.AddTask, finishFlag);
            Measure     uploadMeasure   = new Measure(parameters.MaxRatePeriod, parameters.UploadRateFudge);
            Measure     downloadMeasure = new Measure(parameters.MaxRatePeriod);
            RateMeasure rateMeasure     = new RateMeasure(StorageWrapper.LeftLength);
            Downloader  downloader      =
                new NormalDownloader(StorageWrapper, new PiecePicker(pieces.Count), parameters.RequestBackLog,
                                     parameters.MaxRatePeriod, pieces.Count, downloadMeasure, parameters.SnubTime,
                                     rateMeasure.DataCameIn);

            Connecter connecter =
                new Connecter(downloader, Choker, pieces.Count, StorageWrapper.IsEverythingPending, uploadMeasure,
                              parameters.MaxUploadRate << 10, rawServer.AddTask);

            byte[] infoHash = Globals.GetSha1Hash(BEncodingFactory.ByteArrayEncode(infoNode));//Globals.Sha1.ComputeHash(BEncodingFactory.ByteArrayEncode(infoNode));

            Encrypter encrypter = new Encrypter(connecter, rawServer, myID, parameters.MaxMessageLength, rawServer.AddTask,
                                                parameters.KeepAliveInterval, infoHash, parameters.MaxInitiate);
            //ReRequester reRequester =
            //    new ReRequester((rootNode["announce"] as BytesNode).StringText, parameters.RerequestInterval,
            //                    rawServer.AddTask, connecter.GetConnectionsCount, parameters.MinPeers,
            //                    encrypter.StartConnect, rawServer.AddExternalTask,
            //                    StorageWrapper.GetLeftLength, uploadMeasure.GetTotalLength, downloadMeasure.GetTotalLength,
            //                    listenPort, parameters.IP,
            //                    myID, infoHash, parameters.HttpTimeout, null, parameters.MaxInitiate, finishFlag);

            DownloaderFeedback downloaderFeedback =
                new DownloaderFeedback(Choker, rawServer.AddTask, statusFunction, uploadMeasure.GetUpdatedRate,
                                       downloadMeasure.GetUpdatedRate, rateMeasure.GetTimeLeft, rateMeasure.GetLeftTime,
                                       fileLength, finishFlag, parameters.DisplayInterval,
                                       parameters.Spew);

            statusFunction("connection to peers", -1, -1, -1, -1);
            //TODO: finishedHelper.errorfunc

            finishedHelper.FinishFlag = finishFlag;
            //finishedHelper.ReRequester = reRequester;
            finishedHelper.RateMeasure = rateMeasure;

            //reRequester.d(0);
            rawServer.ListenForever(encrypter);
            //reRequester.Announce(2, null);
        }