Exemplo n.º 1
0
        /// <summary>
        /// Determines was all the segments from range been downloaded
        /// </summary>
        /// <param name="startPos">File position from wich bytes should be read</param>
        /// <param name="count"></param>
        /// <param name="makeRequest">If true the not-finished segments will be marked as High-Priority segments</param>
        /// <returns></returns>
        protected bool CanRead(long startPos, int count, bool makeRequest = true)
        {
            if (!StorageContainer.Available)
            {
                return(false);
            }

            var startIndex = GetSegmentIndex(startPos, SegmentLength);
            var endIndex   = GetSegmentIndex(startPos + count - 1, SegmentLength);

            lock (SyncRoot)
            {
                for (int i = startIndex; i <= endIndex; i++)
                {
                    if (!StorageContainer.CanReadSegment(i) || !_downloadedSegments[i])
                    {
                        if (makeRequest)
                        {
                            if (!HighPrioritySegments.Contains(i))
                            {
                                Logger.Info("Add high priority segment {0}", i);
                                HighPrioritySegments.Add(i);
                            }
                        }
                        return(false);
                    }
                }
                return(true);
            }
        }
Exemplo n.º 2
0
        public void FinishSegment(int index, Source src)
        {
            if (index < 0 || index >= _totalSegmentsCount)
            {
                throw new InvalidOperationException();
            }

            bool downloadFinished;

            lock (_syncRoot)
            {
                if (!_activeSegments[index] || _downloadedSegments[index])
                {
                    return;
                }

                _activeSegmentsCount--;
                _activeSegments[index] = false;
                _doneSegmentsCount++;
                _downloadedSegments[index] = true;
                downloadFinished           = _doneSegmentsCount == _totalSegmentsCount;
                ActiveSources.Remove(src);
                HighPrioritySegments.Remove(index);
            }

            OnSegmentFinished(new SegmentEventArgs
            {
                SegmentInfo = new SegmentInfo {
                    Index = index
                },
                DownloadItem = this,
                Source       = src
            });

            if (downloadFinished)
            {
                OnDownloadFinished();
            }
        }