-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDownloadManager.cs
58 lines (49 loc) · 1.3 KB
/
DownloadManager.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
namespace DotNet40TLSThumbprintPinning
{
internal class DownloadManager
{
private readonly Queue<DownloadRequest> _requestQueue = new Queue<DownloadRequest>();
// Very simple KeepAlive logic, we want to use it if we have more than 1 request
internal bool ShouldUseKeepalive => _requestQueue.Count > 1;
public void AddRequest(DownloadRequest req)
{
_requestQueue.Enqueue(req);
}
public void ClearDownloads()
{
_requestQueue.Clear();
}
public void DownloadFilesFromQueue()
{
while (_requestQueue.Count > 0)
{
var req = _requestQueue.Peek();
req.SetupRequest();
if (req is HttpDownloadRequest httpReq)
{
try
{
httpReq.Request.KeepAlive = ShouldUseKeepalive;
Console.WriteLine($"Starting HTTP download request to {httpReq.Request.RequestUri}");
var response = (HttpWebResponse) httpReq.Request.GetResponse();
using (var stream = response.GetResponseStream())
using (var fw = File.OpenWrite($"{Environment.CurrentDirectory}\\{req.Filename}"))
{
stream?.CopyTo(fw);
}
httpReq.OnFinishCallback();
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
_requestQueue.Dequeue();
}
}
}
}