Ejemplo n.º 1
0
        /// <summary>
        /// 根据输入参数更新 Peer 的状态。
        /// </summary>
        /// <param name="inputParameters">BT 客户端请求 Tracker 服务器时传递的参数。</param>
        public void UpdateStatus(AnnounceInputParameters inputParameters)
        {
            var now = DateTime.Now;

            var elapsedTime = (now - LastRequestTrackerTime).TotalSeconds;

            if (elapsedTime < 1)
            {
                elapsedTime = 1;
            }

            ClientAddress = inputParameters.ClientAddress;
            // 通过差值除以消耗的时间,得到每秒的大概下载速度。
            DownloadSpeed          = (int)((inputParameters.Downloaded - DownLoaded) / elapsedTime);
            DownLoaded             = inputParameters.Downloaded;
            UploadSpeed            = (int)((inputParameters.Uploaded) / elapsedTime);
            Uploaded               = inputParameters.Uploaded;
            Left                   = inputParameters.Left;
            PeerId                 = inputParameters.PeerId;
            LastRequestTrackerTime = now;

            // 如果没有剩余数据,则表示 Peer 已经完成下载。
            if (Left == 0)
            {
                IsCompleted = true;
            }
        }
Ejemplo n.º 2
0
        public Peer(AnnounceInputParameters inputParameters)
        {
            UniqueId = inputParameters.PeerId;

            // 根据输入参数更新 Peer 的状态。
            UpdateStatus(inputParameters);
        }
 /// <summary>
 /// 检测参数与种子唯一标识的状态。
 /// </summary>
 private void CheckParameters(string infoHash, AnnounceInputParameters inputParameters)
 {
     if (string.IsNullOrEmpty(infoHash))
     {
         throw new Exception("种子的唯一标识不能为空。");
     }
     if (inputParameters == null)
     {
         throw new Exception("BT 客户端传入的参数不能为空。");
     }
 }
        public void DeletePeer(string infoHash, AnnounceInputParameters inputParameters)
        {
            CheckParameters(infoHash, inputParameters);

            if (!_peers.ContainsKey(infoHash))
            {
                return;
            }

            _peers[infoHash].RemoveAll(p => p.UniqueId == inputParameters.PeerId);

            UpdateBitTorrentStatus(infoHash);
        }
        public Peer AddPeer(string infoHash, AnnounceInputParameters inputParameters)
        {
            CheckParameters(infoHash, inputParameters);

            var newPeer = new Peer(inputParameters);

            if (!_peers.ContainsKey(infoHash))
            {
                _peers.TryAdd(infoHash, new List <Peer> {
                    newPeer
                });
            }

            _peers[infoHash].Add(newPeer);

            UpdateBitTorrentStatus(infoHash);

            return(newPeer);
        }
        public void UpdatePeer(string infoHash, AnnounceInputParameters inputParameters)
        {
            CheckParameters(infoHash, inputParameters);

            if (!_peers.ContainsKey(inputParameters.InfoHash))
            {
                _peers.TryAdd(infoHash, new List <Peer>());
            }
            if (!_bitTorrentStatus.ContainsKey(inputParameters.InfoHash))
            {
                _bitTorrentStatus.TryAdd(infoHash, new BitTorrentStatus());
            }

            // 如果 Peer 不存在则添加,否则更新其状态。
            var peers = _peers[infoHash];
            var peer  = peers.FirstOrDefault(p => p.UniqueId == inputParameters.PeerId);

            if (peer == null)
            {
                AddPeer(infoHash, inputParameters);
            }
            else
            {
                peer.UpdateStatus(inputParameters);
            }

            // 根据事件更新种子状态与 Peer 信息。
            if (inputParameters.Event == TorrentEvent.Stopped)
            {
                DeletePeer(infoHash, inputParameters);
            }
            if (inputParameters.Event == TorrentEvent.Completed)
            {
                _bitTorrentStatus[infoHash].Downloaded++;
            }

            UpdateBitTorrentStatus(infoHash);
        }