Exemple #1
0
        private void ReadCallBack(IAsyncResult ar)
        {
            CDownloadFile cdfParent = (CDownloadFile)ar.AsyncState;

            if (cdfParent.m_blFileDownloading == true)
            {
                try
                {
                    int iBytesRead = -1;
                    if ((iBytesRead = cdfParent.m_stmResponseStream.EndRead(ar)) > 0)
                    {
                        if (this.CompleteFileData.Length < this.BytesDownloaded + iBytesRead)
                        {
                            byte[] resizedFileData = new byte[this.CompleteFileData.Length + iBytesRead];

                            this.CompleteFileData.CopyTo(resizedFileData, 0);

                            this.ma_bCompleteFile = resizedFileData;
                        }

                        Array.Copy(this.ma_bBufferStream, 0, this.CompleteFileData, this.BytesDownloaded, iBytesRead);
                        cdfParent.m_iReadBytes += iBytesRead;

                        IAsyncResult arResult = cdfParent.m_stmResponseStream.BeginRead(cdfParent.ma_bBufferStream, 0, CDownloadFile.INT_BUFFER_SIZE, new AsyncCallback(cdfParent.ReadCallBack), cdfParent);

                        ThreadPool.RegisterWaitForSingleObject(arResult.AsyncWaitHandle, new WaitOrTimerCallback(cdfParent.ReadTimeoutCallback), cdfParent, cdfParent.m_iTimeout, true);
                    }
                    else
                    {
                        cdfParent.m_blFileDownloading = false;
                        if (cdfParent.DownloadComplete != null)
                        {
                            cdfParent.DownloadComplete(cdfParent);
                            //cdfParent.DownloadComplete(cdfParent);
                        }

                        cdfParent.m_stmResponseStream.Close();
                        cdfParent.m_stmResponseStream.Dispose();
                        cdfParent.m_stmResponseStream = null;
                    }
                }
                catch (Exception e)
                {
                    cdfParent.m_blFileDownloading = false;
                    if (cdfParent.DownloadError != null)
                    {
                        cdfParent.m_strLastError = e.Message;

                        cdfParent.DownloadError(cdfParent);
                        //cdfParent.DownloadError(cdfParent);
                    }
                }
            }
        }
Exemple #2
0
        private void ResponseCallback(IAsyncResult ar)
        {
            CDownloadFile cdfParent = (CDownloadFile)ar.AsyncState;

            try
            {
                cdfParent.m_wrResponse = cdfParent.m_wrRequest.EndGetResponse(ar);

                string strContentLength = null;
                if ((strContentLength = cdfParent.m_wrResponse.Headers["Content-Length"]) != null)
                {
                    cdfParent.m_iCompleteFileSize = Convert.ToInt32(strContentLength);
                    cdfParent.ma_bCompleteFile    = new byte[cdfParent.m_iCompleteFileSize];

                    cdfParent.m_blUnknownSize = false;

                    if (cdfParent.DownloadDiscoveredFileSize != null)
                    {
                        //cdfParent.DownloadDiscoveredFileSize(cdfParent);

                        cdfParent.DownloadDiscoveredFileSize(cdfParent);
                    }
                }

                cdfParent.ma_bCompleteFile = new byte[0];

                cdfParent.m_stmResponseStream = cdfParent.m_wrResponse.GetResponseStream();

                IAsyncResult arResult = cdfParent.m_stmResponseStream.BeginRead(cdfParent.ma_bBufferStream, 0, CDownloadFile.INT_BUFFER_SIZE, new AsyncCallback(cdfParent.ReadCallBack), cdfParent);

                ThreadPool.RegisterWaitForSingleObject(arResult.AsyncWaitHandle, new WaitOrTimerCallback(cdfParent.ReadTimeoutCallback), cdfParent, cdfParent.m_iTimeout, true);
            }
            catch (Exception e)
            {
                cdfParent.m_blFileDownloading = false;
                if (cdfParent.DownloadError != null)
                {
                    cdfParent.m_strLastError = e.Message;

                    cdfParent.DownloadError(cdfParent);
                    //cdfParent.DownloadError(cdfParent);
                }
            }
        }
Exemple #3
0
        private void ReadTimeoutCallback(object objState, bool blTimedOut)
        {
            if (blTimedOut == true)
            {
                CDownloadFile cdfParent = (CDownloadFile)objState;
                if (cdfParent != null && cdfParent.m_stmResponseStream != null)
                {
                    cdfParent.m_stmResponseStream.Close();

                    if (cdfParent.DownloadError != null)
                    {
                        cdfParent.m_strLastError = "Read Timeout";

                        cdfParent.DownloadError(cdfParent);
                        //cdfParent.DownloadError(cdfParent);
                    }
                }
            }
        }