Skip to content

Commit 66fe24b

Browse files
authored
Use a shorter timeout for CnCNet player count retrieval (CnCNet#896)
1 parent 60b26af commit 66fe24b

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

DXMainClient/Domain/Multiplayer/CnCNet/CnCNetPlayerCountTask.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ public static class CnCNetPlayerCountTask
2222
public static void InitializeService(CancellationTokenSource cts)
2323
{
2424
cncnetLiveStatusIdentifier = ClientConfiguration.Instance.CnCNetLiveStatusIdentifier;
25-
PlayerCount = GetCnCNetPlayerCount();
25+
26+
// This call is synchronous. Therefore, we use a short timeout to avoid blocking the main thread for too long.
27+
PlayerCount = GetCnCNetPlayerCount(timeoutMilliseconds: 1000);
2628

2729
CnCNetGameCountUpdated?.Invoke(null, new PlayerCountEventArgs(PlayerCount));
2830
ThreadPool.QueueUserWorkItem(new WaitCallback(RunService), cts);
@@ -41,12 +43,12 @@ private static void RunService(object tokenObj)
4143
}
4244
else
4345
{
44-
CnCNetGameCountUpdated?.Invoke(null, new PlayerCountEventArgs(GetCnCNetPlayerCount()));
46+
CnCNetGameCountUpdated?.Invoke(null, new PlayerCountEventArgs(GetCnCNetPlayerCount(timeoutMilliseconds: 5000)));
4547
}
4648
}
4749
}
4850

49-
private static int GetCnCNetPlayerCount()
51+
private static int GetCnCNetPlayerCount(int timeoutMilliseconds = 5000)
5052
{
5153
try
5254
{
@@ -56,7 +58,7 @@ private static int GetCnCNetPlayerCount()
5658
if (string.IsNullOrWhiteSpace(ClientConfiguration.Instance.CnCNetPlayerCountURL))
5759
return -1;
5860

59-
WebClient client = new ExtendedWebClient();
61+
WebClient client = new ExtendedWebClient(timeout: timeoutMilliseconds);
6062
Stream data = client.OpenRead(ClientConfiguration.Instance.CnCNetPlayerCountURL);
6163

6264
string info = string.Empty;

DXMainClient/Domain/Multiplayer/CnCNet/ExtendedWebClient.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,15 @@ namespace DTAClient.Domain.Multiplayer.CnCNet
88
/// </summary>
99
class ExtendedWebClient : WebClient
1010
{
11+
/// <summary>
12+
/// Initializes a new instance of the <see cref="ExtendedWebClient"/> class with a default timeout of 10 seconds.
13+
/// </summary>
1114
public ExtendedWebClient() : this(timeout: 10000) { }
1215

16+
/// <summary>
17+
/// Initializes a new instance of the <see cref="ExtendedWebClient"/> class with a specified timeout.
18+
/// </summary>
19+
/// <param name="timeout">Gets or sets the length of time, in milliseconds, before the request times out.</param>
1320
public ExtendedWebClient(int timeout)
1421
{
1522
this.timeout = timeout;

0 commit comments

Comments
 (0)