dangerous release (possibly memory leak and deadlock)
This commit is contained in:
parent
a1cb6592eb
commit
741d34a5e0
10 changed files with 164 additions and 115 deletions
|
|
@ -10,6 +10,7 @@ public class PngPipeEncoder : IDisposable
|
|||
{
|
||||
private const int BufferSize = 8 * 1024;
|
||||
private const int FlushThreshold = 1024;
|
||||
private const int PipeChunkThreshold = 16 * 1024;
|
||||
|
||||
private readonly PipeWriter _outputPipe;
|
||||
private readonly MemoryStream _memoryStream;
|
||||
|
|
@ -60,33 +61,33 @@ public class PngPipeEncoder : IDisposable
|
|||
_outputPipe.Write(headerBytes);
|
||||
}
|
||||
|
||||
public void WriteData(IBuffer<byte> buffer, bool disposeBuffer = true, CancellationToken cancellationToken = default)
|
||||
public async Task WriteDataAsync(IBuffer<byte> buffer, bool disposeBuffer = true, CancellationToken cancellationToken = default)
|
||||
{
|
||||
_zlibStream.Write([0]);
|
||||
|
||||
var dataSlice = buffer.Span;
|
||||
while (dataSlice.Length > FlushThreshold)
|
||||
var offset = 0;
|
||||
while (buffer.Length - offset > FlushThreshold)
|
||||
{
|
||||
_zlibStream.Write(dataSlice[..FlushThreshold]);
|
||||
_zlibStream.Flush();
|
||||
dataSlice = dataSlice[FlushThreshold..];
|
||||
if(_memoryStream.Length >= BufferSize)
|
||||
Flush(cancellationToken);
|
||||
_zlibStream.Write(buffer.Span.Slice(offset, FlushThreshold));
|
||||
await _zlibStream.FlushAsync(cancellationToken);
|
||||
offset += FlushThreshold;
|
||||
if(_outputPipe.UnflushedBytes >= PipeChunkThreshold)
|
||||
await FlushAsync(cancellationToken);
|
||||
}
|
||||
|
||||
if (dataSlice.Length > 0)
|
||||
if (buffer.Length > offset)
|
||||
{
|
||||
_zlibStream.Write(dataSlice);
|
||||
_zlibStream.Flush();
|
||||
_zlibStream.Write(buffer.Span[offset..]);
|
||||
await _zlibStream.FlushAsync(cancellationToken);
|
||||
_shouldFlush = true;
|
||||
}
|
||||
|
||||
if(disposeBuffer) buffer.Dispose();
|
||||
}
|
||||
|
||||
private void Flush(CancellationToken cancellationToken)
|
||||
private async Task FlushAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
_zlibStream.Flush();
|
||||
await _zlibStream.FlushAsync(cancellationToken);
|
||||
var dataSize = (int)(_memoryStream.Length - 8);
|
||||
|
||||
_memoryStream.Write("\0\0\0\0"u8);
|
||||
|
|
@ -102,15 +103,17 @@ public class PngPipeEncoder : IDisposable
|
|||
BinaryPrimitives.WriteUInt32BigEndian(buffer.AsSpan(dataSize + 8), crc);
|
||||
|
||||
_outputPipe.Write(buffer.AsSpan(0, dataSize + 12));
|
||||
await _outputPipe.FlushAsync(cancellationToken);
|
||||
|
||||
_memoryStream.SetLength(8);
|
||||
_memoryStream.Position = 8;
|
||||
_shouldFlush = false;
|
||||
}
|
||||
|
||||
public void WriteEndOfFile(CancellationToken cancellationToken = default)
|
||||
public async Task WriteEndOfFileAsync(CancellationToken cancellationToken = default)
|
||||
{
|
||||
if(_shouldFlush)
|
||||
Flush(cancellationToken);
|
||||
await FlushAsync(cancellationToken);
|
||||
|
||||
Span<byte> endChunk = [
|
||||
0x00, 0x00, 0x00, 0x00, // Length
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue