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
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
24
26
28 {
29 get
30 {
31 if (base.InternalPeekCompleted)
32 {
34 }
35 ExecutionContext context = _context;
36 if (context != null)
37 {
38 return context;
39 }
40 if ((_flags & StateFlags.PostBlockFinished) == 0)
41 {
42 lock (_lock)
43 {
44 }
45 }
46 if (base.InternalPeekCompleted)
47 {
49 }
50 return _context;
51 }
52 }
53
54 internal ContextAwareResult(bool captureIdentity, bool forceCaptureContext, object myObject, object myState, AsyncCallback myCallBack)
55 : this(captureIdentity, forceCaptureContext, threadSafeContextCopy: false, myObject, myState, myCallBack)
56 {
57 }
58
59 internal ContextAwareResult(bool captureIdentity, bool forceCaptureContext, bool threadSafeContextCopy, object myObject, object myState, AsyncCallback myCallBack)
60 : base(myObject, myState, myCallBack)
61 {
62 if (forceCaptureContext)
63 {
64 _flags = StateFlags.CaptureContext;
65 }
66 if (captureIdentity)
67 {
68 _flags |= StateFlags.CaptureIdentity;
69 }
70 if (threadSafeContextCopy)
71 {
72 _flags |= StateFlags.ThreadSafeContextCopy;
73 }
74 }
75
76 internal object StartPostingAsyncOp()
77 {
78 return StartPostingAsyncOp(lockCapture: true);
79 }
80
81 internal object StartPostingAsyncOp(bool lockCapture)
82 {
83 _lock = (lockCapture ? new object() : null);
84 _flags |= StateFlags.PostBlockStarted;
85 return _lock;
86 }
87
88 internal bool FinishPostingAsyncOp()
89 {
90 if ((_flags & (StateFlags.PostBlockStarted | StateFlags.PostBlockFinished)) != StateFlags.PostBlockStarted)
91 {
92 return false;
93 }
94 _flags |= StateFlags.PostBlockFinished;
95 ExecutionContext cachedContext = null;
96 return CaptureOrComplete(ref cachedContext, returnContext: false);
97 }
98
99 protected override void Cleanup()
100 {
101 base.Cleanup();
102 if (System.Net.NetEventSource.Log.IsEnabled())
103 {
104 System.Net.NetEventSource.Info(this, null, "Cleanup");
105 }
107 }
108
109 private bool CaptureOrComplete(ref ExecutionContext cachedContext, bool returnContext)
110 {
111 bool flag = base.AsyncCallback != null || (_flags & StateFlags.CaptureContext) != 0;
112 if ((_flags & StateFlags.CaptureIdentity) != 0 && !base.InternalPeekCompleted && !flag)
113 {
114 if (System.Net.NetEventSource.Log.IsEnabled())
115 {
116 System.Net.NetEventSource.Info(this, "starting identity capture", "CaptureOrComplete");
117 }
119 }
120 if (flag && !base.InternalPeekCompleted)
121 {
122 if (System.Net.NetEventSource.Log.IsEnabled())
123 {
124 System.Net.NetEventSource.Info(this, "starting capture", "CaptureOrComplete");
125 }
126 if (cachedContext == null)
127 {
128 cachedContext = ExecutionContext.Capture();
129 }
130 if (cachedContext != null)
131 {
132 if (!returnContext)
133 {
134 _context = cachedContext;
135 cachedContext = null;
136 }
137 else
138 {
139 _context = cachedContext;
140 }
141 }
142 if (System.Net.NetEventSource.Log.IsEnabled())
143 {
144 System.Net.NetEventSource.Info(this, $"_context:{_context}", "CaptureOrComplete");
145 }
146 }
147 else
148 {
149 if (System.Net.NetEventSource.Log.IsEnabled())
150 {
151 System.Net.NetEventSource.Info(this, "Skipping capture", "CaptureOrComplete");
152 }
153 cachedContext = null;
154 }
155 if (base.CompletedSynchronously)
156 {
157 if (System.Net.NetEventSource.Log.IsEnabled())
158 {
159 System.Net.NetEventSource.Info(this, "Completing synchronously", "CaptureOrComplete");
160 }
161 base.Complete(IntPtr.Zero);
162 return true;
163 }
164 return false;
165 }
166
167 protected override void Complete(IntPtr userToken)
168 {
169 if (System.Net.NetEventSource.Log.IsEnabled())
170 {
171 System.Net.NetEventSource.Info(this, $"_context(set):{_context != null} userToken:{userToken}", "Complete");
172 }
173 if ((_flags & StateFlags.PostBlockStarted) == 0)
174 {
175 base.Complete(userToken);
176 }
177 else
178 {
179 if (base.CompletedSynchronously)
180 {
181 return;
182 }
183 ExecutionContext context = _context;
184 if (userToken != IntPtr.Zero || context == null)
185 {
186 base.Complete(userToken);
187 return;
188 }
189 ExecutionContext.Run(context, delegate(object s)
190 {
191 ((System.Net.ContextAwareResult)s).CompleteCallback();
192 }, this);
193 }
194 }
195
196 private void CompleteCallback()
197 {
198 if (System.Net.NetEventSource.Log.IsEnabled())
199 {
200 System.Net.NetEventSource.Info(this, "Context set, calling callback.", "CompleteCallback");
201 }
202 base.Complete(IntPtr.Zero);
203 }
204
209
210 private void CleanupInternal()
211 {
212 if (_windowsIdentity != null)
213 {
215 _windowsIdentity = null;
216 }
217 }
218}
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 string net_completed_result
Definition SR.cs:138
Definition SR.cs:7
static ? ExecutionContext Capture()
static void Run(ExecutionContext executionContext, ContextCallback callback, object? state)
static readonly IntPtr Zero
Definition IntPtr.cs:18