private Task <int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken, object state)
        {
            ValidateState(cancellationToken);

            var tcs  = new TaskCompletionSource <int>(state);
            var task = _body.ReadAsync(new ArraySegment <byte>(buffer, offset, count), cancellationToken);

            task.AsTask().ContinueWith((task2, state2) =>
            {
                var tcs2 = (TaskCompletionSource <int>)state2;
                if (task2.IsCanceled)
                {
                    tcs2.SetCanceled();
                }
                else if (task2.IsFaulted)
                {
                    tcs2.SetException(task2.Exception);
                }
                else
                {
                    tcs2.SetResult(task2.Result);
                }
            }, tcs, cancellationToken);
            return(tcs.Task);
        }
예제 #2
0
        public override Task <int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
        {
            var task = ValidateState(cancellationToken);

            if (task == null)
            {
                return(_body.ReadAsync(new ArraySegment <byte>(buffer, offset, count), cancellationToken));
            }
            return(task);
        }