Esempio n. 1
0
        public virtual async Task <int> CreateDownloadTask(
            string url,
            IProgress <DownloadBytesProgress> progressReporter
            )
        {
            var receivedBytes = 0;
            var client        = new WebClient();

            OnStartWait();
            using (var storage = OpenStorage(url))
                using (var stream = await client.OpenReadTaskAsync(url))
                {
                    _throttle.WaitOne();
                    OnStopWait();

                    var buffer     = new byte[BUFFER_SIZE];
                    var totalBytes = Int32.Parse(client.ResponseHeaders [HttpResponseHeader.ContentLength]);

                    while (true)
                    {
                        var bytesRead = await stream.ReadAsync(buffer, 0, buffer.Length);

                        if (bytesRead == 0)
                        {
                            _throttle.Release();
                            break;
                        }

                        Decrypt(buffer, bytesRead);

                        storage.Write(buffer, 0, bytesRead);

                        receivedBytes += bytesRead;
                        if (progressReporter != null)
                        {
                            var args = new DownloadBytesProgress(url, receivedBytes, totalBytes);
                            progressReporter.Report(args);
                        }
                    }
                }

            return(receivedBytes);
        }
Esempio n. 2
0
        public virtual async Task<int> CreateDownloadTask(
            string url,
            IProgress<DownloadBytesProgress> progressReporter
        )
        {
            var receivedBytes = 0;
            var client = new WebClient ();

            OnStartWait ();
            using (var storage = OpenStorage (url))
            using (var stream = await client.OpenReadTaskAsync (url))
            {
                _throttle.WaitOne ();
                OnStopWait ();

                var buffer = new byte[BUFFER_SIZE];
                var totalBytes = Int32.Parse (client.ResponseHeaders [HttpResponseHeader.ContentLength]);

                while (true)
                {
                    var bytesRead = await stream.ReadAsync (buffer, 0, buffer.Length);
                    if (bytesRead == 0)
                    {
                        _throttle.Release ();
                        break;
                    }

                    Decrypt (buffer, bytesRead);

                    storage.Write (buffer, 0, bytesRead);

                    receivedBytes += bytesRead;
                    if (progressReporter != null)
                    {
                        var args = new DownloadBytesProgress (url, receivedBytes, totalBytes);
                        progressReporter.Report (args);
                    }
                }
            }

            return receivedBytes;
        }