Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
MockStream.cs
Go to the documentation of this file.
2using System.IO;
5
7
8internal sealed class MockStream : QuicStreamProvider
9{
10 internal sealed class StreamState
11 {
12 public readonly long _streamId;
13
15
17
19
21
23
25
27
29
30 public StreamState(long streamId, bool bidirectional)
31 {
32 _streamId = streamId;
34 _inboundStreamBuffer = (bidirectional ? new StreamBuffer() : null);
35 _outboundWritesCompletedTcs = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
36 _inboundWritesCompletedTcs = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
37 }
38 }
39
40 private bool _disposed;
41
42 private readonly bool _isInitiator;
43
44 private readonly MockConnection _connection;
45
46 private readonly StreamState _streamState;
47
48 private bool _writesCanceled;
49
50 internal override long StreamId
51 {
52 get
53 {
56 }
57 }
58
60 {
61 get
62 {
63 if (!_isInitiator)
64 {
66 }
68 }
69 }
70
71 internal override bool CanTimeout => false;
72
73 internal override int ReadTimeout
74 {
75 get
76 {
77 throw new InvalidOperationException();
78 }
79 set
80 {
81 throw new InvalidOperationException();
82 }
83 }
84
85 internal override int WriteTimeout
86 {
87 get
88 {
89 throw new InvalidOperationException();
90 }
91 set
92 {
93 throw new InvalidOperationException();
94 }
95 }
96
97 internal override bool CanRead
98 {
99 get
100 {
101 if (!_disposed)
102 {
103 return ReadStreamBuffer != null;
104 }
105 return false;
106 }
107 }
108
109 internal override bool ReadsCompleted => ReadStreamBuffer?.IsComplete ?? false;
110
112 {
113 get
114 {
115 if (!_isInitiator)
116 {
118 }
120 }
121 }
122
123 internal override bool CanWrite
124 {
125 get
126 {
127 if (!_disposed)
128 {
129 return WriteStreamBuffer != null;
130 }
131 return false;
132 }
133 }
134
136 {
137 get
138 {
139 if (!_isInitiator)
140 {
142 }
144 }
145 }
146
147 internal MockStream(MockConnection connection, StreamState streamState, bool isInitiator)
148 {
149 _connection = connection;
150 _streamState = streamState;
151 _isInitiator = isInitiator;
152 }
153
154 internal override int Read(Span<byte> buffer)
155 {
157 StreamBuffer readStreamBuffer = ReadStreamBuffer;
158 if (readStreamBuffer == null)
159 {
160 throw new NotSupportedException();
161 }
162 return readStreamBuffer.Read(buffer);
163 }
164
166 {
168 StreamBuffer readStreamBuffer = ReadStreamBuffer;
169 if (readStreamBuffer == null)
170 {
171 throw new NotSupportedException();
172 }
173 int num = await readStreamBuffer.ReadAsync(buffer, cancellationToken).ConfigureAwait(continueOnCapturedContext: false);
174 if (num == 0)
175 {
176 long? connectionError = _connection.ConnectionError;
177 if (connectionError.HasValue)
178 {
179 long valueOrDefault = connectionError.GetValueOrDefault();
180 throw new QuicConnectionAbortedException(valueOrDefault);
181 }
182 long num2 = (_isInitiator ? _streamState._inboundReadErrorCode : _streamState._outboundReadErrorCode);
183 if (num2 != 0L)
184 {
185 throw (num2 == -1) ? ((QuicException)new QuicOperationAbortedException()) : ((QuicException)new QuicStreamAbortedException(num2));
186 }
187 }
188 return num;
189 }
190
191 internal override void Write(ReadOnlySpan<byte> buffer)
192 {
195 {
196 throw new OperationCanceledException();
197 }
198 StreamBuffer writeStreamBuffer = WriteStreamBuffer;
199 if (writeStreamBuffer == null)
200 {
201 throw new NotSupportedException();
202 }
203 writeStreamBuffer.Write(buffer);
204 }
205
210
212 {
215 {
216 cancellationToken.ThrowIfCancellationRequested();
217 throw new OperationCanceledException();
218 }
219 StreamBuffer streamBuffer = WriteStreamBuffer;
220 if (streamBuffer == null)
221 {
222 throw new NotSupportedException();
223 }
224 long? connectionError = _connection.ConnectionError;
225 if (connectionError.HasValue)
226 {
227 long valueOrDefault = connectionError.GetValueOrDefault();
228 throw new QuicConnectionAbortedException(valueOrDefault);
229 }
230 long num = (_isInitiator ? _streamState._inboundWriteErrorCode : _streamState._outboundWriteErrorCode);
231 if (num != 0L)
232 {
233 throw new QuicStreamAbortedException(num);
234 }
235 using (cancellationToken.UnsafeRegister(delegate(object s)
236 {
237 MockStream mockStream = (MockStream)s;
238 Volatile.Write(ref mockStream._writesCanceled, value: true);
239 }, this))
240 {
241 await streamBuffer.WriteAsync(buffer, cancellationToken).ConfigureAwait(continueOnCapturedContext: false);
242 if (endStream)
243 {
244 streamBuffer.EndWrite();
246 }
247 }
248 }
249
254
256 {
257 throw new NotImplementedException();
258 }
259
261 {
262 for (int i = 0; i < buffers.Length; i++)
263 {
264 await WriteAsync(buffers.Span[i], cancellationToken).ConfigureAwait(continueOnCapturedContext: false);
265 }
266 }
267
272
273 internal override void Flush()
274 {
276 }
277
279 {
281 return Task.CompletedTask;
282 }
283
284 internal override void AbortRead(long errorCode)
285 {
286 if (_isInitiator)
287 {
288 _streamState._outboundWriteErrorCode = errorCode;
290 }
291 else
292 {
293 _streamState._inboundWriteErrorCode = errorCode;
295 }
297 }
298
299 internal override void AbortWrite(long errorCode)
300 {
301 if (_isInitiator)
302 {
303 _streamState._outboundReadErrorCode = errorCode;
305 }
306 else
307 {
308 _streamState._inboundReadErrorCode = errorCode;
310 }
312 }
313
315 {
317 return default(ValueTask);
318 }
319
334
335 private void CheckDisposed()
336 {
337 if (_disposed)
338 {
339 throw new ObjectDisposedException("QuicStream");
340 }
341 }
342
343 public override void Dispose()
344 {
345 if (!_disposed)
346 {
347 Shutdown();
348 _disposed = true;
349 }
350 }
351
352 public override ValueTask DisposeAsync()
353 {
354 if (!_disposed)
355 {
356 Shutdown();
357 _disposed = true;
358 }
359 return default(ValueTask);
360 }
361
367}
async ValueTask< int > ReadAsync(Memory< byte > buffer, CancellationToken cancellationToken=default(CancellationToken))
int Read(Span< byte > buffer)
void Write(ReadOnlySpan< byte > buffer)
async ValueTask WriteAsync(ReadOnlyMemory< byte > buffer, CancellationToken cancellationToken=default(CancellationToken))
override ValueTask WaitForWriteCompletionAsync(CancellationToken cancellationToken=default(CancellationToken))
override int Read(Span< byte > buffer)
override ValueTask WriteAsync(ReadOnlyMemory< ReadOnlyMemory< byte > > buffers, bool endStream, CancellationToken cancellationToken=default(CancellationToken))
override ValueTask WriteAsync(ReadOnlySequence< byte > buffers, bool endStream, CancellationToken cancellationToken=default(CancellationToken))
override void AbortWrite(long errorCode)
override void AbortRead(long errorCode)
override async ValueTask WriteAsync(ReadOnlyMemory< byte > buffer, bool endStream, CancellationToken cancellationToken=default(CancellationToken))
override async ValueTask WriteAsync(ReadOnlyMemory< ReadOnlyMemory< byte > > buffers, CancellationToken cancellationToken=default(CancellationToken))
override void Write(ReadOnlySpan< byte > buffer)
override ValueTask WriteAsync(ReadOnlyMemory< byte > buffer, CancellationToken cancellationToken=default(CancellationToken))
override ValueTask ShutdownCompleted(CancellationToken cancellationToken=default(CancellationToken))
MockStream(MockConnection connection, StreamState streamState, bool isInitiator)
override async ValueTask< int > ReadAsync(Memory< byte > buffer, CancellationToken cancellationToken=default(CancellationToken))
override Task FlushAsync(CancellationToken cancellationToken)
override ValueTask WriteAsync(ReadOnlySequence< byte > buffers, CancellationToken cancellationToken=default(CancellationToken))
static Task CompletedTask
Definition Task.cs:1120
static bool Read(ref bool location)
Definition Volatile.cs:67
static void Write(ref bool location, bool value)
Definition Volatile.cs:74
ConfiguredValueTaskAwaitable ConfigureAwait(bool continueOnCapturedContext)
Definition ValueTask.cs:312