ServerCallContext.cs 6.7 KB

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