private void WriteSegment() { while (true) { SegmentDownloadInfo segmentDownloadInfo; lock (m_SegmentWriterLocker) { while (m_OutputBufferQueue.Count == 0) { Monitor.Wait(m_SegmentWriterLocker); } segmentDownloadInfo = m_OutputBufferQueue.Dequeue(); } if (segmentDownloadInfo == null) { if (m_SegmentDownloader.IsDownloadFinished) { m_SegmentDownloader.SetDownloadFinishDate(DateTime.Now); } return; } lock (m_SegmentWriter) { if (segmentDownloadInfo.CurrentPosition != -1) { m_SegmentWriter.Write(segmentDownloadInfo.CurrentPosition, segmentDownloadInfo.Buffer, segmentDownloadInfo.Size); EnqueueDownloadSegment(new byte[m_BufferSize]); } } } }
/// <summary> /// Starts the segment download task. /// </summary> public void Download() { int readSize; byte[] buffer = new byte[m_BufferSize]; do { readSize = m_SegmentDownloader.Download(buffer); lock (m_SegmentWriter) { m_SegmentWriter.Write(m_SegmentDownloader.CurrentPosition, buffer, readSize); m_SegmentDownloader.IncreaseCurrentPosition(readSize); if (m_SegmentDownloader.IsDownloadFinished) { m_SegmentDownloader.SetDownloadFinishDate(DateTime.Now); break; } } }while (readSize > 0); }
public void Write(long startPosition, byte[] buffer, int count) { Thread.Sleep(1000); m_SegmentWriter.Write(startPosition, buffer, count); }