Index: third_party/grpc/src/csharp/Grpc.Core/Internal/ClientResponseStream.cs |
diff --git a/third_party/protobuf/csharp/src/Google.Protobuf/ByteArray.cs b/third_party/grpc/src/csharp/Grpc.Core/Internal/ClientResponseStream.cs |
similarity index 52% |
copy from third_party/protobuf/csharp/src/Google.Protobuf/ByteArray.cs |
copy to third_party/grpc/src/csharp/Grpc.Core/Internal/ClientResponseStream.cs |
index b19962794b0d052b467fd2007fd09ad1d7fe6d54..d6e34a0f04431602230284f2185d002ed9d65db4 100644 |
--- a/third_party/protobuf/csharp/src/Google.Protobuf/ByteArray.cs |
+++ b/third_party/grpc/src/csharp/Grpc.Core/Internal/ClientResponseStream.cs |
@@ -1,7 +1,7 @@ |
-#region Copyright notice and license |
-// Protocol Buffers - Google's data interchange format |
-// Copyright 2008 Google Inc. All rights reserved. |
-// https://developers.google.com/protocol-buffers/ |
+#region Copyright notice and license |
+ |
+// Copyright 2015, Google Inc. |
+// All rights reserved. |
// |
// Redistribution and use in source and binary forms, with or without |
// modification, are permitted provided that the following conditions are |
@@ -28,52 +28,62 @@ |
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
+ |
#endregion |
using System; |
+using System.Collections.Generic; |
+using System.Threading; |
+using System.Threading.Tasks; |
-namespace Google.Protobuf |
+namespace Grpc.Core.Internal |
{ |
- /// <summary> |
- /// Provides a utility routine to copy small arrays much more quickly than Buffer.BlockCopy |
- /// </summary> |
- internal static class ByteArray |
+ internal class ClientResponseStream<TRequest, TResponse> : IAsyncStreamReader<TResponse> |
+ where TRequest : class |
+ where TResponse : class |
{ |
- /// <summary> |
- /// The threshold above which you should use Buffer.BlockCopy rather than ByteArray.Copy |
- /// </summary> |
- private const int CopyThreshold = 12; |
+ readonly AsyncCall<TRequest, TResponse> call; |
+ TResponse current; |
- /// <summary> |
- /// Determines which copy routine to use based on the number of bytes to be copied. |
- /// </summary> |
- internal static void Copy(byte[] src, int srcOffset, byte[] dst, int dstOffset, int count) |
+ public ClientResponseStream(AsyncCall<TRequest, TResponse> call) |
{ |
- if (count > CopyThreshold) |
- { |
- Buffer.BlockCopy(src, srcOffset, dst, dstOffset, count); |
- } |
- else |
+ this.call = call; |
+ } |
+ |
+ public TResponse Current |
+ { |
+ get |
{ |
- int stop = srcOffset + count; |
- for (int i = srcOffset; i < stop; i++) |
+ if (current == null) |
{ |
- dst[dstOffset++] = src[i]; |
+ throw new InvalidOperationException("No current element is available."); |
} |
+ return current; |
} |
} |
- /// <summary> |
- /// Reverses the order of bytes in the array |
- /// </summary> |
- internal static void Reverse(byte[] bytes) |
+ public async Task<bool> MoveNext(CancellationToken token) |
{ |
- for (int first = 0, last = bytes.Length - 1; first < last; first++, last--) |
+ if (token != CancellationToken.None) |
{ |
- byte temp = bytes[first]; |
- bytes[first] = bytes[last]; |
- bytes[last] = temp; |
+ throw new InvalidOperationException("Cancellation of individual reads is not supported."); |
} |
+ var taskSource = new AsyncCompletionTaskSource<TResponse>(); |
+ call.StartReadMessage(taskSource.CompletionDelegate); |
+ var result = await taskSource.Task.ConfigureAwait(false); |
+ this.current = result; |
+ |
+ if (result == null) |
+ { |
+ await call.StreamingCallFinishedTask.ConfigureAwait(false); |
+ return false; |
+ } |
+ return true; |
+ } |
+ |
+ public void Dispose() |
+ { |
+ // TODO(jtattermusch): implement the semantics of stream disposal. |
} |
} |
-} |
+} |