Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
NetEventSource.cs
Go to the documentation of this file.
7
8namespace System.Net;
9
10[EventSource(Name = "Private.InternalDiagnostics.System.Net.Sockets", LocalizationResources = "FxResources.System.Net.Sockets.SR")]
11internal sealed class NetEventSource : EventSource
12{
13 public static class Keywords
14 {
15 public const EventKeywords Default = (EventKeywords)1L;
16
17 public const EventKeywords Debug = (EventKeywords)2L;
18 }
19
20 public static readonly System.Net.NetEventSource Log = new System.Net.NetEventSource();
21
22 [NonEvent]
23 public static void Accepted(Socket socket, object remoteEp, object localEp)
24 {
25 if (Log.IsEnabled())
26 {
27 Log.Accepted(IdOf(remoteEp), IdOf(localEp), GetHashCode(socket));
28 }
29 }
30
31 [Event(17, Keywords = (EventKeywords)1L, Level = EventLevel.Informational)]
32 private void Accepted(string remoteEp, string localEp, int socketHash)
33 {
34 WriteEvent(17, remoteEp, localEp, socketHash);
35 }
36
37 [NonEvent]
38 public static void Connected(Socket socket, object localEp, object remoteEp)
39 {
40 if (Log.IsEnabled())
41 {
42 Log.Connected(IdOf(localEp), IdOf(remoteEp), GetHashCode(socket));
43 }
44 }
45
46 [Event(18, Keywords = (EventKeywords)1L, Level = EventLevel.Informational)]
47 private void Connected(string localEp, string remoteEp, int socketHash)
48 {
49 WriteEvent(18, localEp, remoteEp, socketHash);
50 }
51
52 [NonEvent]
53 public static void ConnectedAsyncDns(Socket socket)
54 {
55 if (Log.IsEnabled())
56 {
57 Log.ConnectedAsyncDns(GetHashCode(socket));
58 }
59 }
60
61 [Event(19, Keywords = (EventKeywords)1L, Level = EventLevel.Informational)]
62 private void ConnectedAsyncDns(int socketHash)
63 {
64 WriteEvent(19, socketHash);
65 }
66
67 [NonEvent]
68 public static void DumpBuffer(object thisOrContextObject, Memory<byte> buffer, int offset, int count, [CallerMemberName] string memberName = null)
69 {
70 if (Log.IsEnabled() && offset >= 0 && offset <= buffer.Length - count)
71 {
72 buffer = buffer.Slice(offset, Math.Min(count, 1024));
73 ArraySegment<byte> segment;
74 byte[] buffer2 = ((MemoryMarshal.TryGetArray((ReadOnlyMemory<byte>)buffer, out segment) && segment.Offset == 0 && segment.Count == buffer.Length) ? segment.Array : buffer.ToArray());
75 Log.DumpBuffer(IdOf(thisOrContextObject), memberName, buffer2);
76 }
77 }
78
79 [NonEvent]
80 public static void Info(object thisOrContextObject, FormattableString formattableString = null, [CallerMemberName] string memberName = null)
81 {
82 if (Log.IsEnabled())
83 {
84 Log.Info(IdOf(thisOrContextObject), memberName, (formattableString != null) ? Format(formattableString) : "");
85 }
86 }
87
88 [NonEvent]
89 public static void Info(object thisOrContextObject, object message, [CallerMemberName] string memberName = null)
90 {
91 if (Log.IsEnabled())
92 {
93 Log.Info(IdOf(thisOrContextObject), memberName, Format(message).ToString());
94 }
95 }
96
97 [Event(4, Level = EventLevel.Informational, Keywords = (EventKeywords)1L)]
98 private void Info(string thisOrContextObject, string memberName, string message)
99 {
100 WriteEvent(4, thisOrContextObject, memberName ?? "(?)", message);
101 }
102
103 [NonEvent]
104 public static void Error(object thisOrContextObject, FormattableString formattableString, [CallerMemberName] string memberName = null)
105 {
106 if (Log.IsEnabled())
107 {
108 Log.ErrorMessage(IdOf(thisOrContextObject), memberName, Format(formattableString));
109 }
110 }
111
112 [NonEvent]
113 public static void Error(object thisOrContextObject, object message, [CallerMemberName] string memberName = null)
114 {
115 if (Log.IsEnabled())
116 {
117 Log.ErrorMessage(IdOf(thisOrContextObject), memberName, Format(message).ToString());
118 }
119 }
120
121 [Event(5, Level = EventLevel.Error, Keywords = (EventKeywords)1L)]
122 private void ErrorMessage(string thisOrContextObject, string memberName, string message)
123 {
124 WriteEvent(5, thisOrContextObject, memberName ?? "(?)", message);
125 }
126
127 [NonEvent]
128 public static void DumpBuffer(object thisOrContextObject, byte[] buffer, int offset, int count, [CallerMemberName] string memberName = null)
129 {
130 if (Log.IsEnabled() && offset >= 0 && offset <= buffer.Length - count)
131 {
132 count = Math.Min(count, 1024);
133 byte[] array = buffer;
134 if (offset != 0 || count != buffer.Length)
135 {
136 array = new byte[count];
138 }
139 Log.DumpBuffer(IdOf(thisOrContextObject), memberName, array);
140 }
141 }
142
143 [NonEvent]
144 public unsafe static void DumpBuffer(object thisOrContextObject, IntPtr bufferPtr, int count, [CallerMemberName] string memberName = null)
145 {
146 if (Log.IsEnabled())
147 {
148 byte[] array = new byte[Math.Min(count, 1024)];
149 fixed (byte* destination = array)
150 {
151 Buffer.MemoryCopy((void*)bufferPtr, destination, array.Length, array.Length);
152 }
153 Log.DumpBuffer(IdOf(thisOrContextObject), memberName, array);
154 }
155 }
156
157 [Event(7, Level = EventLevel.Verbose, Keywords = (EventKeywords)2L)]
158 private void DumpBuffer(string thisOrContextObject, string memberName, byte[] buffer)
159 {
160 WriteEvent(7, thisOrContextObject, memberName ?? "(?)", buffer);
161 }
162
163 [NonEvent]
164 public static string IdOf(object value)
165 {
166 if (value == null)
167 {
168 return "(null)";
169 }
170 return value.GetType().Name + "#" + GetHashCode(value);
171 }
172
173 [NonEvent]
174 public static int GetHashCode(object value)
175 {
176 return value?.GetHashCode() ?? 0;
177 }
178
179 [NonEvent]
180 public static object Format(object value)
181 {
182 if (value == null)
183 {
184 return "(null)";
185 }
186 string text = null;
187 if (text != null)
188 {
189 return text;
190 }
191 if (value is Array array)
192 {
193 return $"{array.GetType().GetElementType()}[{((Array)value).Length}]";
194 }
196 {
197 return $"{collection.GetType().Name}({collection.Count})";
198 }
199 if (value is SafeHandle safeHandle)
200 {
201 return $"{safeHandle.GetType().Name}:{safeHandle.GetHashCode()}(0x{safeHandle.DangerousGetHandle():X})";
202 }
203 if (value is IntPtr)
204 {
205 return $"0x{value:X}";
206 }
207 string text2 = value.ToString();
208 if (text2 == null || text2 == value.GetType().FullName)
209 {
210 return IdOf(value);
211 }
212 return value;
213 }
214
215 [NonEvent]
216 private static string Format(FormattableString s)
217 {
218 switch (s.ArgumentCount)
219 {
220 case 0:
221 return s.Format;
222 case 1:
223 return string.Format(s.Format, Format(s.GetArgument(0)));
224 case 2:
225 return string.Format(s.Format, Format(s.GetArgument(0)), Format(s.GetArgument(1)));
226 case 3:
227 return string.Format(s.Format, Format(s.GetArgument(0)), Format(s.GetArgument(1)), Format(s.GetArgument(2)));
228 default:
229 {
230 object[] arguments = s.GetArguments();
231 object[] array = new object[arguments.Length];
232 for (int i = 0; i < arguments.Length; i++)
233 {
234 array[i] = Format(arguments[i]);
235 }
236 return string.Format(s.Format, array);
237 }
238 }
239 }
240
241 [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:UnrecognizedReflectionPattern", Justification = "Parameters to this method are primitive and are trimmer safe")]
242 [NonEvent]
243 private unsafe void WriteEvent(int eventId, string arg1, string arg2, byte[] arg3)
244 {
245 //The blocks IL_004f, IL_0053, IL_0055, IL_006e, IL_0155 are reachable both inside and outside the pinned region starting at IL_004c. ILSpy has duplicated these blocks in order to place them both within and outside the `fixed` statement.
246 if (!Log.IsEnabled())
247 {
248 return;
249 }
250 if (arg1 == null)
251 {
252 arg1 = "";
253 }
254 if (arg2 == null)
255 {
256 arg2 = "";
257 }
258 if (arg3 == null)
259 {
260 arg3 = Array.Empty<byte>();
261 }
262 fixed (char* ptr5 = arg1)
263 {
264 char* intPtr;
265 byte[] array;
266 int size;
267 EventData* intPtr2;
268 nint num;
269 nint num2;
270 nint num3;
271 if (arg2 == null)
272 {
273 char* ptr;
274 intPtr = (ptr = null);
275 array = arg3;
276 fixed (byte* ptr2 = array)
277 {
278 byte* ptr3 = ptr2;
279 size = arg3.Length;
280 EventData* ptr4 = stackalloc EventData[4];
281 intPtr2 = ptr4;
282 *intPtr2 = new EventData
283 {
284 DataPointer = (IntPtr)ptr5,
285 Size = (arg1.Length + 1) * 2
286 };
287 num = (nint)(ptr4 + 1);
288 *(EventData*)num = new EventData
289 {
290 DataPointer = (IntPtr)ptr,
291 Size = (arg2.Length + 1) * 2
292 };
293 num2 = (nint)(ptr4 + 2);
294 *(EventData*)num2 = new EventData
295 {
296 DataPointer = (IntPtr)(&size),
297 Size = 4
298 };
299 num3 = (nint)(ptr4 + 3);
300 *(EventData*)num3 = new EventData
301 {
302 DataPointer = (IntPtr)ptr3,
303 Size = size
304 };
305 WriteEventCore(eventId, 4, ptr4);
306 }
307 return;
308 }
309 fixed (char* ptr6 = &arg2.GetPinnableReference())
310 {
311 char* ptr;
312 intPtr = (ptr = ptr6);
313 array = arg3;
314 fixed (byte* ptr2 = array)
315 {
316 byte* ptr3 = ptr2;
317 size = arg3.Length;
318 EventData* ptr4 = stackalloc EventData[4];
319 intPtr2 = ptr4;
320 *intPtr2 = new EventData
321 {
322 DataPointer = (IntPtr)ptr5,
323 Size = (arg1.Length + 1) * 2
324 };
325 num = (nint)(ptr4 + 1);
326 *(EventData*)num = new EventData
327 {
328 DataPointer = (IntPtr)ptr,
329 Size = (arg2.Length + 1) * 2
330 };
331 num2 = (nint)(ptr4 + 2);
332 *(EventData*)num2 = new EventData
333 {
334 DataPointer = (IntPtr)(&size),
335 Size = 4
336 };
337 num3 = (nint)(ptr4 + 3);
338 *(EventData*)num3 = new EventData
339 {
340 DataPointer = (IntPtr)ptr3,
341 Size = size
342 };
343 WriteEventCore(eventId, 4, ptr4);
344 }
345 }
346 }
347 }
348
349 [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:UnrecognizedReflectionPattern", Justification = "Parameters to this method are primitive and are trimmer safe")]
350 [NonEvent]
351 private unsafe void WriteEvent(int eventId, string arg1, string arg2, int arg3)
352 {
353 //The blocks IL_0044 are reachable both inside and outside the pinned region starting at IL_0041. ILSpy has duplicated these blocks in order to place them both within and outside the `fixed` statement.
354 if (!Log.IsEnabled())
355 {
356 return;
357 }
358 if (arg1 == null)
359 {
360 arg1 = "";
361 }
362 if (arg2 == null)
363 {
364 arg2 = "";
365 }
366 fixed (char* ptr3 = arg1)
367 {
368 char* intPtr;
369 EventData* intPtr2;
370 nint num;
371 nint num2;
372 if (arg2 == null)
373 {
374 char* ptr;
375 intPtr = (ptr = null);
376 EventData* ptr2 = stackalloc EventData[3];
377 intPtr2 = ptr2;
378 *intPtr2 = new EventData
379 {
380 DataPointer = (IntPtr)ptr3,
381 Size = (arg1.Length + 1) * 2
382 };
383 num = (nint)(ptr2 + 1);
384 *(EventData*)num = new EventData
385 {
386 DataPointer = (IntPtr)ptr,
387 Size = (arg2.Length + 1) * 2
388 };
389 num2 = (nint)(ptr2 + 2);
390 *(EventData*)num2 = new EventData
391 {
392 DataPointer = (IntPtr)(&arg3),
393 Size = 4
394 };
395 WriteEventCore(eventId, 3, ptr2);
396 return;
397 }
398 fixed (char* ptr4 = &arg2.GetPinnableReference())
399 {
400 char* ptr;
401 intPtr = (ptr = ptr4);
402 EventData* ptr2 = stackalloc EventData[3];
403 intPtr2 = ptr2;
404 *intPtr2 = new EventData
405 {
406 DataPointer = (IntPtr)ptr3,
407 Size = (arg1.Length + 1) * 2
408 };
409 num = (nint)(ptr2 + 1);
410 *(EventData*)num = new EventData
411 {
412 DataPointer = (IntPtr)ptr,
413 Size = (arg2.Length + 1) * 2
414 };
415 num2 = (nint)(ptr2 + 2);
416 *(EventData*)num2 = new EventData
417 {
418 DataPointer = (IntPtr)(&arg3),
419 Size = 4
420 };
421 WriteEventCore(eventId, 3, ptr2);
422 }
423 }
424 }
425}
static unsafe void MemoryCopy(void *source, void *destination, long destinationSizeInBytes, long sourceBytesToCopy)
Definition Buffer.cs:195
static void BlockCopy(Array src, int srcOffset, Array dst, int dstOffset, int count)
Definition Buffer.cs:102
unsafe void WriteEventCore(int eventId, int eventDataCount, EventData *data)
static byte Min(byte val1, byte val2)
Definition Math.cs:912
void DumpBuffer(string thisOrContextObject, string memberName, byte[] buffer)
static void ConnectedAsyncDns(Socket socket)
static readonly System.Net.NetEventSource Log
void ErrorMessage(string thisOrContextObject, string memberName, string message)
static void Info(object thisOrContextObject, FormattableString formattableString=null, [CallerMemberName] string memberName=null)
static void Error(object thisOrContextObject, object message, [CallerMemberName] string memberName=null)
void Info(string thisOrContextObject, string memberName, string message)
unsafe void WriteEvent(int eventId, string arg1, string arg2, int arg3)
static string IdOf(object value)
static string Format(FormattableString s)
static void Error(object thisOrContextObject, FormattableString formattableString, [CallerMemberName] string memberName=null)
void Connected(string localEp, string remoteEp, int socketHash)
static int GetHashCode(object value)
unsafe void WriteEvent(int eventId, string arg1, string arg2, byte[] arg3)
unsafe void WriteEvent(int eventId, int arg1, int arg2, int arg3, string arg4, string arg5)
static void Connected(Socket socket, object localEp, object remoteEp)
static void DumpBuffer(object thisOrContextObject, Memory< byte > buffer, int offset, int count, [CallerMemberName] string memberName=null)
static void Accepted(Socket socket, object remoteEp, object localEp)
static void Info(object thisOrContextObject, object message, [CallerMemberName] string memberName=null)
static object Format(object value)
void Accepted(string remoteEp, string localEp, int socketHash)
static unsafe void DumpBuffer(object thisOrContextObject, IntPtr bufferPtr, int count, [CallerMemberName] string memberName=null)
void ConnectedAsyncDns(int socketHash)
static void DumpBuffer(object thisOrContextObject, byte[] buffer, int offset, int count, [CallerMemberName] string memberName=null)