Skip to content

Commit

Permalink
fix: Fixed missing PipeStreamReader read check. Refactored.
Browse files Browse the repository at this point in the history
  • Loading branch information
HavenDV committed Apr 21, 2022
1 parent 968327e commit 030e9ba
Showing 1 changed file with 24 additions and 21 deletions.
45 changes: 24 additions & 21 deletions src/libs/H.Pipes/IO/PipeStreamReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public PipeStreamReader(PipeStream stream)

#endregion

#region Private stream readers
#region Methods

/// <summary>
/// Reads the length of the next message (in bytes) from the client.
Expand All @@ -49,38 +49,38 @@ public PipeStreamReader(PipeStream stream)
/// <exception cref="IOException">Any I/O error occurred.</exception>
private async Task<int> ReadLengthAsync(CancellationToken cancellationToken = default)
{
var buffer = new byte[sizeof(int)];
#if NETSTANDARD2_1 || NETCOREAPP3_1_OR_GREATER
var read = await BaseStream.ReadAsync(buffer, cancellationToken).ConfigureAwait(false);
#elif NET461_OR_GREATER || NETSTANDARD2_0
var read = await BaseStream.ReadAsync(buffer, 0, buffer.Length, cancellationToken).ConfigureAwait(false);
#else
#error Target Framework is not supported
#endif
if (read == 0)
var bytes = await ReadAsync(
length: sizeof(int),
throwIfReadLessThanLength: false,
cancellationToken: cancellationToken).ConfigureAwait(false);
if (!bytes.Any())
{
IsConnected = false;
return 0;
}

if (read != buffer.Length)
{
throw new IOException($"Expected {buffer.Length} bytes but read {read}");
}

return IPAddress.NetworkToHostOrder(BitConverter.ToInt32(buffer, 0));
return IPAddress.NetworkToHostOrder(BitConverter.ToInt32(bytes, 0));
}

private async Task<byte[]> ReadAsync(int length, CancellationToken cancellationToken = default)
private async Task<byte[]> ReadAsync(
int length,
bool throwIfReadLessThanLength = true,
CancellationToken cancellationToken = default)
{
var buffer = new byte[length];
#if NETSTANDARD2_1 || NETCOREAPP3_1_OR_GREATER
await BaseStream.ReadAsync(buffer, cancellationToken).ConfigureAwait(false);
var read = await BaseStream.ReadAsync(buffer, cancellationToken).ConfigureAwait(false);
#elif NET461_OR_GREATER || NETSTANDARD2_0
await BaseStream.ReadAsync(buffer, 0, buffer.Length, cancellationToken).ConfigureAwait(false);
var read = await BaseStream.ReadAsync(buffer, 0, buffer.Length, cancellationToken).ConfigureAwait(false);
#else
#error Target Framework is not supported
#endif
# endif
if (read != buffer.Length)
{
return throwIfReadLessThanLength
? throw new IOException($"Expected {buffer.Length} bytes but read {read}")
: Array.Empty<byte>();
}

return buffer;
}
Expand All @@ -96,7 +96,10 @@ private async Task<byte[]> ReadAsync(int length, CancellationToken cancellationT

return length == 0
? default
: await ReadAsync(length, cancellationToken).ConfigureAwait(false);
: await ReadAsync(
length: length,
throwIfReadLessThanLength: true,
cancellationToken: cancellationToken).ConfigureAwait(false);
}

#endregion
Expand Down

0 comments on commit 030e9ba

Please sign in to comment.