Exemplo n.º 1
0
        public async Task BufferingDataPastEndOfStreamCanBeReadAgain()
        {
            var protocol = new TestProtocol();
            var stream   = new ThrowAfterZeroByteReadStream();
            var writer   = PipeWriter.Create(stream);
            var reader   = new MessagePipeReader(PipeReader.Create(stream), protocol);

            protocol.WriteMessage(Encoding.ASCII.GetBytes("Hello World"), writer);
            await writer.FlushAsync().ConfigureAwait(false);

            stream.Position = 0;

            var readResult = await reader.ReadAsync();

            var buffer = readResult.Buffer;

            reader.AdvanceTo(buffer.Start, buffer.End);

            // Make sure IsCompleted is true
            readResult = await reader.ReadAsync();

            buffer = readResult.Buffer;
            reader.AdvanceTo(buffer.Start, buffer.End);
            Assert.True(readResult.IsCompleted);

            var value = await ReadFromPipeAsString(reader);

            Assert.Equal("Hello World", value);
            reader.Complete();
        }
Exemplo n.º 2
0
        public async Task ReadMessagesAsynchronouslyWorks()
        {
            var options = new PipeOptions(useSynchronizationContext: false, readerScheduler: PipeScheduler.Inline, writerScheduler: PipeScheduler.Inline);
            var pair    = DuplexPipe.CreateConnectionPair(options, options);

            await using var connection = new DefaultConnectionContext(Guid.NewGuid().ToString(), pair.Transport, pair.Application);
            var data     = Encoding.UTF8.GetBytes("Hello World");
            var protocol = new TestProtocol();

            async Task WritingTask()
            {
                var writer = connection.Application.Output;

                for (var i = 0; i < 3; i++)
                {
                    protocol.WriteMessage(data, writer);
                    await writer.FlushAsync().ConfigureAwait(false);
                }

                await writer.CompleteAsync().ConfigureAwait(false);
            }

            async Task ReadingTask()
            {
                var reader = connection.CreatePipeReader(protocol);

                while (true)
                {
                    var result = await reader.ReadAsync().ConfigureAwait(false);

                    var buffer = result.Buffer;

                    if (buffer.Length < 3 * data.Length)
                    {
                        reader.AdvanceTo(buffer.Start, buffer.End);
                        continue;
                    }

                    Assert.Equal(Enumerable.Repeat(data, 3).SelectMany(a => a).ToArray(), buffer.ToArray());

                    reader.AdvanceTo(buffer.End);
                    result = await reader.ReadAsync().ConfigureAwait(false);

                    Assert.True(result.IsCompleted);
                    break;
                }

                await reader.CompleteAsync();
            }

            var readingTask = ReadingTask();
            var writingTask = WritingTask();

            await writingTask;
            await readingTask;
        }
Exemplo n.º 3
0
        public async Task CanUseMultipleMessageReadersOnSameUnderlyingReader()
        {
            var protocol         = new TestProtocol();
            var pipe             = new Pipe();
            var underlyingReader = PipeReader.Create(pipe.Reader.AsStream());

            var reader = new MessagePipeReader(underlyingReader, protocol);

            protocol.WriteMessage(Encoding.ASCII.GetBytes("Hello"), pipe.Writer);
            protocol.WriteMessage(Encoding.ASCII.GetBytes("World"), pipe.Writer);
            await pipe.Writer.FlushAsync();

            var readResult = await reader.ReadAsync();

            Assert.Equal("Hello", Encoding.ASCII.GetString(readResult.Buffer.ToArray()));
            reader.Complete();

            reader     = new MessagePipeReader(underlyingReader, protocol);
            readResult = await reader.ReadAsync();

            Assert.Equal("World", Encoding.ASCII.GetString(readResult.Buffer.ToArray()));

            reader.Complete();
        }
Exemplo n.º 4
0
        public async Task DoesNotReturnCompletedIfUnderlyingReaderIsCompletedIfThereAreMoreMessagesToParse()
        {
            var protocol     = new TestProtocol();
            var bufferWriter = new ArrayBufferWriter <byte>();

            protocol.WriteMessage(new byte[100], bufferWriter);
            protocol.WriteMessage(new byte[100], bufferWriter);
            var reader = new MessagePipeReader(
                new CompletingPipeReader(new ReadOnlySequence <byte>(bufferWriter.WrittenMemory)),
                protocol);

            var readResult = await reader.ReadAsync();

            var buffer = readResult.Buffer;

            Assert.Equal(100, buffer.Length);
            Assert.False(readResult.IsCompleted);

            reader.AdvanceTo(buffer.Start);
            readResult = await reader.ReadAsync();

            buffer = readResult.Buffer;

            Assert.Equal(200, buffer.Length);
            Assert.False(readResult.IsCompleted);

            reader.AdvanceTo(buffer.Start);
            readResult = await reader.ReadAsync();

            buffer = readResult.Buffer;

            Assert.Equal(200, buffer.Length);
            Assert.True(readResult.IsCompleted);

            reader.Complete();
        }
Exemplo n.º 5
0
        private static MessagePipeReader CreateReader(out Func <byte[], Task> writeFunc)
        {
            var protocol = new TestProtocol();
            var stream   = new MemoryStream();
            var writer   = PipeWriter.Create(stream);
            var reader   = new MessagePipeReader(PipeReader.Create(stream), protocol);

            long written = 0;

            writeFunc = async bytes =>
            {
                var position = stream.Position;
                stream.Position = written;
                protocol.WriteMessage(bytes, writer);
                await writer.FlushAsync().ConfigureAwait(false);

                written         = stream.Position;
                stream.Position = position;
            };
            return(reader);
        }