ServerCallContext.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. #region Copyright notice and license
  2. // Copyright 2015 gRPC authors.
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. #endregion
  16. using System;
  17. using System.Threading;
  18. using System.Threading.Tasks;
  19. using Grpc.Core.Internal;
  20. namespace Grpc.Core
  21. {
  22. /// <summary>
  23. /// Context for a server-side call.
  24. /// </summary>
  25. public class ServerCallContext
  26. {
  27. private readonly CallSafeHandle callHandle;
  28. private readonly string method;
  29. private readonly string host;
  30. private readonly DateTime deadline;
  31. private readonly Metadata requestHeaders;
  32. private readonly CancellationToken cancellationToken;
  33. private readonly Metadata responseTrailers = new Metadata();
  34. private readonly Func<Metadata, Task> writeHeadersFunc;
  35. private readonly IHasWriteOptions writeOptionsHolder;
  36. private readonly Lazy<AuthContext> authContext;
  37. private readonly Func<string> testingOnlyPeerGetter;
  38. private readonly Func<AuthContext> testingOnlyAuthContextGetter;
  39. private readonly Func<ContextPropagationToken> testingOnlyContextPropagationTokenFactory;
  40. private Status status = Status.DefaultSuccess;
  41. internal ServerCallContext(CallSafeHandle callHandle, string method, string host, DateTime deadline, Metadata requestHeaders, CancellationToken cancellationToken,
  42. Func<Metadata, Task> writeHeadersFunc, IHasWriteOptions writeOptionsHolder)
  43. : this(callHandle, method, host, deadline, requestHeaders, cancellationToken, writeHeadersFunc, writeOptionsHolder, null, null, null)
  44. {
  45. }
  46. // Additional constructor params should be used for testing only
  47. internal ServerCallContext(CallSafeHandle callHandle, string method, string host, DateTime deadline, Metadata requestHeaders, CancellationToken cancellationToken,
  48. Func<Metadata, Task> writeHeadersFunc, IHasWriteOptions writeOptionsHolder,
  49. Func<string> testingOnlyPeerGetter, Func<AuthContext> testingOnlyAuthContextGetter, Func<ContextPropagationToken> testingOnlyContextPropagationTokenFactory)
  50. {
  51. this.callHandle = callHandle;
  52. this.method = method;
  53. this.host = host;
  54. this.deadline = deadline;
  55. this.requestHeaders = requestHeaders;
  56. this.cancellationToken = cancellationToken;
  57. this.writeHeadersFunc = writeHeadersFunc;
  58. this.writeOptionsHolder = writeOptionsHolder;
  59. this.authContext = new Lazy<AuthContext>(GetAuthContextEager);
  60. this.testingOnlyPeerGetter = testingOnlyPeerGetter;
  61. this.testingOnlyAuthContextGetter = testingOnlyAuthContextGetter;
  62. this.testingOnlyContextPropagationTokenFactory = testingOnlyContextPropagationTokenFactory;
  63. }
  64. /// <summary>
  65. /// Asynchronously sends response headers for the current call to the client. This method may only be invoked once for each call and needs to be invoked
  66. /// before any response messages are written. Writing the first response message implicitly sends empty response headers if <c>WriteResponseHeadersAsync</c> haven't
  67. /// been called yet.
  68. /// </summary>
  69. /// <param name="responseHeaders">The response headers to send.</param>
  70. /// <returns>The task that finished once response headers have been written.</returns>
  71. public Task WriteResponseHeadersAsync(Metadata responseHeaders)
  72. {
  73. return writeHeadersFunc(responseHeaders);
  74. }
  75. /// <summary>
  76. /// Creates a propagation token to be used to propagate call context to a child call.
  77. /// </summary>
  78. public ContextPropagationToken CreatePropagationToken(ContextPropagationOptions options = null)
  79. {
  80. if (testingOnlyContextPropagationTokenFactory != null)
  81. {
  82. return testingOnlyContextPropagationTokenFactory();
  83. }
  84. return new ContextPropagationToken(callHandle, deadline, cancellationToken, options);
  85. }
  86. /// <summary>Name of method called in this RPC.</summary>
  87. public string Method
  88. {
  89. get
  90. {
  91. return this.method;
  92. }
  93. }
  94. /// <summary>Name of host called in this RPC.</summary>
  95. public string Host
  96. {
  97. get
  98. {
  99. return this.host;
  100. }
  101. }
  102. /// <summary>Address of the remote endpoint in URI format.</summary>
  103. public string Peer
  104. {
  105. get
  106. {
  107. if (testingOnlyPeerGetter != null)
  108. {
  109. return testingOnlyPeerGetter();
  110. }
  111. // Getting the peer lazily is fine as the native call is guaranteed
  112. // not to be disposed before user-supplied server side handler returns.
  113. // Most users won't need to read this field anyway.
  114. return this.callHandle.GetPeer();
  115. }
  116. }
  117. /// <summary>Deadline for this RPC.</summary>
  118. public DateTime Deadline
  119. {
  120. get
  121. {
  122. return this.deadline;
  123. }
  124. }
  125. /// <summary>Initial metadata sent by client.</summary>
  126. public Metadata RequestHeaders
  127. {
  128. get
  129. {
  130. return this.requestHeaders;
  131. }
  132. }
  133. /// <summary>Cancellation token signals when call is cancelled.</summary>
  134. public CancellationToken CancellationToken
  135. {
  136. get
  137. {
  138. return this.cancellationToken;
  139. }
  140. }
  141. /// <summary>Trailers to send back to client after RPC finishes.</summary>
  142. public Metadata ResponseTrailers
  143. {
  144. get
  145. {
  146. return this.responseTrailers;
  147. }
  148. }
  149. /// <summary> Status to send back to client after RPC finishes.</summary>
  150. public Status Status
  151. {
  152. get
  153. {
  154. return this.status;
  155. }
  156. set
  157. {
  158. status = value;
  159. }
  160. }
  161. /// <summary>
  162. /// Allows setting write options for the following write.
  163. /// For streaming response calls, this property is also exposed as on IServerStreamWriter for convenience.
  164. /// Both properties are backed by the same underlying value.
  165. /// </summary>
  166. public WriteOptions WriteOptions
  167. {
  168. get
  169. {
  170. return writeOptionsHolder.WriteOptions;
  171. }
  172. set
  173. {
  174. writeOptionsHolder.WriteOptions = value;
  175. }
  176. }
  177. /// <summary>
  178. /// Gets the <c>AuthContext</c> associated with this call.
  179. /// Note: Access to AuthContext is an experimental API that can change without any prior notice.
  180. /// </summary>
  181. public AuthContext AuthContext
  182. {
  183. get
  184. {
  185. if (testingOnlyAuthContextGetter != null)
  186. {
  187. return testingOnlyAuthContextGetter();
  188. }
  189. return authContext.Value;
  190. }
  191. }
  192. private AuthContext GetAuthContextEager()
  193. {
  194. using (var authContextNative = callHandle.GetAuthContext())
  195. {
  196. return authContextNative.ToAuthContext();
  197. }
  198. }
  199. }
  200. /// <summary>
  201. /// Allows sharing write options between ServerCallContext and other objects.
  202. /// </summary>
  203. internal interface IHasWriteOptions
  204. {
  205. /// <summary>
  206. /// Gets or sets the write options.
  207. /// </summary>
  208. WriteOptions WriteOptions { get; set; }
  209. }
  210. }