private async Task TransferLoop() { System.Diagnostics.Debug.WriteLine("PERF: EncoderStorage enters transfer loop. Avoid this if possible."); var buffer = new byte[0x10000]; for (;;) { var fetched = await mEncoderOutput.ReadAsync(buffer, 0, buffer.Length, StreamMode.Partial).ConfigureAwait(false); System.Diagnostics.Debug.Assert(0 <= fetched && fetched <= buffer.Length); if (fetched == 0) { mCompletionTask.SetResult(null); return; } // TODO: calculate checksum asynchronously? if (mCalculateChecksum) { mChecksum = CRC.Update(mChecksum, buffer, 0, fetched); } await mStream.WriteAsync(buffer, 0, fetched).ConfigureAwait(false); } }
public void WhenLauncherHasQueuedDelegate_ItShouldCompleted() { var sut = new SingleRunningDelegateLauncher(); var tcs = new AsyncTaskCompletionSource(); sut.Launch(ct => { tcs.SetResult(); }, t => { }); ConcurrentAssert.EnsureThatTaskIsCompleted(tcs.Task); }
public void WhenLauncherHasQueuedDelegate_TheOnFinishedActionShouldBeCalledIf() { var sut = new SingleRunningDelegateLauncher(); var tcs = new AsyncTaskCompletionSource(); Task task = null; sut.Launch(ct => { }, t => { tcs.SetResult(); task = t; }); ConcurrentAssert.EnsureThatTaskIsCompleted(tcs.Task); Assert.IsNotNull(task); Assert.IsTrue(task.IsCompleted); Assert.IsFalse(task.IsFaulted); }
public async Task <int> WriteAsync(byte[] buffer, int offset, int count, StreamMode mode) { System.Diagnostics.Debug.Assert(mEncoderOutputToConnectionInput == null); System.Diagnostics.Debug.Assert(buffer != null); System.Diagnostics.Debug.Assert(0 <= offset && offset < buffer.Length); System.Diagnostics.Debug.Assert(0 < count && count <= buffer.Length - offset); if (mConnectionOutputToEncoderInput != null) { var written = await mConnectionOutputToEncoderInput.WriteAsync(buffer, offset, count, mode); mTotalLength += written; return(written); } int result = 0; do { lock (this) { while (mBuffer == null) { Monitor.Wait(this); } var written = Math.Min(count, mEnding - mOffset); Buffer.BlockCopy(buffer, offset, mBuffer, mOffset, written); mTotalLength += written; result += written; offset += written; count -= written; mBuffer = null; mResult.SetResult(written); } }while (mode == StreamMode.Complete && count > 0); return(result); }
public void WhenLauncherHasQueuedDelegate_TheOnFinishedActionShouldBeCalledAfterDelegateHasFinished() { var sut = new SingleRunningDelegateLauncher(); var tcs = new AsyncTaskCompletionSource(); var tcs2 = new AsyncTaskCompletionSource(); Task task = null; sut.Launch(ct => { tcs.Task.Wait(); }, t => { tcs2.SetResult(); task = t; }); ConcurrentAssert.EnsureThatTaskIsNeverCompleted(tcs2.Task); tcs.SetResult(); ConcurrentAssert.EnsureThatTaskIsCompleted(tcs2.Task); }
private void WriteOutput() { while (mDecoder.AvailableOutputLength > 0) { OutputFrame frame; lock (mSyncObject) { System.Diagnostics.Debug.Assert(mRunning); if (mOutputQueue.Count == 0) { break; } frame = mOutputQueue.Peek(); } var capacity = frame.mEnding - frame.mOffset; var copySize = Math.Min(capacity, mDecoder.AvailableOutputLength); mDecoder.ReadOutputData(frame.mBuffer, frame.mOffset, copySize); frame.mOffset += copySize; if (copySize == capacity || frame.mMode == StreamMode.Partial) { frame.mCompletion.SetResult(frame.mOffset - frame.mOrigin); lock (mSyncObject) { System.Diagnostics.Debug.Assert(mRunning); var other = mOutputQueue.Dequeue(); System.Diagnostics.Debug.Assert(other == frame); } } } if (mDecoder.IsOutputComplete && !mFlushControl.Task.IsCompleted) { mFlushControl.SetResult(null); } }
public Task <IDisposable> LockAsync(TimeSpan timeout, CancellationToken token) { var tcs = new AsyncTaskCompletionSource <IDisposable>(); _am.EnterAsync(timeout, token).ContinueWith(t => { if (t.IsCanceled) { tcs.TrySetCanceled(token); } else if (t.IsFaulted) { tcs.TrySetException(t.Exception); } else { tcs.SetResult(new Disposable(_am)); } }); return(tcs.Task); }