private void zkb_DoWork(object sender, DoWorkEventArgs e) { string redistURL = @"https://redisq.zkillboard.com/listen.php"; string strContent = string.Empty; try { HttpClient hc = new HttpClient(); var response = hc.GetAsync(redistURL).Result; if (response.IsSuccessStatusCode) { strContent = response.Content.ReadAsStringAsync().Result; } } catch { e.Result = -1; return; } ZKBData.ZkbData z = ZKBData.ZkbData.FromJson(strContent); if (z != null && z.Package != null) { ZKBDataSimple zs = new ZKBDataSimple(); zs.KillID = long.Parse(z.Package.KillId.ToString()); zs.VictimAllianceID = long.Parse(z.Package.Killmail.Victim.AllianceId.ToString()); zs.VictimCharacterID = long.Parse(z.Package.Killmail.Victim.CharacterId.ToString()); zs.VictimCorpID = long.Parse(z.Package.Killmail.Victim.CharacterId.ToString()); zs.SystemName = EveManager.Instance.GetEveSystemNameFromID(z.Package.Killmail.SolarSystemId); if (zs.SystemName == string.Empty) { zs.SystemName = z.Package.Killmail.SolarSystemId.ToString(); } zs.KillTime = z.Package.Killmail.KillmailTime.ToLocalTime(); string shipID = z.Package.Killmail.Victim.ShipTypeId.ToString(); if (EveManager.Instance.ShipTypes.Keys.Contains(shipID)) { zs.ShipType = EveManager.Instance.ShipTypes[shipID]; } else { zs.ShipType = "Unknown (" + shipID + ")"; } zs.VictimAllianceName = EveManager.Instance.GetAllianceName(zs.VictimAllianceID); Application.Current.Dispatcher.Invoke((Action)(() => { KillStream.Insert(0, zs); })); } e.Result = 0; }
/// <summary> /// The main update function /// </summary> private void UpdateThreadFunc() { string redistURL = @"https://redisq.zkillboard.com/listen.php"; int cleanupCounter = 0; while (updateThreadRunning) { if (PauseUpdate) { if (KillStream.Count > 0) { Application.Current.Dispatcher.Invoke((Action)(() => { KillStream.Clear(); }), DispatcherPriority.Normal); } Thread.Sleep(5000); continue; } HttpWebRequest request = (HttpWebRequest)WebRequest.Create(redistURL); request.Method = WebRequestMethods.Http.Get; request.Timeout = 10000; request.Proxy = null; HttpWebResponse response; try { response = request.GetResponse() as HttpWebResponse; } catch { Thread.Sleep(1000); continue; } Stream responseStream = response.GetResponseStream(); using (StreamReader sr = new StreamReader(responseStream)) { // Need to return this response string strContent = sr.ReadToEnd(); try { ZKBData.ZkbData z = ZKBData.ZkbData.FromJson(strContent); if (z.Package != null) { ZKBDataSimple zs = new ZKBDataSimple(); zs.KillID = z.Package.KillId.ToString(); zs.VictimAllianceID = z.Package.Killmail.Victim.AllianceId.ToString(); zs.VictimCharacterID = z.Package.Killmail.Victim.CharacterId.ToString(); zs.VictimCorpID = z.Package.Killmail.Victim.CharacterId.ToString(); zs.SystemName = EveManager.Instance.GetEveSystemNameFromID(z.Package.Killmail.SolarSystemId.ToString()); if (zs.SystemName == string.Empty) { zs.SystemName = z.Package.Killmail.SolarSystemId.ToString(); } zs.KillTime = z.Package.Killmail.KillmailTime; string shipID = z.Package.Killmail.Victim.ShipTypeId.ToString(); if (EveManager.Instance.ShipTypes.Keys.Contains(shipID)) { zs.ShipType = EveManager.Instance.ShipTypes[shipID]; } else { zs.ShipType = "Unknown (" + shipID + ")"; } zs.VictimAllianceName = EveManager.Instance.GetAllianceName(zs.VictimAllianceID); Application.Current.Dispatcher.Invoke((Action)(() => { KillStream.Insert(0, zs); }), DispatcherPriority.Normal); } else { // nothing was returned by the request; rather than spam the server just wait 5 seconds Thread.Sleep(5000); } cleanupCounter++; // now clean up the list if (cleanupCounter > 5) { List <string> AllianceIDs = new List <string>(); Application.Current.Dispatcher.Invoke((Action)(() => { for (int i = KillStream.Count - 1; i >= 0; i--) { if (KillStream[i].VictimAllianceName == string.Empty) { if (!EveManager.Instance.AllianceIDToTicker.Keys.Contains(KillStream[i].VictimAllianceID) && !AllianceIDs.Contains(KillStream[i].VictimAllianceID) && KillStream[i].VictimAllianceID != "0") { AllianceIDs.Add(KillStream[i].VictimAllianceID); } else { KillStream[i].VictimAllianceName = EveManager.Instance.GetAllianceName(KillStream[i].VictimAllianceID); } } if (KillStream[i].KillTime + TimeSpan.FromMinutes(30) < DateTimeOffset.Now) { KillStream.RemoveAt(i); } } if (AllianceIDs.Count > 0) { EveManager.Instance.ResolveAllianceIDs(AllianceIDs); } }), DispatcherPriority.Normal); cleanupCounter = 0; } } catch { } } // wait for the next request Thread.Sleep(100); } }
private async void zkb_DoWork(object sender, DoWorkEventArgs e) { string redistURL = @"https://redisq.zkillboard.com/listen.php"; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(redistURL); request.Method = WebRequestMethods.Http.Get; request.Timeout = 60000; request.UserAgent = "SMT/0.82"; request.KeepAlive = true; request.Proxy = null; HttpWebResponse response; try { response = request.GetResponse() as HttpWebResponse; } catch (Exception) { e.Result = -1; return; } Stream responseStream = response.GetResponseStream(); using (StreamReader sr = new StreamReader(responseStream)) { try { // Need to return this response string strContent = sr.ReadToEnd(); ZKBData.ZkbData z = ZKBData.ZkbData.FromJson(strContent); if (z.Package != null) { ZKBDataSimple zs = new ZKBDataSimple(); zs.KillID = long.Parse(z.Package.KillId.ToString()); zs.VictimAllianceID = long.Parse(z.Package.Killmail.Victim.AllianceId.ToString()); zs.VictimCharacterID = long.Parse(z.Package.Killmail.Victim.CharacterId.ToString()); zs.VictimCorpID = long.Parse(z.Package.Killmail.Victim.CharacterId.ToString()); zs.SystemName = EveManager.Instance.GetEveSystemNameFromID(z.Package.Killmail.SolarSystemId); if (zs.SystemName == string.Empty) { zs.SystemName = z.Package.Killmail.SolarSystemId.ToString(); } zs.KillTime = z.Package.Killmail.KillmailTime; string shipID = z.Package.Killmail.Victim.ShipTypeId.ToString(); if (EveManager.Instance.ShipTypes.Keys.Contains(shipID)) { zs.ShipType = EveManager.Instance.ShipTypes[shipID]; } else { zs.ShipType = "Unknown (" + shipID + ")"; } zs.VictimAllianceName = EveManager.Instance.GetAllianceName(zs.VictimAllianceID); Application.Current.Dispatcher.Invoke((Action)(() => { KillStream.Insert(0, zs); })); } } catch { e.Result = -1; return; } } e.Result = 0; }