Example #1
0
        /// <summary>
        /// The main.
        /// </summary>
        /// <param name="args">
        /// The args.
        /// </param>
        private static void Main(string[] args)
        {
            counterLock = new object();
            consoleLock = new object();
            var options = new Options();

            if (!Parser.Default.ParseArguments(args, options))
            {
                Usage(options);
                return;
            }

            if (string.IsNullOrEmpty(options.Uri))
            {
                Usage(options);
                return;
            }

            Uri uri;

            try
            {
                uri = new Uri(options.Uri);
            }
            catch (UriFormatException)
            {
                Usage(options);
                return;
            }

            NetworkCredential networkCredentials;

            if (!string.IsNullOrEmpty(options.Username) && string.IsNullOrEmpty(options.Password))
            {
                networkCredentials = new NetworkCredential(options.Username, GetPassword());
            }
            else
            {
                networkCredentials = new NetworkCredential(options.Username, options.Password);
            }

            lock (consoleLock)
            {
                cursorWidth = Console.WindowWidth - 3;
                for (cursorTop = Console.CursorTop; cursorTop + 1 > Console.BufferHeight - 3; cursorTop--)
                {
                    Console.WriteLine();
                }

                WriteSpeed("Starting...");
                WriteTimes(TimeSpan.FromSeconds(0), TimeSpan.FromSeconds(0));
                Console.SetCursorPosition(0, cursorTop + 2);
                Console.Write('[');
                Console.SetCursorPosition(cursorWidth + 1, cursorTop + 2);
                Console.Write(']');
            }

            try
            {
                var downloadAccelerator = new DownloadAccelerator(
                    uri,
                    options.OutputFile,
                    options.NumberOfConnections,
                    networkCredentials,
                    options.IgnoreCertificateErrors,
                    options.ChunkSize);

                downloadAccelerator.DataApplied += ProgressCallback;
                size = downloadAccelerator.Size;

                var reportCancellationTokenSource = new CancellationTokenSource();
                ReportSpeed(reportCancellationTokenSource.Token);

                downloadAccelerator.Download().Wait();
                reportCancellationTokenSource.Cancel();

                lock (consoleLock)
                {
                    Console.SetCursorPosition(0, cursorTop + 3);
                }
            }
            catch (DownloadAcceleratorException ex)
            {
                Console.SetCursorPosition(0, cursorTop + 3);
                Console.WriteLine(ex.Message);
            }
            catch (Exception ex)
            {
                Console.SetCursorPosition(0, cursorTop + 3);
                Console.WriteLine("Unknown Error: {0}. Aborting.", ex.Message);
            }
        }
Example #2
0
        /// <summary>
        /// The main.
        /// </summary>
        /// <param name="args">
        /// The args.
        /// </param>
        private static void Main(string[] args)
        {
            counterLock = new object();
            consoleLock = new object();
            var options = new Options();

            if (!Parser.Default.ParseArguments(args, options))
            {
                Usage(options);
                return;
            }

            if (string.IsNullOrEmpty(options.Uri))
            {
                Usage(options);
                return;
            }

            Uri uri;

            try
            {
                uri = new Uri(options.Uri);
            }
            catch (UriFormatException)
            {
                Usage(options);
                return;
            }

            NetworkCredential networkCredentials;

            if (!string.IsNullOrEmpty(options.Username) && string.IsNullOrEmpty(options.Password))
            {
                networkCredentials = new NetworkCredential(options.Username, GetPassword());
            }
            else
            {
                networkCredentials = new NetworkCredential(options.Username, options.Password);
            }

            lock (consoleLock)
            {
                cursorWidth = Console.WindowWidth - 3;
                for (cursorTop = Console.CursorTop; cursorTop + 1 > Console.BufferHeight - 3; cursorTop--)
                {
                    Console.WriteLine();
                }

                WriteSpeed("Starting...");
                WriteTimes(TimeSpan.FromSeconds(0), TimeSpan.FromSeconds(0));
                Console.SetCursorPosition(0, cursorTop + 2);
                Console.Write('[');
                Console.SetCursorPosition(cursorWidth + 1, cursorTop + 2);
                Console.Write(']');
            }

            try
            {
                var downloadAccelerator = new DownloadAccelerator(
                    uri,
                    options.OutputFile,
                    options.NumberOfConnections,
                    networkCredentials,
                    options.IgnoreCertificateErrors,
                    options.ChunkSize);

                downloadAccelerator.DataApplied += ProgressCallback;
                size = downloadAccelerator.Size;

                var reportCancellationTokenSource = new CancellationTokenSource();
                ReportSpeed(reportCancellationTokenSource.Token);

                downloadAccelerator.Download().Wait();
                reportCancellationTokenSource.Cancel();

                lock (consoleLock)
                {
                    Console.SetCursorPosition(0, cursorTop + 3);
                }
            }
            catch (DownloadAcceleratorException ex)
            {
                Console.SetCursorPosition(0, cursorTop + 3);
                Console.WriteLine(ex.Message);
            }
            catch (Exception ex)
            {
                Console.SetCursorPosition(0, cursorTop + 3);
                Console.WriteLine("Unknown Error: {0}. Aborting.", ex.Message);
            }
        }