Skip to content

Commit ab5f297

Browse files
committed
Reduce download timeout to 3 seconds
1 parent 5f6babd commit ab5f297

File tree

6 files changed

+42
-14
lines changed

6 files changed

+42
-14
lines changed

FlashpointInstaller/src/Common.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Drawing;
55
using System.IO;
66
using System.Linq;
7+
using System.Net;
78
using System.Windows.Forms;
89
using System.Xml;
910

@@ -153,6 +154,8 @@ public static class FPM
153154
public static string ListURL { get; set; } = "https://nexus-dev.unstable.life/repository/stable/components.xml";
154155
// The parsed component list XML
155156
public static XmlDocument XmlTree { get; set; }
157+
// WebClient instance used to download files
158+
public static _WebClient Client { get; } = new _WebClient();
156159

157160
// Check if Visual C++ 2015 x86 redistributable is installed
158161
public static bool RedistInstalled
@@ -383,4 +386,15 @@ public static string GetFormattedBytes(long bytes)
383386
}
384387
}
385388
}
389+
390+
// Modified WebClient class with shortened timeout
391+
public class _WebClient : WebClient
392+
{
393+
protected override WebRequest GetWebRequest(Uri address)
394+
{
395+
var request = base.GetWebRequest(address);
396+
request.Timeout = 3000;
397+
return request;
398+
}
399+
}
386400
}

FlashpointInstaller/src/Forms/Operate.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@ public partial class Operate : Form
2121

2222
List<Component> markedComponents = new List<Component>();
2323

