From 1ff6b050cabce405aed023c13e0c35e97fd07bd5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Graziano?= Date: Fri, 19 Apr 2019 15:58:38 +0200 Subject: [PATCH 1/2] Memory Leak fix : when stream is closed, it have to throw an Exception, otherwise it will generate a big memory leak (tested with Titanium which keep in memory the connection - supposing it's alive because it just reads 0 bytes) --- src/StreamExtended/Network/CustomBufferedStream.cs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/StreamExtended/Network/CustomBufferedStream.cs b/src/StreamExtended/Network/CustomBufferedStream.cs index c81659c..a57a4d7 100644 --- a/src/StreamExtended/Network/CustomBufferedStream.cs +++ b/src/StreamExtended/Network/CustomBufferedStream.cs @@ -432,6 +432,7 @@ public bool FillBuffer() else { closed = true; + throw new IOException($"{nameof(CustomBufferedStream)} closed"); } return result; @@ -439,7 +440,7 @@ public bool FillBuffer() catch { closed = true; - return false; + throw;//rethrow } } @@ -481,6 +482,7 @@ public bool FillBuffer() else { closed = true; + throw new IOException($"{nameof(CustomBufferedStream)} closed"); } return result; @@ -488,7 +490,7 @@ public bool FillBuffer() catch { closed = true; - return false; + throw;//rethrow } } From 2c8c45d381f137b2f06e6b410984094654a3ecce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Graziano?= Date: Fri, 19 Apr 2019 15:59:47 +0200 Subject: [PATCH 2/2] Extends the interface with PeekBytesAsync (will be used for improve Peeking instead of byte per byte) --- src/StreamExtended/Network/CopyStream.cs | 5 ++++ .../Network/CustomBufferedPeekStream.cs | 11 +++++++++ .../Network/CustomBufferedStream.cs | 24 +++++++++++++++++++ .../Network/ICustomStreamReader.cs | 8 +++++++ 4 files changed, 48 insertions(+) diff --git a/src/StreamExtended/Network/CopyStream.cs b/src/StreamExtended/Network/CopyStream.cs index 33a3291..197f887 100644 --- a/src/StreamExtended/Network/CopyStream.cs +++ b/src/StreamExtended/Network/CopyStream.cs @@ -54,6 +54,11 @@ public byte PeekByteFromBuffer(int index) return reader.PeekByteAsync(index, cancellationToken); } + public Task PeekBytesAsync(int index, int size, CancellationToken cancellationToken = default(CancellationToken)) + { + return reader.PeekBytesAsync(index, size, cancellationToken); + } + public void Flush() { //send out the current data from from the buffer diff --git a/src/StreamExtended/Network/CustomBufferedPeekStream.cs b/src/StreamExtended/Network/CustomBufferedPeekStream.cs index 01f4cb7..ab3d862 100644 --- a/src/StreamExtended/Network/CustomBufferedPeekStream.cs +++ b/src/StreamExtended/Network/CustomBufferedPeekStream.cs @@ -85,6 +85,17 @@ byte ICustomStreamReader.PeekByteFromBuffer(int index) return baseStream.PeekByteFromBuffer(index); } + /// + /// Peeks bytes asynchronous. + /// + /// The index. + /// The cancellation token. + /// + Task ICustomStreamReader.PeekBytesAsync(int index, int size, CancellationToken cancellationToken) + { + return baseStream.PeekBytesAsync(index, size, cancellationToken); + } + /// /// Peeks a byte asynchronous. /// diff --git a/src/StreamExtended/Network/CustomBufferedStream.cs b/src/StreamExtended/Network/CustomBufferedStream.cs index a57a4d7..9db04b9 100644 --- a/src/StreamExtended/Network/CustomBufferedStream.cs +++ b/src/StreamExtended/Network/CustomBufferedStream.cs @@ -240,6 +240,30 @@ public override int ReadByte() return streamBuffer[bufferPos + index]; } + /// + /// Peeks bytes asynchronous. + /// + /// The index. + /// The cancellation token. + /// + public async Task PeekBytesAsync(int index, int size, CancellationToken cancellationToken = default(CancellationToken)) + { + + if (Available <= index) + { + await FillBufferAsync(cancellationToken); + } + + if (Available <= (index + size)) + { + return null; + } + + var vRet = new byte[size]; + Array.Copy(streamBuffer, index, vRet, 0, size); + return vRet; + } + /// /// Peeks a byte from buffer. /// diff --git a/src/StreamExtended/Network/ICustomStreamReader.cs b/src/StreamExtended/Network/ICustomStreamReader.cs index 058dc01..89d3278 100644 --- a/src/StreamExtended/Network/ICustomStreamReader.cs +++ b/src/StreamExtended/Network/ICustomStreamReader.cs @@ -37,6 +37,14 @@ public interface ICustomStreamReader /// Task PeekByteAsync(int index, CancellationToken cancellationToken = default(CancellationToken)); + /// + /// Peeks bytes asynchronous. + /// + /// The index. + /// The cancellation token. + /// + Task PeekBytesAsync(int index, int size, CancellationToken cancellationToken = default(CancellationToken)); + byte ReadByteFromBuffer(); ///