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