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.Runtime.CompilerServices; |
| 36 using System.Threading.Tasks; |
| 37 |
| 38 namespace Grpc.Core |
| 39 { |
| 40 /// <summary> |
| 41 /// Return type for single request - single response call. |
| 42 /// </summary> |
| 43 /// <typeparam name="TResponse">Response message type for this call.</typepa
ram> |
| 44 public sealed class AsyncUnaryCall<TResponse> : IDisposable |
| 45 { |
| 46 readonly Task<TResponse> responseAsync; |
| 47 readonly Task<Metadata> responseHeadersAsync; |
| 48 readonly Func<Status> getStatusFunc; |
| 49 readonly Func<Metadata> getTrailersFunc; |
| 50 readonly Action disposeAction; |
| 51 |
| 52 internal AsyncUnaryCall(Task<TResponse> responseAsync, Task<Metadata> re
sponseHeadersAsync, Func<Status> getStatusFunc, Func<Metadata> getTrailersFunc,
Action disposeAction) |
| 53 { |
| 54 this.responseAsync = responseAsync; |
| 55 this.responseHeadersAsync = responseHeadersAsync; |
| 56 this.getStatusFunc = getStatusFunc; |
| 57 this.getTrailersFunc = getTrailersFunc; |
| 58 this.disposeAction = disposeAction; |
| 59 } |
| 60 |
| 61 /// <summary> |
| 62 /// Asynchronous call result. |
| 63 /// </summary> |
| 64 public Task<TResponse> ResponseAsync |
| 65 { |
| 66 get |
| 67 { |
| 68 return this.responseAsync; |
| 69 } |
| 70 } |
| 71 |
| 72 /// <summary> |
| 73 /// Asynchronous access to response headers. |
| 74 /// </summary> |
| 75 public Task<Metadata> ResponseHeadersAsync |
| 76 { |
| 77 get |
| 78 { |
| 79 return this.responseHeadersAsync; |
| 80 } |
| 81 } |
| 82 |
| 83 /// <summary> |
| 84 /// Allows awaiting this object directly. |
| 85 /// </summary> |
| 86 public TaskAwaiter<TResponse> GetAwaiter() |
| 87 { |
| 88 return responseAsync.GetAwaiter(); |
| 89 } |
| 90 |
| 91 /// <summary> |
| 92 /// Gets the call status if the call has already finished. |
| 93 /// Throws InvalidOperationException otherwise. |
| 94 /// </summary> |
| 95 public Status GetStatus() |
| 96 { |
| 97 return getStatusFunc(); |
| 98 } |
| 99 |
| 100 /// <summary> |
| 101 /// Gets the call trailing metadata if the call has already finished. |
| 102 /// Throws InvalidOperationException otherwise. |
| 103 /// </summary> |
| 104 public Metadata GetTrailers() |
| 105 { |
| 106 return getTrailersFunc(); |
| 107 } |
| 108 |
| 109 /// <summary> |
| 110 /// Provides means to cleanup after the call. |
| 111 /// If the call has already finished normally (request stream has been c
ompleted and call result has been received), doesn't do anything. |
| 112 /// Otherwise, requests cancellation of the call which should terminate
all pending async operations associated with the call. |
| 113 /// As a result, all resources being used by the call should be released
eventually. |
| 114 /// </summary> |
| 115 public void Dispose() |
| 116 { |
| 117 disposeAction.Invoke(); |
| 118 } |
| 119 } |
| 120 } |
OLD | NEW |