Пример #1
0
        private static void requestor_OnRequestCompleted(HttpRequestor sender, bool success, object tag, string url, byte[] data)
        {
            if (success)
            {
                // Fetch version information
                int    newversion = 0;
                string updatelink = null;
                try
                {
                    string page = UTF8Encoding.UTF8.GetString(data);
                    string x    = "##" + updatecode.ToString() + "##";
                    x = x.ToUpper();
                    int i = page.IndexOf(x);
                    if (i == -1)
                    {
                        requestor.OnRequestCompleted += requestor_OnRequestCompleted;
                        requestor = null;
                        return;
                    }
                    page = page.Substring(i + x.Length);
                    i    = page.IndexOf("##");
                    if (i == -1)
                    {
                        requestor.OnRequestCompleted += requestor_OnRequestCompleted;
                        requestor = null;
                        return;
                    }
                    string versionstr = page.Substring(0, i);
                    newversion = int.Parse(versionstr);
                    page       = page.Substring(i + 2);
                    i          = page.IndexOf("##");
                    if (i == -1)
                    {
                        requestor.OnRequestCompleted += requestor_OnRequestCompleted;
                        requestor = null;
                        return;
                    }
                    updatelink = page.Substring(0, i);
                }
                catch (Exception ex)
                {
                    EventLogger.Log(ex);
                    requestor.OnRequestCompleted += requestor_OnRequestCompleted;
                    requestor = null;
                    return;
                }

                // Compare to existing version
                if (existingversion < newversion)
                {
                    updateform = new AutoUpdate(newversion);
                    parentform.Invoke(new ThreadStart(ShowUpdateForm));
                }
            }
            requestor.OnRequestCompleted += requestor_OnRequestCompleted;
            requestor = null;
        }
Пример #2
0
        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);
            }
        }