Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit 6e448d6

Browse files
committed
Address PR feedback
1 parent 2c309c8 commit 6e448d6

File tree

5 files changed

+30
-28
lines changed

5 files changed

+30
-28
lines changed

src/System.IO.Pipes/src/System.IO.Pipes.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,8 @@
132132
<Compile Include="System\IO\Pipes\NamedPipeClientStream.Windows.cs" />
133133
<Compile Include="System\IO\Pipes\NamedPipeServerStream.Windows.cs" />
134134
<Compile Include="System\IO\Pipes\PipeCompletionSource.cs" />
135-
<Compile Include="System\IO\Pipes\PipeIOCompletionSource.cs" />
136135
<Compile Include="System\IO\Pipes\PipeStream.Windows.cs" />
136+
<Compile Include="System\IO\Pipes\ReadWriteCompletionSource.cs" />
137137
</ItemGroup>
138138
<ItemGroup Condition=" '$(TargetsUnix)' == 'true' ">
139139
<Compile Include="Microsoft\Win32\SafeHandles\SafePipeHandle.Unix.cs" />

src/System.IO.Pipes/src/System/IO/Pipes/ConnectionCompletionSource.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ protected override void AsyncCallback(uint errorCode, uint numBytes)
3333
base.AsyncCallback(errorCode, numBytes);
3434
}
3535

36-
protected override void HandleError()
36+
protected override void HandleError(int errorCode)
3737
{
38-
TrySetException(Win32Marshal.GetExceptionForWin32Error(ErrorCode));
38+
TrySetException(Win32Marshal.GetExceptionForWin32Error(errorCode));
3939
}
4040
}
4141

src/System.IO.Pipes/src/System/IO/Pipes/PipeCompletionSource.cs

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ internal abstract unsafe class PipeCompletionSource<TResult> : TaskCompletionSou
2121
private readonly ThreadPoolBoundHandle _threadPoolBinding;
2222

2323
private CancellationTokenRegistration _cancellationRegistration;
24+
private int _errorCode;
2425
private NativeOverlapped* _overlapped;
2526
private int _state;
2627

@@ -52,8 +53,6 @@ internal NativeOverlapped* Overlapped
5253
[SecurityCritical]get { return _overlapped; }
5354
}
5455

55-
protected int ErrorCode { get; private set; }
56-
5756
internal void RegisterForCancellation()
5857
{
5958
#if DEBUG
@@ -83,7 +82,7 @@ internal void RegisterForCancellation()
8382

8483
// If we have the result state of completed IO call CompleteCallback(result).
8584
// Otherwise IO not completed.
86-
if (state == ResultSuccess || state == ResultError)
85+
if ((state & (ResultSuccess | ResultError)) != 0)
8786
{
8887
CompleteCallback(state);
8988
}
@@ -107,9 +106,16 @@ internal void ReleaseResources()
107106

108107
protected virtual void AsyncCallback(uint errorCode, uint numBytes)
109108
{
110-
ErrorCode = (int)errorCode;
111-
112-
int resultState = errorCode == 0 ? ResultSuccess : ResultError;
109+
int resultState;
110+
if (errorCode == 0)
111+
{
112+
resultState = ResultSuccess;
113+
}
114+
else
115+
{
116+
resultState = ResultError;
117+
_errorCode = (int)errorCode;
118+
}
113119

114120
// Store the result so that other threads can observe it
115121
// and if no other thread is registering cancellation, continue.
@@ -125,24 +131,21 @@ protected virtual void AsyncCallback(uint errorCode, uint numBytes)
125131
}
126132
}
127133

128-
protected abstract void HandleError();
134+
protected abstract void HandleError(int errorCode);
129135

