private Downloader( ResourceLocation rl, ResourceLocation[] mirrors, string localFile) { this.threads = new List <Thread>(); this.resourceLocation = rl; if (mirrors == null) { this.mirrors = new List <ResourceLocation>(); } else { this.mirrors = new List <ResourceLocation>(mirrors); } this.localFile = localFile; extentedProperties = new Dictionary <string, object>(); defaultDownloadProvider = rl.BindProtocolProviderInstance(this); segmentCalculator = new MinSizeSegmentCalculator(); this.MirrorSelector = new SequentialMirrorSelector(); }
private Downloader( ResourceLocation rl, ResourceLocation[] mirrors, string localFile) { this.threads = new List<Thread>(); this.resourceLocation = rl; if (mirrors == null) { this.mirrors = new List<ResourceLocation>(); } else { this.mirrors = new List<ResourceLocation>(mirrors); } this.localFile = localFile; extentedProperties = new Dictionary<string, object>(); defaultDownloadProvider = rl.BindProtocolProviderInstance(this); segmentCalculator = new MinSizeSegmentCalculator(); this.MirrorSelector = new SequentialMirrorSelector(); }
private void SegmentThreadProc(object objSegment) { Segment segment = (Segment)objSegment; segment.LastError = null; try { if (segment.EndPosition > 0 && segment.StartPosition >= segment.EndPosition) { segment.State = SegmentState.Finished; // raise the event OnSegmentStoped(segment); return; } int buffSize = 8192; byte[] buffer = new byte[buffSize]; segment.State = SegmentState.Connecting; // raise the event OnSegmentStarting(segment); if (segment.InputStream == null) { // get the next URL (It can the the main url or some mirror) ResourceLocation location = this.MirrorSelector.GetNextResourceLocation(); // get the protocol provider for that mirror IProtocolProvider provider = location.BindProtocolProviderInstance(this); while (location != this.ResourceLocation) { Stream tempStream; // get the remote file info on mirror RemoteFileInfo tempRemoteInfo = provider.GetFileInfo(location, out tempStream); if (tempStream != null) { tempStream.Dispose(); } // check if the file on mirror is the same if (tempRemoteInfo.FileSize == remoteFileInfo.FileSize && tempRemoteInfo.AcceptRanges == remoteFileInfo.AcceptRanges) { // if yes, stop looking for the mirror break; } lock (mirrors) { // the file on the mirror is not the same, so remove from the mirror list mirrors.Remove(location); } // the file on the mirror is different // so get other mirror to use in the segment location = this.MirrorSelector.GetNextResourceLocation(); provider = location.BindProtocolProviderInstance(this); } // get the input stream from start position segment.InputStream = provider.CreateStream(location, segment.StartPosition, segment.EndPosition); // change the segment URL to the mirror URL segment.CurrentURL = location.URL; } else { // change the segment URL to the main URL segment.CurrentURL = this.resourceLocation.URL; } using (segment.InputStream) { // raise the event OnSegmentStarted(segment); // change the segment state segment.State = SegmentState.Downloading; segment.CurrentTry = 0; long readSize; do { // reads the buffer from input stream readSize = segment.InputStream.Read(buffer, 0, buffSize); // check if the segment has reached the end if (segment.EndPosition > 0 && segment.StartPosition + readSize > segment.EndPosition) { // adjust the 'readSize' to write only necessary bytes readSize = (segment.EndPosition - segment.StartPosition); if (readSize <= 0) { segment.StartPosition = segment.EndPosition; break; } } // locks the stream to avoid that other threads changes // the position of stream while this thread is writing into the stream lock (segment.OutputStream) { segment.OutputStream.Position = segment.StartPosition; segment.OutputStream.Write(buffer, 0, (int)readSize); } // increse the start position of the segment and also calculates the rate segment.IncreaseStartPosition(readSize); // check if the stream has reached its end if (segment.EndPosition > 0 && segment.StartPosition >= segment.EndPosition) { segment.StartPosition = segment.EndPosition; break; } // check if the user have requested to pause the download if (state == DownloaderState.Pausing) { segment.State = SegmentState.Paused; break; } //Thread.Sleep(1500); }while (readSize > 0); if (segment.State == SegmentState.Downloading) { segment.State = SegmentState.Finished; // try to create other segment, // spliting the missing bytes from one existing segment AddNewSegmentIfNeeded(); } } // raise the event OnSegmentStoped(segment); } catch (Exception ex) { // store the error information segment.State = SegmentState.Error; segment.LastError = ex; Debug.WriteLine(ex.ToString()); // raise the event OnSegmentFailed(segment); } finally { // clean up the segment segment.InputStream = null; } }