public async Task FillPipeAsync(Stream sourceStream, PipeWriter writer, CancellationToken cancellationToken) { await using var statsStream = new BufferCopyStream(sourceStream); await using var gzipStream = new GZipStream(statsStream, CompressionMode.Decompress); try { int read; FlushResult flushed; do { var memory = writer.GetMemory(Config.MinimumBufferSize); read = await gzipStream.ReadAsync(memory, cancellationToken); writer.Advance(read); SourcePosition = statsStream.Position; flushed = await writer.FlushAsync(cancellationToken).ConfigureAwait(false); } while (read > 0 && !(flushed.IsCompleted || flushed.IsCanceled || cancellationToken.IsCancellationRequested)); var buf = statsStream.GetBufferedBytes(); if (buf.Length > 3) { LogSize = BitConverter.ToInt32(buf.AsSpan(buf.Length - 4, 4)); } } catch (Exception e) { Config.Log.Error(e, "Error filling the log pipe"); await writer.CompleteAsync(e); return; } await writer.CompleteAsync(); }
public async Task FillPipeAsync(Stream sourceStream, PipeWriter writer, CancellationToken cancellationToken) { try { using (var statsStream = new BufferCopyStream(sourceStream)) using (var zipReader = ZipReader.Open(statsStream)) while (zipReader.MoveToNextEntry()) { if (!zipReader.Entry.IsDirectory && zipReader.Entry.Key.EndsWith(".log", StringComparison.InvariantCultureIgnoreCase) && !zipReader.Entry.Key.Contains("tty.log", StringComparison.InvariantCultureIgnoreCase)) { LogSize = zipReader.Entry.Size; using (var rarStream = zipReader.OpenEntryStream()) { int read; FlushResult flushed; do { var memory = writer.GetMemory(Config.MinimumBufferSize); read = await rarStream.ReadAsync(memory, cancellationToken); writer.Advance(read); SourcePosition = statsStream.Position; flushed = await writer.FlushAsync(cancellationToken).ConfigureAwait(false); SourcePosition = statsStream.Position; } while (read > 0 && !(flushed.IsCompleted || flushed.IsCanceled || cancellationToken.IsCancellationRequested)); } writer.Complete(); return; } SourcePosition = statsStream.Position; } Config.Log.Warn("No rar entries that match the log criteria"); } catch (Exception e) { Config.Log.Error(e, "Error filling the log pipe"); } writer.Complete(); }