示例#1
0
        private void WorkerTaskEntrypoint()
        {
            var token = tokenSource.Token;

            while (!tokenSource.IsCancellationRequested)
            {
                PendingOp pendingOp;
                try
                {
                    pendingOp = pendingOps.Take(token);
                    if (pendingOp.FlushRequested)
                    {
                        stream.Flush();
                        flushBlockerEvent.Set();
                        continue;
                    }
                }
                catch (OperationCanceledException)
                {
                    if (tokenSource.IsCancellationRequested)
                    {
                        break;
                    }
                    throw;
                }
                writeGatekeeper.Release();
                ShadowBufferData buffer = pendingOp.buffer;
                stream.Write(buffer.buffer, 0, buffer.byteCount);
                ReleaseBuffer(buffer);
            }
        }
示例#2
0
 protected override void Dispose(bool disposing)
 {
     if (!disposed)
     {
         try
         {
             if (disposing)
             {
                 bool repositionStream = stream?.CanSeek ?? false;
                 if (reader != null)
                 {
                     if (!reader.AbortAsync().Wait(config.ReaderAbortTimeoutMs))
                     {
                         repositionStream = false;
                     }
                 }
                 if (repositionStream)
                 {
                     stream.Position = innerStreamStartPos + totBytesRead + currShadowBufferIx;
                 }
             }
         }
         finally
         {
             base.Dispose(disposing);
             disposed = true;
         }
     }
     shadowBuffer = null;
     reader       = null;
 }
示例#3
0
 internal SynchronousReader(Stream stream, BufferedStreamReaderConfig config)
 {
     this.stream             = stream;
     stopPrefetchAfterXBytes = config.StopPrefetchAfterXBytes;
     shadowBufferSize        = config.ShadowBufferSize;
     totalBytesProcessed     = 0;
     shadowBuffer            = new ShadowBufferData(shadowBufferSize);
 }
示例#4
0
 internal BufferedStreamWriter(Stream stream, BufferedStreamWriterConfig config, IWriter writer)
 {
     this.stream        = stream;
     this.writer        = writer;
     shadowBufferSize   = config.ShadowBufferSize;
     currShadowBufferIx = 0;
     shadowBuffer       = writer.RequestBuffer();
 }
示例#5
0
 private void ReleaseBuffer(ShadowBufferData buffer)
 {
     if (buffer == null)
     {
         return;
     }
     availableBuffers.Enqueue(buffer);
 }
示例#6
0
 protected override void Dispose(bool disposing)
 {
     if (disposing)
     {
         Flush();
         writer?.Abort();
     }
     shadowBuffer = null;
     base.Dispose(disposing);
 }
示例#7
0
 private void FlushShadowBuffer()
 {
     if (currShadowBufferIx > 0)
     {
         shadowBuffer.byteCount = currShadowBufferIx;
         writer.ReturnBufferAndWrite(shadowBuffer);
         shadowBuffer       = writer.RequestBuffer();
         currShadowBufferIx = 0;
     }
 }
示例#8
0
        private async Task FlushShadowBufferAsync(CancellationToken token)
        {
            if (currShadowBufferIx > 0)
            {
                shadowBuffer.byteCount = currShadowBufferIx;
                await writer.ReturnBufferAndWriteAsync(shadowBuffer, token).ConfigureAwait(false);

                shadowBuffer       = writer.RequestBuffer();
                currShadowBufferIx = 0;
            }
        }
示例#9
0
 private void FillShadowBuffer()
 {
     totBytesRead      += currShadowBufferIx;
     currShadowBufferIx = 0;
     if (shadowBuffer != null)
     {
         reader.ReturnBuffer(shadowBuffer);
     }
     shadowBuffer            = reader.RequestNewBuffer();
     currentShadowBufferSize = shadowBuffer?.byteCount ?? 0;
 }
示例#10
0
        private async Task FillShadowBufferAsync(CancellationToken token)
        {
            totBytesRead      += currShadowBufferIx;
            currShadowBufferIx = 0;
            if (shadowBuffer != null)
            {
                await reader.ReturnBufferAsync(shadowBuffer, token).ConfigureAwait(false);
            }
            shadowBuffer = await reader.RequestNewBufferAsync(token).ConfigureAwait(false);

            currentShadowBufferSize = shadowBuffer?.byteCount ?? 0;
        }
