public RemoteFileInfo GetFileInfo(ResourceLocation rl, out System.IO.Stream stream) { RemoteFileInfo result = proxy.GetFileInfo(rl, out stream); if (stream != null) { stream = new LimitedRateStreamProxy(stream, speedLimit); } return(result); }
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) { // get the remote file info on mirror RemoteFileInfo tempRemoteInfo = provider.GetFileInfo(location, out Stream 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 segment.InputStream.ReadTimeout = streamTimeout; 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.Message); // raise the event OnSegmentFailed(segment); } finally { // clean up the segment segment.InputStream = null; } }
private void StartDownloadThreadProc(object objSegmentCount) { SetState(DownloaderState.Preparing); int segmentCount = Math.Min((int)objSegmentCount, Settings.Default.MaxSegments); Stream inputStream = null; int currentTry = 0; do { lastError = null; if (state == DownloaderState.Pausing) { SetState(DownloaderState.NeedToPrepare); return; } SetState(DownloaderState.Preparing); currentTry++; try { remoteFileInfo = defaultDownloadProvider.GetFileInfo(this.ResourceLocation, out inputStream); break; } catch (ThreadAbortException) { SetState(DownloaderState.NeedToPrepare); return; } catch (Exception ex) { lastError = ex; if (currentTry < Settings.Default.MaxRetries) { SetState(DownloaderState.WaitingForReconnect); Thread.Sleep(TimeSpan.FromSeconds(Settings.Default.RetryDelay)); } else { SetState(DownloaderState.NeedToPrepare); return; } } }while (true); try { lastError = null; StartSegments(segmentCount, inputStream); } catch (ThreadAbortException) { throw; } catch (Exception ex) { lastError = ex; SetState(DownloaderState.EndedWithError); } }
// Token: 0x06000124 RID: 292 RVA: 0x000068CC File Offset: 0x00004ACC private void SegmentThreadProc(object objSegment) { Segment segment = (Segment)objSegment; segment.LastError = null; try { bool flag = segment.EndPosition > 0L && segment.StartPosition >= segment.EndPosition; if (flag) { segment.State = SegmentState.Finished; this.OnSegmentStoped(segment); } else { byte[] array = new byte[4096]; segment.State = SegmentState.Connecting; this.OnSegmentStarting(segment); bool flag2 = segment.InputStream == null; if (flag2) { ResourceLocation nextResourceLocation = this.MirrorSelector.GetNextResourceLocation(); IProtocolProvider protocolProvider = nextResourceLocation.BindProtocolProviderInstance(this); while (nextResourceLocation != this.ResourceLocation) { Stream stream; RemoteFileInfo fileInfo = protocolProvider.GetFileInfo(nextResourceLocation, out stream); bool flag3 = stream != null; if (flag3) { stream.Dispose(); } bool flag4 = fileInfo.FileSize == this.remoteFileInfo.FileSize && fileInfo.AcceptRanges == this.remoteFileInfo.AcceptRanges; if (flag4) { break; } List <ResourceLocation> list = this.mirrors; List <ResourceLocation> obj = list; lock (obj) { this.mirrors.Remove(nextResourceLocation); } nextResourceLocation = this.MirrorSelector.GetNextResourceLocation(); protocolProvider = nextResourceLocation.BindProtocolProviderInstance(this); } segment.InputStream = protocolProvider.CreateStream(nextResourceLocation, segment.StartPosition, segment.EndPosition); segment.CurrentURL = nextResourceLocation.URL; } else { segment.CurrentURL = this.resourceLocation.URL; } using (segment.InputStream) { this.OnSegmentStarted(segment); segment.State = SegmentState.Downloading; segment.CurrentTry = 0; for (;;) { long num = (long)segment.InputStream.Read(array, 0, array.Length); bool flag6 = segment.EndPosition > 0L && segment.StartPosition + num > segment.EndPosition; if (flag6) { num = segment.EndPosition - segment.StartPosition; bool flag7 = num <= 0L; if (flag7) { break; } } Stream outputStream = segment.OutputStream; Stream obj2 = outputStream; lock (obj2) { segment.OutputStream.Position = segment.StartPosition; segment.OutputStream.Write(array, 0, (int)num); } segment.IncreaseStartPosition(num); bool flag9 = segment.EndPosition > 0L && segment.StartPosition >= segment.EndPosition; if (flag9) { goto Block_21; } bool flag10 = this.state == DownloaderState.Pausing; if (flag10) { goto Block_22; } bool flag11 = num <= 0L; if (flag11) { goto Block_23; } } segment.StartPosition = segment.EndPosition; goto IL_2E4; Block_21: segment.StartPosition = segment.EndPosition; goto IL_2E4; Block_22: segment.State = SegmentState.Paused; Block_23: IL_2E4: bool flag12 = segment.State == SegmentState.Downloading; if (flag12) { segment.State = SegmentState.Finished; this.AddNewSegmentIfNeeded(); } } this.OnSegmentStoped(segment); } } catch (Exception ex) { segment.State = SegmentState.Error; segment.LastError = ex; this.OnSegmentFailed(segment); } finally { segment.InputStream = null; } }