Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
AsyncStreamReader.cs
Go to the documentation of this file.
2using System.IO;
4using System.Text;
7
8namespace System.Diagnostics;
9
10internal sealed class AsyncStreamReader : IDisposable
11{
12 private readonly Stream _stream;
13
14 private readonly Decoder _decoder;
15
16 private readonly byte[] _byteBuffer;
17
18 private readonly char[] _charBuffer;
19
20 private readonly Action<string> _userCallBack;
21
22 private readonly CancellationTokenSource _cts;
23
25
26 private readonly Queue<string> _messageQueue;
27
29
31
32 private bool _cancelOperation;
33
34 private int _currentLinePos;
35
37
39 {
41 _userCallBack = callback;
42 _decoder = encoding.GetDecoder();
43 _byteBuffer = new byte[1024];
44 int maxCharCount = encoding.GetMaxCharCount(1024);
45 _charBuffer = new char[maxCharCount];
48 }
49
50 internal void BeginReadLine()
51 {
52 _cancelOperation = false;
53 if (_sb == null)
54 {
55 _sb = new StringBuilder(1024);
57 }
58 else
59 {
61 }
62 }
63
64 internal void CancelOperation()
65 {
66 _cancelOperation = true;
67 }
68
70 {
71 while (true)
72 {
73 try
74 {
76 if (num == 0)
77 {
78 break;
79 }
83 goto IL_00e2;
84 }
85 catch (IOException)
86 {
87 }
89 {
90 }
91 break;
92 IL_00e2:
94 {
95 return;
96 }
97 }
99 {
100 if (_sb.Length != 0)
101 {
103 _sb.Length = 0;
104 }
106 }
108 }
109
111 {
112 int i = _currentLinePos;
113 int num = 0;
114 int length = _sb.Length;
115 if (_bLastCarriageReturn && length > 0 && _sb[0] == '\n')
116 {
117 i = 1;
118 num = 1;
119 _bLastCarriageReturn = false;
120 }
121 for (; i < length; i++)
122 {
123 char c = _sb[i];
124 if (c == '\r' || c == '\n')
125 {
126 string item = _sb.ToString(num, i - num);
127 num = i + 1;
128 if (c == '\r' && num < length && _sb[num] == '\n')
129 {
130 num++;
131 i++;
132 }
134 {
136 }
137 }
138 }
139 if (length > 0 && _sb[length - 1] == '\r')
140 {
142 }
143 if (num < length)
144 {
145 if (num == 0)
146 {
147 _currentLinePos = i;
148 return;
149 }
150 _sb.Remove(0, num);
151 _currentLinePos = 0;
152 }
153 else
154 {
155 _sb.Length = 0;
156 _currentLinePos = 0;
157 }
158 }
159
161 {
162 try
163 {
164 while (true)
165 {
166 string obj;
168 {
169 if (_messageQueue.Count == 0)
170 {
171 break;
172 }
174 goto IL_0038;
175 }
176 IL_0038:
177 if (!_cancelOperation)
178 {
180 }
181 }
182 return false;
183 }
184 catch (Exception source)
185 {
187 {
189 {
190 ((ExceptionDispatchInfo)edi).Throw();
192 return true;
193 }
194 throw;
195 }
196 }
197
198 public void Dispose()
199 {
200 _cts.Cancel();
201 }
202}
AsyncStreamReader(Stream stream, Action< string > callback, Encoding encoding)
readonly Queue< string > _messageQueue
readonly CancellationTokenSource _cts
bool FlushMessageQueue(bool rethrowInNewThread)
readonly Action< string > _userCallBack
Task< int > ReadAsync(byte[] buffer, int offset, int count)
Definition Stream.cs:762
static ExceptionDispatchInfo Capture(Exception source)
int GetChars(byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex)
int GetMaxCharCount(int byteCount)
virtual Decoder GetDecoder()
Definition Encoding.cs:1004
StringBuilder Remove(int startIndex, int length)
override string ToString()
StringBuilder Append(char value, int repeatCount)
new ConfiguredTaskAwaitable< TResult > ConfigureAwait(bool continueOnCapturedContext)
Definition Task.cs:226
static Task Run(Action action)
Definition Task.cs:3395
static Task CompletedTask
Definition Task.cs:1120
static bool QueueUserWorkItem(WaitCallback callBack)