tgstation-server 6.12.0
The /tg/station 13 server suite
Loading...
Searching...
No Matches
TopicClientExtensions.cs
Go to the documentation of this file.
1using System;
2using System.Net;
3using System.Threading;
4using System.Threading.Tasks;
5
6using Byond.TopicSender;
7
8using Microsoft.Extensions.Logging;
9
11
13{
18 {
22 static ulong topicRequestId;
23
35 public static async ValueTask<TopicResponse?> SendWithOptionalPriority(
36 this ITopicClient topicClient,
37 IAsyncDelayer delayer,
38 ILogger logger,
39 string queryString,
40 ushort port,
41 bool priority,
42 CancellationToken cancellationToken)
43 {
44 const int PrioritySendAttempts = 5;
45 var endpoint = new IPEndPoint(IPAddress.Loopback, port);
46 var firstSend = true;
47
48 for (var i = PrioritySendAttempts - 1; i >= 0 && (priority || firstSend); --i)
49 try
50 {
51 firstSend = false;
52
53 var localRequestId = Interlocked.Increment(ref topicRequestId);
54 logger.LogTrace("Begin topic request #{requestId}: {query}", localRequestId, queryString);
55 var byondResponse = await topicClient.SendTopic(
56 endpoint,
57 queryString,
58 cancellationToken);
59
60 logger.LogTrace("End topic request #{requestId}", localRequestId);
61 return byondResponse;
62 }
63 catch (Exception ex) when (ex is not OperationCanceledException && ex is not ArgumentException)
64 {
65 logger.LogWarning(ex, "SendTopic exception!{retryDetails}", priority ? $" {i} attempts remaining." : String.Empty);
66
67 if (priority && i > 0)
68 await delayer.Delay(TimeSpan.FromSeconds(2), cancellationToken);
69 }
70
71 return null;
72 }
73 }
74}
static async ValueTask< TopicResponse?> SendWithOptionalPriority(this ITopicClient topicClient, IAsyncDelayer delayer, ILogger logger, string queryString, ushort port, bool priority, CancellationToken cancellationToken)
Send a queryString with optional repeated priority.
static ulong topicRequestId
Counter for topic request logging.
ValueTask Delay(TimeSpan timeSpan, CancellationToken cancellationToken)
Create a Task that completes after a given timeSpan .