Skip to content

Conversation

@xPaw
Copy link
Member

@xPaw xPaw commented Jan 14, 2022

Fixes #48

StreamReader.Peek() just returns -1 instead of blocking, we mitigate that by using Read() which does block.

@xPaw xPaw requested a review from yaakov-h January 14, 2022 13:38
@JustArchi
Copy link
Contributor

#51

Copy link
Contributor

@JustArchi JustArchi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

New tests pass so LGTM

@JustArchi

This comment has been minimized.

Copy link
Member

@yaakov-h yaakov-h left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if

/// </summary>
internal sealed class BlockingStream : Stream
{
private static readonly byte[] TestData = Encoding.UTF8.GetBytes(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO this would be better as a wrapper for an existing Stream than as a weird stream with hardcoded content.


private long _backingPosition;

internal BlockingStream(int maxReadAtOnce = 1)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If maxReadAtOnce is customisable then BlockingStream isn't guaranteed to be blocking.
It looks like it's only guaranteed to be blocking when our maxReadAtOnce is smaller than the StreamReader's bufferSize, which is also configurable.

It may also be possible that if StreamReader's bufferSize is 1 then it will never block. 🤔

// Never read more than user-configured limit
if (count > MaxReadAtOnce) count = MaxReadAtOnce;

for (var i = 0; i < count; i++) buffer[offset++] = TestData[_backingPosition++];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe use Array.BlockCopy?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Buffer.BlockCopy, yup.

internal class StreamsTestCase
{
[TestCase(1)]
[TestCase(4096)]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think 4096 gets us as blocking stream, as per the previous comment. The default buffer size is 1024.

}

var text = textReader.ReadLine();
var text = sb.ToString();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this loop? Is there something wrong with textReader.ReadLine() from the old code? If its the case that we might have something in our little peekedNext field, can we not deal with that in a simpler manner?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if it would be simpler to build a TextReader wrapper.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I initially wanted to just use Stream, but that had its own problems.

I think ReadLine may return just for \r without being followed by \n which isn't how Valve deals with it (always looking for \n only).

@xPaw
Copy link
Member Author

xPaw commented Jan 20, 2022

I moved BlockingStream inside the test class so it's more clear it should not be reused.

Co-authored-by: Yaakov <yaakov-h@users.noreply.github.com>
@xPaw xPaw merged commit d90f09e into master Jan 21, 2022
@xPaw xPaw deleted the fix-48 branch January 21, 2022 08:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

VKV can't parse KV when reading directly from HttpClient's stream using HttpCompletionOption.ResponseHeadersRead

4 participants