示例#11
0
 public BufferedStreamReader(Stream stream, BufferedStreamReaderConfig config)
 {
     this.stream             = stream;
     this.config             = config;
     innerStreamStartPos     = stream.CanSeek ? stream.Position : 0;
     currentShadowBufferSize = config.ShadowBufferSize;
     totBytesRead            = 0;
     if (config.UsePreFetch)
     {
         reader = new PreFetchReader(stream, config);
     }
     else
     {
         reader = new SynchronousReader(stream, config);
     }
     shadowBuffer = null;
     FillShadowBuffer();
 }
示例#12
0
        public async Task <ShadowBufferData> RequestNewBufferAsync(CancellationToken token)
        {
            if (finished)
            {
                return(null);
            }
            if (!await finishedBuffers.OutputAvailableAsync(token).ConfigureAwait(false))
            {
                finished = true;
                return(null);
            }
            ShadowBufferData newBuffer = await finishedBuffers.DequeueAsync(token).ConfigureAwait(false);

            if (newBuffer.byteCount == 0)
            {
                finished = true;
                return(null);
            }
            return(newBuffer);
        }
示例#13
0
        public ShadowBufferData RequestNewBuffer()
        {
            if (finished)
            {
                return(null);
            }
            if (!finishedBuffers.OutputAvailable())
            {
                finished = true;
                return(null);
            }
            ShadowBufferData newBuffer = finishedBuffers.Dequeue();

            if (newBuffer.byteCount == 0)
            {
                finished = true;
                return(null);
            }
            return(newBuffer);
        }
示例#14
0
 internal static PendingOp CreateBufferWriteRequest(ShadowBufferData buffer)
 {
     return(new PendingOp(buffer, false));
 }
示例#15
0
 public async Task ReturnBufferAsync(ShadowBufferData newBuffer, CancellationToken token)
 {
     await idleBuffers.EnqueueAsync(newBuffer, token).ConfigureAwait(false);
 }
示例#16
0
 public void Abort()
 {
     shadowBuffer?.Dispose();
     shadowBuffer = null;
 }
示例#17
0
 public void ReturnBuffer(ShadowBufferData newBuffer)
 {
     idleBuffers.Enqueue(newBuffer);
 }
示例#18
0
 public void ReturnBuffer(ShadowBufferData newBuffer)
 {
 }
示例#19
0
 public Task ReturnBufferAsync(ShadowBufferData newBuffer, CancellationToken token)
 {
     return(Task.CompletedTask);
 }
 public void ReturnBufferAndWrite(ShadowBufferData sourceBuffer)
 {
     stream.Write(sourceBuffer.buffer, 0, sourceBuffer.byteCount);
 }
 public SynchronousWriter(Stream stream, BufferedStreamWriterConfig config)
 {
     this.stream  = stream;
     shadowBuffer = new ShadowBufferData(config.ShadowBufferSize);
 }
 public async Task ReturnBufferAndWriteAsync(ShadowBufferData sourceBuffer, CancellationToken token)
 {
     await stream.WriteAsync(sourceBuffer.buffer, 0, sourceBuffer.byteCount, token).ConfigureAwait(false);
 }
示例#23
0
 private PendingOp(ShadowBufferData buffer, bool FlushRequested)
 {
     this.buffer         = buffer;
     this.FlushRequested = FlushRequested;
 }
示例#24
0
 private void AddBufferWriteRequestOp(ShadowBufferData buffer)
 {
     pendingOps.Add(PendingOp.CreateBufferWriteRequest(buffer));
 }
示例#25
0
 public void ReturnBufferAndWrite(ShadowBufferData sourceBuffer)
 {
     writeGatekeeper.Wait(tokenSource.Token);
     AddBufferWriteRequestOp(sourceBuffer);
 }
示例#26
0
        public async Task ReturnBufferAndWriteAsync(ShadowBufferData sourceBuffer, CancellationToken token)
        {
            await writeGatekeeper.WaitAsync(token).ConfigureAwait(false);

            AddBufferWriteRequestOp(sourceBuffer);
        }