ContextPropagationTokenImpl.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #region Copyright notice and license
  2. // Copyright 2019 The gRPC Authors
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. #endregion
  16. using System;
  17. using System.Threading;
  18. using Grpc.Core.Utils;
  19. namespace Grpc.Core.Internal
  20. {
  21. /// <summary>
  22. /// Implementation of <c>ContextPropagationToken</c> that carries
  23. /// all fields needed for context propagation by C-core based implementation of gRPC.
  24. /// Instances of <c>ContextPropagationToken</c> that are not of this
  25. /// type will be recognized as "foreign" and will be silently ignored
  26. /// (treated as if null).
  27. /// </summary>
  28. internal class ContextPropagationTokenImpl : ContextPropagationToken
  29. {
  30. /// <summary>
  31. /// Default propagation mask used by C core.
  32. /// </summary>
  33. private const ContextPropagationFlags DefaultCoreMask = (ContextPropagationFlags)0xffff;
  34. /// <summary>
  35. /// Default propagation mask used by C# - we want to propagate deadline
  36. /// and cancellation token by our own means, everything else will be propagated
  37. /// by C core automatically (according to <c>DefaultCoreMask</c>).
  38. /// </summary>
  39. internal const ContextPropagationFlags DefaultMask = DefaultCoreMask
  40. & ~ContextPropagationFlags.Deadline & ~ContextPropagationFlags.Cancellation;
  41. readonly CallSafeHandle parentCall;
  42. readonly DateTime deadline;
  43. readonly CancellationToken cancellationToken;
  44. readonly ContextPropagationOptions options;
  45. internal ContextPropagationTokenImpl(CallSafeHandle parentCall, DateTime deadline, CancellationToken cancellationToken, ContextPropagationOptions options)
  46. {
  47. this.parentCall = GrpcPreconditions.CheckNotNull(parentCall);
  48. this.deadline = deadline;
  49. this.cancellationToken = cancellationToken;
  50. this.options = options ?? ContextPropagationOptions.Default;
  51. }
  52. /// <summary>
  53. /// Gets the native handle of the parent call.
  54. /// </summary>
  55. internal CallSafeHandle ParentCall
  56. {
  57. get
  58. {
  59. return this.parentCall;
  60. }
  61. }
  62. /// <summary>
  63. /// Gets the parent call's deadline.
  64. /// </summary>
  65. internal DateTime ParentDeadline
  66. {
  67. get
  68. {
  69. return this.deadline;
  70. }
  71. }
  72. /// <summary>
  73. /// Gets the parent call's cancellation token.
  74. /// </summary>
  75. internal CancellationToken ParentCancellationToken
  76. {
  77. get
  78. {
  79. return this.cancellationToken;
  80. }
  81. }
  82. /// <summary>
  83. /// Get the context propagation options.
  84. /// </summary>
  85. internal ContextPropagationOptions Options
  86. {
  87. get
  88. {
  89. return this.options;
  90. }
  91. }
  92. }
  93. internal static class ContextPropagationTokenExtensions
  94. {
  95. /// <summary>
  96. /// Converts given <c>ContextPropagationToken</c> to <c>ContextPropagationTokenImpl</c>
  97. /// if possible or returns null.
  98. /// Being able to convert means that the context propagation token is recognized as
  99. /// "ours" (was created by this implementation).
  100. /// </summary>
  101. public static ContextPropagationTokenImpl AsImplOrNull(this ContextPropagationToken instanceOrNull)
  102. {
  103. return instanceOrNull as ContextPropagationTokenImpl;
  104. }
  105. }
  106. }