Skip to content

Commit 076151d

Browse files
Make resources download timeout after 20 seconds instead of 2 minutes
1 parent 9202833 commit 076151d

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

fCraft/Network/HttpUtil.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,19 @@ public static HttpWebRequest CreateRequest([NotNull] Uri uri, TimeSpan timeout)
6868
}
6969
}
7070
return req;
71+
}
72+
73+
public static WebClient CreateWebClient(TimeSpan timeout) {
74+
return new CustomWebClient(timeout);
75+
}
76+
77+
class CustomWebClient : WebClient {
78+
readonly TimeSpan timeout;
79+
public CustomWebClient(TimeSpan timeout) { this.timeout = timeout; }
80+
81+
protected override WebRequest GetWebRequest(Uri address) {
82+
return CreateRequest(address, timeout);
83+
}
7184
}
7285
}
7386
}

fCraft/System/Server.cs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -481,13 +481,18 @@ static void DownloadResource(string url, string file) {
481481
if (File.Exists(file)) return;
482482

483483
try {
484-
new WebClient().DownloadFile(url, file);
485-
} catch (WebException ex) {
486-
WebResponse resp = ex.Response;
487-
if (resp != null) resp.Close();
484+
TimeSpan timeout = TimeSpan.FromSeconds(20);
485+
using (WebClient c = HttpUtil.CreateWebClient(timeout)) {
486+
c.DownloadFile(url, file);
487+
}
488+
} catch (Exception ex) {
489+
WebException webEx = ex as WebException;
490+
if (webEx != null) {
491+
WebResponse resp = webEx.Response;
492+
if (resp != null) resp.Close();
493+
}
488494

489-
Logger.Log(LogType.Warning, "Error downloading resource {0}: {1}",
490-
file, ex);
495+
Logger.Log(LogType.Warning, "Error downloading resource {0}: {1}", file, ex);
491496
}
492497
}
493498

0 commit comments

Comments
 (0)