OLD | NEW |
(Empty) | |
| 1 #region Copyright notice and license |
| 2 |
| 3 // Copyright 2015, Google Inc. |
| 4 // All rights reserved. |
| 5 // |
| 6 // Redistribution and use in source and binary forms, with or without |
| 7 // modification, are permitted provided that the following conditions are |
| 8 // met: |
| 9 // |
| 10 // * Redistributions of source code must retain the above copyright |
| 11 // notice, this list of conditions and the following disclaimer. |
| 12 // * Redistributions in binary form must reproduce the above |
| 13 // copyright notice, this list of conditions and the following disclaimer |
| 14 // in the documentation and/or other materials provided with the |
| 15 // distribution. |
| 16 // * Neither the name of Google Inc. nor the names of its |
| 17 // contributors may be used to endorse or promote products derived from |
| 18 // this software without specific prior written permission. |
| 19 // |
| 20 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 |
| 32 #endregion |
| 33 |
| 34 using System; |
| 35 using System.Collections.Generic; |
| 36 using System.Threading.Tasks; |
| 37 |
| 38 namespace Grpc.Core.Utils |
| 39 { |
| 40 /// <summary> |
| 41 /// Extension methods that simplify work with gRPC streaming calls. |
| 42 /// </summary> |
| 43 public static class AsyncStreamExtensions |
| 44 { |
| 45 /// <summary> |
| 46 /// Reads the entire stream and executes an async action for each elemen
t. |
| 47 /// </summary> |
| 48 public static async Task ForEachAsync<T>(this IAsyncStreamReader<T> stre
amReader, Func<T, Task> asyncAction) |
| 49 where T : class |
| 50 { |
| 51 while (await streamReader.MoveNext().ConfigureAwait(false)) |
| 52 { |
| 53 await asyncAction(streamReader.Current).ConfigureAwait(false); |
| 54 } |
| 55 } |
| 56 |
| 57 /// <summary> |
| 58 /// Reads the entire stream and creates a list containing all the elemen
ts read. |
| 59 /// </summary> |
| 60 public static async Task<List<T>> ToListAsync<T>(this IAsyncStreamReader
<T> streamReader) |
| 61 where T : class |
| 62 { |
| 63 var result = new List<T>(); |
| 64 while (await streamReader.MoveNext().ConfigureAwait(false)) |
| 65 { |
| 66 result.Add(streamReader.Current); |
| 67 } |
| 68 return result; |
| 69 } |
| 70 |
| 71 /// <summary> |
| 72 /// Writes all elements from given enumerable to the stream. |
| 73 /// Completes the stream afterwards unless close = false. |
| 74 /// </summary> |
| 75 public static async Task WriteAllAsync<T>(this IClientStreamWriter<T> st
reamWriter, IEnumerable<T> elements, bool complete = true) |
| 76 where T : class |
| 77 { |
| 78 foreach (var element in elements) |
| 79 { |
| 80 await streamWriter.WriteAsync(element).ConfigureAwait(false); |
| 81 } |
| 82 if (complete) |
| 83 { |
| 84 await streamWriter.CompleteAsync().ConfigureAwait(false); |
| 85 } |
| 86 } |
| 87 |
| 88 /// <summary> |
| 89 /// Writes all elements from given enumerable to the stream. |
| 90 /// </summary> |
| 91 public static async Task WriteAllAsync<T>(this IServerStreamWriter<T> st
reamWriter, IEnumerable<T> elements) |
| 92 where T : class |
| 93 { |
| 94 foreach (var element in elements) |
| 95 { |
| 96 await streamWriter.WriteAsync(element).ConfigureAwait(false); |
| 97 } |
| 98 } |
| 99 } |
| 100 } |
OLD | NEW |