Merge pull request #28 from paulcbetts/clean-up-cancellation

Clean up cancellation
This commit is contained in:
Paul Betts 2013-12-19 12:14:25 -08:00
Родитель aa180ed031 1fe664d04a
Коммит 8f729d8135
1 изменённых файлов: 6 добавлений и 21 удалений

Просмотреть файл

@ -3,17 +3,10 @@ using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; 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.IO;
using System.Net;
using System.Net.Http; using System.Net.Http;
using OkHttp; using OkHttp;
using System.Net;
namespace ModernHttpClient namespace ModernHttpClient
{ {
@ -34,22 +27,16 @@ namespace ModernHttpClient
await Task.Run(async () => { await Task.Run(async () => {
var contentStream = await request.Content.ReadAsStreamAsync().ConfigureAwait(false); var contentStream = await request.Content.ReadAsStreamAsync().ConfigureAwait(false);
await copyToAsync(contentStream, rq.OutputStream, cancellationToken).ConfigureAwait(false); await copyToAsync(contentStream, rq.OutputStream, cancellationToken).ConfigureAwait(false);
}).ConfigureAwait(false); }, cancellationToken).ConfigureAwait(false);
rq.OutputStream.Close(); rq.OutputStream.Close();
} }
return await Task.Run (() => { return await Task.Run (() => {
if (cancellationToken.IsCancellationRequested) {
throw new TaskCanceledException();
}
// NB: This is the line that blocks until we have headers // NB: This is the line that blocks until we have headers
var ret = new HttpResponseMessage((HttpStatusCode)rq.ResponseCode); var ret = new HttpResponseMessage((HttpStatusCode)rq.ResponseCode);
if (cancellationToken.IsCancellationRequested) { cancellationToken.ThrowIfCancellationRequested();
throw new TaskCanceledException();
}
ret.Content = new StreamContent(new ConcatenatingStream(new Func<Stream>[] { ret.Content = new StreamContent(new ConcatenatingStream(new Func<Stream>[] {
() => rq.InputStream, () => rq.InputStream,
@ -60,7 +47,7 @@ namespace ModernHttpClient
ret.RequestMessage = request; ret.RequestMessage = request;
return ret; return ret;
}); }, cancellationToken).ConfigureAwait(false);
} }
async Task copyToAsync(Stream source, Stream target, CancellationToken ct) async Task copyToAsync(Stream source, Stream target, CancellationToken ct)
@ -77,10 +64,8 @@ namespace ModernHttpClient
} }
} while (!ct.IsCancellationRequested && read > 0); } while (!ct.IsCancellationRequested && read > 0);
if (ct.IsCancellationRequested) { ct.ThrowIfCancellationRequested();
throw new OperationCanceledException(); }, ct).ConfigureAwait(false);
}
});
} }
} }
} }