private void ReadCallBack(IAsyncResult asyncResult) { // Get the DownloadInfo object from AsyncResult. DownloadInfo info = (DownloadInfo)asyncResult.AsyncState; // Retrieve the ResponseStream that was set in RespCallback. Stream responseStream = info.ResponseStream; try { // Read info.BufferRead to verify that it contains data. int bytesRead = responseStream.EndRead(asyncResult); if (bytesRead > 0) { if (info.useFastBuffers) { System.Array.Copy(info.BufferRead, 0, info.dataBufferFast, info.bytesProcessed, bytesRead); } else { for (int b = 0; b < bytesRead; b++) { info.dataBufferSlow.Add(info.BufferRead[b]); } } info.bytesProcessed += bytesRead; //LibSys.StatusBar.Trace("IP: WebDownload:ReadCallBack() - " + info.baseName + " " + info.strUrl + " bytesRead=" + bytesRead); // If a registered progress-callback, inform it of our download progress so far. if (info.ProgressCallback != null) { info.ProgressCallback(info); } // Continue reading data until responseStream.EndRead returns –1. IAsyncResult ar = responseStream.BeginRead( info.BufferRead, 0, BUFFER_SIZE, new AsyncCallback(ReadCallBack), info); } else { responseStream.Close(); allDone.Set(); } } catch (Exception e) { LibSys.StatusBar.Trace("Error: WebDownload:ResponseCallback " + info.baseName + " " + info.strUrl + " exception " + e.Message); responseStream.Close(); allDone.Set(); } return; }
private void _progressCallback(DownloadInfo info) { //LibSys.StatusBar.Trace("IP: DloadProgressForm:progressCallback() - " + info.baseName + " - " + info.bytesProcessed + " of " + info.dataLength); bytesDownloadedTextBox.Text = info.bytesProcessed.ToString("#,##0"); if (info.dataLength != -1) { progressBar.Minimum = 0; progressBar.Maximum = info.dataLength; progressBar.Value = info.bytesProcessed; totalBytesTextBox.Text = info.dataLength.ToString("#,##0"); } else { progressBar.Visible = false; totalBytesTextBox.Text = "Total File Size Not Known"; } }
private void completeCallback(object formObj, DownloadInfo info, string dldFileName, byte[] dataDownloaded) { #if DEBUG LibSys.StatusBar.Trace("IP: DloadNoForm:completeCallback() - " + info.baseName + " " + (dataDownloaded == null? "null" : "" + dataDownloaded.Length) + " bytes loaded"); #endif if (dataDownloaded == null || Project.is404(dataDownloaded)) { string message = "failed: " + (dataDownloaded == null ? "no connection or no data" : "404 - file not found"); LibSys.StatusBar.Error(message); } else { FileStream fs = null; try { fs = new FileStream(dldFileName, FileMode.Create); fs.Write(dataDownloaded, 0, dataDownloaded.Length); fs.Close(); fs = null; LibSys.StatusBar.Trace("OK: file " + dldFileName + " created"); } catch (Exception e) { string message = "failed: " + dldFileName + " - " + e.Message; LibSys.StatusBar.Error(message); } finally { if (fs != null) { fs.Close(); } } } m_completed = true; }
private static void imageDownloadCompleteCallback( object otile, DownloadInfo info, string imageFileName, byte[] dataDownloaded ) { #if DEBUG LibSys.StatusBar.Trace("IP: TileCache:imageDownloadCompleteCallback() - " + info.baseName + " " + (dataDownloaded == null? "null" : "" + dataDownloaded.Length) + " bytes loaded"); #endif Backdrop backdrop = (Backdrop)m_backdropCache[info.baseName]; Tile tile = (Tile)otile; if(dataDownloaded == null || info.is404 || Project.is404(dataDownloaded)) { string comment = dataDownloaded == null ? "no data" : "404"; if(backdrop != null) { backdrop.IsEmpty = true; // proven empty } ProgressMonitor.markComplete(info.monitored, false, comment); } else { FileStream fs = null; try { string comment = "" + dataDownloaded.Length + " bytes"; fs = new FileStream(imageFileName, FileMode.Create); fs.Write(dataDownloaded, 0, dataDownloaded.Length); fs.Close(); fs = null; #if DEBUG LibSys.StatusBar.Trace("OK: file " + imageFileName + " created"); #endif if(backdrop != null) { backdrop.Fill(); } ProgressMonitor.markComplete(info.monitored, true, comment); } catch (Exception e) { #if DEBUG LibSys.StatusBar.Error("" + e); #endif if(backdrop != null) { backdrop.IsEmpty = true; // proven empty } ProgressMonitor.markComplete(info.monitored, false, e.Message); } finally { if(fs != null) { fs.Close(); } } } if(tile != null) { tile.backdropArrived(backdrop); } }
public static void init(string cgibinptrUrl) { m_mapsPath = Project.GetMapsTempPath(); LibSys.StatusBar.WriteLine("Actual location for mapping cache in " + m_mapsPath); try { #if DEBUG LibSys.StatusBar.Trace("IP: reaching cgibinptr URL='" + cgibinptrUrl + "'"); #endif /* * this code has long uncontrolled timeout, and has been replaced with the WebDownload-based code below HttpWebRequest req = (HttpWebRequest)WebRequest.Create(cgibinptrUrl); if(Project.suspendKeepAlive) { req.KeepAlive = false; } WebResponse res = req.GetResponse(); Stream responseStream = res.GetResponseStream(); StreamReader reader = new StreamReader (responseStream); */ WebDownload webDL = new WebDownload(Project.webTimeoutMs); // default 7 seconds to time out // Create the state object. DownloadInfo info = new DownloadInfo(); info.baseName = ""; info.strUrl = cgibinptrUrl; info.addMonitoredMethod = null; byte[] downloadedData = null; int tries = 1; int maxTries = 1; while (tries <= maxTries && downloadedData == null && !info.is404) { downloadedData = webDL.Download(info, ProgressCallback); // will timeout #if DEBUG if(downloadedData != null) { LibSys.StatusBar.Trace("IP: try " + tries + " TileCache:Download() - " + cgibinptrUrl + " delivered " + downloadedData.Length + " bytes" ); } else { LibSys.StatusBar.Trace("IP: try " + tries + " TileCache:Download() - " + cgibinptrUrl + " delivered null bytes" ); } #endif tries++; } if(downloadedData == null || downloadedData.Length == 0) { LibSys.StatusBar.Error("failed to reach QuakeMap.com"); return; // Project.serverAvailable will be set to false, and no web attempts will take place. } LibSys.StatusBar.Trace("OK: reached QuakeMap.com"); string responseString = Project.ByteArrayToStr(downloadedData); StringReader reader = new StringReader (responseString); string upgrVersion = ""; bool ignoreUpgrade = false; int state = 0; string line; while((line=reader.ReadLine()) != null) { try { switch(state) { case 0: if(line.StartsWith("MAPSERVER=")) { MappingServer ms = new MappingServer(line.Substring("MAPSERVER=".Length)); #if DEBUG LibSys.StatusBar.Trace("OK: TileCache() - ms='" + ms + "'"); #endif m_mappingServers.Add(ms); } else if(line.StartsWith("ZIPSERVER=")) { ZipcodeServer zs = new ZipcodeServer(line.Substring("ZIPSERVER=".Length)); #if DEBUG LibSys.StatusBar.Trace("OK: TileCache() - zs='" + zs + "'"); #endif m_zipcodeServer = zs; } else if(line.StartsWith("TILERABOUT=")) { Project.ABOUT_URL = line.Substring("TILERABOUT=".Length); #if DEBUG LibSys.StatusBar.Trace("OK: TileCache() - about='" + Project.ABOUT_URL + "'"); #endif } else if(line.StartsWith("TILERORDER=")) { Project.ORDER_URL = line.Substring("TILERORDER=".Length); #if DEBUG LibSys.StatusBar.Trace("OK: TileCache() - order='" + Project.ORDER_URL + "'"); #endif } else if(line.StartsWith("TILERDLOAD=")) { Project.DLOAD_URL = line.Substring("TILERDLOAD=".Length); #if DEBUG LibSys.StatusBar.Trace("OK: TileCache() - download='" + Project.DLOAD_URL + "'"); #endif } else if(line.StartsWith("TILERUPDATE=")) { Project.UPDATE_URL = line.Substring("TILERUPDATE=".Length); #if DEBUG LibSys.StatusBar.Trace("OK: TileCache() - update='" + Project.UPDATE_URL + "'"); #endif } else if(line.StartsWith("TILERPRIVACY=")) { Project.PRIVACY_URL = line.Substring("TILERPRIVACY=".Length); #if DEBUG LibSys.StatusBar.Trace("OK: TileCache() - privacy='" + Project.PRIVACY_URL + "'"); #endif } else if(line.StartsWith("TILERPDA=")) { Project.PDA_URL = line.Substring("TILERPDA=".Length); #if DEBUG LibSys.StatusBar.Trace("OK: TileCache() - pda='" + Project.PDA_URL + "'"); #endif } else if(line.StartsWith("TILERHELP=")) { Project.HELP_FILE_URL = line.Substring("TILERHELP=".Length); #if DEBUG LibSys.StatusBar.Trace("OK: TileCache() - help='" + Project.HELP_FILE_URL + "'"); #endif } else if(line.StartsWith("TILERHDATE=")) { string sDate = line.Substring("TILERHDATE=".Length); Project.HELP_FILE_DATE = Convert.ToDateTime(sDate); #if DEBUG LibSys.StatusBar.Trace("OK: TileCache() - helpFileDate='" + Project.HELP_FILE_DATE + "'"); #endif } else if(line.StartsWith("TILERMISC=")) { Project.MISC_FOLDER_URL = line.Substring("TILERMISC=".Length); #if DEBUG LibSys.StatusBar.Trace("OK: TileCache() - misc='" + Project.MISC_FOLDER_URL + "'"); #endif } else if(line.StartsWith("TILERSAMPLES=")) { Project.SAMPLES_FOLDER_URL = line.Substring("TILERSAMPLES=".Length); #if DEBUG LibSys.StatusBar.Trace("OK: TileCache() - samples='" + Project.SAMPLES_FOLDER_URL + "'"); #endif } else if(line.StartsWith("MESSAGE=")) { state = 1; Project.serverMessage = ""; } else if(line.StartsWith("UPGR")) { upgrVersion = line.Substring(4); if(Project.PROGRAM_VERSION_RELEASEDATE.Equals(upgrVersion)) { ignoreUpgrade = true; } else { Project.upgradeMessage = "\n"; } state = 2; } break; case 1: if(line.StartsWith("ENDMESSAGE")) { state = 0; } else { Project.serverMessage += (line + "\n"); } break; case 2: if(line.StartsWith("ENDUPGR")) { state = 0; } else { if(!ignoreUpgrade) { Project.upgradeMessage += (line + "\n"); } } break; } } catch {} } if(!Project.serverMessageLast.Equals(Project.serverMessage) && Project.upgradeMessage.Length == 0) { string message = Project.serverMessage; // + Project.upgradeMessage; upgrade message shows up in MainForm LibSys.StatusBar.Trace(message); if(greetingForm == null) { Project.MessageBox(null, message); } else { System.Windows.Forms.MessageBox.Show (greetingForm, message, Project.PROGRAM_NAME_HUMAN, System.Windows.Forms.MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } Project.serverMessageLast = Project.serverMessage; } } catch (Exception e) { LibSys.StatusBar.Error("exception: " + e.Message); } }
private static void gcDownloadCompleteCallback( object obj, DownloadInfo info, string gcFileName, byte[] dataDownloaded ) { LibSys.StatusBar.Trace("IP: DlgWeeklyCacheImport:gcDownloadCompleteCallback() - " + info.baseName + " " + (dataDownloaded == null? "null" : "" + dataDownloaded.Length) + " bytes loaded"); if(m_stop) { ProgressMonitor.markComplete(info.monitored, false, "stopped"); return; } if(dataDownloaded == null || dataDownloaded.Length < 100 || Project.is404(dataDownloaded)) { string comment = dataDownloaded == null ? "no data" : "404"; ProgressMonitor.markComplete(info.monitored, false, comment); } else { try { string comment = "" + dataDownloaded.Length + " bytes"; LibSys.StatusBar.Trace("OK: link " + info.strUrl + " delivered"); bool success = processGcPage(info.strUrl, dataDownloaded); if(!success) { m_errorCount++; } ProgressMonitor.markComplete(info.monitored, true, comment); } catch (Exception e) { LibSys.StatusBar.Error("" + e.Message); ProgressMonitor.markComplete(info.monitored, false, e.Message); } } }
// good for LibSys.ThreadPool2 //public void Download(object state, DateTime requestEnqueueTime ) // good for LibSys.ThreadPool public void Download(object state ) { //LibSys.StatusBar.Trace("IP: " + threadCount + " DownloadThread:Download() - " + DownloadUrl); if ( CompleteCallback != null && DownloadUrl != "" ) { threadCount++; WebDownload webDL = new WebDownload(0); // no timeout, except for natural TCP/IP stack one. // Create the state object. DownloadInfo info = new DownloadInfo(); info.baseName = _baseName; info.strUrl = _downloadUrl; info.addMonitoredMethod = addMonitoredMethod; // may be null // Make sure progress monitor is created: if(info.addMonitoredMethod != null) { info.monitored = new Monitored(); info.monitored.Comment = info.strUrl; info.addMonitoredMethod(info.monitored); } byte[] downloadedData = null; if(Project.serverAvailable) { int tries = 1; while (true) { //try //{ downloadedData = webDL.Download(info, ProgressCallback); //} //catch (Exception e) //{ // LibSys.StatusBar.Error("try " + tries + " DownloadThread:Download() - " + DownloadUrl + " exception " + e.Message ); //} #if DEBUG if(downloadedData != null) { LibSys.StatusBar.Trace("IP: try " + tries + " DownloadThread:Download() - " + DownloadUrl + " delivered " + downloadedData.Length + " bytes" ); } else { LibSys.StatusBar.Trace("IP: try " + tries + " DownloadThread:Download() - " + DownloadUrl + " delivered null bytes" ); } #endif tries++; if(tries <= maxTries && downloadedData == null && !info.is404) { // if there was a server error (Web Exception, error 500), give it some time to get in shape before retrying: Thread.Sleep(3000); } else { break; } } } threadCount--; CompleteCallback( _tile, info, _fileName, downloadedData ); } }
private void ResponseCallback(IAsyncResult ar) { // Get the DownloadInfo object from the async result were // we're storing all of the temporary data and the download // buffer. DownloadInfo info = (DownloadInfo)ar.AsyncState; //LibSys.StatusBar.Trace("IP: WebDownload:ResponseCallback() - " + info.baseName + " " + info.strUrl); // Get the WebRequest from RequestState. HttpWebRequest req = info.Request; try { // Call EndGetResponse, which produces the WebResponse object // that came from the request issued above. WebResponse resp = req.EndGetResponse(ar); // Find the data size from the headers. string strContentLength = resp.Headers["Content-Length"]; //LibSys.StatusBar.Trace("IP: WebDownload:ResponseCallback() - ContentLength=" + strContentLength); if (strContentLength != null) { info.dataLength = Convert.ToInt32(strContentLength); info.dataBufferFast = new byte[info.dataLength]; } else { info.useFastBuffers = false; info.dataBufferSlow = new System.Collections.ArrayList(BUFFER_SIZE); } // Start reading data from the response stream. Stream ResponseStream = resp.GetResponseStream(); // Store the response stream in RequestState to read // the stream asynchronously. info.ResponseStream = ResponseStream; // we are about to actually read bytes from the pipe. // Pass do.BufferRead to BeginRead. IAsyncResult iarRead = ResponseStream.BeginRead(info.BufferRead, 0, BUFFER_SIZE, new AsyncCallback(ReadCallBack), info); //LibSys.StatusBar.Trace("IP: WebDownload:ResponseCallback() " + info.baseName + " " + info.strUrl + " done"); } catch (WebException we) { if (we.Message.IndexOf("404") >= 0) { info.is404 = true; // signal that there is no need for more tries } else { LibSys.StatusBar.Trace("Error: WebDownload:ResponseCallback " + info.baseName + " " + info.strUrl + " Web exception " + we.Message); } allDone.Set(); } catch (Exception e) { LibSys.StatusBar.Trace("Error: WebDownload:ResponseCallback " + info.baseName + " " + info.strUrl + " exception " + e.Message); allDone.Set(); } }
private void _progressCallback( DownloadInfo info ) { //LibSys.StatusBar.Trace("IP: DloadProgressForm:progressCallback() - " + info.baseName + " - " + info.bytesProcessed + " of " + info.dataLength); bytesDownloadedTextBox.Text = info.bytesProcessed.ToString("#,##0"); if ( info.dataLength != -1 ) { progressBar.Minimum = 0; progressBar.Maximum = info.dataLength; progressBar.Value = info.bytesProcessed; totalBytesTextBox.Text = info.dataLength.ToString("#,##0"); } else { progressBar.Visible = false; totalBytesTextBox.Text = "Total File Size Not Known"; } }
private void progressCallback( DownloadInfo info ) { // marshall the call to the thread where this form was created, so that // component-related operations are not hanging the message pump this.Invoke(new DownloadProgressHandler(_progressCallback), new object[] { info } ); }
private void progressCallback(DownloadInfo info) { // marshall the call to the thread where this form was created, so that // component-related operations are not hanging the message pump this.Invoke(new DownloadProgressHandler(_progressCallback), new object[] { info }); }
private void completeCallback( object formObj, DownloadInfo info, string dldFileName, byte[] dataDownloaded ) { #if DEBUG LibSys.StatusBar.Trace("IP: DloadNoForm:completeCallback() - " + info.baseName + " " + (dataDownloaded == null? "null" : "" + dataDownloaded.Length) + " bytes loaded"); #endif if(dataDownloaded == null || Project.is404(dataDownloaded)) { string message = "failed: " + (dataDownloaded == null ? "no connection or no data" : "404 - file not found"); LibSys.StatusBar.Error(message); } else { FileStream fs = null; try { fs = new FileStream(dldFileName, FileMode.Create); fs.Write(dataDownloaded, 0, dataDownloaded.Length); fs.Close(); fs = null; LibSys.StatusBar.Trace("OK: file " + dldFileName + " created"); } catch (Exception e) { string message = "failed: " + dldFileName + " - " + e.Message; LibSys.StatusBar.Error(message); } finally { if(fs != null) { fs.Close(); } } } m_completed = true; }
private static void eqDownloadCompleteCallback( object ouls, DownloadInfo info, string eqFileName, byte[] dataDownloaded ) { UrlListStruct uls = (UrlListStruct)ouls; LibSys.StatusBar.Trace("IP: - " + (dataDownloaded == null? "null" : "" + dataDownloaded.Length) + " bytes loaded from " + uls.url); if(dataDownloaded == null || Project.is404(dataDownloaded)) { string comment = dataDownloaded == null ? "no data" : "404"; // proven empty LibSys.StatusBar.Error("failed to download from " + uls.url); ProgressMonitor.markComplete(info.monitored, false, comment); if(Project.eqUseOldData) { try // who knows if the file is there in the first place... { uls.processor(uls.url, eqFileName, uls.source); } catch {} } } else { FileStream fs = null; try { string comment = "" + dataDownloaded.Length + " bytes"; fs = new FileStream(eqFileName, FileMode.Create); fs.Write(dataDownloaded, 0, dataDownloaded.Length); fs.Close(); fs = null; LibSys.StatusBar.Trace("IP: - file " + eqFileName + " created"); uls.processor(uls.url, eqFileName, uls.source); ProgressMonitor.markComplete(info.monitored, true, comment); } catch (Exception e) { LibSys.StatusBar.Error("file " + eqFileName + " " + e.Message); ProgressMonitor.markComplete(info.monitored, false, e.Message); } finally { if(fs != null) { fs.Close(); } } } m_threadCount--; //if(m_threadCount <= 0 && m_pictureManager != null) // last Complete { RefreshEarthquakesDisplayed(); m_pictureManager.Refresh(); } }
//public void Download(object state, DateTime requestEnqueueTime ) // good for LibSys.ThreadPool public void Download(object state) // good for LibSys.ThreadPool2 { //LibSys.StatusBar.Trace("IP: " + threadCount + " DownloadThread:Download() - " + DownloadUrl); if (CompleteCallback != null && DownloadUrl != "") { threadCount++; WebDownload webDL = new WebDownload(0); // no timeout, except for natural TCP/IP stack one. // Create the state object. DownloadInfo info = new DownloadInfo(); info.baseName = _baseName; info.strUrl = _downloadUrl; info.addMonitoredMethod = addMonitoredMethod; // may be null // Make sure progress monitor is created: if (info.addMonitoredMethod != null) { info.monitored = new Monitored(); info.monitored.Comment = info.strUrl; info.addMonitoredMethod(info.monitored); } byte[] downloadedData = null; if (Project.serverAvailable) { int tries = 1; while (true) { //try //{ downloadedData = webDL.Download(info, ProgressCallback); //} //catch (Exception e) //{ // LibSys.StatusBar.Error("try " + tries + " DownloadThread:Download() - " + DownloadUrl + " exception " + e.Message ); //} #if DEBUG if (downloadedData != null) { LibSys.StatusBar.Trace("IP: try " + tries + " DownloadThread:Download() - " + DownloadUrl + " delivered " + downloadedData.Length + " bytes"); } else { LibSys.StatusBar.Trace("IP: try " + tries + " DownloadThread:Download() - " + DownloadUrl + " delivered null bytes"); } #endif tries++; if (tries <= maxTries && downloadedData == null && !info.is404) { // if there was a server error (Web Exception, error 500), give it some time to get in shape before retrying: Thread.Sleep(3000); } else { break; } } } threadCount--; CompleteCallback(_tile, info, _fileName, downloadedData); } }
public byte[] Download(DownloadInfo info, DownloadProgressHandler progressCallback) { //LibSys.StatusBar.Trace("IP: downloading - " + info.baseName + " == " + info.strUrl); allDoneLast = allDone; // Ensure flag set correctly. allDone.Reset(); m_baseName = info.baseName; // Get the URI from the command line. Uri httpSite = new Uri(info.strUrl); // Create the request object. HttpWebRequest req = (HttpWebRequest)WebRequest.Create(httpSite); if (Project.suspendKeepAlive) { req.KeepAlive = false; } /* * see Project.ApplyGlobalHTTPProxy() * * if(Project.useProxy) * { * WebProxy proxy = new WebProxy(Project.proxyServer, Project.proxyPort); * req.Proxy = proxy; * } */ #if DEBUG LibSys.StatusBar.Trace("IP: downloading - " + info.baseName + " == " + info.strUrl + " after proxy: " + req.Proxy.GetProxy(httpSite)); #endif // Put the request into the state object so it can be passed around. info.Request = req; // Assign the callbacks info.ProgressCallback += progressCallback; /* * // this is to debug ThreadPool and ProgressMonitor * if(info.strUrl.IndexOf("ashx") != -1) * { * info.dataLength = 20; * int ccc = 0; * while (ccc++ < 20) * { * Thread.Sleep(500); * if ( info.ProgressCallback != null ) * { * //LibSys.StatusBar.Trace("IP: loading... " + info.baseName); * info.bytesProcessed = ccc; * info.ProgressCallback(info); * } * } * LibSys.StatusBar.Trace("OK: finished " + info.baseName); * return null; * } */ // Issue the async request. IAsyncResult r = (IAsyncResult)req.BeginGetResponse(new AsyncCallback(ResponseCallback), info); // Wait until the ManualResetEvent is set so that the application // does not exit until after the callback is called. bool hasSignal; if (m_timeOutMs == 0) { hasSignal = allDone.WaitOne(); } else { #if DEBUG //LibSys.StatusBar.Trace("IP: WebDownload:Download() at WaitOne - " + DateTime.Now + " - " + m_baseName + " " + info.strUrl); #endif hasSignal = allDone.WaitOne(m_timeOutMs, false); #if DEBUG //LibSys.StatusBar.Trace("IP: WebDownload:Download() after WaitOne - " + DateTime.Now + " - " + hasSignal + " " + m_baseName + " " + info.strUrl); #endif } if (!hasSignal) { allDone.Set(); info.hasTimedOut = true; return(null); } // Pass back the downloaded information. if (info.useFastBuffers) { return(info.dataBufferFast); } else { byte[] data = new byte[info.dataBufferSlow.Count]; for (int b = 0; b < info.dataBufferSlow.Count; b++) { data[b] = (byte)info.dataBufferSlow[b]; } return(data); } }
private static void imageDownloadProgressCallback( DownloadInfo info ) { //LibSys.StatusBar.Trace("IP: TileCache:imageDownloadProgressCallback() - " + info.baseName + " - " + info.bytesProcessed + " of " + info.dataLength); if(info.monitored != null) { if(info.dataLength > 0) { info.monitored.Progress = info.bytesProcessed * 100 / info.dataLength; ProgressMonitor.WorkValues(); } else { info.monitored.Progress = 20; } } }
private void completeCallback( object formObj, DownloadInfo info, string dldFileName, byte[] dataDownloaded ) { // marshall the call to the thread where this form was created, so that // component-related operations are not hanging the message pump this.Invoke(new DownloadCompleteHandler(_completeCallback), new object[] { formObj, info, dldFileName, dataDownloaded } ); }
private void completeCallback(object formObj, DownloadInfo info, string dldFileName, byte[] dataDownloaded) { // marshall the call to the thread where this form was created, so that // component-related operations are not hanging the message pump this.Invoke(new DownloadCompleteHandler(_completeCallback), new object[] { formObj, info, dldFileName, dataDownloaded }); }
private void _completeCallback( object formObj, DownloadInfo info, string dldFileName, byte[] dataDownloaded ) { #if DEBUG LibSys.StatusBar.Trace("IP: DloadProgressForm:completeCallback() - " + info.baseName + " " + (dataDownloaded == null? "null" : "" + dataDownloaded.Length) + " bytes loaded"); #endif Form form = (Form)formObj; if(dataDownloaded == null || Project.is404(dataDownloaded)) { string message = "failed: " + (dataDownloaded == null ? "no connection or no data" : "404 - file not found"); messageLabel.Text = message; LibSys.StatusBar.Error(message); if(m_closeIfFailed) { closeSoon(); } } else { if ( !progressBar.Visible ) { progressBar.Visible = true; progressBar.Minimum = 0; progressBar.Value = progressBar.Maximum = 1; totalBytesTextBox.Text = bytesDownloadedTextBox.Text; } FileStream fs = null; try { fs = new FileStream(dldFileName, FileMode.Create); fs.Write(dataDownloaded, 0, dataDownloaded.Length); fs.Close(); fs = null; LibSys.StatusBar.Trace("OK: file " + dldFileName + " created"); if(m_doRun) { // actually for help file only Help.ShowHelp(form, dldFileName); } closeNow(null, EventArgs.Empty); } catch (Exception e) { string message = "failed: " + e.Message; messageLabel.Text = message; LibSys.StatusBar.Error(message); if(m_closeIfFailed) { closeSoon(); } } finally { if(fs != null) { fs.Close(); } } } }
private static void imageDownloadCompleteCallback( object otile, DownloadInfo info, string imageFileName, byte[] dataDownloaded ) { #if DEBUG LibSys.StatusBar.Trace("IP: TerraserverCache:imageDownloadCompleteCallback() - " + info.baseName + " " + (dataDownloaded == null? "null" : "" + dataDownloaded.Length) + " bytes loaded"); #endif // tilesBeingLoadedCount = tilesBeingLoadedCount > 0 ? tilesBeingLoadedCount - 1 : 0; Backdrop backdrop = (Backdrop)m_backdropCache[info.baseName]; TileTerra tile = (TileTerra)otile; // can be null for preload if(dataDownloaded == null || dataDownloaded.Length < 100 || Project.is404(dataDownloaded)) { string comment = dataDownloaded == null ? "no data" : "404"; if(backdrop != null) { backdrop.IsEmpty = true; // proven empty } ProgressMonitor.markComplete(info.monitored, false, comment); } /* else if(dataDownloaded.Length == 8321) // cottage cheese { string comment = "cottage cheese tile"; backdrop.IsEmpty = true; // proven empty tile.IsCottageCheese = true; ProgressMonitor.markComplete(info.monitored, false, comment); } */ else { FileStream fs = null; try { string comment = "" + dataDownloaded.Length + " bytes"; if(dataDownloaded.Length == 8321) // cottage cheese { if(tile != null) { tile.IsCottageCheese = true; } comment = "cottage cheese tile"; } fs = new FileStream(imageFileName, FileMode.Create); fs.Write(dataDownloaded, 0, dataDownloaded.Length); fs.Close(); fs = null; #if DEBUG LibSys.StatusBar.Trace("OK: file " + imageFileName + " created"); #endif if(backdrop != null) { backdrop.Fill(); } ProgressMonitor.markComplete(info.monitored, true, comment); } catch (Exception e) { #if DEBUG LibSys.StatusBar.Error("" + e.Message); #endif if(backdrop != null) { backdrop.IsEmpty = true; // proven empty } ProgressMonitor.markComplete(info.monitored, false, e.Message); } finally { if(fs != null) { fs.Close(); } } } if(tile != null) { tile.backdropArrived(backdrop); } }
public byte[] Download( DownloadInfo info, DownloadProgressHandler progressCallback ) { //LibSys.StatusBar.Trace("IP: downloading - " + info.baseName + " == " + info.strUrl); allDoneLast = allDone; // Ensure flag set correctly. allDone.Reset(); m_baseName = info.baseName; // Get the URI from the command line. Uri httpSite = new Uri(info.strUrl); // Create the request object. HttpWebRequest req = (HttpWebRequest)WebRequest.Create(httpSite); if(Project.suspendKeepAlive) { req.KeepAlive = false; } /* * see Project.ApplyGlobalHTTPProxy() * if(Project.useProxy) { WebProxy proxy = new WebProxy(Project.proxyServer, Project.proxyPort); req.Proxy = proxy; } */ #if DEBUG LibSys.StatusBar.Trace("IP: downloading - " + info.baseName + " == " + info.strUrl + " after proxy: " + req.Proxy.GetProxy(httpSite)); #endif // Put the request into the state object so it can be passed around. info.Request = req; // Assign the callbacks info.ProgressCallback += progressCallback; /* // this is to debug ThreadPool and ProgressMonitor if(info.strUrl.IndexOf("ashx") != -1) { info.dataLength = 20; int ccc = 0; while (ccc++ < 20) { Thread.Sleep(500); if ( info.ProgressCallback != null ) { //LibSys.StatusBar.Trace("IP: loading... " + info.baseName); info.bytesProcessed = ccc; info.ProgressCallback(info); } } LibSys.StatusBar.Trace("OK: finished " + info.baseName); return null; } */ // Issue the async request. IAsyncResult r = (IAsyncResult) req.BeginGetResponse(new AsyncCallback(ResponseCallback), info); // Wait until the ManualResetEvent is set so that the application // does not exit until after the callback is called. bool hasSignal; if(m_timeOutMs == 0) { hasSignal = allDone.WaitOne(); } else { #if DEBUG //LibSys.StatusBar.Trace("IP: WebDownload:Download() at WaitOne - " + DateTime.Now + " - " + m_baseName + " " + info.strUrl); #endif hasSignal = allDone.WaitOne(m_timeOutMs, false); #if DEBUG //LibSys.StatusBar.Trace("IP: WebDownload:Download() after WaitOne - " + DateTime.Now + " - " + hasSignal + " " + m_baseName + " " + info.strUrl); #endif } if(!hasSignal) { allDone.Set(); info.hasTimedOut = true; return null; } // Pass back the downloaded information. if ( info.useFastBuffers ) { return info.dataBufferFast; } else { byte[] data = new byte[ info.dataBufferSlow.Count ]; for ( int b=0; b<info.dataBufferSlow.Count; b++ ) { data[b] = (byte) info.dataBufferSlow[b]; } return data; } }
public static void init(string cgibinptrUrl) { try { #if DEBUG LibSys.StatusBar.Trace("IP: reaching cgibinptr URL='" + cgibinptrUrl + "'"); #endif WebDownload webDL = new WebDownload(Project.webTimeoutMs); // default 7 seconds to time out // Create the state object. DownloadInfo info = new DownloadInfo(); info.baseName = ""; info.strUrl = cgibinptrUrl; info.addMonitoredMethod = null; byte[] downloadedData = null; int tries = 1; int maxTries = 1; while (tries <= maxTries && downloadedData == null && !info.is404) { downloadedData = webDL.Download(info, ProgressCallback); // will timeout #if DEBUG if(downloadedData != null) { LibSys.StatusBar.Trace("IP: try " + tries + " WebsiteInfo:Download() - " + cgibinptrUrl + " delivered " + downloadedData.Length + " bytes" ); } else { LibSys.StatusBar.Trace("IP: try " + tries + " WebsiteInfo:Download() - " + cgibinptrUrl + " delivered null bytes" ); } #endif tries++; } if(downloadedData == null || downloadedData.Length == 0) { LibSys.StatusBar.Error("failed to reach QuakeMap.com"); return; // Project.serverAvailable will be set to false, and no web attempts will take place. } LibSys.StatusBar.Trace("OK: reached QuakeMap.com"); string responseString = Project.ByteArrayToStr(downloadedData); StringReader reader = new StringReader (responseString); string upgrVersion = ""; bool ignoreUpgrade = false; int state = 0; string line; while((line=reader.ReadLine()) != null) { try { switch(state) { case 0: if(line.StartsWith("GBWABOUT=")) { Project.ABOUT_URL = line.Substring("GBWABOUT=".Length); #if DEBUG LibSys.StatusBar.Trace("OK: WebsiteInfo() - about='" + Project.ABOUT_URL + "'"); #endif HasReachedServer = true; } else if(line.StartsWith("GBWDLOAD=")) { Project.DLOAD_URL = line.Substring("GBWDLOAD=".Length); #if DEBUG LibSys.StatusBar.Trace("OK: WebsiteInfo() - download='" + Project.DLOAD_URL + "'"); #endif } else if(line.StartsWith("GBWUPDATE=")) { Project.UPDATE_URL = line.Substring("GBWUPDATE=".Length); #if DEBUG LibSys.StatusBar.Trace("OK: WebsiteInfo() - update='" + Project.UPDATE_URL + "'"); #endif } else if(line.StartsWith("GBWHELP=")) { Project.HELP_FILE_URL = line.Substring("GBWHELP=".Length); #if DEBUG LibSys.StatusBar.Trace("OK: WebsiteInfo() - help='" + Project.HELP_FILE_URL + "'"); #endif } else if(line.StartsWith("GBWHDATE=")) { string sDate = line.Substring("GBWHDATE=".Length); Project.HELP_FILE_DATE = Convert.ToDateTime(sDate); #if DEBUG LibSys.StatusBar.Trace("OK: WebsiteInfo() - helpFileDate='" + Project.HELP_FILE_DATE + "'"); #endif } else if(line.StartsWith("GBWGPSBABELHOME=")) { Project.GPSBABEL_URL = line.Substring("GBWGPSBABELHOME=".Length); #if DEBUG LibSys.StatusBar.Trace("OK: WebsiteInfo() - GPSBabel Home='" + Project.GPSBABEL_URL + "'"); #endif } else if(line.StartsWith("GBWMISC=")) { Project.MISC_FOLDER_URL = line.Substring("GBWMISC=".Length); #if DEBUG LibSys.StatusBar.Trace("OK: WebsiteInfo() - misc='" + Project.MISC_FOLDER_URL + "'"); #endif } else if(line.StartsWith("MESSAGE=")) { state = 1; Project.serverMessage = ""; } else if(line.StartsWith("UPGR")) { upgrVersion = line.Substring(4); if(Project.PROGRAM_VERSION_RELEASEDATE.Equals(upgrVersion)) { ignoreUpgrade = true; } else { Project.upgradeMessage = "\n"; } state = 2; } break; case 1: if(line.StartsWith("ENDMESSAGE")) { state = 0; } else { Project.serverMessage += (line + "\n"); } break; case 2: if(line.StartsWith("ENDUPGR")) { state = 0; } else { if(!ignoreUpgrade) { Project.upgradeMessage += (line + "\n"); } } break; } } catch {} } if(!Project.serverMessageLast.Equals(Project.serverMessage) && Project.upgradeMessage.Length == 0) { string message = Project.serverMessage; // + Project.upgradeMessage; upgrade message shows up in MainForm LibSys.StatusBar.Trace(message); Project.MessageBox(null, message); Project.serverMessageLast = Project.serverMessage; } } catch (Exception e) { LibSys.StatusBar.Error("exception: " + e.Message); } }