ServerCallContext.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. public Task WriteResponseHeadersAsync(Metadata responseHeaders)
  68. {
  69. return writeHeadersFunc(responseHeaders);
  70. }
  71. /// <summary>
  72. /// Creates a propagation token to be used to propagate call context to a child call.
  73. /// </summary>
  74. public ContextPropagationToken CreatePropagationToken(ContextPropagationOptions options = null)
  75. {
  76. return new ContextPropagationToken(callHandle, deadline, cancellationToken, options);
  77. }
  78. /// <summary>Name of method called in this RPC.</summary>
  79. public string Method
  80. {
  81. get
  82. {
  83. return this.method;
  84. }
  85. }
  86. /// <summary>Name of host called in this RPC.</summary>
  87. public string Host
  88. {
  89. get
  90. {
  91. return this.host;
  92. }
  93. }
  94. /// <summary>Address of the remote endpoint in URI format.</summary>
  95. public string Peer
  96. {
  97. get
  98. {
  99. return this.peer;
  100. }
  101. }
  102. /// <summary>Deadline for this RPC.</summary>
  103. public DateTime Deadline
  104. {
  105. get
  106. {
  107. return this.deadline;
  108. }
  109. }
  110. /// <summary>Initial metadata sent by client.</summary>
  111. public Metadata RequestHeaders
  112. {
  113. get
  114. {
  115. return this.requestHeaders;
  116. }
  117. }
  118. /// <summary>Cancellation token signals when call is cancelled.</summary>
  119. public CancellationToken CancellationToken
  120. {
  121. get
  122. {
  123. return this.cancellationToken;
  124. }
  125. }
  126. /// <summary>Trailers to send back to client after RPC finishes.</summary>
  127. public Metadata ResponseTrailers
  128. {
  129. get
  130. {
  131. return this.responseTrailers;
  132. }
  133. }
  134. /// <summary> Status to send back to client after RPC finishes.</summary>
  135. public Status Status
  136. {
  137. get
  138. {
  139. return this.status;
  140. }
  141. set
  142. {
  143. status = value;
  144. }
  145. }
  146. /// <summary>
  147. /// Allows setting write options for the following write.
  148. /// For streaming response calls, this property is also exposed as on IServerStreamWriter for convenience.
  149. /// Both properties are backed by the same underlying value.
  150. /// </summary>
  151. public WriteOptions WriteOptions
  152. {
  153. get
  154. {
  155. return writeOptionsHolder.WriteOptions;
  156. }
  157. set
  158. {
  159. writeOptionsHolder.WriteOptions = value;
  160. }
  161. }
  162. }
  163. /// <summary>
  164. /// Allows sharing write options between ServerCallContext and other objects.
  165. /// </summary>
  166. public interface IHasWriteOptions
  167. {
  168. WriteOptions WriteOptions { get; set; }
  169. }
  170. }