Ejemplo n.º 1
0
        public static ValueTask <int> ReadAsync(this IPipeReader input, Span <byte> destination)
        {
            while (true)
            {
                var awaiter = input.ReadAsync();

                if (!awaiter.IsCompleted)
                {
                    break;
                }

                var result      = awaiter.GetResult();
                var inputBuffer = result.Buffer;

                var fin    = result.IsCompleted;
                var sliced = inputBuffer.Slice(0, Math.Min(inputBuffer.Length, destination.Length));
                sliced.CopyTo(destination);
                int actual = sliced.Length;
                input.Advance(sliced.End);

                if (actual != 0)
                {
                    return(new ValueTask <int>(actual));
                }
                else if (fin)
                {
                    return(new ValueTask <int>(0));
                }
            }

            return(new ValueTask <int>(input.ReadAsyncAwaited(destination)));
        }
Ejemplo n.º 2
0
        public static ValueTask <int> ReadAsync(this IPipeReader input, ArraySegment <byte> destination)
        {
            while (true)
            {
                var awaiter = input.ReadAsync();

                if (!awaiter.IsCompleted)
                {
                    break;
                }

                var result      = awaiter.GetResult();
                var inputBuffer = result.Buffer;

                var length = (int)Math.Min(inputBuffer.Length, destination.Count);

                var sliced = inputBuffer.Slice(0, length);
                sliced.CopyTo(destination);

                input.Advance(sliced.End);

                if (length != 0)
                {
                    return(new ValueTask <int>(length));
                }

                if (result.IsCompleted)
                {
                    return(new ValueTask <int>(0));
                }
            }

            return(new ValueTask <int>(input.ReadAsyncAwaited(destination)));
        }