24-
WebClient client = new WebClient();
25-
2624
Stream stream;
2725
ZipArchive archive;
2826
IReader reader;
@@ -40,7 +38,7 @@ private async void Operation_Load(object sender, EventArgs e)
4038
{
4139
TaskbarManager.Instance.SetProgressState(TaskbarProgressBarState.Normal, FPM.Main.Handle);
4240

43-
client.DownloadProgressChanged += OnDownloadProgressChanged;
41+
FPM.Client.DownloadProgressChanged += OnDownloadProgressChanged;
4442

4543
FPM.IterateList(FPM.Main.ComponentList.Nodes, node =>
4644
{
@@ -63,7 +61,7 @@ private async void Operation_Load(object sender, EventArgs e)
6361
{
6462
try
6563
{
66-
stream = new MemoryStream(await client.DownloadDataTaskAsync(new Uri(component.URL)));
64+
stream = new MemoryStream(await FPM.Client.DownloadDataTaskAsync(new Uri(component.URL)));
6765
}
6866
catch (WebException ex)
6967
{
@@ -103,8 +101,8 @@ private async void Operation_Load(object sender, EventArgs e)
103101

104102
if (!File.Exists(redistPath))
105103
{
106-
client.DownloadProgressChanged -= OnDownloadProgressChanged;
107-
await client.DownloadFileTaskAsync("https://aka.ms/vs/17/release/vc_redist.x86.exe", redistPath);
104+
FPM.Client.DownloadProgressChanged -= OnDownloadProgressChanged;
105+
await FPM.Client.DownloadFileTaskAsync("https://aka.ms/vs/17/release/vc_redist.x86.exe", redistPath);
108106
}
109107

110108
await Task.Run(() =>
@@ -131,7 +129,7 @@ private void OnDownloadProgressChanged(object sender, DownloadProgressChangedEve
131129
{
132130
if (cancelStatus != 0)
133131
{
134-
client.CancelAsync();
132+
FPM.Client.CancelAsync();
135133
return;
136134
}
137135

@@ -317,6 +315,8 @@ await Task.Run(() =>
317315
private void Operation_FormClosing(object sender, FormClosingEventArgs e)
318316
{
319317
if (cancelStatus != 2) e.Cancel = true;
318+
319+
FPM.Client.DownloadProgressChanged -= OnDownloadProgressChanged;
320320
}
321321
}
322322
}

FlashpointInstaller/src/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ static void Main(string[] args)
2323

2424
try
2525
{
26-
var listStream = new MemoryStream(new WebClient().DownloadData(FPM.ListURL)) { Position = 0 };
26+
var listStream = new MemoryStream(FPM.Client.DownloadData(FPM.ListURL)) { Position = 0 };
2727

2828
FPM.XmlTree = new XmlDocument();
2929
FPM.XmlTree.Load(listStream);

FlashpointManager/src/Common.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Drawing;
44
using System.IO;
55
using System.Linq;
6+
using System.Net;
67
using System.Windows.Forms;
78
using System.Xml;
89

@@ -154,6 +155,8 @@ public static class FPM
154155
public static Main Main { get => (Main)Application.OpenForms["Main"]; }
155156
// The parsed component list XML
156157
public static XmlDocument XmlTree { get; } = new XmlDocument();
158+
// WebClient instance used to download files
159+
public static _WebClient Client { get; } = new _WebClient();
157160

158161
// Tracks if the manager has been initialized yet
159162
public static bool Ready { get; set; } = false;
@@ -643,4 +646,15 @@ public static void ParseError(string message)
643646
}
644647
}
645648
}
649+
650+
// Modified WebClient class with shortened timeout
651+
public class _WebClient : WebClient
652+
{
653+
protected override WebRequest GetWebRequest(Uri address)
654+
{
655+
var request = base.GetWebRequest(address);
656+
request.Timeout = 3000;
657+
return request;
658+
}
659+
}
646660
}

FlashpointManager/src/Forms/Operate.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@ public partial class Operate : Form
2121
List<Component> addedComponents = new List<Component>();
2222
List<Component> removedComponents = new List<Component>();
2323

24-
WebClient client = new WebClient();
25-
2624
Stream stream;
2725
ZipArchive archive;
2826
IReader reader;
@@ -38,7 +36,7 @@ private async void Operation_Load(object sender, EventArgs e)
3836
{
3937
TaskbarManager.Instance.SetProgressState(TaskbarProgressBarState.Normal, FPM.Main.Handle);
4038

41-
client.DownloadProgressChanged += OnDownloadProgressChanged;
39+
FPM.Client.DownloadProgressChanged += OnDownloadProgressChanged;
4240

4341
if (FPM.OperationMode == OperateMode.Modify)
4442
{
@@ -96,7 +94,7 @@ private async void Operation_Load(object sender, EventArgs e)
9694
{
9795
try
9896
{
99-
stream = new MemoryStream(await client.DownloadDataTaskAsync(new Uri(component.URL)));
97+
stream = new MemoryStream(await FPM.Client.DownloadDataTaskAsync(new Uri(component.URL)));
10098
}
10199
catch (WebException ex)
102100
{
@@ -158,7 +156,7 @@ private void OnDownloadProgressChanged(object sender, DownloadProgressChangedEve
158156
{
159157
if (cancelStatus != 0)
160158
{
161-
client.CancelAsync();
159+
FPM.Client.CancelAsync();
162160
return;
163161
}
164162

@@ -374,6 +372,8 @@ await Task.Run(() =>
374372
private void Operation_FormClosing(object sender, FormClosingEventArgs e)
375373
{
376374
if (cancelStatus != 2) e.Cancel = true;
375+
376+
FPM.Client.DownloadProgressChanged -= OnDownloadProgressChanged;
377377
}
378378
}
379379
}

FlashpointManager/src/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ static void Main(string[] args)
7171
// Attempt to download and parse component list
7272
try
7373
{
74-
listStream = new MemoryStream(new WebClient().DownloadData(FPM.RepoXml));
74+
listStream = new MemoryStream(FPM.Client.DownloadData(FPM.RepoXml));
7575
FPM.XmlTree.Load(listStream);
7676
}
7777
catch (Exception e)

0 commit comments

Comments
 (0)