private async Task TransferLoop() { System.Diagnostics.Debug.WriteLine("PERF: EncoderConnection enters transfer loop. Avoid this if possible."); var buffer = new byte[0x10000]; var offset = 0; var ending = 0; for (;;) { if (!mComplete && ending < buffer.Length) { var fetched = await mEncoderOutputToConnectionInput.ReadAsync(buffer, ending, buffer.Length - ending, StreamMode.Partial).ConfigureAwait(false); System.Diagnostics.Debug.Assert(0 <= fetched && fetched <= buffer.Length - ending); if (fetched == 0) { mComplete = true; } else { ending += fetched; } } if (offset < ending) { var written = await mConnectionOutputToEncoderInput.WriteAsync(buffer, offset, ending - offset, StreamMode.Partial).ConfigureAwait(false); System.Diagnostics.Debug.Assert(0 < written && written <= ending - offset); offset += written; } if (offset == ending) { offset = 0; ending = 0; if (mComplete) { await mConnectionOutputToEncoderInput.CompleteAsync().ConfigureAwait(false); return; } } } }
private async Task TransferLoop() { System.Diagnostics.Debug.WriteLine("PERF: EncoderInput enters transfer loop. Avoid this if possible."); var buffer = new byte[0x10000]; for (;;) { var fetched = await mSession.ReadInternalAsync(buffer, 0, buffer.Length, StreamMode.Partial).ConfigureAwait(false); System.Diagnostics.Debug.Assert(0 <= fetched && fetched <= buffer.Length); if (fetched == 0) { await mEncoderInput.CompleteAsync().ConfigureAwait(false); return; } var written = await mEncoderInput.WriteAsync(buffer, 0, fetched, StreamMode.Complete).ConfigureAwait(false); System.Diagnostics.Debug.Assert(written == fetched); mLength += written; mChecksum = CRC.Update(mChecksum, buffer, 0, written); } }
public Task EncodeAsync(IStreamReader input, IStreamWriter output, CancellationToken ct = default(CancellationToken)) { lock (mSyncObject) { if (mDisposeTask != null) { throw new OperationCanceledException(); } // TODO: make this wait async as well while (mRunning) { Monitor.Wait(mSyncObject); if (mDisposeTask != null) { throw new OperationCanceledException(); } } mRunning = true; } var task = Task.Run(async delegate { var result = mEncoder.Lzma2Enc_Encode(new AsyncOutputProvider(output), new AsyncInputProvider(input), null); if (result != LZMA.Master.LZMA.SZ_OK) { throw new InvalidOperationException(); } await output.CompleteAsync().ConfigureAwait(false); }); mEncoderTask = task.ContinueWith(delegate { lock (mSyncObject) { System.Diagnostics.Debug.Assert(mRunning); mRunning = false; Monitor.PulseAll(mSyncObject); } }, CancellationToken.None, TaskContinuationOptions.DenyChildAttach, TaskScheduler.Default); return(task); }
public async Task CompleteAsync() { if (mOffset != 0) { while (mOffset < 16) { mBuffer1[mOffset++] = 0; } System.Diagnostics.Debug.Assert(mOffset == 16); mBuffer2 = mEncoder.TransformFinalBlock(mBuffer1, 0, 16); var written = await mOutput.WriteAsync(mBuffer2, 0, 16, StreamMode.Complete).ConfigureAwait(false); System.Diagnostics.Debug.Assert(written == 16); mOffset = 0; } System.Diagnostics.Debug.Assert(mOffset == 0); await mOutput.CompleteAsync().ConfigureAwait(false); }
public Task EncodeAsync(IStreamReader input, IStreamWriter output, CancellationToken ct = default(CancellationToken)) { lock (mSyncObject) { if (mDisposeTask != null) throw new OperationCanceledException(); // TODO: make this wait async as well while (mRunning) { Monitor.Wait(mSyncObject); if (mDisposeTask != null) throw new OperationCanceledException(); } mRunning = true; } var task = Task.Run(async delegate { var result = mEncoder.Lzma2Enc_Encode(new AsyncOutputProvider(output), new AsyncInputProvider(input), null); if (result != LZMA.Master.LZMA.SZ_OK) throw new InvalidOperationException(); await output.CompleteAsync().ConfigureAwait(false); }); mEncoderTask = task.ContinueWith(delegate { lock (mSyncObject) { System.Diagnostics.Debug.Assert(mRunning); mRunning = false; Monitor.PulseAll(mSyncObject); } }, CancellationToken.None, TaskContinuationOptions.DenyChildAttach, TaskScheduler.Default); return task; }