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); } }
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; }
internal SynchronousReader(Stream stream, BufferedStreamReaderConfig config) { this.stream = stream; stopPrefetchAfterXBytes = config.StopPrefetchAfterXBytes; shadowBufferSize = config.ShadowBufferSize; totalBytesProcessed = 0; shadowBuffer = new ShadowBufferData(shadowBufferSize); }
internal BufferedStreamWriter(Stream stream, BufferedStreamWriterConfig config, IWriter writer) { this.stream = stream; this.writer = writer; shadowBufferSize = config.ShadowBufferSize; currShadowBufferIx = 0; shadowBuffer = writer.RequestBuffer(); }
private void ReleaseBuffer(ShadowBufferData buffer) { if (buffer == null) { return; } availableBuffers.Enqueue(buffer); }
protected override void Dispose(bool disposing) { if (disposing) { Flush(); writer?.Abort(); } shadowBuffer = null; base.Dispose(disposing); }
private void FlushShadowBuffer() { if (currShadowBufferIx > 0) { shadowBuffer.byteCount = currShadowBufferIx; writer.ReturnBufferAndWrite(shadowBuffer); shadowBuffer = writer.RequestBuffer(); currShadowBufferIx = 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; } }
private void FillShadowBuffer() { totBytesRead += currShadowBufferIx; currShadowBufferIx = 0; if (shadowBuffer != null) { reader.ReturnBuffer(shadowBuffer); } shadowBuffer = reader.RequestNewBuffer(); currentShadowBufferSize = shadowBuffer?.byteCount ?? 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; }
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(); }
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); }
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); }
internal static PendingOp CreateBufferWriteRequest(ShadowBufferData buffer) { return(new PendingOp(buffer, false)); }
public async Task ReturnBufferAsync(ShadowBufferData newBuffer, CancellationToken token) { await idleBuffers.EnqueueAsync(newBuffer, token).ConfigureAwait(false); }
public void Abort() { shadowBuffer?.Dispose(); shadowBuffer = null; }
public void ReturnBuffer(ShadowBufferData newBuffer) { idleBuffers.Enqueue(newBuffer); }
public void ReturnBuffer(ShadowBufferData newBuffer) { }
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); }
private PendingOp(ShadowBufferData buffer, bool FlushRequested) { this.buffer = buffer; this.FlushRequested = FlushRequested; }
private void AddBufferWriteRequestOp(ShadowBufferData buffer) { pendingOps.Add(PendingOp.CreateBufferWriteRequest(buffer)); }
public void ReturnBufferAndWrite(ShadowBufferData sourceBuffer) { writeGatekeeper.Wait(tokenSource.Token); AddBufferWriteRequestOp(sourceBuffer); }
public async Task ReturnBufferAndWriteAsync(ShadowBufferData sourceBuffer, CancellationToken token) { await writeGatekeeper.WaitAsync(token).ConfigureAwait(false); AddBufferWriteRequestOp(sourceBuffer); }