public async Task WriteLargeDataTextUtf8(int length)
        {
            string data = new string('#', length);

            FillRandomStringData(data, length);
            using (var memoryPool = new MemoryPool())
            {
                var readerWriter = new PipelineReaderWriter(memoryPool);

                var output = readerWriter.Alloc();
                output.Append(data, TextEncoding.Utf8);
                var foo = output.Memory.IsEmpty; // trying to see if .Memory breaks
                await output.FlushAsync();

                readerWriter.CompleteWriter();

                int offset = 0;
                while (true)
                {
                    var result = await readerWriter.ReadAsync();

                    var input = result.Buffer;
                    if (input.Length == 0)
                    {
                        break;
                    }

                    string s = ReadableBufferExtensions.GetUtf8String(input);
                    Assert.Equal(data.Substring(offset, input.Length), s);
                    offset += input.Length;
                    readerWriter.Advance(input.End);
                }
                Assert.Equal(data.Length, offset);
            }
        }
        public async Task WriteLargeDataBinary(int length)
        {
            byte[] data = new byte[length];
            new Random(length).NextBytes(data);
            using (var memoryPool = new MemoryPool())
            {
                var readerWriter = new PipelineReaderWriter(memoryPool);

                var output = readerWriter.Alloc();
                output.Write(data);
                var foo = output.Memory.IsEmpty; // trying to see if .Memory breaks
                await output.FlushAsync();

                readerWriter.CompleteWriter();

                int offset = 0;
                while (true)
                {
                    var result = await readerWriter.ReadAsync();

                    var input = result.Buffer;
                    if (input.Length == 0)
                    {
                        break;
                    }

                    Assert.True(input.Equals(new Span <byte>(data, offset, input.Length)));
                    offset += input.Length;
                    readerWriter.Advance(input.End);
                }
                Assert.Equal(data.Length, offset);
            }
        }
        public async Task WriteLargeDataBinary(int length)
        {
            byte[] data = new byte[length];
            new Random(length).NextBytes(data);
            using (var memoryPool = new MemoryPool())
            {
                var readerWriter = new PipelineReaderWriter(memoryPool);

                var output = readerWriter.Alloc();
                output.Write(data);
                var foo = output.Memory.IsEmpty; // trying to see if .Memory breaks
                await output.FlushAsync();
                readerWriter.CompleteWriter();

                int offset = 0;
                while (true)
                {
                    var result = await readerWriter.ReadAsync();
                    var input = result.Buffer;
                    if (input.Length == 0) break;

                    Assert.True(input.Equals(new Span<byte>(data, offset, input.Length)));
                    offset += input.Length;
                    readerWriter.Advance(input.End);
                }
                Assert.Equal(data.Length, offset);
            }
        }
示例#4
0
        private async Task ProcessWrites()
        {
            try
            {
                while (true)
                {
                    var result = await _output.ReadAsync();

                    var buffer = result.Buffer;

                    try
                    {
                        // Make sure we're on the libuv thread
                        await _thread;

                        if (buffer.IsEmpty && result.IsCompleted)
                        {
                            break;
                        }

                        if (!buffer.IsEmpty)
                        {
                            BeginWrite(buffer);
                        }
                    }
                    finally
                    {
                        _output.Advance(buffer.End);
                    }
                }
            }
            catch (Exception ex)
            {
                _output.CompleteReader(ex);
            }
            finally
            {
                _output.CompleteReader();

                // Drain the pending writes
                if (_pendingWrites > 0)
                {
                    _drainWrites = new TaskCompletionSource <object>();

                    await _drainWrites.Task;
                }

                _handle.Dispose();

                // We'll never call the callback after disposing the handle
                _input.CompleteWriter();
            }
        }
        public async Task CanWriteUInt64ToBuffer(ulong value, string valueAsString)
        {
            using (var memoryPool = new MemoryPool())
            {
                var readerWriter = new PipelineReaderWriter(memoryPool);
                var buffer = readerWriter.Alloc();
                buffer.Append(value, EncodingData.InvariantUtf8);
                await buffer.FlushAsync();

                var result = await readerWriter.ReadAsync();
                var inputBuffer = result.Buffer;

                Assert.Equal(valueAsString, inputBuffer.GetUtf8String());
            }
        }
        public async Task CanWriteUInt64ToBuffer(ulong value, string valueAsString)
        {
            using (var memoryPool = new MemoryPool())
            {
                var readerWriter = new PipelineReaderWriter(memoryPool);
                var buffer       = readerWriter.Alloc();
                buffer.Append(value, EncodingData.InvariantUtf8);
                await buffer.FlushAsync();

                var result = await readerWriter.ReadAsync();

                var inputBuffer = result.Buffer;

                Assert.Equal(valueAsString, inputBuffer.GetUtf8String());
            }
        }
        private async Task ProcessSends()
        {
            while (true)
            {
                var result = await _output.ReadAsync();

                var buffer = result.Buffer;

                if (buffer.IsEmpty && result.IsCompleted)
                {
                    break;
                }

                var enumerator = buffer.GetEnumerator();

                if (enumerator.MoveNext())
                {
                    var current = enumerator.Current;

                    while (enumerator.MoveNext())
                    {
                        var next = enumerator.Current;

                        await SendAsync(current, endOfMessage : false);

                        current = next;
                    }

                    await PreviousSendingComplete;

                    _sendingBuffer = buffer.Preserve();

                    await SendAsync(current, endOfMessage : true);
                }

                _output.Advance(buffer.End);
            }

            _output.CompleteReader();
        }
示例#8
0
 public override int Read(byte[] buffer, int offset, int count)
 {
     return(_readerWriter.ReadAsync(new Span <byte>(buffer, offset, count)).GetAwaiter().GetResult());
 }
        public async Task WriteLargeDataTextUtf8(int length)
        {
            string data = new string('#', length);
            FillRandomStringData(data, length);
            using (var memoryPool = new MemoryPool())
            {
                var readerWriter = new PipelineReaderWriter(memoryPool);

                var output = readerWriter.Alloc();
                output.Append(data, TextEncoding.Utf8);
                var foo = output.Memory.IsEmpty; // trying to see if .Memory breaks
                await output.FlushAsync();
                readerWriter.CompleteWriter();

                int offset = 0;
                while (true)
                {
                    var result = await readerWriter.ReadAsync();
                    var input = result.Buffer;
                    if (input.Length == 0) break;

                    string s = ReadableBufferExtensions.GetUtf8String(input);
                    Assert.Equal(data.Substring(offset, input.Length), s);
                    offset += input.Length;
                    readerWriter.Advance(input.End);
                }
                Assert.Equal(data.Length, offset);
            }
        }