public static async Task BinaryMessagePipeline() { var pipe = new Pipe(); var bytes = Encoding.UTF8.GetBytes("abcde"); IMessage message = new BinaryMessage((ReadOnlySequence <byte>) new ChunkSequence <byte>(bytes, 2), "msg"); ThreadPool.QueueUserWorkItem(async state => { await message.CopyToAsync(pipe.Writer).ConfigureAwait(false); pipe.Writer.Complete(); }); var content = new MemoryStream(); while (true) { var read = await pipe.Reader.ReadAsync().ConfigureAwait(false); foreach (var chunk in read.Buffer) { await content.WriteAsync(chunk).ConfigureAwait(false); } pipe.Reader.AdvanceTo(read.Buffer.End); if (read.IsCompleted) { break; } } content.Seek(0, SeekOrigin.Begin); using (var reader = new StreamReader(content, Encoding.UTF8, false, 1024, true)) { Equal("abcde", reader.ReadToEnd()); } content.Dispose(); }
public static async Task BinaryMessagePipeline() { var pipe = new Pipe(); var bytes = Encoding.UTF8.GetBytes("abcde"); IMessage message = new BinaryMessage(ToReadOnlySequence <byte>(bytes, 2), "msg"); ThreadPool.QueueUserWorkItem(async state => { await message.WriteToAsync(pipe.Writer).ConfigureAwait(false); pipe.Writer.Complete(); }); using var content = new MemoryStream(); await pipe.Reader.CopyToAsync(content); content.Seek(0, SeekOrigin.Begin); using (var reader = new StreamReader(content, Encoding.UTF8, false, 1024, true)) { Equal("abcde", reader.ReadToEnd()); } }