1 #region Copyright notice and license 2 3 // Copyright 2015 gRPC authors. 4 // 5 // Licensed under the Apache License, Version 2.0 (the "License"); 6 // you may not use this file except in compliance with the License. 7 // You may obtain a copy of the License at 8 // 9 // http://www.apache.org/licenses/LICENSE-2.0 10 // 11 // Unless required by applicable law or agreed to in writing, software 12 // distributed under the License is distributed on an "AS IS" BASIS, 13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 // See the License for the specific language governing permissions and 15 // limitations under the License. 16 17 #endregion 18 19 using System; 20 using System.Collections.Generic; 21 using System.Threading; 22 using System.Threading.Tasks; 23 24 namespace Grpc.Core 25 { 26 /// <summary> 27 /// Context for a server-side call. 28 /// </summary> 29 public abstract class ServerCallContext 30 { 31 private Dictionary<object, object> userState; 32 33 /// <summary> 34 /// Creates a new instance of <c>ServerCallContext</c>. 35 /// </summary> ServerCallContext()36 protected ServerCallContext() 37 { 38 } 39 40 /// <summary> 41 /// 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 42 /// before any response messages are written. Writing the first response message implicitly sends empty response headers if <c>WriteResponseHeadersAsync</c> haven't 43 /// been called yet. 44 /// </summary> 45 /// <param name="responseHeaders">The response headers to send.</param> 46 /// <returns>The task that finished once response headers have been written.</returns> WriteResponseHeadersAsync(Metadata responseHeaders)47 public Task WriteResponseHeadersAsync(Metadata responseHeaders) 48 { 49 return WriteResponseHeadersAsyncCore(responseHeaders); 50 } 51 52 /// <summary> 53 /// Creates a propagation token to be used to propagate call context to a child call. 54 /// </summary> CreatePropagationToken(ContextPropagationOptions options = null)55 public ContextPropagationToken CreatePropagationToken(ContextPropagationOptions options = null) 56 { 57 return CreatePropagationTokenCore(options); 58 } 59 60 /// <summary>Name of method called in this RPC.</summary> 61 public string Method => MethodCore; 62 63 /// <summary>Name of host called in this RPC.</summary> 64 public string Host => HostCore; 65 66 /// <summary>Address of the remote endpoint in URI format.</summary> 67 public string Peer => PeerCore; 68 69 /// <summary>Deadline for this RPC. The call will be automatically cancelled once the deadline is exceeded.</summary> 70 public DateTime Deadline => DeadlineCore; 71 72 /// <summary>Initial metadata sent by client.</summary> 73 public Metadata RequestHeaders => RequestHeadersCore; 74 75 /// <summary>Cancellation token signals when call is cancelled. It is also triggered when the deadline is exceeeded or there was some other error (e.g. network problem).</summary> 76 public CancellationToken CancellationToken => CancellationTokenCore; 77 78 /// <summary>Trailers to send back to client after RPC finishes.</summary> 79 public Metadata ResponseTrailers => ResponseTrailersCore; 80 81 /// <summary> Status to send back to client after RPC finishes.</summary> 82 public Status Status 83 { 84 get 85 { 86 return StatusCore; 87 } 88 89 set 90 { 91 StatusCore = value; 92 } 93 } 94 95 /// <summary> 96 /// Allows setting write options for the following write. 97 /// For streaming response calls, this property is also exposed as on IServerStreamWriter for convenience. 98 /// Both properties are backed by the same underlying value. 99 /// </summary> 100 public WriteOptions WriteOptions 101 { 102 get 103 { 104 return WriteOptionsCore; 105 } 106 107 set 108 { 109 WriteOptionsCore = value; 110 } 111 } 112 113 /// <summary> 114 /// Gets the <c>AuthContext</c> associated with this call. 115 /// Note: Access to AuthContext is an experimental API that can change without any prior notice. 116 /// </summary> 117 public AuthContext AuthContext => AuthContextCore; 118 119 /// <summary> 120 /// Gets a dictionary that can be used by the various interceptors and handlers of this 121 /// call to store arbitrary state. 122 /// </summary> 123 public IDictionary<object, object> UserState => UserStateCore; 124 125 /// <summary>Provides implementation of a non-virtual public member.</summary> WriteResponseHeadersAsyncCore(Metadata responseHeaders)126 protected abstract Task WriteResponseHeadersAsyncCore(Metadata responseHeaders); 127 /// <summary>Provides implementation of a non-virtual public member.</summary> CreatePropagationTokenCore(ContextPropagationOptions options)128 protected abstract ContextPropagationToken CreatePropagationTokenCore(ContextPropagationOptions options); 129 /// <summary>Provides implementation of a non-virtual public member.</summary> 130 protected abstract string MethodCore { get; } 131 /// <summary>Provides implementation of a non-virtual public member.</summary> 132 protected abstract string HostCore { get; } 133 /// <summary>Provides implementation of a non-virtual public member.</summary> 134 protected abstract string PeerCore { get; } 135 /// <summary>Provides implementation of a non-virtual public member.</summary> 136 protected abstract DateTime DeadlineCore { get; } 137 /// <summary>Provides implementation of a non-virtual public member.</summary> 138 protected abstract Metadata RequestHeadersCore { get; } 139 /// <summary>Provides implementation of a non-virtual public member.</summary> 140 protected abstract CancellationToken CancellationTokenCore { get; } 141 /// <summary>Provides implementation of a non-virtual public member.</summary> 142 protected abstract Metadata ResponseTrailersCore { get; } 143 /// <summary>Provides implementation of a non-virtual public member.</summary> 144 protected abstract Status StatusCore { get; set; } 145 /// <summary>Provides implementation of a non-virtual public member.</summary> 146 protected abstract WriteOptions WriteOptionsCore { get; set; } 147 /// <summary>Provides implementation of a non-virtual public member.</summary> 148 protected abstract AuthContext AuthContextCore { get; } 149 /// <summary>Provides implementation of a non-virtual public member.</summary> 150 protected virtual IDictionary<object, object> UserStateCore 151 { 152 get 153 { 154 if (userState == null) 155 { 156 userState = new Dictionary<object, object>(); 157 } 158 159 return userState; 160 } 161 } 162 } 163 } 164