130136
private void Cancel()
131137
{
132138
SafeHandle handle = _threadPoolBinding.Handle;
133139
NativeOverlapped* overlapped = Overlapped;
134140

135141
// If the handle is still valid, attempt to cancel the IO
136-
if (!handle.IsInvalid)
142+
if (!handle.IsInvalid && !Interop.mincore.CancelIoEx(handle, overlapped))
137143
{
138-
if (!Interop.mincore.CancelIoEx(handle, overlapped))
139-
{
140-
// This case should not have any consequences although
141-
// it will be easier to debug if there exists any special case
142-
// we are not aware of.
143-
int errorCode = Marshal.GetLastWin32Error();
144-
Debug.WriteLine("CancelIoEx finished with error code {0}.", errorCode);
145-
}
144+
// This case should not have any consequences although
145+
// it will be easier to debug if there exists any special case
146+
// we are not aware of.
147+
int errorCode = Marshal.GetLastWin32Error();
148+
Debug.WriteLine("CancelIoEx finished with error code {0}.", errorCode);
146149
}
147150
}
148151

@@ -154,7 +157,7 @@ private void CompleteCallback(int resultState)
154157

155158
if (resultState == ResultError)
156159
{
157-
if (ErrorCode == Interop.mincore.Errors.ERROR_OPERATION_ABORTED)
160+
if (_errorCode == Interop.mincore.Errors.ERROR_OPERATION_ABORTED)
158161
{
159162
if (_cancellationToken.CanBeCanceled && !_cancellationToken.IsCancellationRequested)
160163
{
@@ -169,7 +172,7 @@ private void CompleteCallback(int resultState)
169172
}
170173
else
171174
{
172-
HandleError();
175+
HandleError(_errorCode);
173176
}
174177
}
175178
else

src/System.IO.Pipes/src/System/IO/Pipes/PipeStream.Windows.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ private Task WriteAsyncCorePrivate(byte[] buffer, int offset, int count, Cancell
312312
}
313313
else
314314
{
315-
var completionSource = new PipeIOCompletionSource(this, buffer, cancellationToken, isWrite: true);
315+
var completionSource = new ReadWriteCompletionSource(this, buffer, cancellationToken, isWrite: true);
316316
int errorCode = 0;
317317

318318
// Queue an async WriteFile operation and pass in a packed overlapped
@@ -462,7 +462,7 @@ private Task<int> ReadAsyncCorePrivate(byte[] buffer, int offset, int count, Can
462462
}
463463
else
464464
{
465-
var completionSource = new PipeIOCompletionSource(this, buffer, cancellationToken, isWrite: false);
465+
var completionSource = new ReadWriteCompletionSource(this, buffer, cancellationToken, isWrite: false);
466466

467467
// Queue an async ReadFile operation and pass in a packed overlapped
468468
int errorCode = 0;

src/System.IO.Pipes/src/System/IO/Pipes/PipeIOCompletionSource.cs renamed to src/System.IO.Pipes/src/System/IO/Pipes/ReadWriteCompletionSource.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,22 @@
66

77
namespace System.IO.Pipes
88
{
9-
internal sealed class PipeIOCompletionSource : PipeCompletionSource<int>
9+
internal sealed class ReadWriteCompletionSource : PipeCompletionSource<int>
1010
{
1111
private readonly bool _isWrite;
1212
private readonly PipeStream _pipeStream;
1313

1414
private bool _isMessageComplete;
1515
private int _numBytes; // number of buffer read OR written
1616

17-
internal PipeIOCompletionSource(PipeStream stream, byte[] buffer, CancellationToken cancellationToken, bool isWrite)
17+
internal ReadWriteCompletionSource(PipeStream stream, byte[] buffer, CancellationToken cancellationToken, bool isWrite)
1818
: base(stream._threadPoolBinding, cancellationToken, pinData: buffer)
1919
{
2020
Debug.Assert(buffer != null, "buffer is null");
2121

2222
_pipeStream = stream;
2323
_isWrite = isWrite;
2424
_isMessageComplete = true;
25-
_numBytes = 0;
2625
}
2726

2827
internal override void SetCompletedSynchronously()
@@ -66,9 +65,9 @@ protected override void AsyncCallback(uint errorCode, uint numBytes)
6665
base.AsyncCallback(errorCode, numBytes);
6766
}
6867

69-
protected override void HandleError()
68+
protected override void HandleError(int errorCode)
7069
{
71-
TrySetException(_pipeStream.WinIOError(ErrorCode));
70+
TrySetException(_pipeStream.WinIOError(errorCode));
7271
}
7372
}
7473
}

0 commit comments

Comments
 (0)