/// <summary> /// Instantiate a one-time use Factory /// </summary> /// <param name="DescLocation">XML Description Uri</param> /// <param name="MaxSeconds">Device Refresh Cycle</param> /// <param name="deviceCB">Success Callback</param> /// <param name="failedCB">Failure Callback</param> public UPnPDeviceFactory(Uri DescLocation, int MaxSeconds, UPnPDeviceHandler deviceCB, UPnPDeviceFailedHandler failedCB, IPAddress localaddr, string usn) { InstanceTracker.Add(this); httprequestor = new HttpRequestor(); httprequestor.OnRequestCompleted += httprequestor_OnRequestCompleted; expected_usn = usn; CBLock = new object(); OnDevice += deviceCB; OnFailed2 += failedCB; DUrl = DescLocation.ToString(); MaxAge = MaxSeconds; this.localaddr = localaddr; httprequestor.LaunchRequest(DescLocation.ToString(), null, null, null, null); }
public static void UpdateCheck(Form parent) { if (requestor != null) { return; } if (File.Exists(Application.StartupPath + "\\AutoUpdateTool.exe") == false) { return; } if (ReadUpdateSettings() == false) { return; } if (AllowAutoUpdate == false) { return; } parentform = parent; requestor = new HttpRequestor(); requestor.OnRequestCompleted += requestor_OnRequestCompleted; requestor.LaunchProxyRequest(updatelink, null, 1); }
private void httprequestor_OnRequestCompleted(HttpRequestor sender, bool success, object tag, string url, byte[] data) { try { if (url == null) { EventLogger.Log(this, EventLogEntryType.Error, "HTTP: Url is empty"); if (TempDevice != null) { TempDevice = null; } return; } if (!success) { int checkpoint = 0; try { EventLogger.Log(this, EventLogEntryType.Error, "HTTP: Could not connect to target: " + url); checkpoint = 1; if (OnFailed2 != null) { OnFailed2(this, new Uri(url), new Exception("Could not connect to target"), expected_usn); } checkpoint = 2; if (TempDevice != null) { TempDevice = null; } checkpoint = 3; } catch (Exception ex) { EventLogger.Log(ex); ex.Data["v-success"] = success; ex.Data["v-tag"] = tag; ex.Data["v-url"] = url; ex.Data["v-data"] = data; ex.Data["checkpoint"] = checkpoint; AutoUpdate.ReportCrash(Application.ProductName, ex); } return; } if (data == null) { EventLogger.Log(this, EventLogEntryType.Error, "HTTP: Data is empty"); if (OnFailed2 != null) { OnFailed2(this, new Uri(url), new Exception("Data is empty"), expected_usn); } if (TempDevice != null) { TempDevice = null; } return; } string html = UTF8Encoding.UTF8.GetString(data); if (tag != null) { bool IsOK = false; lock (CBLock) { try { ((UPnPService)tag).ParseSCPD(html, 0); } catch (Exception e) { EventLogger.Log(e, "Invalid SCPD XML on device:\r\n Friendly: " + TempDevice.FriendlyName + "\r\n Service: " + ((UPnPService)tag).ServiceURN + "\r\n URL: " + url + "\r\n XML:\r\n" + html + "\r\n"); return; } --ServiceNum; if ((ServiceNum == 0) && (OnDevice != null)) { IsOK = true; } } if (IsOK) { TempDevice.descXmlLocation = new Uri(url); OnDevice(this, TempDevice, new Uri(url)); TempDevice = null; } return; } try { TempDevice = UPnPDevice.Parse(html, new Uri(url), localaddr); } catch (Exception ex) { EventLogger.Log(ex, "UPnP Device Description XML parsing exception: URL=" + url); if (OnFailed2 != null) { OnFailed2(this, new Uri(url), new Exception("UPnP Device Description XML parsing exception: URL=" + url), expected_usn); } if (TempDevice != null) { TempDevice = null; } return; } if (TempDevice == null) { //OpenSource.Utilities.EventLogger.Log(this, System.Diagnostics.EventLogEntryType.Error, "Invalid UPnP Device Description: URL=" + url); if (OnFailed2 != null) { OnFailed2(this, new Uri(url), new Exception("Invalid UPnP Device Description XML @" + url), expected_usn); } if (TempDevice != null) { TempDevice = null; } return; } if (expected_usn != null && TempDevice.UniqueDeviceName != expected_usn) { EventLogger.Log(this, EventLogEntryType.Error, string.Format("Unique ID mismatch between SSDP packet and device description: {0} / {1} ", TempDevice.UniqueDeviceName, expected_usn)); if (OnFailed2 != null) { OnFailed2(this, new Uri(url), new Exception(string.Format("Unique ID mismatch between SSDP packet and device description: {0} / {1} ", TempDevice.UniqueDeviceName, expected_usn)), expected_usn); } if (TempDevice != null) { TempDevice = null; } return; } TempDevice.LocationURL = url; TempDevice.ExpirationTimeout = MaxAge; if (TempDevice != null) { ServiceNum = FetchServiceCount(TempDevice); if (ServiceNum == 0) { if (OnDevice != null) { OnDevice(this, TempDevice, new Uri(url)); TempDevice = null; return; } } FetchServiceDocuments(TempDevice); } } catch (NullReferenceException ex) { // This happens often, so add data to this exception. ex.Data["v-success"] = success; ex.Data["v-tag"] = tag; ex.Data["v-url"] = url; ex.Data["v-data"] = data; EventLogger.Log(ex); AutoUpdate.ReportCrash(Application.ProductName, ex); } }