示例#1
0
        /// <summary>
        /// Async HTTP Read Callback
        /// IAsyncResult asyncResult (input) -> data necesary to access the webdonwload instance, because
        /// This is a static method and has not the Webdownload instance
        /// </summary>
        private static void AsyncReadCallback(IAsyncResult asyncResult)
        {
            WebDownload webDL = (WebDownload)asyncResult.AsyncState;

            Stream responseStream = webDL.responseStream;

            try
            {
                int read = responseStream.EndRead(asyncResult);
                if (read > 0)
                {
                    webDL.ContentStream.Write(webDL.readBuffer, 0, read);
                    webDL.BytesProcessed += read;
                    webDL.OnProgressCallback(webDL.BytesProcessed, webDL.ContentLength);

                    IAsyncResult asynchronousResult = responseStream.BeginRead(webDL.readBuffer, 0, webDL.readBuffer.Length, new AsyncCallback(AsyncReadCallback), webDL);

                    return;
                }
                else
                {
                    if (webDL.BytesProcessed <= 0)
                    {
                        Exception myException = new Exception("400 No tile");

                        webDL.SaveException(myException);

                        webDL.AsyncFinishDownload();
                        return;
                    }

                    webDL.SaveException(null);
                    webDL.AsyncFinishDownload();
                }
            }
            catch (IOException ex)
            {
                Utility.Log.Write(Log.Levels.Debug, "NET", "AsyncReadCallback(): IOException: " + ex.Message.ToString() + webDL.BytesProcessed);
                if (webDL.num_retry > 5)
                {
                    webDL.SaveException(new Exception("Unable to connect to the remote server several Async Read tries have been broke"));
                    webDL.AsyncFinishDownload();
                    return;
                }
                webDL.num_retry++;

                webDL.AsyncFinishPrepareRetry();

                webDL.DownloadAsync();
            }
            catch (WebException e)
            {
                if (webDL.timedOut == false)
                {
                    // request cancelled.
                    Utility.Log.Write(Log.Levels.Debug, "NET", "AsyncReadCallback(): WebException: " + e.Status.ToString());
                    webDL.SaveException(e);
                    webDL.AsyncFinishDownload();
                    //webDL.Cancel();
                }
            }
            catch (NullReferenceException e)
            {
                Utility.Log.Write(Log.Levels.Debug, "NET", "AsyncReadCallback(): NullReferenceException: " + e.Message);
                webDL.SaveException(e);
                webDL.AsyncFinishDownload();
            }
            catch (Exception e)
            {
                Utility.Log.Write(Log.Levels.Debug, "NET", "AsyncReadCallback(): Exception: " + e.Message);
                webDL.SaveException(e);
                //Cancel();
                webDL.AsyncFinishDownload();
            }
        }