Skip to content

Commit

Permalink
Allow reading CoapBlockStream if data is still buffered.
Browse files Browse the repository at this point in the history
  • Loading branch information
NZSmartie committed Jan 1, 2018
1 parent a139707 commit 7954c95
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/CoAPNet/CoapBlockStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public int BlockSize
}

/// <inheritdoc/>
public override bool CanRead => !_endOfStream && (_readerTask?.IsCompleted ?? false);
public override bool CanRead => !_endOfStream || _reader.Length > 0;

/// <inheritdoc/>
public override bool CanSeek => false;
Expand Down Expand Up @@ -302,9 +302,14 @@ public override async Task<int> ReadAsync(byte[] buffer, int offset, int count,
var read = 0;
while (read < count && !cancellationToken.IsCancellationRequested)
{
await _readerEvent.WaitAsync(cancellationToken);
if (!_endOfStream)
await _readerEvent.WaitAsync(cancellationToken);

read += _reader.Dequeue(buffer, offset + read, count - read);
var bytesDequeued = _reader.Dequeue(buffer, offset + read, count - read);
read += bytesDequeued;

if (bytesDequeued == 0 && _endOfStream)
break;
}

return read;
Expand Down

0 comments on commit 7954c95

Please sign in to comment.