コード例 #1
0
ファイル: PipeExtensions.cs プロジェクト: woaisoft/dotNext
        private static async ValueTask <int> ReadLengthAsync(this PipeReader reader, StringLengthEncoding lengthFormat, CancellationToken token)
        {
            ValueTask <int> result;
            var             littleEndian = BitConverter.IsLittleEndian;

            switch (lengthFormat)
            {
            default:
                throw new ArgumentOutOfRangeException(nameof(lengthFormat));

            case StringLengthEncoding.Plain:
                result = reader.ReadAsync <int>(token);
                break;

            case StringLengthEncoding.PlainLittleEndian:
                littleEndian = true;
                goto case StringLengthEncoding.Plain;

            case StringLengthEncoding.PlainBigEndian:
                littleEndian = false;
                goto case StringLengthEncoding.Plain;

            case StringLengthEncoding.Compressed:
                result = reader.Read7BitEncodedIntAsync(token);
                break;
            }

            var length = await result.ConfigureAwait(false);

            length.ReverseIfNeeded(littleEndian);
            return(length);
        }