|
1 | | -using System; |
2 | | -using System.IO; |
3 | | -using System.Net.Http; |
4 | | -using System.Text; |
5 | | -using System.Threading; |
6 | | -using System.Threading.Tasks; |
7 | | -using Newtonsoft.Json; |
8 | | - |
9 | | -namespace Docker.DotNet.Models |
10 | | -{ |
11 | | - internal static class StreamUtil |
12 | | - { |
13 | | - private static Newtonsoft.Json.JsonSerializer _serializer = new Newtonsoft.Json.JsonSerializer(); |
14 | | - |
15 | | - internal static async Task MonitorStreamAsync(Task<Stream> streamTask, DockerClient client, CancellationToken cancel, IProgress<string> progress) |
16 | | - { |
17 | | - using (var stream = await streamTask) |
18 | | - { |
19 | | - // ReadLineAsync must be cancelled by closing the whole stream. |
20 | | - using (cancel.Register(() => stream.Dispose())) |
21 | | - { |
22 | | - using (var reader = new StreamReader(stream, new UTF8Encoding(false))) |
23 | | - { |
24 | | - string line; |
25 | | - while ((line = await reader.ReadLineAsync()) != null) |
26 | | - { |
27 | | - progress.Report(line); |
28 | | - } |
29 | | - } |
30 | | - } |
31 | | - } |
32 | | - } |
33 | | - |
34 | | - internal static async Task MonitorStreamForMessagesAsync<T>(Task<Stream> streamTask, DockerClient client, CancellationToken cancel, IProgress<T> progress) |
35 | | - { |
36 | | - using (var stream = await streamTask) |
37 | | - using (var reader = new StreamReader(stream, new UTF8Encoding(false))) |
38 | | - using (var jsonReader = new JsonTextReader(reader) { SupportMultipleContent = true }) |
39 | | - { |
40 | | - while (await jsonReader.ReadAsync().WithCancellation(cancel)) |
41 | | - { |
42 | | - var ev = _serializer.Deserialize<T>(jsonReader); |
43 | | - progress?.Report(ev); |
44 | | - } |
45 | | - } |
46 | | - } |
47 | | - |
48 | | - internal static async Task MonitorResponseForMessagesAsync<T>(Task<HttpResponseMessage> responseTask, DockerClient client, CancellationToken cancel, IProgress<T> progress) |
49 | | - { |
50 | | - using (var response = await responseTask) |
51 | | - { |
52 | | - await client.HandleIfErrorResponseAsync(response.StatusCode, response); |
53 | | - |
54 | | - using (var stream = await response.Content.ReadAsStreamAsync()) |
55 | | - { |
56 | | - // ReadLineAsync must be cancelled by closing the whole stream. |
57 | | - using (cancel.Register(() => stream.Dispose())) |
58 | | - { |
59 | | - using (var reader = new StreamReader(stream, new UTF8Encoding(false))) |
60 | | - { |
61 | | - string line; |
62 | | - try |
63 | | - { |
64 | | - while ((line = await reader.ReadLineAsync()) != null) |
65 | | - { |
66 | | - var prog = client.JsonSerializer.DeserializeObject<T>(line); |
67 | | - if (prog == null) continue; |
68 | | - |
69 | | - progress.Report(prog); |
70 | | - } |
71 | | - } |
72 | | - catch (ObjectDisposedException) |
73 | | - { |
74 | | - // The subsequent call to reader.ReadLineAsync() after cancellation |
75 | | - // will fail because we disposed the stream. Just ignore here. |
76 | | - } |
77 | | - } |
78 | | - } |
79 | | - } |
80 | | - } |
81 | | - } |
82 | | - |
83 | | - private static async Task<T> WithCancellation<T>(this Task<T> task, CancellationToken cancellationToken) |
84 | | - { |
85 | | - var tcs = new TaskCompletionSource<bool>(); |
86 | | - using (cancellationToken.Register(s => ((TaskCompletionSource<bool>)s).TrySetResult(true), tcs)) |
87 | | - { |
88 | | - if (task != await Task.WhenAny(task, tcs.Task)) |
89 | | - { |
90 | | - throw new OperationCanceledException(cancellationToken); |
91 | | - } |
92 | | - } |
93 | | - |
94 | | - return await task; |
95 | | - } |
96 | | - } |
97 | | -} |
| 1 | +using System; |
| 2 | +using System.Diagnostics; |
| 3 | +using System.IO; |
| 4 | +using System.Net.Http; |
| 5 | +using System.Text; |
| 6 | +using System.Threading; |
| 7 | +using System.Threading.Tasks; |
| 8 | +using Newtonsoft.Json; |
| 9 | + |
| 10 | +namespace Docker.DotNet.Models |
| 11 | +{ |
| 12 | + internal static class StreamUtil |
| 13 | + { |
| 14 | + internal static async Task MonitorStreamAsync(Task<Stream> streamTask, DockerClient client, CancellationToken cancellationToken, IProgress<string> progress) |
| 15 | + { |
| 16 | + var tcs = new TaskCompletionSource<string>(); |
| 17 | + |
| 18 | + using (var stream = await streamTask) |
| 19 | + using (var reader = new StreamReader(stream, new UTF8Encoding(false))) |
| 20 | + using (cancellationToken.Register(() => tcs.TrySetCanceled(cancellationToken))) |
| 21 | + { |
| 22 | + string line; |
| 23 | + while ((line = await await Task.WhenAny(reader.ReadLineAsync(), tcs.Task)) != null) |
| 24 | + { |
| 25 | + progress.Report(line); |
| 26 | + } |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + internal static async Task MonitorStreamForMessagesAsync<T>(Task<Stream> streamTask, DockerClient client, CancellationToken cancellationToken, IProgress<T> progress) |
| 31 | + { |
| 32 | + var tcs = new TaskCompletionSource<bool>(); |
| 33 | + |
| 34 | + using (var stream = await streamTask) |
| 35 | + using (var reader = new StreamReader(stream, new UTF8Encoding(false))) |
| 36 | + using (var jsonReader = new JsonTextReader(reader) { SupportMultipleContent = true }) |
| 37 | + using (cancellationToken.Register(() => tcs.TrySetCanceled(cancellationToken))) |
| 38 | + { |
| 39 | + while (await await Task.WhenAny(jsonReader.ReadAsync(cancellationToken), tcs.Task)) |
| 40 | + { |
| 41 | + var ev = await client.JsonSerializer.Deserialize<T>(jsonReader, cancellationToken); |
| 42 | + progress.Report(ev); |
| 43 | + } |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + internal static async Task MonitorResponseForMessagesAsync<T>(Task<HttpResponseMessage> responseTask, DockerClient client, CancellationToken cancel, IProgress<T> progress) |
| 48 | + { |
| 49 | + using (var response = await responseTask) |
| 50 | + { |
| 51 | + await MonitorStreamForMessagesAsync<T>(response.Content.ReadAsStreamAsync(), client, cancel, progress); |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | +} |
0 commit comments