Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
MockConnection.cs
Go to the documentation of this file.
9
11
13{
14 internal sealed class StreamLimit
15 {
16 public readonly int MaxCount;
17
18 private int _actualCount;
19
20 private TaskCompletionSource _availableTcs = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
21
22 private readonly object _syncRoot = new object();
23
25
27 {
29 }
30
31 public void Decrement()
32 {
35 {
38 {
40 _availableTcs = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
41 }
42 }
43 taskCompletionSource?.SetResult();
44 }
45
46 public bool TryIncrement()
47 {
49 {
51 {
53 return true;
54 }
55 return false;
56 }
57 }
58
60 {
63 {
64 if (_actualCount > 0)
65 {
66 return default(ValueTask);
67 }
69 }
70 return new ValueTask(availableTcs.Task.WaitAsync(cancellationToken));
71 }
72
77 }
78
91
121
122 private readonly bool _isClient;
123
124 private bool _disposed;
125
127
129
131
132 private object _syncObject = new object();
133
135
137
138 private readonly int _maxUnidirectionalStreams;
139
140 private readonly int _maxBidirectionalStreams;
141
143
145 {
146 get
147 {
148 if (!_isClient)
149 {
151 }
153 }
154 }
155
157 {
158 get
159 {
160 if (!_isClient)
161 {
163 }
165 }
166 }
167
168 internal long? ConnectionError
169 {
170 get
171 {
172 long? num = ((!_isClient) ? _state?._clientErrorCode : _state?._serverErrorCode);
173 if (num == -1)
174 {
175 num = null;
176 }
177 return num;
178 }
179 }
180
181 internal override X509Certificate RemoteCertificate => null;
182
183 internal override bool Connected
184 {
185 get
186 {
188 return _state != null;
189 }
190 }
191
193
195
197 {
198 get
199 {
200 if (_state == null)
201 {
202 throw new InvalidOperationException("not connected");
203 }
205 }
206 }
207
228
238
240 {
241 if (endPoint is IPEndPoint result)
242 {
243 return result;
244 }
246 {
247 if (dnsEndPoint.Host == "127.0.0.1")
248 {
249 return new IPEndPoint(IPAddress.Loopback, dnsEndPoint.Port);
250 }
251 throw new InvalidOperationException("invalid DNS name " + dnsEndPoint.Host);
252 }
253 throw new InvalidOperationException("unknown EndPoint type");
254 }
255
257 {
259 if (Connected)
260 {
261 throw new InvalidOperationException("Already connected");
262 }
264 if (mockListener == null)
265 {
266 throw new InvalidOperationException("Could not find listener");
267 }
269 {
271 };
272 if (!mockListener.TryConnect(_state))
273 {
274 throw new QuicException("Connection refused");
275 }
277 }
278
280 {
282 if (remoteStreamLimit == null)
283 {
284 throw new InvalidOperationException("Not connected");
285 }
286 return remoteStreamLimit.Unidirectional.WaitForAvailableStreams(cancellationToken);
287 }
288
290 {
292 if (remoteStreamLimit == null)
293 {
294 throw new InvalidOperationException("Not connected");
295 }
296 return remoteStreamLimit.Bidirectional.WaitForAvailableStreams(cancellationToken);
297 }
298
300 {
302 if (remoteStreamLimit == null)
303 {
304 throw new InvalidOperationException("Not connected");
305 }
306 if (!remoteStreamLimit.Unidirectional.TryIncrement())
307 {
308 throw new QuicException("No available unidirectional stream");
309 }
312 {
315 }
317 }
318
320 {
322 if (remoteStreamLimit == null)
323 {
324 throw new InvalidOperationException("Not connected");
325 }
326 if (!remoteStreamLimit.Bidirectional.TryIncrement())
327 {
328 throw new QuicException("No available bidirectional stream");
329 }
332 {
335 }
337 }
338
340 {
343 if (state == null)
344 {
345 throw new InvalidOperationException("Not connected");
346 }
348 state._streams[streamState._streamId] = streamState;
349 Channel<MockStream.StreamState> channel = (_isClient ? state._clientInitiatedStreamChannel : state._serverInitiatedStreamChannel);
350 channel.Writer.TryWrite(streamState);
351 return new MockStream(this, streamState, isInitiator: true);
352 }
353
355 {
357 if (remoteStreamLimit == null)
358 {
359 throw new InvalidOperationException("Not connected");
360 }
361 return remoteStreamLimit.Unidirectional.AvailableCount;
362 }
363
365 {
367 if (remoteStreamLimit == null)
368 {
369 throw new InvalidOperationException("Not connected");
370 }
371 return remoteStreamLimit.Bidirectional.AvailableCount;
372 }
373
375 {
378 if (state == null)
379 {
380 throw new InvalidOperationException("Not connected");
381 }
382 Channel<MockStream.StreamState> channel = (_isClient ? state._serverInitiatedStreamChannel : state._clientInitiatedStreamChannel);
383 try
384 {
385 return new MockStream(this, await channel.Reader.ReadAsync(cancellationToken).ConfigureAwait(continueOnCapturedContext: false), isInitiator: false);
386 }
388 {
389 long num = (_isClient ? state._serverErrorCode : state._clientErrorCode);
391 }
392 }
393
395 {
397 if (state != null)
398 {
399 if (state._closed)
400 {
401 return default(ValueTask);
402 }
403 state._closed = true;
404 if (_isClient)
405 {
406 state._clientErrorCode = errorCode;
408 }
409 else
410 {
411 state._serverErrorCode = errorCode;
413 }
415 {
416 stream.Value._outboundWritesCompletedTcs.TrySetException(new QuicConnectionAbortedException(errorCode));
417 stream.Value._inboundWritesCompletedTcs.TrySetException(new QuicConnectionAbortedException(errorCode));
418 }
419 }
420 Dispose();
421 return default(ValueTask);
422 }
423
424 private void CheckDisposed()
425 {
426 if (_disposed)
427 {
428 throw new ObjectDisposedException("QuicConnection");
429 }
430 }
431
433 {
435 if (state != null)
436 {
437 state._clientInitiatedStreamChannel.Writer.TryComplete();
439 while (state._clientInitiatedStreamChannel.Reader.TryRead(out item))
440 {
441 item._outboundReadErrorCode = (item._outboundWriteErrorCode = outboundErrorCode);
442 item._inboundStreamBuffer?.AbortRead();
443 item._outboundStreamBuffer?.EndWrite();
444 }
445 state._serverInitiatedStreamChannel.Writer.TryComplete();
447 while (state._serverInitiatedStreamChannel.Reader.TryRead(out item2))
448 {
449 item2._inboundReadErrorCode = (item2._inboundWriteErrorCode = inboundErrorCode);
450 item2._outboundStreamBuffer?.AbortRead();
451 item2._inboundStreamBuffer?.EndWrite();
452 }
453 }
454 }
455
456 private void Dispose(bool disposing)
457 {
458 if (_disposed)
459 {
460 return;
461 }
462 if (disposing)
463 {
464 DrainAcceptQueue(-1L, -1L);
466 if (localStreamLimit != null)
467 {
468 localStreamLimit.Unidirectional.CloseWaiters();
469 localStreamLimit.Bidirectional.CloseWaiters();
470 }
471 }
472 _disposed = true;
473 }
474
476 {
477 Dispose(disposing: false);
478 }
479
480 public override void Dispose()
481 {
482 Dispose(disposing: true);
483 GC.SuppressFinalize(this);
484 }
485}
static void SuppressFinalize(object obj)
Definition GC.cs:202
Definition GC.cs:8
static readonly IPAddress Loopback
Definition IPAddress.cs:21
readonly ConcurrentDictionary< long, MockStream.StreamState > _streams
readonly Channel< MockStream.StreamState > _serverInitiatedStreamChannel
readonly Channel< MockStream.StreamState > _clientInitiatedStreamChannel
ConnectionState(SslApplicationProtocol applicationProtocol)
PeerStreamLimit(int maxUnidirectional, int maxBidirectional)
ValueTask WaitForAvailableStreams(CancellationToken cancellationToken)
override QuicStreamProvider OpenBidirectionalStream()
override ValueTask WaitForAvailableBidirectionalStreamsAsync(CancellationToken cancellationToken=default(CancellationToken))
override ValueTask WaitForAvailableUnidirectionalStreamsAsync(CancellationToken cancellationToken=default(CancellationToken))
void DrainAcceptQueue(long outboundErrorCode, long inboundErrorCode)
override ValueTask ConnectAsync(CancellationToken cancellationToken=default(CancellationToken))
static IPEndPoint GetIPEndPoint(EndPoint endPoint)
override ValueTask CloseAsync(long errorCode, CancellationToken cancellationToken=default(CancellationToken))
MockConnection(EndPoint remoteEndPoint, SslClientAuthenticationOptions sslClientAuthenticationOptions, IPEndPoint localEndPoint=null, int maxUnidirectionalStreams=100, int maxBidirectionalStreams=100)
override async ValueTask< QuicStreamProvider > AcceptStreamAsync(CancellationToken cancellationToken=default(CancellationToken))
override SslApplicationProtocol NegotiatedApplicationProtocol
MockConnection(IPEndPoint localEndPoint, ConnectionState state)
override QuicStreamProvider OpenUnidirectionalStream()
MockStream OpenStream(long streamId, bool bidirectional)
SslClientAuthenticationOptions _sslClientAuthenticationOptions
static MockListener TryGetListener(IPEndPoint endpoint)
static ValueTask CompletedTask
Definition ValueTask.cs:71