protected async Task ReadPipeAsync(PipeReader reader) { while (true) { var result = await reader.ReadAsync(); var buffer = result.Buffer; SequencePosition consumed = buffer.Start; SequencePosition examined = buffer.End; try { if (result.IsCanceled) { break; } var completed = result.IsCompleted; if (buffer.Length > 0) { if (!ReaderBuffer(buffer, out consumed, out examined)) { completed = true; break; } } if (completed) { break; } } finally { reader.AdvanceTo(consumed, examined); } } reader.Complete(); }
private async Task ReceivePacketsAsync(PipeReader reader) { while (true) { ReadResult result = await reader.ReadAsync(); ReadOnlySequence <byte> buffer = result.Buffer; while (true) { // We can't calculate packet size without the packet header if (buffer.Length < PacketConstants.HeaderSize) { break; } // Make sure the packet fits in the buffer // See: https://mariadb.com/kb/en/library/0-packet/ var bodySize = GetBodySize(buffer); var packetSize = PacketConstants.HeaderSize + bodySize; if (buffer.Length < packetSize) { break; } // Process packet and repeat in case there are more packets in the buffer await OnReceivePacket(buffer.Slice(PacketConstants.HeaderSize, bodySize)); buffer = buffer.Slice(buffer.GetPosition(packetSize)); } reader.AdvanceTo(buffer.Start, buffer.End); if (result.IsCompleted) { break; } } await reader.CompleteAsync(); }
private async Task <Message> Memory_Corruption(PipeReader reader, CancellationToken cancellationToken) { // reverted #region snippet6 Environment.FailFast("This code is terrible, don't use it!"); Message message = null; while (true) { ReadResult result = await reader.ReadAsync(cancellationToken); ReadOnlySequence <byte> buffer = result.Buffer; ReadHeader(ref buffer, out int length); if (length <= buffer.Length) { message = new Message { // Slice the payload from the existing buffer CorruptedPayload = buffer.Slice(0, length) }; buffer = buffer.Slice(length); } if (result.IsCompleted) { break; } reader.AdvanceTo(buffer.Start, buffer.End); if (message != null) { // This code is broken since reader.AdvanceTo() was called with a position *after* the buffer was captured. break; } } return(message); }
private static async Task <int> ReadPipeAsync(PipeReader reader) { var linesCount = 0; while (true) { var result = await reader.ReadAsync(); var buffer = result.Buffer; linesCount += CountNewLines(buffer); if (result.IsCompleted) { break; } } await reader.CompleteAsync(); return(linesCount); }
static async Task <string> ReadAsString(PipeReader reader) { ReadResult result; while (true) { result = await reader.ReadAsync().ConfigureAwait(false); if (result.IsCompleted) { break; } reader.AdvanceTo(result.Buffer.Start, result.Buffer.End); } var body = Encoding.UTF8.GetString(result.Buffer.ToArray()); reader.AdvanceTo(result.Buffer.End); return(body); }
private async Task SendResponse(PipeReader reader) { while (true) { var result = await reader.ReadAsync(); var buffer = result.Buffer; if (buffer.Length <= 0) { break; } await socket.SendAsync(buffer.ToArray(), SocketFlags.None); reader.AdvanceTo(buffer.Start, buffer.End); } reader.Complete(); }
static async Task DoReadsAsync(PipeReader reader, byte[][] reads) { int index = 0; while (true) { ReadResult readResult = await reader.ReadAsync(); ReadOnlySequence <byte> buffer = readResult.Buffer; if (readResult.IsCompleted) { break; } Assert.Equal(reads[index], buffer.ToArray()); reader.AdvanceTo(buffer.End); index++; } reader.Complete(); }
private async ValueTask TaskReaderWorker() { while (!cancellationToken.IsCancellationRequested && pipeReader != null) { ReadResult result = await pipeReader.ReadAsync(cancellationToken); if (result.IsCompleted || result.IsCanceled || result.Buffer.Length == 0) { continue; } ReadOnlySequence <byte> buffer = result.Buffer; // We are looking for Length if (currentLength == 0) { if (buffer.Length >= 4) // 4 bytes { SequencePosition pos = GetFrameLength(buffer, out currentLength); pipeReader.AdvanceTo(pos, pos); continue; } } else if (buffer.Length >= currentLength) { // Parse actual mensaje if enough data try { T message = deserializer(buffer.Slice(0, currentLength)); _ = FireEvent(message); } catch (Exception e) { logger.Error(e, "Error while parsing"); } var bytesUsed = buffer.GetPosition(currentLength); pipeReader.AdvanceTo(bytesUsed, bytesUsed); currentLength = 0; } else { pipeReader.AdvanceTo(buffer.Start, buffer.End); } } }
async Task SendAsync() { PipeReader reader = _requestPipe.Reader; try { while (true) { ReadResult result = await reader.ReadAsync(); ReadOnlySequence <byte> buffer = result.Buffer; try { if (!buffer.IsEmpty) { for (SequencePosition position = buffer.Start; buffer.TryGet(ref position, out ReadOnlyMemory <byte> segment);) { await WriteToSocketAsync(segment).ConfigureAwait(false); } } else if (result.IsCompleted) { break; } } finally { reader.AdvanceTo(buffer.End); } } } catch (Exception e) { Log.TraceEvent(TraceEventType.Error, 0, e.ToString()); } finally { reader.Complete(); } }
private async Task ReceivePacketsAsync(PipeReader reader) { while (true) { ReadResult result = await reader.ReadAsync(); ReadOnlySequence <byte> buffer = result.Buffer; while (true) { // We can't calculate packet size without the event header if (buffer.Length < EventConstants.HeaderSize) { break; } // Make sure the event fits in the buffer var eventHeader = GetEventHeader(buffer); if (buffer.Length < eventHeader.EventLength) { break; } // Process event and repeat in case there are more event in the buffer await OnReceiveEvent(buffer.Slice(0, eventHeader.EventLength)); buffer = buffer.Slice(buffer.GetPosition(eventHeader.EventLength)); } reader.AdvanceTo(buffer.Start, buffer.End); if (result.IsCompleted) { break; } } await reader.CompleteAsync(); _channel.Writer.Complete(); }
public static async ValueTask <FlushResult> CopyFrom( this PipeWriter writer, PipeReader reader, long count, CancellationToken ct) { FlushResult flushResult = default; int writtenSinceFlush = 0; while (count > 0 && !flushResult.IsCompleted) { var readResult = await reader.ReadAsync(ct).ConfigureAwait(false); if (readResult.Buffer.IsEmpty) { throw new InvalidOperationException("unexpected EOF"); } var readBuffer = readResult.Buffer.First; if (readBuffer.Length > count) { readBuffer = readBuffer.Slice(0, (int)count); } var writeBuffer = writer.GetMemory(readBuffer.Length); readBuffer.CopyTo(writeBuffer); writer.Advance(readBuffer.Length); reader.AdvanceTo(readResult.Buffer.GetPosition(readBuffer.Length)); writtenSinceFlush += readBuffer.Length; if (writtenSinceFlush > 1 << 12) { flushResult = await writer.FlushAsync(ct).ConfigureAwait(false); writtenSinceFlush = 0; } count -= readBuffer.Length; } if (writtenSinceFlush > 0) { flushResult = await writer.FlushAsync(ct).ConfigureAwait(false); } return(flushResult); }
private static async IAsyncEnumerable <ResultRow> Reader(PipeReader reader) { FastSerializableKeys <string> keys = null; while (true) { var result = await reader.ReadAsync().ConfigureAwait(false); var buffer = result.Buffer; SequencePosition?position; do { position = buffer.PositionOf((byte)'\n'); if (position == null) { continue; } if (keys == null) { keys = DeserializeKeys(buffer.Slice(0, position.Value)); } else { yield return(DeserializeRow(keys, buffer.Slice(0, position.Value))); } buffer = buffer.Slice(buffer.GetPosition(1, position.Value)); } while (position != null); reader.AdvanceTo(buffer.Start, buffer.End); if (result.IsCompleted) { break; } } await reader.CompleteAsync().ConfigureAwait(false); }
private async Task ReadPipeAsync(TcpClient client, PipeReader reader, CancellationToken cancellationToken) { try { while (true) { var result = await reader.ReadAsync(cancellationToken); var buffer = result.Buffer; try { // Process all messages from the buffer, modifying the input buffer on each iteration. while (TryParseMessage(ref buffer, out var message)) { await ProcessMessageAsync(client, message); } if (result.IsCompleted) { if (buffer.Length > 0) { throw new InvalidOperationException("Incomplete message"); } break; } } finally { reader.AdvanceTo(buffer.Start, buffer.End); } } } finally { await reader.CompleteAsync(); } _logger.LogInformation("There is no more data to read from {RemoteEndpoint}", client.Client.RemoteEndPoint); }
private static async Task BrotliCompressAsync(PipeReader rawReader, PipeWriter writer) { using (var brotli = new BrotliEncoder(1, 24)) { while (true) { var result = await rawReader.ReadAsync(); if (result.Buffer.Length > int.MaxValue) { throw new Exception(); } if (!result.Buffer.IsEmpty) { using (var sourceMemoryOwner = MemoryPool <byte> .Shared.Rent((int)result.Buffer.Length)) using (var destMemoryOwner = MemoryPool <byte> .Shared.Rent((int)result.Buffer.Length)) { var sourceMemory = sourceMemoryOwner.Memory.Slice(0, (int)result.Buffer.Length); result.Buffer.CopyTo(sourceMemory.Span); brotli.Compress(sourceMemory.Span, destMemoryOwner.Memory.Span, out var bytesConsumed, out var bytesWritten, result.IsCompleted); rawReader.AdvanceTo(result.Buffer.GetPosition(bytesConsumed)); var destMemory = destMemoryOwner.Memory.Slice(0, bytesWritten); await writer.WriteAsync(destMemory); } } if (result.IsCompleted) { break; } } rawReader.Complete(); writer.Complete(); } }
private async Task Infinite_loop(PipeReader reader, CancellationToken cancellationToken) { // reverted #region snippet2 Environment.FailFast("This code is terrible, don't use it!"); while (true) { ReadResult result = await reader.ReadAsync(cancellationToken); ReadOnlySequence <byte> infiniteLoopBuffer = result.Buffer; if (result.IsCompleted && infiniteLoopBuffer.IsEmpty) { break; } Process(ref infiniteLoopBuffer, out Message message); reader.AdvanceTo(infiniteLoopBuffer.Start, infiniteLoopBuffer.End); } #endregion }
private static async Task <int> ReadAsync(PipeReader reader) { var count = 0; while (true) { ReadResult result = await reader.ReadAsync(); ReadOnlySequence <byte> buffer = result.Buffer; count += CountEndOfLines(buffer, count); reader.AdvanceTo(buffer.End); if (result.IsCompleted) { break; } } return(count); }
private async ValueTask ReadDataAsync(PipeReader reader) { while (true) { var result = await reader.ReadAsync(); var buffer = result.Buffer; if (buffer.IsEmpty && result.IsCompleted) { break; } foreach (var memory in buffer) { var span = Encoding.ASCII.GetString(memory.Span); Console.Write(span); } reader.AdvanceTo(buffer.End); } }
private async Task ReadPipeAsync(Socket socket, PipeReader reader) { while (true) { ReadResult result = await reader.ReadAsync(); ReadOnlySequence <byte> buffer = result.Buffer; SequencePosition? position = null; //this.serverPacketProcessor.ProcessFromBuffer(result.Buffer.ToArray()); reader.AdvanceTo(result.Buffer.End); if (result.IsCompleted) { break; } } reader.Complete(); }
public async Task <Handshake> GetHandshakeMessage(PipeReader input) { ReadResult readAsync = await input.ReadAsync().ConfigureAwait(false); if (readAsync.Buffer.Length >= FrameHeaderSize) { ReadOnlySequence <byte> handshakeBytes = readAsync.Buffer.Slice(0, FrameHeaderSize); ArraySegment <byte> arraySegment = handshakeBytes.ToArraySegment(); if (Handshake.TryParse(arraySegment, out Handshake result)) { input.AdvanceTo(readAsync.Buffer.GetPosition(FrameHeaderSize)); return(result); } } input.AdvanceTo(readAsync.Buffer.GetPosition(0)); return(null); }
public async Task ReadWithDifferentSettings(int bytesInBuffer, int bufferSize, int minimumReadSize, int[] readBufferSizes) { var options = new StreamPipeReaderOptions(bufferSize: bufferSize, minimumReadSize: minimumReadSize, pool: new HeapBufferPool()); var stream = new MemoryStream(Enumerable.Range(0, bytesInBuffer).Select(i => (byte)i).ToArray()); PipeReader reader = PipeReader.Create(stream, options); for (int i = 0; i < readBufferSizes.Length; i++) { ReadResult readResult = await reader.ReadAsync(); long length = readResult.Buffer.Length; Assert.Equal(readBufferSizes[i], length); reader.AdvanceTo(readResult.Buffer.End); if (length == 0) { Assert.True(readResult.IsCompleted); } } reader.Complete(); }
static async ValueTask <byte[]> ReadAsync(PipeReader read) { var ret = new List <byte>(); while (true) { var res = await read.ReadAsync(); foreach (var seq in res.Buffer) { ret.AddRange(seq.ToArray()); } if (res.IsCompleted) { break; } } return(ret.ToArray()); }
public static async Task <byte[]> ReadAsync(this PipeReader pipeReader, int numBytes) { while (true) { var result = await pipeReader.ReadAsync(); if (result.Buffer.Length < numBytes) { pipeReader.AdvanceTo(result.Buffer.Start, result.Buffer.End); continue; } var buffer = result.Buffer.Slice(0, numBytes); var bytes = buffer.ToArray(); pipeReader.AdvanceTo(buffer.End); return(bytes); } }
public async ValueTask ProcessIncomingPacketAsync(IInputLogger?m, InputPump sender, byte header, int packetLength, PipeReader pipeReader, Func <ValueTask> next, CancellationToken cancellationToken) { if (PacketType.SubscribeAck != (PacketType)header) { await next(); return; } using (m?.ProcessPacket(PacketType.SubscribeAck)) { ReadResult?read = await pipeReader.ReadAsync(m, packetLength); if (!read.HasValue) { return; } Parse(read.Value.Buffer, packetLength, out ushort packetId, out QualityOfService[]? qos, out SequencePosition position); pipeReader.AdvanceTo(position); await _store.OnQos1AckAsync(m, packetId, qos); } }
private async Task ReadPipeAsync(PipeReader reader) { while (true) { var result = await reader.ReadAsync(); var buffer = result.Buffer; SequenceChunk?sequenceChunk = null; do { Log.Trace($"Reading from pipe."); if (result.Buffer.Length > 0) { Log.Debug($"Reading from pipe using bufferLength [{result.Buffer.Length}]."); } sequenceChunk = Settings.PipeSequenceChunkifier.Execute(buffer); if (sequenceChunk != null) { Log.Debug($"Sequence found. Broadcasting [{sequenceChunk.Value.Position}] bytes to event subscribers."); Received?.Invoke(this, DecodeTransmission(buffer, sequenceChunk)); buffer = buffer.Slice(buffer.GetPosition(sequenceChunk.Value.Length, sequenceChunk.Value.Position)); } }while (sequenceChunk?.Position != null); reader.AdvanceTo(buffer.Start, buffer.End); if (result.IsCompleted) { break; } } Log.Trace($"Reader completed."); reader.Complete(); }
private async Task Consumer(PipeReader reader, CancellationToken ct = default) { while (!ct.IsCancellationRequested && !disposedValue) { var result = await reader.ReadAsync(ct); var buffer = result.Buffer; int consumed = 0; while (true) { if (!_bufferProcessors[NextProcessState](buffer, ref consumed)) { break; } } buffer = buffer.Slice(consumed); reader.AdvanceTo(buffer.Start, buffer.End); if (ChunkStreamContext != null) { ChunkStreamContext.ReadUnAcknowledgedSize += consumed; if (ChunkStreamContext.ReadWindowAcknowledgementSize.HasValue) { if (ChunkStreamContext.ReadUnAcknowledgedSize >= ChunkStreamContext.ReadWindowAcknowledgementSize) { ChunkStreamContext._rtmpSession.Acknowledgement((uint)ChunkStreamContext.ReadUnAcknowledgedSize); ChunkStreamContext.ReadUnAcknowledgedSize -= 0; } } } if (result.IsCompleted || result.IsCanceled) { break; } } // Mark the PipeReader as complete reader.Complete(); }
private async Task ReadPipeAsync(Socket socket, PipeReader reader) { while (true) { ReadResult result = await reader.ReadAsync(); ReadOnlySequence <byte> buffer = result.Buffer; SequencePosition? position = null; do { // Find the EOL position = buffer.PositionOf((byte)'\n'); if (position != null) { var line = buffer.Slice(0, position.Value); ProcessLine(socket, line); // This is equivalent to position + 1 var next = buffer.GetPosition(1, position.Value); // Skip what we're already processed including \n buffer = buffer.Slice(next); } } while (position != null); // We sliced the buffer until no more data could be processed // Tell the PipeReader how much we consumed and how much we left to process reader.AdvanceTo(buffer.Start, buffer.End); if (result.IsCompleted) { break; } } reader.Complete(); }
public async Task RunAsync( CancellationToken cancellationToken = default) { var writer = _pipe.Writer; try { ReadResult readResult = default; FlushResult flushResult = default; while (!readResult.IsCompleted && !flushResult.IsCompleted) { readResult = await _input.ReadAsync(cancellationToken); var sequence = readResult.Buffer; while (TryReadFrame(ref sequence, out var frame)) { if (frame.IsEmpty) { continue; } var encoded = TryEncodeFrame(frame, writer); Debug.Assert(encoded > 0, "Opus encode error"); writer.Advance(encoded); } _input.AdvanceTo(sequence.Start, sequence.End); flushResult = await writer.FlushAsync(cancellationToken); } } finally { await writer.CompleteAsync(); await _input.CompleteAsync(); } }
private async Task ReadRecvPipe(PipeReader reader) { try { ReadResult result; do { result = await reader.ReadAsync(); int bytesRead; ReadOnlySequence <byte> buffer = result.Buffer; while ((bytesRead = RecvCipher.TryDecrypt(buffer, out PoolPacketReader packet)) > 0) { try { LogRecv(packet); OnPacket?.Invoke(this, packet); // handle packet } finally { packet.Dispose(); } buffer = buffer.Slice(bytesRead); } reader.AdvanceTo(buffer.Start, buffer.End); } while (!Disposed && !result.IsCompleted); } catch (Exception ex) { if (!Disposed) { Logger.Fatal($"Exception in recv PipeScheduler: {ex}"); } } finally { Disconnect(logoutNotice: true); } }
/// <summary> /// Writes all the content of the PipeReader in the Stream /// </summary> public static async Task CopyToAsync(this PipeReader reader, Stream target, CancellationToken cancellationToken = default(CancellationToken)) { if (target == null) { throw new ArgumentNullException(nameof(target)); } if (reader == null) { throw new ArgumentNullException(nameof(reader)); } try { while (true) { var read = await reader.ReadAsync(cancellationToken); var buffer = read.Buffer; if (buffer.IsEmpty && read.IsCompleted) { break; } foreach (var memoryChunk in buffer) { await target.WriteAsync(memoryChunk, cancellationToken); } reader.AdvanceTo(buffer.End); } reader.Complete(); } catch (Exception err) { reader.Complete(err); } }
IDisposable ReaderSchedule(IObserver <ReadOnlySequence <byte> > observer, CancellationToken cancellationToken = default) { return(NewThreadScheduler.Default.Schedule(async() => { while (!cancellationToken.IsCancellationRequested) { try { var readresult = await _pipeReader.ReadAsync(cancellationToken).ConfigureAwait(false); var buffer = readresult.Buffer; if (buffer.Length <= 0) { continue; } while (ContainsLine(ref buffer, out ReadOnlySequence <byte> line)) { ProcessLine(line, observer); } ////将指针移到下一条数据的头位置。 _pipeReader.AdvanceTo(buffer.Start, buffer.End); if (readresult.IsCompleted) { break; } } catch { break; } } _pipeReader?.Complete(); observer.OnCompleted(); })); }