• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #region Copyright notice and license
2 
3 // Copyright 2019 The 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.Threading;
21 using System.Threading.Tasks;
22 
23 using Grpc.Core.Internal;
24 
25 namespace Grpc.Core
26 {
27     /// <summary>
28     /// Default implementation of <c>ServerCallContext</c>.
29     /// </summary>
30     internal class DefaultServerCallContext : ServerCallContext
31     {
32         private readonly CallSafeHandle callHandle;
33         private readonly string method;
34         private readonly string host;
35         private readonly DateTime deadline;
36         private readonly Metadata requestHeaders;
37         private readonly CancellationToken cancellationToken;
38         private readonly Metadata responseTrailers;
39         private Status status;
40         private readonly IServerResponseStream serverResponseStream;
41         private AuthContext lazyAuthContext;
42 
43         /// <summary>
44         /// Creates a new instance of <c>ServerCallContext</c>.
45         /// To allow reuse of ServerCallContext API by different gRPC implementations, the implementation of some members is provided externally.
46         /// To provide state, this <c>ServerCallContext</c> instance and <c>extraData</c> will be passed to the member implementations.
47         /// </summary>
DefaultServerCallContext(CallSafeHandle callHandle, string method, string host, DateTime deadline, Metadata requestHeaders, CancellationToken cancellationToken, IServerResponseStream serverResponseStream)48         internal DefaultServerCallContext(CallSafeHandle callHandle, string method, string host, DateTime deadline,
49             Metadata requestHeaders, CancellationToken cancellationToken, IServerResponseStream serverResponseStream)
50         {
51             this.callHandle = callHandle;
52             this.method = method;
53             this.host = host;
54             this.deadline = deadline;
55             this.requestHeaders = requestHeaders;
56             this.cancellationToken = cancellationToken;
57             this.responseTrailers = new Metadata();
58             this.status = Status.DefaultSuccess;
59             this.serverResponseStream = serverResponseStream;
60         }
61 
CreatePropagationTokenCore(ContextPropagationOptions options)62         protected override ContextPropagationToken CreatePropagationTokenCore(ContextPropagationOptions options)
63         {
64             return new ContextPropagationTokenImpl(callHandle, deadline, cancellationToken, options);
65         }
66 
WriteResponseHeadersAsyncCore(Metadata responseHeaders)67         protected override Task WriteResponseHeadersAsyncCore(Metadata responseHeaders)
68         {
69             return serverResponseStream.WriteResponseHeadersAsync(responseHeaders);
70         }
71 
72         protected override string MethodCore => method;
73 
74         protected override string HostCore => host;
75 
76         protected override string PeerCore => callHandle.GetPeer();
77 
78         protected override DateTime DeadlineCore => deadline;
79 
80         protected override Metadata RequestHeadersCore => requestHeaders;
81 
82         protected override CancellationToken CancellationTokenCore => cancellationToken;
83 
84         protected override Metadata ResponseTrailersCore => responseTrailers;
85 
86         protected override Status StatusCore
87         {
88             get => status;
89             set => status = value;
90         }
91 
92         protected override WriteOptions WriteOptionsCore
93         {
94             get => serverResponseStream.WriteOptions;
95             set => serverResponseStream.WriteOptions = value;
96         }
97 
98         protected override AuthContext AuthContextCore => lazyAuthContext ?? (lazyAuthContext = GetAuthContextEager());
99 
GetAuthContextEager()100         private AuthContext GetAuthContextEager()
101         {
102             using (var authContextNative = callHandle.GetAuthContext())
103             {
104                 return authContextNative.ToAuthContext();
105             }
106         }
107     }
108 }
109