-
Notifications
You must be signed in to change notification settings - Fork 869
Modernize Stream.ReadEx
#1306
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Modernize Stream.ReadEx
#1306
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -302,7 +302,6 @@ public static IEnumerable<List<T>> Partition<T>(this IEnumerable<T> source, Func | |||||||||||
| } | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| #if !NETSTANDARD1X | ||||||||||||
| public static int ReadEx(this Stream stream, byte[] buffer, int offset, int count, CancellationToken cancellation = default(CancellationToken)) | ||||||||||||
| { | ||||||||||||
| if (stream == null) | ||||||||||||
|
|
@@ -320,32 +319,7 @@ public static IEnumerable<List<T>> Partition<T>(this IEnumerable<T> source, Func | |||||||||||
|
|
||||||||||||
| while (totalReadCount < count) | ||||||||||||
| { | ||||||||||||
| cancellation.ThrowIfCancellationRequested(); | ||||||||||||
|
|
||||||||||||
| int currentReadCount; | ||||||||||||
|
|
||||||||||||
| //Big performance problem with BeginRead for other stream types than NetworkStream. | ||||||||||||
| //Only take the slow path if cancellation is possible. | ||||||||||||
| if (stream is NetworkStream && cancellation.CanBeCanceled) | ||||||||||||
| { | ||||||||||||
| var ar = stream.BeginRead(buffer, offset + totalReadCount, count - totalReadCount, null, null); | ||||||||||||
| if (!ar.CompletedSynchronously) | ||||||||||||
| { | ||||||||||||
| WaitHandle.WaitAny(new WaitHandle[] { ar.AsyncWaitHandle, cancellation.WaitHandle }, -1); | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| //EndRead might block, so we need to test cancellation before calling it. | ||||||||||||
| //This also is a bug because calling EndRead after BeginRead is contractually required. | ||||||||||||
| //A potential fix is to use the ReadAsync API. Another fix is to register a callback with BeginRead that calls EndRead in all cases. | ||||||||||||
| cancellation.ThrowIfCancellationRequested(); | ||||||||||||
|
|
||||||||||||
| currentReadCount = stream.EndRead(ar); | ||||||||||||
| } | ||||||||||||
| else | ||||||||||||
| { | ||||||||||||
| //IO interruption not supported in this path. | ||||||||||||
| currentReadCount = stream.Read(buffer, offset + totalReadCount, count - totalReadCount); | ||||||||||||
| } | ||||||||||||
| int currentReadCount = stream.ReadAsync(buffer, offset + totalReadCount, count - totalReadCount, cancellation).GetAwaiter().GetResult(); | ||||||||||||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. https://learn.microsoft.com/en-us/dotnet/api/system.io.stream.readasync?view=net-10.0 mentions that this overload is support on:
My understanding is that those are all supported platforms for NBitcoin. To be sure, I checked https://www.nuget.org/packages/NBitcoin#supportedframeworks-body-tab as well. I don't see anything missing. btw: I considered |
||||||||||||
|
|
||||||||||||
| if (currentReadCount == 0) | ||||||||||||
| return 0; | ||||||||||||
|
|
@@ -355,44 +329,6 @@ public static IEnumerable<List<T>> Partition<T>(this IEnumerable<T> source, Func | |||||||||||
|
|
||||||||||||
| return totalReadCount; | ||||||||||||
| } | ||||||||||||
| #else | ||||||||||||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Given that .NET Standard 1.x support was removed, the following code is dead code in my opinion.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. #1307 attempts to remove the remaining .NET Standard 1.x code |
||||||||||||
|
|
||||||||||||
| public static int ReadEx(this Stream stream, byte[] buffer, int offset, int count, CancellationToken cancellation = default(CancellationToken)) | ||||||||||||
| { | ||||||||||||
| if(stream == null) throw new ArgumentNullException(nameof(stream)); | ||||||||||||
| if(buffer == null) throw new ArgumentNullException(nameof(buffer)); | ||||||||||||
| if(offset < 0 || offset > buffer.Length) throw new ArgumentOutOfRangeException("offset"); | ||||||||||||
| if(count <= 0 || count > buffer.Length) throw new ArgumentOutOfRangeException("count"); //Disallow 0 as a debugging aid. | ||||||||||||
| if(offset > buffer.Length - count) throw new ArgumentOutOfRangeException("count"); | ||||||||||||
|
|
||||||||||||
| //IO interruption not supported on these platforms. | ||||||||||||
|
|
||||||||||||
| int totalReadCount = 0; | ||||||||||||
| #if !NOSOCKET | ||||||||||||
| var interruptable = stream is NetworkStream && cancellation.CanBeCanceled; | ||||||||||||
| #endif | ||||||||||||
| while(totalReadCount < count) | ||||||||||||
| { | ||||||||||||
| cancellation.ThrowIfCancellationRequested(); | ||||||||||||
| int currentReadCount = 0; | ||||||||||||
| #if !NOSOCKET | ||||||||||||
| if(interruptable) | ||||||||||||
| { | ||||||||||||
| currentReadCount = stream.ReadAsync(buffer, offset + totalReadCount, count - totalReadCount, cancellation).GetAwaiter().GetResult(); | ||||||||||||
| } | ||||||||||||
| else | ||||||||||||
| #endif | ||||||||||||
| { | ||||||||||||
| currentReadCount = stream.Read(buffer, offset + totalReadCount, count - totalReadCount); | ||||||||||||
| } | ||||||||||||
| if(currentReadCount == 0) | ||||||||||||
| return 0; | ||||||||||||
| totalReadCount += currentReadCount; | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| return totalReadCount; | ||||||||||||
| } | ||||||||||||
| #endif | ||||||||||||
|
|
||||||||||||
| #if HAS_SPAN | ||||||||||||
| public static int ReadEx(this Stream stream, Span<byte> buffer, CancellationToken cancellation = default(CancellationToken)) | ||||||||||||
|
|
||||||||||||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is my understanding that .NET Standard support 1.x was removed in #1243 (commit 4780a7f).