Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions DXMainClient/Domain/Multiplayer/CnCNet/CnCNetPlayerCountTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ public static class CnCNetPlayerCountTask
public static void InitializeService(CancellationTokenSource cts)
{
cncnetLiveStatusIdentifier = ClientConfiguration.Instance.CnCNetLiveStatusIdentifier;
PlayerCount = GetCnCNetPlayerCount();

// This call is synchronous. Therefore, we use a short timeout to avoid blocking the main thread for too long.
PlayerCount = GetCnCNetPlayerCount(timeoutMilliseconds: 1000);

CnCNetGameCountUpdated?.Invoke(null, new PlayerCountEventArgs(PlayerCount));
ThreadPool.QueueUserWorkItem(new WaitCallback(RunService), cts);
Expand All @@ -41,12 +43,12 @@ private static void RunService(object tokenObj)
}
else
{
CnCNetGameCountUpdated?.Invoke(null, new PlayerCountEventArgs(GetCnCNetPlayerCount()));
CnCNetGameCountUpdated?.Invoke(null, new PlayerCountEventArgs(GetCnCNetPlayerCount(timeoutMilliseconds: 5000)));
}
}
}

private static int GetCnCNetPlayerCount()
private static int GetCnCNetPlayerCount(int timeoutMilliseconds = 5000)
{
try
{
Expand All @@ -56,7 +58,7 @@ private static int GetCnCNetPlayerCount()
if (string.IsNullOrWhiteSpace(ClientConfiguration.Instance.CnCNetPlayerCountURL))
return -1;

WebClient client = new ExtendedWebClient();
WebClient client = new ExtendedWebClient(timeout: timeoutMilliseconds);
Stream data = client.OpenRead(ClientConfiguration.Instance.CnCNetPlayerCountURL);

string info = string.Empty;
Expand Down
7 changes: 7 additions & 0 deletions DXMainClient/Domain/Multiplayer/CnCNet/ExtendedWebClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,15 @@ namespace DTAClient.Domain.Multiplayer.CnCNet
/// </summary>
class ExtendedWebClient : WebClient
{
/// <summary>
/// Initializes a new instance of the <see cref="ExtendedWebClient"/> class with a default timeout of 10 seconds.
/// </summary>
public ExtendedWebClient() : this(timeout: 10000) { }

/// <summary>
/// Initializes a new instance of the <see cref="ExtendedWebClient"/> class with a specified timeout.
/// </summary>
/// <param name="timeout">Gets or sets the length of time, in milliseconds, before the request times out.</param>
public ExtendedWebClient(int timeout)
{
this.timeout = timeout;
Expand Down
Loading