ServerCallContext.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. #region Copyright notice and license
  2. // Copyright 2015, Google Inc.
  3. // All rights reserved.
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are
  7. // met:
  8. //
  9. // * Redistributions of source code must retain the above copyright
  10. // notice, this list of conditions and the following disclaimer.
  11. // * Redistributions in binary form must reproduce the above
  12. // copyright notice, this list of conditions and the following disclaimer
  13. // in the documentation and/or other materials provided with the
  14. // distribution.
  15. // * Neither the name of Google Inc. nor the names of its
  16. // contributors may be used to endorse or promote products derived from
  17. // this software without specific prior written permission.
  18. //
  19. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. #endregion
  31. using System;
  32. using System.Runtime.CompilerServices;
  33. using System.Threading;
  34. using System.Threading.Tasks;
  35. using Grpc.Core.Internal;
  36. namespace Grpc.Core
  37. {
  38. /// <summary>
  39. /// Context for a server-side call.
  40. /// </summary>
  41. public class ServerCallContext
  42. {
  43. private readonly CallSafeHandle callHandle;
  44. private readonly string method;
  45. private readonly string host;
  46. private readonly DateTime deadline;
  47. private readonly Metadata requestHeaders;
  48. private readonly CancellationToken cancellationToken;
  49. private readonly Metadata responseTrailers = new Metadata();
  50. private Status status = Status.DefaultSuccess;
  51. private Func<Metadata, Task> writeHeadersFunc;
  52. private IHasWriteOptions writeOptionsHolder;
  53. internal ServerCallContext(CallSafeHandle callHandle, string method, string host, DateTime deadline, Metadata requestHeaders, CancellationToken cancellationToken,
  54. Func<Metadata, Task> writeHeadersFunc, IHasWriteOptions writeOptionsHolder)
  55. {
  56. this.callHandle = callHandle;
  57. this.method = method;
  58. this.host = host;
  59. this.deadline = deadline;
  60. this.requestHeaders = requestHeaders;
  61. this.cancellationToken = cancellationToken;
  62. this.writeHeadersFunc = writeHeadersFunc;
  63. this.writeOptionsHolder = writeOptionsHolder;
  64. }
  65. /// <summary>
  66. /// 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
  67. /// before any response messages are written. Writing the first response message implicitly sends empty response headers if <c>WriteResponseHeadersAsync</c> haven't
  68. /// been called yet.
  69. /// </summary>
  70. /// <param name="responseHeaders">The response headers to send.</param>
  71. /// <returns>The task that finished once response headers have been written.</returns>
  72. public Task WriteResponseHeadersAsync(Metadata responseHeaders)
  73. {
  74. return writeHeadersFunc(responseHeaders);
  75. }
  76. /// <summary>
  77. /// Creates a propagation token to be used to propagate call context to a child call.
  78. /// </summary>
  79. public ContextPropagationToken CreatePropagationToken(ContextPropagationOptions options = null)
  80. {
  81. return new ContextPropagationToken(callHandle, deadline, cancellationToken, options);
  82. }
  83. /// <summary>Name of method called in this RPC.</summary>
  84. public string Method
  85. {
  86. get
  87. {
  88. return this.method;
  89. }
  90. }
  91. /// <summary>Name of host called in this RPC.</summary>
  92. public string Host
  93. {
  94. get
  95. {
  96. return this.host;
  97. }
  98. }
  99. /// <summary>Address of the remote endpoint in URI format.</summary>
  100. public string Peer
  101. {
  102. get
  103. {
  104. // Getting the peer lazily is fine as the native call is guaranteed
  105. // not to be disposed before user-supplied server side handler returns.
  106. // Most users won't need to read this field anyway.
  107. return this.callHandle.GetPeer();
  108. }
  109. }
  110. /// <summary>Deadline for this RPC.</summary>
  111. public DateTime Deadline
  112. {
  113. get
  114. {
  115. return this.deadline;
  116. }
  117. }
  118. /// <summary>Initial metadata sent by client.</summary>
  119. public Metadata RequestHeaders
  120. {
  121. get
  122. {
  123. return this.requestHeaders;
  124. }
  125. }
  126. /// <summary>Cancellation token signals when call is cancelled.</summary>
  127. public CancellationToken CancellationToken
  128. {
  129. get
  130. {
  131. return this.cancellationToken;
  132. }
  133. }
  134. /// <summary>Trailers to send back to client after RPC finishes.</summary>
  135. public Metadata ResponseTrailers
  136. {
  137. get
  138. {
  139. return this.responseTrailers;
  140. }
  141. }
  142. /// <summary> Status to send back to client after RPC finishes.</summary>
  143. public Status Status
  144. {
  145. get
  146. {
  147. return this.status;
  148. }
  149. set
  150. {
  151. status = value;
  152. }
  153. }
  154. /// <summary>
  155. /// Allows setting write options for the following write.
  156. /// For streaming response calls, this property is also exposed as on IServerStreamWriter for convenience.
  157. /// Both properties are backed by the same underlying value.
  158. /// </summary>
  159. public WriteOptions WriteOptions
  160. {
  161. get
  162. {
  163. return writeOptionsHolder.WriteOptions;
  164. }
  165. set
  166. {
  167. writeOptionsHolder.WriteOptions = value;
  168. }
  169. }
  170. }
  171. /// <summary>
  172. /// Allows sharing write options between ServerCallContext and other objects.
  173. /// </summary>
  174. public interface IHasWriteOptions
  175. {
  176. /// <summary>
  177. /// Gets or sets the write options.
  178. /// </summary>
  179. WriteOptions WriteOptions { get; set; }
  180. }
  181. }