Example #1
0
        /* Methode zum asynchronen Download einer Datei */
        public void DownloadAsync(string url, Stream destStream, int blockSize,
                                  DownloadProgress downloadProgress, DownloadEnd downloadEnd)
        {
            // Das Download-Status-Objekt, das über das Status-
            // Feld des asynchronen Aufrufs weitergegeben wird,
            // erzeugen und initialisieren
            DownloadStatus downloadStatus = new DownloadStatus();

            downloadStatus.BlockSize        = blockSize;
            downloadStatus.ReadBuffer       = new Byte[blockSize];
            downloadStatus.DownloadProgress = downloadProgress;
            downloadStatus.DownloadEnd      = downloadEnd;
            downloadStatus.DestStream       = destStream;
            downloadStatus.manualResetEvent = new ManualResetEvent(false);
            downloadStatus.manualResetEvent.Reset();

            // WebRequest-Instanz für den Download erzeugen
            downloadStatus.Request = WebRequest.Create(url);

            // Die Antwort asynchron abfragen
            IAsyncResult asyncResult = (IAsyncResult)downloadStatus.Request.BeginGetResponse(
                new AsyncCallback(ResponseCallback), downloadStatus);

            //downloadStatus.manualResetEvent.WaitOne();
        }
Example #2
0
 /* Konstruktor */
 public Download(string url, Stream destStream, int blockSize,
                 DownloadProgress downloadProgress, DownloadEnd downloadEnd,
                 DownloadError downloadError)
 {
     this.Url              = url;
     this.BlockSize        = blockSize;
     this.DownloadProgress = downloadProgress;
     this.DownloadEnd      = downloadEnd;
     this.DestStream       = destStream;
     this.DownloadError    = downloadError;
 }
Example #3
0
        /* Methode zum synchronen Download einer Datei */
        public void DownloadSync(string url, Stream destStream, int blockSize,
                                 DownloadProgress downloadProgress, DownloadEnd downloadEnd,
                                 DownloadError downloadError)
        {
            // Download-Objekt erzeugen und initialisieren
            Download download = new Download(url, destStream, blockSize,
                                             downloadProgress, downloadEnd, downloadError);

            // Download synchron starten
            download.PerformDownload();
        }
 /* Konstruktor */
 public Download(string url, Stream destStream, int blockSize,
     DownloadProgress downloadProgress, DownloadEnd downloadEnd,
     DownloadError downloadError)
 {
     this.Url = url;
     this.BlockSize = blockSize;
     this.DownloadProgress = downloadProgress;
     this.DownloadEnd  = downloadEnd;
     this.DestStream = destStream;
     this.DownloadError = downloadError ;
 }
        /* Methode zum synchronen Download einer Datei */
        public void DownloadSync(string url, Stream destStream, int blockSize,
            DownloadProgress downloadProgress, DownloadEnd downloadEnd,
            DownloadError downloadError)
        {
            // Download-Objekt erzeugen und initialisieren
            Download download = new Download(url, destStream, blockSize,
                downloadProgress, downloadEnd, downloadError);

            // Download synchron starten
            download.PerformDownload();
        }
        /* Methode zum asynchronen Download einer Datei */
        public void DownloadAsync(string url, Stream destStream, int blockSize, 
            DownloadProgress downloadProgress, DownloadEnd downloadEnd,
            DownloadError downloadError)
        {
            // Download-Objekt erzeugen und initialisieren
            Download download = new Download(url, destStream, blockSize,
                downloadProgress, downloadEnd, downloadError);

            // Thread für den Download starten
            Thread downloadThread = new Thread(new ThreadStart(download.PerformDownload));
            downloadThread.Start();
        }
Example #7
0
        /* Methode zum asynchronen Download einer Datei */
        public void DownloadAsync(string url, Stream destStream, int blockSize,
                                  DownloadProgress downloadProgress, DownloadEnd downloadEnd,
                                  DownloadError downloadError)
        {
            // Download-Objekt erzeugen und initialisieren
            Download download = new Download(url, destStream, blockSize,
                                             downloadProgress, downloadEnd, downloadError);

            // Thread für den Download starten
            Thread downloadThread = new Thread(new ThreadStart(download.PerformDownload));

            downloadThread.Start();
        }
