Merge pull request #28 from paulcbetts/clean-up-cancellation
Clean up cancellation
This commit is contained in:
Коммит
8f729d8135
|
@ -3,17 +3,10 @@ using System.Threading;
|
|||
using System.Threading.Tasks;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using Android.App;
|
||||
using Android.Content;
|
||||
using Android.OS;
|
||||
using Android.Runtime;
|
||||
using Android.Views;
|
||||
using Android.Widget;
|
||||
using System.IO;
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
using OkHttp;
|
||||
using System.Net;
|
||||
|
||||
namespace ModernHttpClient
|
||||
{
|
||||
|
@ -34,22 +27,16 @@ namespace ModernHttpClient
|
|||
await Task.Run(async () => {
|
||||
var contentStream = await request.Content.ReadAsStreamAsync().ConfigureAwait(false);
|
||||
await copyToAsync(contentStream, rq.OutputStream, cancellationToken).ConfigureAwait(false);
|
||||
}).ConfigureAwait(false);
|
||||
}, cancellationToken).ConfigureAwait(false);
|
||||
|
||||
rq.OutputStream.Close();
|
||||
}
|
||||
|
||||
return await Task.Run (() => {
|
||||
if (cancellationToken.IsCancellationRequested) {
|
||||
throw new TaskCanceledException();
|
||||
}
|
||||
|
||||
// NB: This is the line that blocks until we have headers
|
||||
var ret = new HttpResponseMessage((HttpStatusCode)rq.ResponseCode);
|
||||
|
||||
if (cancellationToken.IsCancellationRequested) {
|
||||
throw new TaskCanceledException();
|
||||
}
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
|
||||
ret.Content = new StreamContent(new ConcatenatingStream(new Func<Stream>[] {
|
||||
() => rq.InputStream,
|
||||
|
@ -60,7 +47,7 @@ namespace ModernHttpClient
|
|||
|
||||
ret.RequestMessage = request;
|
||||
return ret;
|
||||
});
|
||||
}, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
async Task copyToAsync(Stream source, Stream target, CancellationToken ct)
|
||||
|
@ -77,10 +64,8 @@ namespace ModernHttpClient
|
|||
}
|
||||
} while (!ct.IsCancellationRequested && read > 0);
|
||||
|
||||
if (ct.IsCancellationRequested) {
|
||||
throw new OperationCanceledException();
|
||||
}
|
||||
});
|
||||
ct.ThrowIfCancellationRequested();
|
||||
}, ct).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Загрузка…
Ссылка в новой задаче