public async Task WriteLargeDataBinary(int length) { byte[] data = new byte[length]; new Random(length).NextBytes(data); using (var memoryPool = new MemoryPool()) { var pipe = new Pipe(memoryPool); var output = pipe.Writer.Alloc(); output.Write(data); var foo = output.Buffer.IsEmpty; // trying to see if .Memory breaks await output.FlushAsync(); pipe.Writer.Complete(); int offset = 0; while (true) { var result = await pipe.Reader.ReadAsync(); var input = result.Buffer; if (input.Length == 0) { break; } Assert.True(input.EqualsTo(new Span <byte>(data, offset, input.Length))); offset += input.Length; pipe.Advance(input.End); } Assert.Equal(data.Length, offset); } }
public async Task WriteLargeDataTextAscii(int length) { string data = new string('#', length); FillRandomStringData(data, length); using (var memoryPool = new MemoryPool()) { var pipe = new Pipe(memoryPool); var output = pipe.Writer.Alloc(); output.Append(data, TextEncoder.Utf8); var foo = output.Buffer.IsEmpty; // trying to see if .Memory breaks await output.FlushAsync(); pipe.Writer.Complete(); int offset = 0; while (true) { var result = await pipe.Reader.ReadAsync(); var input = result.Buffer; if (input.Length == 0) { break; } string s = ReadableBufferExtensions.GetAsciiString(input); Assert.Equal(data.Substring(offset, input.Length), s); offset += input.Length; pipe.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 pipe = new Pipe(new PipeOptions(memoryPool)); var output = pipe.Writer.Alloc(); output.Write(data); var foo = output.Buffer.IsEmpty; // trying to see if .Memory breaks await output.FlushAsync(); pipe.Writer.Complete(); long offset = 0; while (true) { var result = await pipe.Reader.ReadAsync(); var input = result.Buffer; if (input.Length == 0) { break; } // We are able to cast because test arguments are in range of int Assert.Equal(new Span <byte>(data, (int)offset, (int)input.Length).ToArray(), input.ToArray()); offset += input.Length; pipe.Advance(input.End); } Assert.Equal(data.Length, offset); } }
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(); } }
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(); }
public async Task WriteLargeDataTextAscii(int length) { string data = new string('#', length); FillRandomStringData(data, length); using (var memoryPool = new MemoryPool()) { var pipe = new Pipe(new PipeOptions(memoryPool)); var output = pipe.Writer.Alloc(); output.AsOutput().Append(data, SymbolTable.InvariantUtf8); var foo = output.Buffer.IsEmpty; // trying to see if .Memory breaks await output.FlushAsync(); pipe.Writer.Complete(); long offset = 0; while (true) { var result = await pipe.Reader.ReadAsync(); var input = result.Buffer; if (input.Length == 0) { break; } string s = ReadableBufferExtensions.GetAsciiString(input); // We are able to cast because test arguments are in range of int Assert.Equal(data.Substring((int)offset, (int)input.Length), s); offset += input.Length; pipe.Advance(input.End); } Assert.Equal(data.Length, offset); } }