ServerCallContext.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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 Status status = Status.DefaultSuccess;
  35. private Func<Metadata, Task> writeHeadersFunc;
  36. private IHasWriteOptions writeOptionsHolder;
  37. private Lazy<AuthContext> authContext;
  38. internal ServerCallContext(CallSafeHandle callHandle, string method, string host, DateTime deadline, Metadata requestHeaders, CancellationToken cancellationToken,
  39. Func<Metadata, Task> writeHeadersFunc, IHasWriteOptions writeOptionsHolder)
  40. {
  41. this.callHandle = callHandle;
  42. this.method = method;
  43. this.host = host;
  44. this.deadline = deadline;
  45. this.requestHeaders = requestHeaders;
  46. this.cancellationToken = cancellationToken;
  47. this.writeHeadersFunc = writeHeadersFunc;
  48. this.writeOptionsHolder = writeOptionsHolder;
  49. this.authContext = new Lazy<AuthContext>(GetAuthContextEager);
  50. }
  51. /// <summary>
  52. /// 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
  53. /// before any response messages are written. Writing the first response message implicitly sends empty response headers if <c>WriteResponseHeadersAsync</c> haven't
  54. /// been called yet.
  55. /// </summary>
  56. /// <param name="responseHeaders">The response headers to send.</param>
  57. /// <returns>The task that finished once response headers have been written.</returns>
  58. public Task WriteResponseHeadersAsync(Metadata responseHeaders)
  59. {
  60. return writeHeadersFunc(responseHeaders);
  61. }
  62. /// <summary>
  63. /// Creates a propagation token to be used to propagate call context to a child call.
  64. /// </summary>
  65. public ContextPropagationToken CreatePropagationToken(ContextPropagationOptions options = null)
  66. {
  67. return new ContextPropagationToken(callHandle, deadline, cancellationToken, options);
  68. }
  69. /// <summary>Name of method called in this RPC.</summary>
  70. public string Method
  71. {
  72. get
  73. {
  74. return this.method;
  75. }
  76. }
  77. /// <summary>Name of host called in this RPC.</summary>
  78. public string Host
  79. {
  80. get
  81. {
  82. return this.host;
  83. }
  84. }
  85. /// <summary>Address of the remote endpoint in URI format.</summary>
  86. public string Peer
  87. {
  88. get
  89. {
  90. // Getting the peer lazily is fine as the native call is guaranteed
  91. // not to be disposed before user-supplied server side handler returns.
  92. // Most users won't need to read this field anyway.
  93. return this.callHandle.GetPeer();
  94. }
  95. }
  96. /// <summary>Deadline for this RPC.</summary>
  97. public DateTime Deadline
  98. {
  99. get
  100. {
  101. return this.deadline;
  102. }
  103. }
  104. /// <summary>Initial metadata sent by client.</summary>
  105. public Metadata RequestHeaders
  106. {
  107. get
  108. {
  109. return this.requestHeaders;
  110. }
  111. }
  112. /// <summary>Cancellation token signals when call is cancelled.</summary>
  113. public CancellationToken CancellationToken
  114. {
  115. get
  116. {
  117. return this.cancellationToken;
  118. }
  119. }
  120. /// <summary>Trailers to send back to client after RPC finishes.</summary>
  121. public Metadata ResponseTrailers
  122. {
  123. get
  124. {
  125. return this.responseTrailers;
  126. }
  127. }
  128. /// <summary> Status to send back to client after RPC finishes.</summary>
  129. public Status Status
  130. {
  131. get
  132. {
  133. return this.status;
  134. }
  135. set
  136. {
  137. status = value;
  138. }
  139. }
  140. /// <summary>
  141. /// Allows setting write options for the following write.
  142. /// For streaming response calls, this property is also exposed as on IServerStreamWriter for convenience.
  143. /// Both properties are backed by the same underlying value.
  144. /// </summary>
  145. public WriteOptions WriteOptions
  146. {
  147. get
  148. {
  149. return writeOptionsHolder.WriteOptions;
  150. }
  151. set
  152. {
  153. writeOptionsHolder.WriteOptions = value;
  154. }
  155. }
  156. /// <summary>
  157. /// Gets the <c>AuthContext</c> associated with this call.
  158. /// Note: Access to AuthContext is an experimental API that can change without any prior notice.
  159. /// </summary>
  160. public AuthContext AuthContext
  161. {
  162. get
  163. {
  164. return authContext.Value;
  165. }
  166. }
  167. private AuthContext GetAuthContextEager()
  168. {
  169. using (var authContextNative = callHandle.GetAuthContext())
  170. {
  171. return authContextNative.ToAuthContext();
  172. }
  173. }
  174. }
  175. /// <summary>
  176. /// Allows sharing write options between ServerCallContext and other objects.
  177. /// </summary>
  178. public interface IHasWriteOptions
  179. {
  180. /// <summary>
  181. /// Gets or sets the write options.
  182. /// </summary>
  183. WriteOptions WriteOptions { get; set; }
  184. }
  185. }