Пример #1
0
        public override async ValueTask <string> ReadStringAsync(CancellationToken cancellationToken)
        {
            // read length
            var length = (int) await ReadVarInt32Async(cancellationToken);

            if (length == 0)
            {
                return(string.Empty);
            }

            // read and decode data
            if (length < PreAllocatedBuffer.Length)
            {
                await Trans.ReadAllAsync(PreAllocatedBuffer, 0, length, cancellationToken);

                return(Encoding.UTF8.GetString(PreAllocatedBuffer, 0, length));
            }

            Transport.CheckReadBytesAvailable(length);

            var buf = ArrayPool <byte> .Shared.Rent(length);

            try
            {
                await Trans.ReadAllAsync(buf, 0, length, cancellationToken);

                return(Encoding.UTF8.GetString(buf, 0, length));
            }
            finally
            {
                ArrayPool <byte> .Shared.Return(buf);
            }
        }
Пример #2
0
        public override async ValueTask <byte[]> ReadBinaryAsync(CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();

            var size = await ReadI32Async(cancellationToken);

            Transport.CheckReadBytesAvailable(size);
            var buf = new byte[size];
            await Trans.ReadAllAsync(buf, 0, size, cancellationToken);

            return(buf);
        }
Пример #3
0
        public override async ValueTask <byte[]> ReadBinaryAsync(CancellationToken cancellationToken)
        {
            if (cancellationToken.IsCancellationRequested)
            {
                return(await Task.FromCanceled <byte[]>(cancellationToken));
            }

            var size = await ReadI32Async(cancellationToken);

            Transport.CheckReadBytesAvailable(size);
            var buf = new byte[size];
            await Trans.ReadAllAsync(buf, 0, size, cancellationToken);

            return(buf);
        }
Пример #4
0
        private async ValueTask <string> ReadStringBodyAsync(int size, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();

            if (size <= PreAllocatedBuffer.Length)
            {
                await Trans.ReadAllAsync(PreAllocatedBuffer, 0, size, cancellationToken);

                return(Encoding.UTF8.GetString(PreAllocatedBuffer, 0, size));
            }

            Transport.CheckReadBytesAvailable(size);
            var buf = new byte[size];
            await Trans.ReadAllAsync(buf, 0, size, cancellationToken);

            return(Encoding.UTF8.GetString(buf, 0, buf.Length));
        }
Пример #5
0
        public override async ValueTask <byte[]> ReadBinaryAsync(CancellationToken cancellationToken)
        {
            // read length
            var length = (int) await ReadVarInt32Async(cancellationToken);

            if (length == 0)
            {
                return(Array.Empty <byte>());
            }

            // read data
            Transport.CheckReadBytesAvailable(length);
            var buf = new byte[length];
            await Trans.ReadAllAsync(buf, 0, length, cancellationToken);

            return(buf);
        }
Пример #6
0
        protected void CheckReadBytesAvailable(TMap map)
        {
            var elmSize = GetMinSerializedSize(map.KeyType) + GetMinSerializedSize(map.ValueType);

            Transport.CheckReadBytesAvailable(map.Count * elmSize);
        }
Пример #7
0
 protected void CheckReadBytesAvailable(TList list)
 {
     Transport.CheckReadBytesAvailable(list.Count * GetMinSerializedSize(list.ElementType));
 }