Skip to content

Commit

Permalink
Reduce default timeout to 10 seconds (#575)
Browse files Browse the repository at this point in the history
* Reduce default timeout to 10 seconds

* Don't remove the proxy setting when downloading a map

* Specify map download and upload timeouts as 100 seconds
  • Loading branch information
SadPencil authored Nov 3, 2024
1 parent 559c7f4 commit 6dc4a6d
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ private static int GetCnCNetPlayerCount()
{
// Don't fetch the player count if it is explicitly disabled
// For example, the official CnCNet server might be unavailable/unstable in a country with Internet censorship,
// which causes lags in the splash screen. In the worst case, say if packets are dropped, it waits until timeouts --- 30 seconds
// which causes lags in the splash screen. In the worst case, say if packets are dropped, it waits until timeouts
if (string.IsNullOrWhiteSpace(ClientConfiguration.Instance.CnCNetPlayerCountURL))
return -1;

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

string info = string.Empty;
Expand Down
2 changes: 2 additions & 0 deletions DXMainClient/Domain/Multiplayer/CnCNet/ExtendedWebClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ namespace DTAClient.Domain.Multiplayer.CnCNet
/// </summary>
class ExtendedWebClient : WebClient
{
public ExtendedWebClient() : this(timeout: 10000) { }

public ExtendedWebClient(int timeout)

Check warning on line 13 in DXMainClient/Domain/Multiplayer/CnCNet/ExtendedWebClient.cs

View workflow job for this annotation

GitHub Actions / build-clients (Ares)

'WebClient.WebClient()' is obsolete: 'WebRequest, HttpWebRequest, ServicePoint, and WebClient are obsolete. Use HttpClient instead.' (https://aka.ms/dotnet-warnings/SYSLIB0014)

Check warning on line 13 in DXMainClient/Domain/Multiplayer/CnCNet/ExtendedWebClient.cs

View workflow job for this annotation

GitHub Actions / build-clients (TS)

'WebClient.WebClient()' is obsolete: 'WebRequest, HttpWebRequest, ServicePoint, and WebClient are obsolete. Use HttpClient instead.' (https://aka.ms/dotnet-warnings/SYSLIB0014)

Check warning on line 13 in DXMainClient/Domain/Multiplayer/CnCNet/ExtendedWebClient.cs

View workflow job for this annotation

GitHub Actions / build-clients (YR)

'WebClient.WebClient()' is obsolete: 'WebRequest, HttpWebRequest, ServicePoint, and WebClient are obsolete. Use HttpClient instead.' (https://aka.ms/dotnet-warnings/SYSLIB0014)
{
this.timeout = timeout;
Expand Down
27 changes: 5 additions & 22 deletions DXMainClient/Domain/Multiplayer/CnCNet/MapSharer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ public static class MapSharer

private static readonly object locker = new object();

private const int DOWNLOAD_TIMEOUT = 100000; // In milliseconds
private const int UPLOAD_TIMEOUT = 100000; // In milliseconds

/// <summary>
/// Adds a map into the CnCNet map upload queue.
/// </summary>
Expand Down Expand Up @@ -211,6 +214,7 @@ private static void CopyStream(Stream input, Stream output)
private static byte[] UploadFiles(string address, List<FileToUpload> files, NameValueCollection values)
{
WebRequest request = WebRequest.Create(address);

Check warning on line 216 in DXMainClient/Domain/Multiplayer/CnCNet/MapSharer.cs

View workflow job for this annotation

GitHub Actions / build-clients (Ares)

'WebRequest.Create(string)' is obsolete: 'WebRequest, HttpWebRequest, ServicePoint, and WebClient are obsolete. Use HttpClient instead.' (https://aka.ms/dotnet-warnings/SYSLIB0014)

Check warning on line 216 in DXMainClient/Domain/Multiplayer/CnCNet/MapSharer.cs

View workflow job for this annotation

GitHub Actions / build-clients (TS)

'WebRequest.Create(string)' is obsolete: 'WebRequest, HttpWebRequest, ServicePoint, and WebClient are obsolete. Use HttpClient instead.' (https://aka.ms/dotnet-warnings/SYSLIB0014)

Check warning on line 216 in DXMainClient/Domain/Multiplayer/CnCNet/MapSharer.cs

View workflow job for this annotation

GitHub Actions / build-clients (YR)

'WebRequest.Create(string)' is obsolete: 'WebRequest, HttpWebRequest, ServicePoint, and WebClient are obsolete. Use HttpClient instead.' (https://aka.ms/dotnet-warnings/SYSLIB0014)
request.Timeout = UPLOAD_TIMEOUT;
request.Method = "POST";
string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x", NumberFormatInfo.InvariantInfo);
request.ContentType = "multipart/form-data; boundary=" + boundary;
Expand Down Expand Up @@ -382,11 +386,8 @@ private static string DownloadMain(string sha1, string myGame, string mapName, o
destinationFile.Delete();
newFile.Delete();

using (TWebClient webClient = new TWebClient())
using (WebClient webClient = new ExtendedWebClient(DOWNLOAD_TIMEOUT))
{
// TODO enable proxy support for some users
webClient.Proxy = null;

if (string.IsNullOrWhiteSpace(ClientConfiguration.Instance.CnCNetMapDBDownloadURL))
{
success = false;
Expand Down Expand Up @@ -456,23 +457,5 @@ public FileToUpload()
public string ContentType { get; set; }
public Stream Stream { get; set; }
}

class TWebClient : WebClient
{
private int Timeout = 10000;

public TWebClient()
{
// TODO enable proxy support for some users
this.Proxy = null;
}

protected override WebRequest GetWebRequest(Uri address)
{
var webRequest = base.GetWebRequest(address);
webRequest.Timeout = Timeout;
return webRequest;
}
}
}
}
2 changes: 1 addition & 1 deletion DXMainClient/Domain/Multiplayer/CnCNet/TunnelHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ private Task PingCurrentTunnelAsync(bool checkTunnelList = false)

private byte[] GetRawTunnelDataOnline()
{
WebClient client = new WebClient();
WebClient client = new ExtendedWebClient();
return client.DownloadData(MainClientConstants.CNCNET_TUNNEL_LIST_URL);
}

Expand Down

0 comments on commit 6dc4a6d

Please sign in to comment.