| Index: third_party/grpc/src/csharp/Grpc.Core/Internal/AsyncCompletion.cs
|
| diff --git a/third_party/protobuf/csharp/src/Google.Protobuf.JsonDump/Program.cs b/third_party/grpc/src/csharp/Grpc.Core/Internal/AsyncCompletion.cs
|
| similarity index 54%
|
| copy from third_party/protobuf/csharp/src/Google.Protobuf.JsonDump/Program.cs
|
| copy to third_party/grpc/src/csharp/Grpc.Core/Internal/AsyncCompletion.cs
|
| index e8a6073e2bcb6b6f47dafb1f67f31de9b4a8671a..d5bbf676ff74027367a4344ee56efb81664eebf1 100644
|
| --- a/third_party/protobuf/csharp/src/Google.Protobuf.JsonDump/Program.cs
|
| +++ b/third_party/grpc/src/csharp/Grpc.Core/Internal/AsyncCompletion.cs
|
| @@ -1,12 +1,12 @@
|
| #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/
|
| -//
|
| +
|
| +// Copyright 2015-2016, 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
|
| // met:
|
| -//
|
| +//
|
| // * Redistributions of source code must retain the above copyright
|
| // notice, this list of conditions and the following disclaimer.
|
| // * Redistributions in binary form must reproduce the above
|
| @@ -16,7 +16,7 @@
|
| // * Neither the name of Google Inc. nor the names of its
|
| // contributors may be used to endorse or promote products derived from
|
| // this software without specific prior written permission.
|
| -//
|
| +//
|
| // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
| // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
| // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
| @@ -28,45 +28,67 @@
|
| // 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.IO;
|
| +using System.Diagnostics;
|
| +using System.Runtime.CompilerServices;
|
| +using System.Runtime.InteropServices;
|
| +using System.Threading;
|
| +using System.Threading.Tasks;
|
| +using Grpc.Core.Internal;
|
| +using Grpc.Core.Utils;
|
|
|
| -namespace Google.Protobuf.ProtoDump
|
| +namespace Grpc.Core.Internal
|
| {
|
| /// <summary>
|
| - /// Small utility to load a binary message and dump it in JSON format.
|
| + /// If error != null, there's been an error or operation has been cancelled.
|
| + /// </summary>
|
| + internal delegate void AsyncCompletionDelegate<T>(T result, Exception error);
|
| +
|
| + /// <summary>
|
| + /// Helper for transforming AsyncCompletionDelegate into full-fledged Task.
|
| /// </summary>
|
| - internal class Program
|
| + internal class AsyncCompletionTaskSource<T>
|
| {
|
| - private static int Main(string[] args)
|
| + readonly TaskCompletionSource<T> tcs = new TaskCompletionSource<T>();
|
| + readonly AsyncCompletionDelegate<T> completionDelegate;
|
| +
|
| + public AsyncCompletionTaskSource()
|
| {
|
| - if (args.Length != 2)
|
| + completionDelegate = new AsyncCompletionDelegate<T>(HandleCompletion);
|
| + }
|
| +
|
| + public Task<T> Task
|
| + {
|
| + get
|
| {
|
| - Console.Error.WriteLine("Usage: Google.Protobuf.JsonDump <descriptor type name> <input data>");
|
| - Console.Error.WriteLine("The descriptor type name is the fully-qualified message name,");
|
| - Console.Error.WriteLine("including assembly e.g. ProjectNamespace.Message,Company.Project");
|
| - return 1;
|
| + return tcs.Task;
|
| }
|
| - Type type = Type.GetType(args[0]);
|
| - if (type == null)
|
| + }
|
| +
|
| + public AsyncCompletionDelegate<T> CompletionDelegate
|
| + {
|
| + get
|
| {
|
| - Console.Error.WriteLine("Unable to load type {0}.", args[0]);
|
| - return 1;
|
| + return completionDelegate;
|
| }
|
| - if (!typeof(IMessage).IsAssignableFrom(type))
|
| + }
|
| +
|
| + private void HandleCompletion(T value, Exception error)
|
| + {
|
| + if (error == null)
|
| {
|
| - Console.Error.WriteLine("Type {0} doesn't implement IMessage.", args[0]);
|
| - return 1;
|
| + tcs.SetResult(value);
|
| + return;
|
| }
|
| - IMessage message = (IMessage) Activator.CreateInstance(type);
|
| - using (var input = File.OpenRead(args[1]))
|
| + if (error is OperationCanceledException)
|
| {
|
| - message.MergeFrom(input);
|
| + tcs.SetCanceled();
|
| + return;
|
| }
|
| - Console.WriteLine(message);
|
| - return 0;
|
| + tcs.SetException(error);
|
| }
|
| }
|
| -}
|
| +}
|
|
|