Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
HttpResponseStreamAsyncResult.cs
Go to the documentation of this file.
3
4namespace System.Net;
5
7{
9
11
12 private readonly global::Interop.HttpApi.HTTP_DATA_CHUNK[] _dataChunks;
13
14 internal bool _sentHeaders;
15
16 private unsafe static readonly IOCompletionCallback s_IOCallback = Callback;
17
18 private static readonly byte[] s_CRLFArray = new byte[2] { 13, 10 };
19
20 internal ushort dataChunkCount
21 {
22 get
23 {
24 if (_dataChunks == null)
25 {
26 return 0;
27 }
28 return (ushort)_dataChunks.Length;
29 }
30 }
31
32 internal unsafe global::Interop.HttpApi.HTTP_DATA_CHUNK* pDataChunks
33 {
34 get
35 {
36 if (_dataChunks == null)
37 {
38 return null;
39 }
40 return (global::Interop.HttpApi.HTTP_DATA_CHUNK*)(void*)Marshal.UnsafeAddrOfPinnedArrayElement(_dataChunks, 0);
41 }
42 }
43
44 internal HttpResponseStreamAsyncResult(object asyncObject, object userState, AsyncCallback callback)
45 : base(asyncObject, userState, callback)
46 {
47 }
48
49 private static byte[] GetChunkHeader(int size, out int offset)
50 {
51 uint num = 4026531840u;
52 byte[] array = new byte[10];
53 offset = -1;
54 int num2 = 0;
55 while (num2 < 8)
56 {
57 if (offset != -1 || (size & num) != 0L)
58 {
59 uint num3 = (uint)size >> 28;
60 if (num3 < 10)
61 {
62 array[num2] = (byte)(num3 + 48);
63 }
64 else
65 {
66 array[num2] = (byte)(num3 - 10 + 65);
67 }
68 if (offset == -1)
69 {
70 offset = num2;
71 }
72 }
73 num2++;
74 size <<= 4;
75 }
76 array[8] = 13;
77 array[9] = 10;
78 return array;
79 }
80
81 internal unsafe HttpResponseStreamAsyncResult(object asyncObject, object userState, AsyncCallback callback, byte[] buffer, int offset, int size, bool chunked, bool sentHeaders, ThreadPoolBoundHandle boundHandle)
82 : base(asyncObject, userState, callback)
83 {
84 _boundHandle = boundHandle;
85 _sentHeaders = sentHeaders;
86 if (size == 0)
87 {
88 _dataChunks = null;
89 _pOverlapped = boundHandle.AllocateNativeOverlapped(s_IOCallback, this, null);
90 return;
91 }
92 _dataChunks = new global::Interop.HttpApi.HTTP_DATA_CHUNK[(!chunked) ? 1 : 3];
93 if (System.Net.NetEventSource.Log.IsEnabled())
94 {
95 System.Net.NetEventSource.Info(this, "m_pOverlapped:0x" + ((IntPtr)_pOverlapped).ToString("x8"), ".ctor");
96 }
97 object[] array = new object[1 + _dataChunks.Length];
99 int offset2 = 0;
100 byte[] array2 = null;
101 if (chunked)
102 {
103 array2 = GetChunkHeader(size, out offset2);
104 _dataChunks[0] = default(global::Interop.HttpApi.HTTP_DATA_CHUNK);
105 _dataChunks[0].DataChunkType = global::Interop.HttpApi.HTTP_DATA_CHUNK_TYPE.HttpDataChunkFromMemory;
106 _dataChunks[0].BufferLength = (uint)(array2.Length - offset2);
107 array[0] = array2;
108 _dataChunks[1] = default(global::Interop.HttpApi.HTTP_DATA_CHUNK);
109 _dataChunks[1].DataChunkType = global::Interop.HttpApi.HTTP_DATA_CHUNK_TYPE.HttpDataChunkFromMemory;
110 _dataChunks[1].BufferLength = (uint)size;
111 array[1] = buffer;
112 _dataChunks[2] = default(global::Interop.HttpApi.HTTP_DATA_CHUNK);
113 _dataChunks[2].DataChunkType = global::Interop.HttpApi.HTTP_DATA_CHUNK_TYPE.HttpDataChunkFromMemory;
114 _dataChunks[2].BufferLength = (uint)s_CRLFArray.Length;
115 array[2] = s_CRLFArray;
116 }
117 else
118 {
119 _dataChunks[0] = default(global::Interop.HttpApi.HTTP_DATA_CHUNK);
120 _dataChunks[0].DataChunkType = global::Interop.HttpApi.HTTP_DATA_CHUNK_TYPE.HttpDataChunkFromMemory;
121 _dataChunks[0].BufferLength = (uint)size;
122 array[0] = buffer;
123 }
125 if (chunked)
126 {
127 _dataChunks[0].pBuffer = (byte*)(void*)Marshal.UnsafeAddrOfPinnedArrayElement(array2, offset2);
129 _dataChunks[2].pBuffer = (byte*)(void*)Marshal.UnsafeAddrOfPinnedArrayElement(s_CRLFArray, 0);
130 }
131 else
132 {
134 }
135 }
136
137 internal void IOCompleted(uint errorCode, uint numBytes)
138 {
139 IOCompleted(this, errorCode, numBytes);
140 }
141
142 private unsafe static void IOCompleted(HttpResponseStreamAsyncResult asyncResult, uint errorCode, uint numBytes)
143 {
144 if (System.Net.NetEventSource.Log.IsEnabled())
145 {
146 System.Net.NetEventSource.Info(null, $"errorCode:0x{errorCode:x8} numBytes: {numBytes}", "IOCompleted");
147 }
148 object obj = null;
149 try
150 {
151 if (errorCode != 0 && errorCode != 38)
152 {
153 asyncResult.ErrorCode = (int)errorCode;
154 obj = new HttpListenerException((int)errorCode);
155 }
156 else if (asyncResult._dataChunks == null)
157 {
158 obj = 0u;
159 if (System.Net.NetEventSource.Log.IsEnabled())
160 {
161 System.Net.NetEventSource.DumpBuffer(null, IntPtr.Zero, 0, "IOCompleted");
162 }
163 }
164 else
165 {
166 obj = ((asyncResult._dataChunks.Length == 1) ? asyncResult._dataChunks[0].BufferLength : 0u);
167 if (System.Net.NetEventSource.Log.IsEnabled())
168 {
169 for (int i = 0; i < asyncResult._dataChunks.Length; i++)
170 {
171 System.Net.NetEventSource.DumpBuffer(null, (IntPtr)asyncResult._dataChunks[0].pBuffer, (int)asyncResult._dataChunks[0].BufferLength, "IOCompleted");
172 }
173 }
174 }
175 if (System.Net.NetEventSource.Log.IsEnabled())
176 {
177 System.Net.NetEventSource.Info(null, "Calling Complete()", "IOCompleted");
178 }
179 }
180 catch (Exception ex)
181 {
182 obj = ex;
183 }
184 asyncResult.InvokeCallback(obj);
185 }
186
187 private unsafe static void Callback(uint errorCode, uint numBytes, NativeOverlapped* nativeOverlapped)
188 {
189 object nativeOverlappedState = ThreadPoolBoundHandle.GetNativeOverlappedState(nativeOverlapped);
191 if (System.Net.NetEventSource.Log.IsEnabled())
192 {
193 System.Net.NetEventSource.Info(null, "errorCode:0x" + errorCode.ToString("x8") + " numBytes:" + numBytes + " nativeOverlapped:0x" + ((IntPtr)nativeOverlapped).ToString("x8"), "Callback");
194 }
195 IOCompleted(asyncResult, errorCode, numBytes);
196 }
197
198 protected unsafe override void Cleanup()
199 {
200 base.Cleanup();
201 if (_pOverlapped != null)
202 {
204 }
205 }
206}
static unsafe readonly IOCompletionCallback s_IOCallback
unsafe HttpResponseStreamAsyncResult(object asyncObject, object userState, AsyncCallback callback, byte[] buffer, int offset, int size, bool chunked, bool sentHeaders, ThreadPoolBoundHandle boundHandle)
HttpResponseStreamAsyncResult(object asyncObject, object userState, AsyncCallback callback)
static unsafe void IOCompleted(HttpResponseStreamAsyncResult asyncResult, uint errorCode, uint numBytes)
static unsafe void Callback(uint errorCode, uint numBytes, NativeOverlapped *nativeOverlapped)
unsafe global::Interop.HttpApi.HTTP_DATA_CHUNK * pDataChunks
readonly global::Interop.HttpApi.HTTP_DATA_CHUNK[] _dataChunks
static byte[] GetChunkHeader(int size, out int offset)
static readonly System.Net.NetEventSource Log
static void Info(object thisOrContextObject, FormattableString formattableString=null, [CallerMemberName] string memberName=null)
static void DumpBuffer(object thisOrContextObject, byte[] buffer, int offset, int count, [CallerMemberName] string memberName=null)
static unsafe IntPtr UnsafeAddrOfPinnedArrayElement(Array arr, int index)
Definition Marshal.cs:775
unsafe void FreeNativeOverlapped(NativeOverlapped *overlapped)
unsafe NativeOverlapped * AllocateNativeOverlapped(IOCompletionCallback callback, object? state, object? pinData)
static unsafe? object GetNativeOverlappedState(NativeOverlapped *overlapped)
unsafe delegate void IOCompletionCallback(uint errorCode, uint numBytes, NativeOverlapped *pOVERLAP)
static readonly IntPtr Zero
Definition IntPtr.cs:18