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 22 using Grpc.Core.Utils; 23 24 namespace Grpc.Core.Internal 25 { 26 /// <summary> 27 /// Implementation of <c>ContextPropagationToken</c> that carries 28 /// all fields needed for context propagation by C-core based implementation of gRPC. 29 /// Instances of <c>ContextPropagationToken</c> that are not of this 30 /// type will be recognized as "foreign" and will be silently ignored 31 /// (treated as if null). 32 /// </summary> 33 internal class ContextPropagationTokenImpl : ContextPropagationToken 34 { 35 /// <summary> 36 /// Default propagation mask used by C core. 37 /// </summary> 38 private const ContextPropagationFlags DefaultCoreMask = (ContextPropagationFlags)0xffff; 39 40 /// <summary> 41 /// Default propagation mask used by C# - we want to propagate deadline 42 /// and cancellation token by our own means, everything else will be propagated 43 /// by C core automatically (according to <c>DefaultCoreMask</c>). 44 /// </summary> 45 internal const ContextPropagationFlags DefaultMask = DefaultCoreMask 46 & ~ContextPropagationFlags.Deadline & ~ContextPropagationFlags.Cancellation; 47 48 readonly CallSafeHandle parentCall; 49 readonly DateTime deadline; 50 readonly CancellationToken cancellationToken; 51 readonly ContextPropagationOptions options; 52 ContextPropagationTokenImpl(CallSafeHandle parentCall, DateTime deadline, CancellationToken cancellationToken, ContextPropagationOptions options)53 internal ContextPropagationTokenImpl(CallSafeHandle parentCall, DateTime deadline, CancellationToken cancellationToken, ContextPropagationOptions options) 54 { 55 this.parentCall = GrpcPreconditions.CheckNotNull(parentCall); 56 this.deadline = deadline; 57 this.cancellationToken = cancellationToken; 58 this.options = options ?? ContextPropagationOptions.Default; 59 } 60 61 /// <summary> 62 /// Gets the native handle of the parent call. 63 /// </summary> 64 internal CallSafeHandle ParentCall 65 { 66 get 67 { 68 return this.parentCall; 69 } 70 } 71 72 /// <summary> 73 /// Gets the parent call's deadline. 74 /// </summary> 75 internal DateTime ParentDeadline 76 { 77 get 78 { 79 return this.deadline; 80 } 81 } 82 83 /// <summary> 84 /// Gets the parent call's cancellation token. 85 /// </summary> 86 internal CancellationToken ParentCancellationToken 87 { 88 get 89 { 90 return this.cancellationToken; 91 } 92 } 93 94 /// <summary> 95 /// Get the context propagation options. 96 /// </summary> 97 internal ContextPropagationOptions Options 98 { 99 get 100 { 101 return this.options; 102 } 103 } 104 } 105 106 internal static class ContextPropagationTokenExtensions 107 { 108 /// <summary> 109 /// Converts given <c>ContextPropagationToken</c> to <c>ContextPropagationTokenImpl</c> 110 /// if possible or returns null. 111 /// Being able to convert means that the context propagation token is recognized as 112 /// "ours" (was created by this implementation). 113 /// </summary> AsImplOrNull(this ContextPropagationToken instanceOrNull)114 public static ContextPropagationTokenImpl AsImplOrNull(this ContextPropagationToken instanceOrNull) 115 { 116 return instanceOrNull as ContextPropagationTokenImpl; 117 } 118 } 119 } 120