Skip to content

Commit

Permalink
Removed custom deflate pool.
Browse files Browse the repository at this point in the history
  • Loading branch information
zlatanov committed Apr 28, 2021
1 parent f6a4b32 commit f0f09f3
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 407 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
<ItemGroup>
<Compile Include="System\Net\WebSockets\Compression\WebSocketDeflater.cs" />
<Compile Include="System\Net\WebSockets\Compression\WebSocketInflater.cs" />
<Compile Include="System\Net\WebSockets\Compression\ZLibStreamPool.cs" />
<Compile Include="System\Net\WebSockets\ValueWebSocketReceiveResult.cs" />
<Compile Include="System\Net\WebSockets\WebSocket.cs" />
<Compile Include="System\Net\WebSockets\WebSocketCloseStatus.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,23 @@ namespace System.Net.WebSockets.Compression
/// </summary>
internal sealed class WebSocketDeflater : IDisposable
{
private readonly ZLibStreamPool _streamPool;
private readonly int _windowBits;
private ZLibStreamHandle? _stream;
private readonly bool _persisted;

private byte[]? _buffer;

internal WebSocketDeflater(int windowBits, bool persisted)
{
_streamPool = ZLibStreamPool.GetOrCreate(windowBits);
_windowBits = -windowBits; // Negative for raw deflate
_persisted = persisted;
}

public void Dispose()
{
if (_stream is not null)
{
_streamPool.ReturnDeflater(_stream);
_stream.Dispose();
_stream = null;
}
}
Expand Down Expand Up @@ -84,7 +84,7 @@ public ReadOnlySpan<byte> Deflate(ReadOnlySpan<byte> payload, bool endOfMessage)
private void DeflatePrivate(ReadOnlySpan<byte> payload, Span<byte> output, bool endOfMessage,
out int consumed, out int written, out bool needsMoreOutput)
{
_stream ??= _streamPool.GetDeflater();
_stream ??= CreateDeflater();

if (payload.Length == 0)
{
Expand Down Expand Up @@ -119,7 +119,7 @@ private void DeflatePrivate(ReadOnlySpan<byte> payload, Span<byte> output, bool

if (endOfMessage && !_persisted)
{
_streamPool.ReturnDeflater(_stream);
_stream.Dispose();
_stream = null;
}
}
Expand Down Expand Up @@ -201,5 +201,33 @@ private static ErrorCode Deflate(ZLibStreamHandle stream, FlushCode flushCode)
: string.Format(SR.ZLibErrorUnexpected, (int)errorCode);
throw new WebSocketException(message);
}

private ZLibStreamHandle CreateDeflater()
{
ZLibStreamHandle stream;
ErrorCode errorCode;
try
{
errorCode = CreateZLibStreamForDeflate(out stream,
level: CompressionLevel.DefaultCompression,
windowBits: _windowBits,
memLevel: Deflate_DefaultMemLevel,
strategy: CompressionStrategy.DefaultStrategy);
}
catch (Exception cause)
{
throw new WebSocketException(SR.ZLibErrorDLLLoadError, cause);
}

if (errorCode != ErrorCode.Ok)
{

This comment has been minimized.

Copy link
@campersau

campersau Apr 28, 2021

Contributor

Is stream.Dispose(); missing here? It does exist in CreateInflater

f0f09f3#diff-0edaec6e3a3fe21900f67d3293d6b93ebd1e1507fcc789e3de7f5623ed7e055bR277

string message = errorCode == ErrorCode.MemError
? SR.ZLibErrorNotEnoughMemory
: string.Format(SR.ZLibErrorUnexpected, (int)errorCode);
throw new WebSocketException(message);
}

return stream;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ internal sealed class WebSocketInflater : IDisposable
internal const int FlushMarkerLength = 4;
internal static ReadOnlySpan<byte> FlushMarker => new byte[] { 0x00, 0x00, 0xFF, 0xFF };

private readonly ZLibStreamPool _streamPool;
private readonly int _windowBits;
private ZLibStreamHandle? _stream;
private readonly bool _persisted;

Expand Down Expand Up @@ -48,7 +48,7 @@ internal sealed class WebSocketInflater : IDisposable

internal WebSocketInflater(int windowBits, bool persisted)
{
_streamPool = ZLibStreamPool.GetOrCreate(windowBits);
_windowBits = -windowBits; // Negative for raw deflate
_persisted = persisted;
}

Expand All @@ -60,7 +60,7 @@ public void Dispose()
{
if (_stream is not null)
{
_streamPool.ReturnInflater(_stream);
_stream.Dispose();
_stream = null;
}
ReleaseBuffer();
Expand Down Expand Up @@ -128,7 +128,7 @@ public void AddBytes(int totalBytesReceived, bool endOfMessage)
/// </summary>
public unsafe bool Inflate(Span<byte> output, out int written)
{
_stream ??= _streamPool.GetInflater();
_stream ??= CreateInflater();

if (_available > 0 && output.Length > 0)
{
Expand Down Expand Up @@ -192,7 +192,7 @@ private unsafe bool Finish(Span<byte> output, ref int written)
{
if (!_persisted)
{
_streamPool.ReturnInflater(_stream);
_stream.Dispose();
_stream = null;
}
return true;
Expand Down Expand Up @@ -254,5 +254,32 @@ private static unsafe int Inflate(ZLibStreamHandle stream, Span<byte> destinatio
};
throw new WebSocketException(message);
}

private ZLibStreamHandle CreateInflater()
{
ZLibStreamHandle stream;
ErrorCode errorCode;

try
{
errorCode = CreateZLibStreamForInflate(out stream, _windowBits);
}
catch (Exception exception)
{
throw new WebSocketException(SR.ZLibErrorDLLLoadError, exception);
}

if (errorCode == ErrorCode.Ok)
{
return stream;
}

stream.Dispose();

string message = errorCode == ErrorCode.MemError
? SR.ZLibErrorNotEnoughMemory
: string.Format(SR.ZLibErrorUnexpected, (int)errorCode);
throw new WebSocketException(message);
}
}
}
Loading

0 comments on commit f0f09f3

Please sign in to comment.