Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
ContextAwareResult.cs
Go to the documentation of this file.
3
4namespace System.Net;
5
6internal class ContextAwareResult : LazyAsyncResult
7{
8 [Flags]
9 private enum StateFlags : byte
10 {
11 None = 0,
17 }
18
19 private volatile ExecutionContext _context;
20
21 private object _lock;
22
23 private StateFlags _flags;
24
26
27 internal ContextAwareResult(object myObject, object myState, AsyncCallback myCallBack)
28 : this(captureIdentity: false, forceCaptureContext: false, myObject, myState, myCallBack)
29 {
30 }
31
32 internal ContextAwareResult(bool captureIdentity, bool forceCaptureContext, object myObject, object myState, AsyncCallback myCallBack)
33 : this(captureIdentity, forceCaptureContext, threadSafeContextCopy: false, myObject, myState, myCallBack)
34 {
35 }
36
37 internal ContextAwareResult(bool captureIdentity, bool forceCaptureContext, bool threadSafeContextCopy, object myObject, object myState, AsyncCallback myCallBack)
38 : base(myObject, myState, myCallBack)
39 {
40 if (forceCaptureContext)
41 {
42 _flags = StateFlags.CaptureContext;
43 }
44 if (captureIdentity)
45 {
46 _flags |= StateFlags.CaptureIdentity;
47 }
48 if (threadSafeContextCopy)
49 {
50 _flags |= StateFlags.ThreadSafeContextCopy;
51 }
52 }
53
54 internal object StartPostingAsyncOp()
55 {
56 return StartPostingAsyncOp(lockCapture: true);
57 }
58
59 internal object StartPostingAsyncOp(bool lockCapture)
60 {
61 _lock = (lockCapture ? new object() : null);
62 _flags |= StateFlags.PostBlockStarted;
63 return _lock;
64 }
65
66 internal bool FinishPostingAsyncOp()
67 {
68 if ((_flags & (StateFlags.PostBlockStarted | StateFlags.PostBlockFinished)) != StateFlags.PostBlockStarted)
69 {
70 return false;
71 }
72 _flags |= StateFlags.PostBlockFinished;
73 ExecutionContext cachedContext = null;
74 return CaptureOrComplete(ref cachedContext, returnContext: false);
75 }
76
77 protected override void Cleanup()
78 {
79 base.Cleanup();
80 if (System.Net.NetEventSource.Log.IsEnabled())
81 {
82 System.Net.NetEventSource.Info(this, null, "Cleanup");
83 }
85 }
86
87 private bool CaptureOrComplete(ref ExecutionContext cachedContext, bool returnContext)
88 {
89 bool flag = base.AsyncCallback != null || (_flags & StateFlags.CaptureContext) != 0;
90 if ((_flags & StateFlags.CaptureIdentity) != 0 && !base.InternalPeekCompleted && !flag)
91 {
92 if (System.Net.NetEventSource.Log.IsEnabled())
93 {
94 System.Net.NetEventSource.Info(this, "starting identity capture", "CaptureOrComplete");
95 }
97 }
98 if (flag && !base.InternalPeekCompleted)
99 {
100 if (System.Net.NetEventSource.Log.IsEnabled())
101 {
102 System.Net.NetEventSource.Info(this, "starting capture", "CaptureOrComplete");
103 }
104 if (cachedContext == null)
105 {
106 cachedContext = ExecutionContext.Capture();
107 }
108 if (cachedContext != null)
109 {
110 if (!returnContext)
111 {
112 _context = cachedContext;
113 cachedContext = null;
114 }
115 else
116 {
117 _context = cachedContext;
118 }
119 }
120 if (System.Net.NetEventSource.Log.IsEnabled())
121 {
122 System.Net.NetEventSource.Info(this, $"_context:{_context}", "CaptureOrComplete");
123 }
124 }
125 else
126 {
127 if (System.Net.NetEventSource.Log.IsEnabled())
128 {
129 System.Net.NetEventSource.Info(this, "Skipping capture", "CaptureOrComplete");
130 }
131 cachedContext = null;
132 }
133 if (base.CompletedSynchronously)
134 {
135 if (System.Net.NetEventSource.Log.IsEnabled())
136 {
137 System.Net.NetEventSource.Info(this, "Completing synchronously", "CaptureOrComplete");
138 }
139 base.Complete(IntPtr.Zero);
140 return true;
141 }
142 return false;
143 }
144
145 protected override void Complete(IntPtr userToken)
146 {
147 if (System.Net.NetEventSource.Log.IsEnabled())
148 {
149 System.Net.NetEventSource.Info(this, $"_context(set):{_context != null} userToken:{userToken}", "Complete");
150 }
151 if ((_flags & StateFlags.PostBlockStarted) == 0)
152 {
153 base.Complete(userToken);
154 }
155 else
156 {
157 if (base.CompletedSynchronously)
158 {
159 return;
160 }
161 ExecutionContext context = _context;
162 if (userToken != IntPtr.Zero || context == null)
163 {
164 base.Complete(userToken);
165 return;
166 }
167 ExecutionContext.Run(context, delegate(object s)
168 {
169 ((ContextAwareResult)s).CompleteCallback();
170 }, this);
171 }
172 }
173
174 private void CompleteCallback()
175 {
176 if (System.Net.NetEventSource.Log.IsEnabled())
177 {
178 System.Net.NetEventSource.Info(this, "Context set, calling callback.", "CompleteCallback");
179 }
180 base.Complete(IntPtr.Zero);
181 }
182
187
188 private void CleanupInternal()
189 {
190 if (_windowsIdentity != null)
191 {
193 _windowsIdentity = null;
194 }
195 }
196}
ContextAwareResult(object myObject, object myState, AsyncCallback myCallBack)
override void Complete(IntPtr userToken)
ContextAwareResult(bool captureIdentity, bool forceCaptureContext, object myObject, object myState, AsyncCallback myCallBack)
volatile ExecutionContext _context
bool CaptureOrComplete(ref ExecutionContext cachedContext, bool returnContext)
ContextAwareResult(bool captureIdentity, bool forceCaptureContext, bool threadSafeContextCopy, object myObject, object myState, AsyncCallback myCallBack)
object StartPostingAsyncOp(bool lockCapture)
static readonly System.Net.NetEventSource Log
static void Info(object thisOrContextObject, FormattableString formattableString=null, [CallerMemberName] string memberName=null)
static ? ExecutionContext Capture()
static void Run(ExecutionContext executionContext, ContextCallback callback, object? state)
static readonly IntPtr Zero
Definition IntPtr.cs:18