From e2d070fbf52b3ac9b28b099f79a16caacabfb903 Mon Sep 17 00:00:00 2001 From: Paul Betts Date: Thu, 19 Dec 2013 11:17:55 -0800 Subject: [PATCH 1/3] Use ThrowIfCancellationRequested --- src/ModernHttpClient.Android/OkHttpNetworkHandler.cs | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/src/ModernHttpClient.Android/OkHttpNetworkHandler.cs b/src/ModernHttpClient.Android/OkHttpNetworkHandler.cs index 18adde7..4f3033c 100644 --- a/src/ModernHttpClient.Android/OkHttpNetworkHandler.cs +++ b/src/ModernHttpClient.Android/OkHttpNetworkHandler.cs @@ -40,16 +40,12 @@ namespace ModernHttpClient } return await Task.Run (() => { - if (cancellationToken.IsCancellationRequested) { - throw new TaskCanceledException(); - } + cancellationToken.ThrowIfCancellationRequested(); // 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[] { () => rq.InputStream, @@ -77,9 +73,7 @@ namespace ModernHttpClient } } while (!ct.IsCancellationRequested && read > 0); - if (ct.IsCancellationRequested) { - throw new OperationCanceledException(); - } + ct.ThrowIfCancellationRequested(); }); } } From 49ca6756f23859c7b8a8fd3caca14162d8698fef Mon Sep 17 00:00:00 2001 From: Paul Betts Date: Thu, 19 Dec 2013 11:23:12 -0800 Subject: [PATCH 2/3] Cancel our tasks early if we get the chance --- .../OkHttpNetworkHandler.cs | 17 ++++------------- 1 file changed, 4 insertions(+), 13 deletions(-) diff --git a/src/ModernHttpClient.Android/OkHttpNetworkHandler.cs b/src/ModernHttpClient.Android/OkHttpNetworkHandler.cs index 4f3033c..0010350 100644 --- a/src/ModernHttpClient.Android/OkHttpNetworkHandler.cs +++ b/src/ModernHttpClient.Android/OkHttpNetworkHandler.cs @@ -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,14 +27,12 @@ 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 (() => { - cancellationToken.ThrowIfCancellationRequested(); - // NB: This is the line that blocks until we have headers var ret = new HttpResponseMessage((HttpStatusCode)rq.ResponseCode); @@ -56,7 +47,7 @@ namespace ModernHttpClient ret.RequestMessage = request; return ret; - }); + }, cancellationToken).ConfigureAwait(false); } async Task copyToAsync(Stream source, Stream target, CancellationToken ct) @@ -74,7 +65,7 @@ namespace ModernHttpClient } while (!ct.IsCancellationRequested && read > 0); ct.ThrowIfCancellationRequested(); - }); + }, ct); } } } From 1fe664d04aadbcc0c257de990fe7463bc26a9df5 Mon Sep 17 00:00:00 2001 From: Paul Betts Date: Thu, 19 Dec 2013 11:31:24 -0800 Subject: [PATCH 3/3] WHY IS THIS NOT THE DEFAULT OMFG --- src/ModernHttpClient.Android/OkHttpNetworkHandler.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ModernHttpClient.Android/OkHttpNetworkHandler.cs b/src/ModernHttpClient.Android/OkHttpNetworkHandler.cs index 0010350..8f65acb 100644 --- a/src/ModernHttpClient.Android/OkHttpNetworkHandler.cs +++ b/src/ModernHttpClient.Android/OkHttpNetworkHandler.cs @@ -65,7 +65,7 @@ namespace ModernHttpClient } while (!ct.IsCancellationRequested && read > 0); ct.ThrowIfCancellationRequested(); - }, ct); + }, ct).ConfigureAwait(false); } } }