//Start downloaden van een bestand private async Task DownloadFileAsync(string url, string naam) { ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12 | SecurityProtocolType.Ssl3; try { using (WebClient wc = new WebClient()) { wc.DownloadProgressChanged += Client_DownLoadProcessChanged; //Om tussentijdse feedback te kunnen geven. wc.DownloadFileCompleted += Client_DownloadProcessComplete; //Om feedback te kunnen geven bij voltooing //Make instance off class library DownloadEVERadioSessions downloadEVERadioSessionsClassLib = new DownloadEVERadioSessions(); if (UseProxy) //Use a proxy if the user wants to { GetProxyModel proxyinfo = downloadEVERadioSessionsClassLib.GetRandomProxy(); //Get a random proxy wc.Proxy = new WebProxy(proxyinfo.Ip, proxyinfo.Port); //Proxy instellen } //timer per download starten EVERadioSession evers = EVERadioSessions.Find(f => f.FileName == naam.Split('\\').Last()); evers.StopWatch.Start(); //Extra informatie per download bijhouden. evers.TimeStarted = DateTime.Now; await wc.DownloadFileTaskAsync(new Uri(url), naam); } } catch (Exception ex) { Console.WriteLine(ex.Message); if (ex.Message == "Unable to connect to the remote server") { FeedbackList.Add("Error, try again with Proxy enabled"); } else { FeedbackList.Add("Bestand: " + naam.Split('\\').Last() + " - Something went wrong, maybe this helps: " + ex.Message); } FeedbackColor = "Red"; } }
//When the download is progressing. private void Client_DownLoadProcessChanged(object sender, DownloadProgressChangedEventArgs e) { //De meegezonden sessie ophalen string sessiontolookfor = ((Uri)((TaskCompletionSource <object>)e.UserState).Task.AsyncState).Segments[4].ToString(); //Opzoeken van de bestandsnaam in de sessielijst EVERadioSession evers = EVERadioSessions.Find(f => f.FileName == sessiontolookfor); //TODO: Stopwatch per sessie ophalen en downloadsnelheid berekenen. //Console.WriteLine("Downloadsnelheid " + evers.FileName + " = " + (e.BytesReceived / 1024d / evers.StopWatch.Elapsed.TotalSeconds) + "kb/s"); evers.DownloadSpeed = e.BytesReceived / 1024d / evers.StopWatch.Elapsed.TotalSeconds; evers.FileSize = e.TotalBytesToReceive / 1000000; //Als we iets gevonden hebben vullen we een percentage in. if (evers != null) { evers.Progress = e.ProgressPercentage; } }
//When the download completed private void Client_DownloadProcessComplete(object sender, AsyncCompletedEventArgs e) { //De meegezonden sessie ophalen string sessiontolookfor = ((Uri)((TaskCompletionSource <object>)e.UserState).Task.AsyncState).Segments[4].ToString(); //Opzoeken van de bestandsnaam in de sessielijst EVERadioSession evers = EVERadioSessions.Find(f => f.FileName == sessiontolookfor); //Resetten van downloadsnelheid stopwatch evers.StopWatch.Reset(); //Klaar met downloaden evers.IsDownloading = false; //Als we iets gevonden hebben vullen we een percentage in. if (evers != null) { evers.Progress = 100; evers.Achtergrondkleur = "GreenYellow"; } }
/// <summary> /// Checks if a session is downloading /// Starts downloading session if not already downloading /// </summary> /// <param name="sessie"></param> private void HandleSession(EVERadioSession sessie) { string localpath = _downloadfolder + sessie.FileName; if (sessie.IsDownloading)//Als het bestand aan het downloaden is, gaan we het niet onderbreken { FeedbackList.Add("Song " + sessie.FileName + " is already downloading."); } else { if (!IsFullSession(localpath))//Als het een volledig bestand is moeten we het niet opnieuw downloaden { sessie.IsDownloading = true; sessie.Achtergrondkleur = "Orange"; DownloadFileAsync(sessie.FilePath, localpath);//Aanzetten om bestanden te downloaden } else { sessie.IsDownloading = false; sessie.Achtergrondkleur = "GreenYellow"; sessie.Progress = 100; } } }