예제 #1
0
        void FtpClientEx_BytesTransferred(object sender, BytesTransferredEventArgs e)
        {
            long Cnt = e.ByteCount;

            if (BytesTransferredEx != null)
            {
                int Percent = 0;
                if (_FSize != 0)
                {
                    Percent = (int)((double)e.ByteCount / (double)_FSize * 100);
                    TimeSpan ts = BaseDownloader.CalcTimeLeft(start_time, _FSize, Cnt);
                    BytesTransferredEx(Percent, ts, BaseDownloader.CalcSpeed(start_time, Cnt));
                }
                else
                {
                    BytesTransferredEx(0, TimeSpan.Zero, BaseDownloader.CalcSpeed(start_time, Cnt));
                }
            }
        }
예제 #2
0
        private void HttpConnect(string Url)
        {
            WebProxy wpxy = null;

            BytesCounter = 0;

            if (UpdateStatusEvent != null)
            {
                UpdateStatusEvent("Download thread started", DStatus.connecting, -1, -1, TimeSpan.Zero, 0);
            }

            try
            {
                switch (_ProxyState)
                {
                case UseProxy.UseProxy:
                    wpxy             = new WebProxy(_proxyUri, _ByPassLocal);
                    wpxy.Credentials = new NetworkCredential(_UserName, _UserPassword, _Domain);
                    if (UpdateStatusEvent != null)
                    {
                        UpdateStatusEvent("Using proxy", DStatus.connecting, -1, -1, TimeSpan.Zero, 0);
                    }
                    break;

                case UseProxy.UseDefaultProxy:
                    wpxy = WebProxy.GetDefaultProxy();
                    if (UpdateStatusEvent != null)
                    {
                        UpdateStatusEvent("Using default proxy", DStatus.connecting, -1, -1, TimeSpan.Zero, 0);
                    }
                    break;

                case UseProxy.NotProxy:
                    break;
                }

                if (_LocalFolder == null)
                {
                    OutFileName = Application.StartupPath + "\\" + Path.GetFileName(Url);
                }
                else
                {
                    OutFileName = _LocalFolder + "\\" + Path.GetFileName(Url);
                }

                DNTWebRequest = (HttpWebRequest)WebRequest.Create(Url);

                DNTWebRequest.Proxy = wpxy;

                if (UpdateStatusEvent != null)
                {
                    UpdateStatusEvent("trying to GetResponse()", DStatus.connecting, -1, -1, TimeSpan.Zero, 0);
                }

                DNTWebResponse = (HttpWebResponse)DNTWebRequest.GetResponse();

                if (UpdateStatusEvent != null)
                {
                    UpdateStatusEvent("Response received", DStatus.connected, -1, -1, TimeSpan.Zero, 0);
                }

                filestream = new FileStream(OutFileName, FileMode.OpenOrCreate, FileAccess.Write);

                FileLength = DNTWebResponse.ContentLength;

                if (FileLength == -1)
                {
                    FileLength = 1;
                }

                string TYpe = DNTWebResponse.ContentType;

                if (UpdateStatusEvent != null)
                {
                    UpdateStatusEvent("Download started", DStatus.started, FileLength, 0, TimeSpan.Zero, 0);
                }

                Stream strm = DNTWebResponse.GetResponseStream();

                TimeStart = DateTime.Now;

                int ArrSize = 10000;

                byte[] barr = new byte[ArrSize];

                while (!ThreadStop)
                {
                    int Result = strm.Read(barr, 0, ArrSize);

                    if (Result == -1 || Result == 0)
                    {
                        break;
                    }

                    filestream.Write(barr, 0, Result);

                    double Speed = BaseDownloader.CalcSpeed(TimeStart, BytesCounter);

                    if (UpdateStatusEvent != null)
                    {
                        UpdateStatusEvent("Downloading OK ", CurrStatus, FileLength, BytesCounter, CalcTimeLeft(TimeStart, FileLength, BytesCounter), Speed);
                    }

                    _TimeoutTimer.Stop();
                    _TimeoutTimer.Start();

                    BytesCounter += Result;

                    CurrStatus = DStatus.transferring;
                }

                filestream.Flush();
                filestream.Close();
            }
            catch (Exception ex)
            {
                if (filestream != null)
                {
                    filestream.Close();
                }

                CurrStatus = DStatus.error;
                if (UpdateStatusEvent != null)
                {
                    UpdateStatusEvent(ex.Message, CurrStatus, FileLength, BytesCounter, TimeSpan.Zero, 0);
                }
                _TimeoutTimer.Stop();
                return;
            }

            if (AbortFlag)
            {
                CurrStatus = DStatus.aborted;
                if (UpdateStatusEvent != null)
                {
                    UpdateStatusEvent("download aborted", CurrStatus, FileLength, BytesCounter, TimeSpan.Zero, 0);
                }
            }
            else
            {
                CurrStatus = DStatus.complete;
                if (UpdateStatusEvent != null)
                {
                    UpdateStatusEvent("download complete", CurrStatus, FileLength, BytesCounter, TimeSpan.Zero, 0);
                }
            }

            _TimeoutTimer.Stop();
        }