Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
SmtpReplyReaderFactory.cs
Go to the documentation of this file.
2using System.IO;
3using System.Text;
4
5namespace System.Net.Mail;
6
7internal sealed class SmtpReplyReaderFactory
8{
9 private enum ReadState
10 {
11 Status0,
12 Status1,
13 Status2,
17 LastCR,
18 LastLF,
19 Done
20 }
21
22 private sealed class ReadLinesAsyncResult : System.Net.LazyAsyncResult
23 {
25
27
29
30 private static readonly AsyncCallback s_readCallback = ReadCallback;
31
32 private int _read;
33
34 private int _statusRead;
35
36 private readonly bool _oneLine;
37
38 internal ReadLinesAsyncResult(SmtpReplyReaderFactory parent, AsyncCallback callback, object state)
39 : base(null, state, callback)
40 {
41 _parent = parent;
42 }
43
44 internal ReadLinesAsyncResult(SmtpReplyReaderFactory parent, AsyncCallback callback, object state, bool oneLine)
45 : base(null, state, callback)
46 {
48 _parent = parent;
49 }
50
52 {
54 {
56 return;
57 }
58 if (_parent._byteBuffer == null)
59 {
60 _parent._byteBuffer = new byte[256];
61 }
62 _builder = new StringBuilder();
63 _lines = new List<LineInfo>();
64 Read();
65 }
66
67 internal static LineInfo[] End(IAsyncResult result)
68 {
70 readLinesAsyncResult.InternalWaitForCompletion();
71 return readLinesAsyncResult._lines.ToArray();
72 }
73
74 private void Read()
75 {
76 do
77 {
79 if (!asyncResult.CompletedSynchronously)
80 {
81 break;
82 }
84 }
85 while (ProcessRead());
86 }
87
88 private static void ReadCallback(IAsyncResult result)
89 {
90 if (result.CompletedSynchronously)
91 {
92 return;
93 }
94 Exception ex = null;
96 try
97 {
98 readLinesAsyncResult._read = readLinesAsyncResult._parent._bufferedStream.EndRead(result);
99 if (readLinesAsyncResult.ProcessRead())
100 {
102 }
103 }
104 catch (Exception ex2)
105 {
106 ex = ex2;
107 }
108 if (ex != null)
109 {
110 readLinesAsyncResult.InvokeCallback(ex);
111 }
112 }
113
114 private bool ProcessRead()
115 {
116 if (_read == 0)
117 {
119 }
120 int num = 0;
121 while (num != _read)
122 {
123 int num2 = _parent.ProcessRead(_parent._byteBuffer, num, _read - num, readLine: true);
124 if (_statusRead < 4)
125 {
126 int num3 = Math.Min(4 - _statusRead, num2);
127 _statusRead += num3;
128 num += num3;
129 num2 -= num3;
130 if (num2 == 0)
131 {
132 continue;
133 }
134 }
136 num += num2;
137 if (_parent._readState == ReadState.Status0)
138 {
140 _builder = new StringBuilder();
141 _statusRead = 0;
142 if (_oneLine)
143 {
146 return false;
147 }
148 }
149 else if (_parent._readState == ReadState.Done)
150 {
154 return false;
155 }
156 }
157 return true;
158 }
159 }
160
162
163 private byte[] _byteBuffer;
164
166
168
170
172
174
179
180 internal IAsyncResult BeginReadLines(SmtpReplyReader caller, AsyncCallback callback, object state)
181 {
185 }
186
187 internal IAsyncResult BeginReadLine(SmtpReplyReader caller, AsyncCallback callback, object state)
188 {
192 }
193
195 {
196 if (_currentReader != caller)
197 {
198 return;
199 }
200 if (_readState != ReadState.Done)
201 {
202 if (_byteBuffer == null)
203 {
204 _byteBuffer = new byte[256];
205 }
206 while (Read(caller, _byteBuffer, 0, _byteBuffer.Length) != 0)
207 {
208 }
209 }
210 _currentReader = null;
211 }
212
214 {
215 return ReadLinesAsyncResult.End(result);
216 }
217
219 {
221 if (array != null && array.Length != 0)
222 {
223 return array[0];
224 }
225 return default(LineInfo);
226 }
227
229 {
230 if (_currentReader != null)
231 {
233 }
234 _readState = ReadState.Status0;
236 return _currentReader;
237 }
238
239 private unsafe int ProcessRead(byte[] buffer, int offset, int read, bool readLine)
240 {
241 if (read == 0)
242 {
244 }
245 fixed (byte* ptr = buffer)
246 {
247 byte* ptr2 = ptr + offset;
248 byte* ptr3 = ptr2;
249 byte* ptr4 = ptr3 + read;
250 switch (_readState)
251 {
252 case ReadState.Status0:
253 if (ptr3 < ptr4)
254 {
255 byte b = *(ptr3++);
256 if (b < 48 && b > 57)
257 {
259 }
260 _statusCode = (SmtpStatusCode)(100 * (b - 48));
261 goto case ReadState.Status1;
262 }
263 _readState = ReadState.Status0;
264 break;
265 case ReadState.Status1:
266 if (ptr3 < ptr4)
267 {
268 byte b3 = *(ptr3++);
269 if (b3 < 48 && b3 > 57)
270 {
272 }
273 _statusCode += 10 * (b3 - 48);
274 goto case ReadState.Status2;
275 }
276 _readState = ReadState.Status1;
277 break;
278 case ReadState.Status2:
279 if (ptr3 < ptr4)
280 {
281 byte b2 = *(ptr3++);
282 if (b2 < 48 && b2 > 57)
283 {
285 }
286 _statusCode += b2 - 48;
287 goto case ReadState.ContinueFlag;
288 }
289 _readState = ReadState.Status2;
290 break;
291 case ReadState.ContinueFlag:
292 if (ptr3 < ptr4)
293 {
294 byte b4 = *(ptr3++);
295 if (b4 != 32)
296 {
297 if (b4 != 45)
298 {
300 }
301 goto case ReadState.ContinueCR;
302 }
303 goto case ReadState.LastCR;
304 }
305 _readState = ReadState.ContinueFlag;
306 break;
307 case ReadState.ContinueCR:
308 while (ptr3 < ptr4)
309 {
310 if (*(ptr3++) != 13)
311 {
312 continue;
313 }
314 goto case ReadState.ContinueLF;
315 }
316 _readState = ReadState.ContinueCR;
317 break;
318 case ReadState.ContinueLF:
319 if (ptr3 < ptr4)
320 {
321 if (*(ptr3++) != 10)
322 {
324 }
325 if (readLine)
326 {
327 _readState = ReadState.Status0;
328 return (int)(ptr3 - ptr2);
329 }
330 goto case ReadState.Status0;
331 }
332 _readState = ReadState.ContinueLF;
333 break;
334 case ReadState.LastCR:
335 while (ptr3 < ptr4)
336 {
337 if (*(ptr3++) != 13)
338 {
339 continue;
340 }
341 goto case ReadState.LastLF;
342 }
343 _readState = ReadState.LastCR;
344 break;
345 case ReadState.LastLF:
346 if (ptr3 < ptr4)
347 {
348 if (*(ptr3++) != 10)
349 {
351 }
352 goto case ReadState.Done;
353 }
354 _readState = ReadState.LastLF;
355 break;
356 case ReadState.Done:
357 {
358 int result = (int)(ptr3 - ptr2);
359 _readState = ReadState.Done;
360 return result;
361 }
362 }
363 return (int)(ptr3 - ptr2);
364 }
365 }
366
367 internal int Read(SmtpReplyReader caller, byte[] buffer, int offset, int count)
368 {
369 if (count == 0 || _currentReader != caller || _readState == ReadState.Done)
370 {
371 return 0;
372 }
374 int num2 = ProcessRead(buffer, offset, num, readLine: false);
375 if (num2 < num)
376 {
378 }
379 return num2;
380 }
381
383 {
385 if (array != null && array.Length != 0)
386 {
387 return array[0];
388 }
389 return default(LineInfo);
390 }
391
393 {
394 return ReadLines(caller, oneLine: false);
395 }
396
398 {
399 if (caller != _currentReader || _readState == ReadState.Done)
400 {
401 return Array.Empty<LineInfo>();
402 }
403 if (_byteBuffer == null)
404 {
405 _byteBuffer = new byte[256];
406 }
409 int num = 0;
410 int num2 = 0;
411 int num3 = 0;
412 while (true)
413 {
414 if (num2 == num3)
415 {
417 num2 = 0;
418 }
420 if (num < 4)
421 {
422 int num5 = Math.Min(4 - num, num4);
423 num += num5;
424 num2 += num5;
425 num4 -= num5;
426 if (num4 == 0)
427 {
428 continue;
429 }
430 }
431 stringBuilder.Append(Encoding.UTF8.GetString(_byteBuffer, num2, num4));
432 num2 += num4;
433 if (_readState == ReadState.Status0)
434 {
435 num = 0;
436 list.Add(new LineInfo(_statusCode, stringBuilder.ToString(0, stringBuilder.Length - 2)));
437 if (oneLine)
438 {
440 return list.ToArray();
441 }
443 }
444 else if (_readState == ReadState.Done)
445 {
446 break;
447 }
448 }
449 list.Add(new LineInfo(_statusCode, stringBuilder.ToString(0, stringBuilder.Length - 2)));
451 return list.ToArray();
452 }
453}
void Add(TKey key, TValue value)
static byte Min(byte val1, byte val2)
Definition Math.cs:912
override int Read(byte[] buffer, int offset, int count)
void Push(byte[] buffer, int offset, int count)
override int EndRead(IAsyncResult asyncResult)
override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback callback, object state)
ReadLinesAsyncResult(SmtpReplyReaderFactory parent, AsyncCallback callback, object state)
ReadLinesAsyncResult(SmtpReplyReaderFactory parent, AsyncCallback callback, object state, bool oneLine)
IAsyncResult BeginReadLine(SmtpReplyReader caller, AsyncCallback callback, object state)
LineInfo[] EndReadLines(IAsyncResult result)
int Read(SmtpReplyReader caller, byte[] buffer, int offset, int count)
unsafe int ProcessRead(byte[] buffer, int offset, int read, bool readLine)
IAsyncResult BeginReadLines(SmtpReplyReader caller, AsyncCallback callback, object state)
LineInfo[] ReadLines(SmtpReplyReader caller)
LineInfo ReadLine(SmtpReplyReader caller)
LineInfo[] ReadLines(SmtpReplyReader caller, bool oneLine)
static string net_io_readfailure
Definition SR.cs:158
static string Format(string resourceFormat, object p1)
Definition SR.cs:118
static string net_io_connectionclosed
Definition SR.cs:160
static string SmtpInvalidResponse
Definition SR.cs:162
Definition SR.cs:7
static Encoding UTF8
Definition Encoding.cs:526
override string ToString()
StringBuilder Append(char value, int repeatCount)