Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
SslSessionsCache.cs
Go to the documentation of this file.
5
6namespace System.Net.Security;
7
8internal static class SslSessionsCache
9{
10 private readonly struct SslCredKey : IEquatable<SslCredKey>
11 {
12 private readonly byte[] _thumbPrint;
13
14 private readonly int _allowedProtocols;
15
17
18 private readonly bool _isServerMode;
19
27
28 public override int GetHashCode()
29 {
30 int num = 0;
31 if (_thumbPrint.Length != 0)
32 {
33 num ^= _thumbPrint[0];
34 if (1 < _thumbPrint.Length)
35 {
36 num ^= _thumbPrint[1] << 8;
37 }
38 if (2 < _thumbPrint.Length)
39 {
40 num ^= _thumbPrint[2] << 16;
41 }
42 if (3 < _thumbPrint.Length)
43 {
44 num ^= _thumbPrint[3] << 24;
45 }
46 }
47 num ^= _allowedProtocols;
48 num ^= (int)_encryptionPolicy;
49 return num ^ (_isServerMode ? 65536 : 131072);
50 }
51
52 public override bool Equals([NotNullWhen(true)] object obj)
53 {
54 if (obj is SslCredKey)
55 {
56 return Equals((SslCredKey)obj);
57 }
58 return false;
59 }
60
61 public bool Equals(SslCredKey other)
62 {
63 byte[] thumbPrint = _thumbPrint;
64 byte[] thumbPrint2 = other._thumbPrint;
65 if (thumbPrint.Length != thumbPrint2.Length)
66 {
67 return false;
68 }
69 if (_encryptionPolicy != other._encryptionPolicy)
70 {
71 return false;
72 }
73 if (_allowedProtocols != other._allowedProtocols)
74 {
75 return false;
76 }
77 if (_isServerMode != other._isServerMode)
78 {
79 return false;
80 }
81 for (int i = 0; i < thumbPrint.Length; i++)
82 {
83 if (thumbPrint[i] != thumbPrint2[i])
84 {
85 return false;
86 }
87 }
88 return true;
89 }
90 }
91
93
95 {
96 if (s_cachedCreds.IsEmpty)
97 {
98 if (System.Net.NetEventSource.Log.IsEnabled())
99 {
100 System.Net.NetEventSource.Info(null, $"Not found, Current Cache Count = {s_cachedCreds.Count}", "TryCachedCredential");
101 }
102 return null;
103 }
106 if (cachedCredential == null || cachedCredential.IsClosed || cachedCredential.IsInvalid)
107 {
108 if (System.Net.NetEventSource.Log.IsEnabled())
109 {
110 System.Net.NetEventSource.Info(null, $"Not found or invalid, Current Cache Coun = {s_cachedCreds.Count}", "TryCachedCredential");
111 }
112 return null;
113 }
114 if (System.Net.NetEventSource.Log.IsEnabled())
115 {
116 System.Net.NetEventSource.Info(null, $"Found a cached Handle = {cachedCredential}", "TryCachedCredential");
117 }
118 return cachedCredential;
119 }
120
122 {
123 if (!s_cachedCreds.TryGetValue(key, out var value))
124 {
125 return null;
126 }
127 return value.Target;
128 }
129
131 {
132 if (creds.IsInvalid)
133 {
134 if (System.Net.NetEventSource.Log.IsEnabled())
135 {
136 System.Net.NetEventSource.Info(null, $"Refused to cache an Invalid Handle {creds}, Current Cache Count = {s_cachedCreds.Count}", "CacheCredential");
137 }
138 return;
139 }
142 if (cachedCredential == null || cachedCredential.IsClosed || cachedCredential.IsInvalid)
143 {
145 {
147 if (cachedCredential == null || cachedCredential.IsClosed || cachedCredential.IsInvalid)
148 {
150 if (safeCredentialReference != null)
151 {
153 if (System.Net.NetEventSource.Log.IsEnabled())
154 {
155 System.Net.NetEventSource.Info(null, $"Caching New Handle = {creds}, Current Cache Count = {s_cachedCreds.Count}", "CacheCredential");
156 }
158 }
159 }
160 else if (System.Net.NetEventSource.Log.IsEnabled())
161 {
162 System.Net.NetEventSource.Info(null, $"CacheCredential() (locked retry) Found already cached Handle = {cachedCredential}", "CacheCredential");
163 }
164 return;
165 }
166 }
167 if (System.Net.NetEventSource.Log.IsEnabled())
168 {
169 System.Net.NetEventSource.Info(null, $"CacheCredential() Ignoring incoming handle = {creds} since found already cached Handle = {cachedCredential}", "CacheCredential");
170 }
171 static void ShrinkCredentialCache()
172 {
173 if (s_cachedCreds.Count % 32 == 0)
174 {
176 for (int i = 0; i < array.Length; i++)
177 {
179 SafeFreeCredentials target = value.Target;
181 if (target == null)
182 {
183 s_cachedCreds.TryRemove(array[i].Key, out value2);
184 }
185 else
186 {
187 value.Dispose();
189 if (value != null)
190 {
191 s_cachedCreds[array[i].Key] = value;
192 }
193 else
194 {
195 s_cachedCreds.TryRemove(array[i].Key, out value2);
196 }
197 }
198 }
199 if (System.Net.NetEventSource.Log.IsEnabled())
200 {
201 System.Net.NetEventSource.Info(null, $"Scavenged cache, New Cache Count = {s_cachedCreds.Count}", "CacheCredential");
202 }
203 }
204 }
205 }
206}
static readonly System.Net.NetEventSource Log
static void Info(object thisOrContextObject, FormattableString formattableString=null, [CallerMemberName] string memberName=null)
static System.Net.Security.SafeCredentialReference CreateReference(System.Net.Security.SafeFreeCredentials target)
static readonly ConcurrentDictionary< SslCredKey, SafeCredentialReference > s_cachedCreds
static SafeFreeCredentials TryCachedCredential(byte[] thumbPrint, SslProtocols sslProtocols, bool isServer, EncryptionPolicy encryptionPolicy)
static SafeFreeCredentials GetCachedCredential(SslCredKey key)
static void CacheCredential(SafeFreeCredentials creds, byte[] thumbPrint, SslProtocols sslProtocols, bool isServer, EncryptionPolicy encryptionPolicy)
override bool Equals([NotNullWhen(true)] object obj)
SslCredKey(byte[] thumbPrint, int allowedProtocols, bool isServerMode, EncryptionPolicy encryptionPolicy)