/// <summary> /// Processes the request async. /// </summary> public async override Task ProcessAsync() { try { DataFeedBasicRequest dfRequest; try { dfRequest = await Receive <Structures.Basic.DataFeedBasicRequest>(); } catch (WebException) { dfRequest = new DataFeedBasicRequest("ForceDownload"); } Logging.Log($"Received data feed request from { ((IPEndPoint)client.Client.RemoteEndPoint).Address.ToString() }. Data: { dfRequest.ToString() }"); Structures.Basic.DataFeedBasicResponse dfRes = null; await Task.Run(() => { if (dfRequest.Version == "ForceDownload" || dfRequest.Version != DataFeed.Basic.Version) { dfRes = new Structures.Basic.DataFeedBasicResponse() { Data = DataFeed.Basic }; } else { dfRes = new Structures.Basic.DataFeedBasicResponse(); } }); Send(dfRes); Logging.Log($"Data feed response to { ((IPEndPoint)client.Client.RemoteEndPoint).Address.ToString() } was successfully send."); } catch (Exception ex) { Logging.Log($"Data feed request from { ((IPEndPoint)client.Client.RemoteEndPoint).Address.ToString() } could not be processed. { Logging.LogException(ex) }"); } Dispose(); }
/// <summary> /// Loads data while starting the application. /// </summary> public static async Task DownloadAsync(bool forceDownload = false, int timeout = 5000) { Downloaded = false; try { Structures.Basic.DataFeedBasicResponse response = null; try { using (var sr = new System.IO.StreamReader(BasePath + "basic/.version")) response = await new BasicDataProcessing().ProcessAsync(new Structures.Basic.DataFeedBasicRequest(sr.ReadLine()), timeout); } catch (Exception ex) { if (ex is WebException && (forceDownload || !System.IO.Directory.Exists(BasePath + "basic/"))) // Server offline and data does not exist. Cannot continue. { throw; } else if (ex is System.IO.IOException) // Data does not exist or the version file is corrupted. { try { response = await new BasicDataProcessing().ProcessAsync(new Structures.Basic.DataFeedBasicRequest(), timeout); } catch (Exception innerEx) { // Some data exists, we do not have to do anything. if (innerEx is WebException) { throw; } } } } if (response != null && response.ShouldBeUpdated) { response.Data.Save(BasePath); } } catch (Exception ex) { if (ex is WebException) // Server offline. { throw; } else { throw new ArgumentException("Fatal error. Cannot process the data."); } } Downloaded = true; Load(); }