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; |
| 37 using System.Threading.Tasks; |
| 38 |
| 39 using Grpc.Core.Internal; |
| 40 |
| 41 namespace Grpc.Core |
| 42 { |
| 43 /// <summary> |
| 44 /// Context for a server-side call. |
| 45 /// </summary> |
| 46 public class ServerCallContext |
| 47 { |
| 48 private readonly CallSafeHandle callHandle; |
| 49 private readonly string method; |
| 50 private readonly string host; |
| 51 private readonly string peer; |
| 52 private readonly DateTime deadline; |
| 53 private readonly Metadata requestHeaders; |
| 54 private readonly CancellationToken cancellationToken; |
| 55 private readonly Metadata responseTrailers = new Metadata(); |
| 56 |
| 57 private Status status = Status.DefaultSuccess; |
| 58 private Func<Metadata, Task> writeHeadersFunc; |
| 59 private IHasWriteOptions writeOptionsHolder; |
| 60 |
| 61 internal ServerCallContext(CallSafeHandle callHandle, string method, str
ing host, string peer, DateTime deadline, Metadata requestHeaders, CancellationT
oken cancellationToken, |
| 62 Func<Metadata, Task> writeHeadersFunc, IHasWriteOptions writeOptions
Holder) |
| 63 { |
| 64 this.callHandle = callHandle; |
| 65 this.method = method; |
| 66 this.host = host; |
| 67 this.peer = peer; |
| 68 this.deadline = deadline; |
| 69 this.requestHeaders = requestHeaders; |
| 70 this.cancellationToken = cancellationToken; |
| 71 this.writeHeadersFunc = writeHeadersFunc; |
| 72 this.writeOptionsHolder = writeOptionsHolder; |
| 73 } |
| 74 |
| 75 /// <summary> |
| 76 /// Asynchronously sends response headers for the current call to the cl
ient. This method may only be invoked once for each call and needs to be invoked |
| 77 /// before any response messages are written. Writing the first response
message implicitly sends empty response headers if <c>WriteResponseHeadersAsync
</c> haven't |
| 78 /// been called yet. |
| 79 /// </summary> |
| 80 /// <param name="responseHeaders">The response headers to send.</param> |
| 81 /// <returns>The task that finished once response headers have been writ
ten.</returns> |
| 82 public Task WriteResponseHeadersAsync(Metadata responseHeaders) |
| 83 { |
| 84 return writeHeadersFunc(responseHeaders); |
| 85 } |
| 86 |
| 87 /// <summary> |
| 88 /// Creates a propagation token to be used to propagate call context to
a child call. |
| 89 /// </summary> |
| 90 public ContextPropagationToken CreatePropagationToken(ContextPropagation
Options options = null) |
| 91 { |
| 92 return new ContextPropagationToken(callHandle, deadline, cancellatio
nToken, options); |
| 93 } |
| 94 |
| 95 /// <summary>Name of method called in this RPC.</summary> |
| 96 public string Method |
| 97 { |
| 98 get |
| 99 { |
| 100 return this.method; |
| 101 } |
| 102 } |
| 103 |
| 104 /// <summary>Name of host called in this RPC.</summary> |
| 105 public string Host |
| 106 { |
| 107 get |
| 108 { |
| 109 return this.host; |
| 110 } |
| 111 } |
| 112 |
| 113 /// <summary>Address of the remote endpoint in URI format.</summary> |
| 114 public string Peer |
| 115 { |
| 116 get |
| 117 { |
| 118 return this.peer; |
| 119 } |
| 120 } |
| 121 |
| 122 /// <summary>Deadline for this RPC.</summary> |
| 123 public DateTime Deadline |
| 124 { |
| 125 get |
| 126 { |
| 127 return this.deadline; |
| 128 } |
| 129 } |
| 130 |
| 131 /// <summary>Initial metadata sent by client.</summary> |
| 132 public Metadata RequestHeaders |
| 133 { |
| 134 get |
| 135 { |
| 136 return this.requestHeaders; |
| 137 } |
| 138 } |
| 139 |
| 140 /// <summary>Cancellation token signals when call is cancelled.</summary
> |
| 141 public CancellationToken CancellationToken |
| 142 { |
| 143 get |
| 144 { |
| 145 return this.cancellationToken; |
| 146 } |
| 147 } |
| 148 |
| 149 /// <summary>Trailers to send back to client after RPC finishes.</summar
y> |
| 150 public Metadata ResponseTrailers |
| 151 { |
| 152 get |
| 153 { |
| 154 return this.responseTrailers; |
| 155 } |
| 156 } |
| 157 |
| 158 /// <summary> Status to send back to client after RPC finishes.</summary
> |
| 159 public Status Status |
| 160 { |
| 161 get |
| 162 { |
| 163 return this.status; |
| 164 } |
| 165 |
| 166 set |
| 167 { |
| 168 status = value; |
| 169 } |
| 170 } |
| 171 |
| 172 /// <summary> |
| 173 /// Allows setting write options for the following write. |
| 174 /// For streaming response calls, this property is also exposed as on IS
erverStreamWriter for convenience. |
| 175 /// Both properties are backed by the same underlying value. |
| 176 /// </summary> |
| 177 public WriteOptions WriteOptions |
| 178 { |
| 179 get |
| 180 { |
| 181 return writeOptionsHolder.WriteOptions; |
| 182 } |
| 183 |
| 184 set |
| 185 { |
| 186 writeOptionsHolder.WriteOptions = value; |
| 187 } |
| 188 } |
| 189 } |
| 190 |
| 191 /// <summary> |
| 192 /// Allows sharing write options between ServerCallContext and other objects
. |
| 193 /// </summary> |
| 194 public interface IHasWriteOptions |
| 195 { |
| 196 /// <summary> |
| 197 /// Gets or sets the write options. |
| 198 /// </summary> |
| 199 WriteOptions WriteOptions { get; set; } |
| 200 } |
| 201 } |
OLD | NEW |