Example #8
0
        private void Completed(object sender, AsyncCompletedEventArgs e)
        {
            sw.Reset();

            if (e.Cancelled == true)
            {
                MessageBox.Show("Download has been canceled.");
            }
            else
            {
                Debug.WriteLine("Download completed!");
                DownloadEnd?.Invoke(this, EventArgs.Empty);
            }
        }
Example #9
0
        /* Methode zum synchronen Download einer Datei */
        public void DownloadSync(string url, Stream destStream, int blockSize,
                                 DownloadProgress downloadProgress, DownloadEnd downloadEnd)
        {
            // WebRequest-Instanz für den Download erzeugen
            WebRequest request = WebRequest.Create(url);

            // Die Antwort anfordern
            WebResponse response = request.GetResponse();
            long        fileSize = response.ContentLength;

            // Den Antwort-Stream ermitteln
            Stream responseStream = response.GetResponseStream();

            // Stream blockweise lesen und in den Zielstream schreiben
            int bytesRead      = 0;
            int totalBytesRead = 0;

            byte[] buffer = new byte[blockSize];
            do
            {
                bytesRead       = responseStream.Read(buffer, 0, blockSize);
                totalBytesRead += bytesRead;
                destStream.Write(buffer, 0, bytesRead);

                // Fortschritt melden
                if (downloadProgress != null)
                {
                    downloadProgress(DownloadState.ReadingData, totalBytesRead, fileSize);
                }
            } while (bytesRead > 0);

            // Den Response-Stream und das WebResponse-Objekt schließen
            responseStream.Close();
            response.Close();

            // Ende des Download melden
            if (downloadEnd != null)
            {
                downloadEnd(destStream);
            }
        }
Example #10
0
        private Task <bool> RunSyncItem(Manifest.SyncItem f,
                                        bool verify, bool simulate,
                                        DownloadProgressChanged dpc, DownloadEnd de, DownloadMessage dm,
                                        CancellationTokenSource cts,
                                        string overrideDestination = null)
        {
            switch (f.type)
            {
            case "rsync":
                RSyncDownloader dd = new RSyncDownloader(this);
                dd.appPath         = AppPath;
                dd.tmpPath         = TmpPath;
                dd.VerifyChecksums = verify;
                dd.Simulate        = simulate;
                return(dd.Download(LatestManifest.rsyncUrl + "/" + f.name, f, RootPath,
                                   dpc, de, dm, cts, overrideDestination));

            case "delete":
                return(Task <bool> .Run(() =>
                {
                    if (f.name.EndsWith("/") && Directory.Exists(RootPath + "/" + f.name))
                    {
                        dm.Invoke("Deleting directory " + f.name);
                        Directory.Delete(RootPath + "/" + f.name, true);
                    }
                    else if (File.Exists(RootPath + "/" + f.name))
                    {
                        dm.Invoke("Deleting file " + f.name);
                        File.Delete(RootPath + "/" + f.name);
                    }
                    return true;
                }));

            default:
                return(null);
            }
        }
Example #11
0
        private Task<bool> RunSyncItem(Manifest.SyncItem f,
            bool verify, bool simulate,
            DownloadProgressChanged dpc, DownloadEnd de, DownloadMessage dm,
            CancellationTokenSource cts,
            string overrideDestination = null)
        {
            switch (f.type)
            {
                case "rsync":
                    RSyncDownloader dd = new RSyncDownloader(this);
                    dd.appPath = AppPath;
                    dd.tmpPath = TmpPath;
                    dd.VerifyChecksums = verify;
                    dd.Simulate = simulate;
                    return dd.Download(LatestManifest.rsyncUrl + "/" + f.name, f, RootPath,
                        dpc, de, dm, cts, overrideDestination);

                case "delete":
                    return Task<bool>.Run(() =>
                    {
                        if (f.name.EndsWith("/") && Directory.Exists(RootPath + "/" + f.name)) {
                            dm.Invoke("Deleting directory " + f.name);
                            Directory.Delete(RootPath + "/" + f.name, true);
                        } else if (File.Exists(RootPath + "/" + f.name)) {
                            dm.Invoke("Deleting file " + f.name);
                            File.Delete(RootPath + "/" + f.name);
                        }
                        return true;
                    });

                default:
                    return null;
            }
        }
Example #12
0
 public static void OnDownloadEnd(object sender, DownloadEventArgs ev) => DownloadEnd?.Invoke(sender, ev);
Example #13
0
 private void OnDownloadEnd(DownloadEndEventArgs e)
 {
     try { DownloadEnd?.Invoke(this, e); } catch { }
 }