@@ -13,9 +13,10 @@ internal unsafe class AsyncAcceptContext : IValueTaskSource<RequestContext>, IDi
13
13
{
14
14
private static readonly IOCompletionCallback IOCallback = IOWaitCallback ;
15
15
private readonly PreAllocatedOverlapped _preallocatedOverlapped ;
16
+ private NativeOverlapped * _overlapped ;
16
17
17
18
// mutable struct; do not make this readonly
18
- private ManualResetValueTaskSourceCore < RequestContext > _tcs = new ( )
19
+ private ManualResetValueTaskSourceCore < RequestContext > _mrvts = new ( )
19
20
{
20
21
// We want to run continuations on the IO threads
21
22
RunContinuationsAsynchronously = false
@@ -33,7 +34,7 @@ internal AsyncAcceptContext(HttpSysListener server)
33
34
34
35
internal ValueTask < RequestContext > AcceptAsync ( )
35
36
{
36
- _tcs . Reset ( ) ;
37
+ _mrvts . Reset ( ) ;
37
38
38
39
AllocateNativeRequest ( ) ;
39
40
@@ -46,7 +47,7 @@ internal ValueTask<RequestContext> AcceptAsync()
46
47
return ValueTask . FromException < RequestContext > ( new HttpSysException ( ( int ) statusCode ) ) ;
47
48
}
48
49
49
- return new ValueTask < RequestContext > ( this , _tcs . Version ) ;
50
+ return new ValueTask < RequestContext > ( this , _mrvts . Version ) ;
50
51
}
51
52
52
53
private static void IOCompleted ( AsyncAcceptContext asyncContext , uint errorCode , uint numBytes )
@@ -58,7 +59,7 @@ private static void IOCompleted(AsyncAcceptContext asyncContext, uint errorCode,
58
59
if ( errorCode != UnsafeNclNativeMethods . ErrorCodes . ERROR_SUCCESS &&
59
60
errorCode != UnsafeNclNativeMethods . ErrorCodes . ERROR_MORE_DATA )
60
61
{
61
- asyncContext . _tcs . SetException ( new HttpSysException ( ( int ) errorCode ) ) ;
62
+ asyncContext . _mrvts . SetException ( new HttpSysException ( ( int ) errorCode ) ) ;
62
63
return ;
63
64
}
64
65
@@ -78,14 +79,14 @@ private static void IOCompleted(AsyncAcceptContext asyncContext, uint errorCode,
78
79
asyncContext . _nativeRequestContext = null ;
79
80
80
81
var requestContext = new RequestContext ( server , nativeContext ) ;
81
- asyncContext . _tcs . SetResult ( requestContext ) ;
82
+ asyncContext . _mrvts . SetResult ( requestContext ) ;
82
83
83
84
complete = true ;
84
85
}
85
86
}
86
87
catch ( Exception ex )
87
88
{
88
- asyncContext . _tcs . SetException ( ex ) ;
89
+ asyncContext . _mrvts . SetException ( ex ) ;
89
90
}
90
91
finally
91
92
{
@@ -111,13 +112,13 @@ private static void IOCompleted(AsyncAcceptContext asyncContext, uint errorCode,
111
112
{
112
113
// someother bad error, possible(?) return values are:
113
114
// ERROR_INVALID_HANDLE, ERROR_INSUFFICIENT_BUFFER, ERROR_OPERATION_ABORTED
114
- asyncContext . _tcs . SetException ( new HttpSysException ( ( int ) statusCode ) ) ;
115
+ asyncContext . _mrvts . SetException ( new HttpSysException ( ( int ) statusCode ) ) ;
115
116
}
116
117
}
117
118
}
118
119
catch ( Exception exception )
119
120
{
120
- asyncContext . _tcs . SetException ( exception ) ;
121
+ asyncContext . _mrvts . SetException ( exception ) ;
121
122
}
122
123
}
123
124
@@ -144,7 +145,7 @@ private uint QueueBeginGetContext()
144
145
_nativeRequestContext . NativeRequest ,
145
146
_nativeRequestContext . Size ,
146
147
& bytesTransferred ,
147
- _nativeRequestContext . NativeOverlapped ) ;
148
+ _overlapped ) ;
148
149
149
150
if ( statusCode == UnsafeNclNativeMethods . ErrorCodes . ERROR_INVALID_PARAMETER && _nativeRequestContext . RequestId != 0 )
150
151
{
@@ -179,10 +180,14 @@ private void AllocateNativeRequest(uint? size = null, ulong requestId = 0)
179
180
_nativeRequestContext ? . Dispose ( ) ;
180
181
181
182
var boundHandle = Server . RequestQueue . BoundHandle ;
182
- var nativeOverlapped = new SafeNativeOverlapped ( boundHandle ,
183
- boundHandle . AllocateNativeOverlapped ( _preallocatedOverlapped ) ) ;
184
183
185
- _nativeRequestContext = new NativeRequestContext ( nativeOverlapped , Server . MemoryPool , size , requestId ) ;
184
+ if ( _overlapped != null )
185
+ {
186
+ boundHandle . FreeNativeOverlapped ( _overlapped ) ;
187
+ }
188
+
189
+ _nativeRequestContext = new NativeRequestContext ( Server . MemoryPool , size , requestId ) ;
190
+ _overlapped = boundHandle . AllocateNativeOverlapped ( _preallocatedOverlapped ) ;
186
191
}
187
192
188
193
public void Dispose ( )
@@ -199,23 +204,31 @@ protected virtual void Dispose(bool disposing)
199
204
_nativeRequestContext . ReleasePins ( ) ;
200
205
_nativeRequestContext . Dispose ( ) ;
201
206
_nativeRequestContext = null ;
207
+
208
+ var boundHandle = Server . RequestQueue . BoundHandle ;
209
+
210
+ if ( _overlapped != null )
211
+ {
212
+ boundHandle . FreeNativeOverlapped ( _overlapped ) ;
213
+ _overlapped = null ;
214
+ }
202
215
}
203
216
}
204
217
}
205
218
206
219
public RequestContext GetResult ( short token )
207
220
{
208
- return _tcs . GetResult ( token ) ;
221
+ return _mrvts . GetResult ( token ) ;
209
222
}
210
223
211
224
public ValueTaskSourceStatus GetStatus ( short token )
212
225
{
213
- return _tcs . GetStatus ( token ) ;
226
+ return _mrvts . GetStatus ( token ) ;
214
227
}
215
228
216
229
public void OnCompleted ( Action < object > continuation , object state , short token , ValueTaskSourceOnCompletedFlags flags )
217
230
{
218
- _tcs . OnCompleted ( continuation , state , token , flags ) ;
231
+ _mrvts . OnCompleted ( continuation , state , token , flags ) ;
219
232
}
220
233
}
221
234
}
0 commit comments