private static void LoadPersistedObjects(DownloadItem[] downloads) { for (int i = 0; i < downloads.Length; i++) { List<Segment> segments = new List<Segment>(); for (int j = 0; j < downloads[i].Segments.Length; j++) { Segment seg = new Segment(); seg.Index = downloads[i].Segments[j].Index; seg.InitialStartPosition = downloads[i].Segments[j].InitialStartPositon; seg.StartPosition = downloads[i].Segments[j].StartPositon; seg.EndPosition = downloads[i].Segments[j].EndPosition; segments.Add(seg); } Downloader d = DownloadManager.Instance.Add( downloads[i].rl, downloads[i].mirrors, downloads[i].LocalFile, segments, downloads[i].remoteInfo, downloads[i].requestedSegments, false, downloads[i].createdDateTime); if (downloads[i].extendedProperties != null) { SerializableDictionary<string, object>.Enumerator e = downloads[i].extendedProperties.GetEnumerator(); while (e.MoveNext()) { d.ExtendedProperties.Add(e.Current.Key, e.Current.Value); } } } }
private void StartSegments(int segmentCount, Stream inputStream) { // notifies OnInfoReceived(); // allocs the file on disk AllocLocalFile(); long segmentSize; CalculatedSegment[] calculatedSegments; if (!remoteFileInfo.AcceptRanges) { calculatedSegments = new CalculatedSegment[] { new CalculatedSegment(0, remoteFileInfo.FileSize) }; } else { calculatedSegments = this.SegmentCalculator.GetSegments(segmentCount, remoteFileInfo); } lock (threads) threads.Clear(); lock (segments) segments.Clear(); for (int i = 0; i < calculatedSegments.Length; i++) { Segment segment = new Segment(); if (i == 0) { segment.InputStream = inputStream; } segment.Index = i; segment.InitialStartPosition = calculatedSegments[i].StartPosition; segment.StartPosition = calculatedSegments[i].StartPosition; segment.EndPosition = calculatedSegments[i].EndPosition; segments.Add(segment); } RunSegments(); }
private void StartSegment(Segment newSegment) { Thread segmentThread = new Thread(new ParameterizedThreadStart(SegmentThreadProc)); segmentThread.Start(newSegment); lock (threads) { threads.Add(segmentThread); } }
private void AddNewSegmentIfNeeded() { lock (segments) { for (int i = 0; i < this.segments.Count; i++) { Segment oldSegment = this.segments[i]; if (oldSegment.State == SegmentState.Downloading && oldSegment.Left.TotalSeconds > Settings.Default.MinSegmentLeftToStartNewSegment && oldSegment.MissingTransfer / 2 >= Settings.Default.MinSegmentSize) { // get the half of missing size of oldSegment long newSize = oldSegment.MissingTransfer / 2; // create a new segment allocation the half old segment Segment newSegment = new Segment(); newSegment.Index = this.segments.Count; newSegment.StartPosition = oldSegment.StartPosition + newSize; newSegment.InitialStartPosition = newSegment.StartPosition; newSegment.EndPosition = oldSegment.EndPosition; newSegment.OutputStream = oldSegment.OutputStream; // removes bytes from old segments oldSegment.EndPosition = oldSegment.EndPosition - newSize; // add the new segment to the list segments.Add(newSegment); StartSegment(newSegment); break; } } } }
protected virtual void OnSegmentStoped(Segment segment) { if (SegmentStoped != null) { SegmentStoped(this, new SegmentEventArgs(this, segment)); } }
protected virtual void OnSegmentStarting(Segment segment) { if (SegmentStarting != null) { SegmentStarting(this, new SegmentEventArgs(this, segment)); } }
protected virtual void OnRestartingSegment(Segment segment) { if (RestartingSegment != null) { RestartingSegment(this, new SegmentEventArgs(this, segment)); } }
//private static void LoadPersistedObjects(DownloadItem[] downloads) private static void LoadPersistedObjects(DownloadParameters downloadParameters) { string directory = downloadParameters.defaultDownloadDirectory; DownloadManager.Instance.DefaultDownloadDirectorySource = directory; if (!Path.IsPathRooted(directory)) directory = Path.Combine(GetDatabaseDirectory(), directory); DownloadManager.Instance.DefaultDownloadDirectory = directory; DownloadManager.Instance.LastDownloadId = downloadParameters.lastDownloadId; DownloadItem[] downloads = downloadParameters.downloadItems; for (int i = 0; i < downloads.Length; i++) { List<Segment> segments = new List<Segment>(); for (int j = 0; j < downloads[i].Segments.Length; j++) { Segment seg = new Segment(); seg.Index = downloads[i].Segments[j].Index; seg.InitialStartPosition = downloads[i].Segments[j].InitialStartPositon; seg.StartPosition = downloads[i].Segments[j].StartPositon; seg.EndPosition = downloads[i].Segments[j].EndPosition; segments.Add(seg); } //Downloader d = DownloadManager.Instance.Add( // downloads[i].rl, // downloads[i].mirrors, // downloads[i].LocalFile, // segments, // downloads[i].remoteInfo, // downloads[i].requestedSegments, // false, // downloads[i].createdDateTime); Downloader d = new Downloader(downloads[i].id, downloads[i].rl, downloads[i].mirrors, downloads[i].LocalFile, segments, downloads[i].remoteInfo, downloads[i].requestedSegments, downloads[i].createdDateTime); DownloadManager.Instance.Add(d, false); if (downloads[i].extendedProperties != null) { SerializableDictionary<string, object>.Enumerator e = downloads[i].extendedProperties.GetEnumerator(); while (e.MoveNext()) { d.ExtendedProperties.Add(e.Current.Key, e.Current.Value); } } } }
public SegmentEventArgs(Downloader d, Segment segment) : base(d) { this.segment = segment; }