Ensure Begin/End Read/Write methods are correctly implemented

- Add Begin/End Write methods to FrameResponseStream
- Fix state check of Begin/End Read methods in FrameRequestStream
This commit is contained in:
Stephen Halter 2016-07-28 16:15:12 -07:00
parent 4337d2a4a7
commit 5aee524768
2 changed files with 42 additions and 6 deletions

View File

@ -76,8 +76,6 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Http
#if NET451
public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback callback, object state)
{
ValidateState(default(CancellationToken));
var task = ReadAsync(buffer, offset, count, default(CancellationToken), state);
if (callback != null)
{
@ -93,11 +91,9 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Http
private Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken, object state)
{
ValidateState(cancellationToken);
var tcs = new TaskCompletionSource<int>(state);
var task = _body.ReadAsync(new ArraySegment<byte>(buffer, offset, count), cancellationToken);
task.AsTask().ContinueWith((task2, state2) =>
var task = ReadAsync(buffer, offset, count, cancellationToken);
task.ContinueWith((task2, state2) =>
{
var tcs2 = (TaskCompletionSource<int>)state2;
if (task2.IsCanceled)

View File

@ -85,6 +85,46 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Internal.Http
_frameControl.Write(new ArraySegment<byte>(buffer, offset, count));
}
#if NET451
public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback callback, object state)
{
var task = WriteAsync(buffer, offset, count, default(CancellationToken), state);
if (callback != null)
{
task.ContinueWith(t => callback.Invoke(t));
}
return task;
}
public override void EndWrite(IAsyncResult asyncResult)
{
((Task<object>)asyncResult).GetAwaiter().GetResult();
}
private Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken, object state)
{
var tcs = new TaskCompletionSource<object>(state);
var task = WriteAsync(buffer, offset, count, cancellationToken);
task.ContinueWith((task2, state2) =>
{
var tcs2 = (TaskCompletionSource<object>)state2;
if (task2.IsCanceled)
{
tcs2.SetCanceled();
}
else if (task2.IsFaulted)
{
tcs2.SetException(task2.Exception);
}
else
{
tcs2.SetResult(null);
}
}, tcs, cancellationToken);
return tcs.Task;
}
#endif
public override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
{
var task = ValidateState(cancellationToken);