Esempio n. 1
0
        /// <summary>Occurs when an asynchronous download operation completes.</summary>
        private void webClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            // Retrieve the CustomWebClient object
            CustomWebClient webClient = sender as CustomWebClient;

            if (webClient == null)
            {
                return;
            }

            // Local variables
            string     ip         = "-";
            string     status     = "-";
            StatusCode newStatus  = StatusCode.Online;
            int        imageIndex = 0;
            // Regex to match IP address
            Regex regex = new Regex(@"(?:[0-9]{1,3}\.){3}[0-9]{1,3}");

            if (e.Error == null && regex.IsMatch(e.Result)) // String downloaded successfully and contains IP address
            {
                // Get the first instance of an IP in the result using regex
                Match match = regex.Match(e.Result);

                // Remove line breaks from the IP address
                ip = match.Value;

                StatusMain.Text = "Testing speed at: " + Global.ConvertTime(DateTime.Now);

                // Begin downloading the data, passing the IP to the DownloadDataCompleted event
                if (Settings.Default.AutoDownload)
                {
                    string url          = string.Empty;
                    long   maximumSpeed = 0;

                    foreach (ListViewItem item in Global.ConnectionLog)
                    {
                        long speed = Global.ToInt64(item.SubItems[5].Text);

                        if (speed > maximumSpeed)
                        {
                            maximumSpeed = speed;
                        }
                    }

                    // Auto-select URL based on maximum download speed
                    if (maximumSpeed < 1024000)
                    {
                        // Download 3 MB file
                        url = "http://onlyforusebyspeedtest.loggger.com/~3mbfile.zip";
                    }
                    else if (maximumSpeed < 1024000 * 5)
                    {
                        // Download 6 MB file
                        url = "http://onlyforusebyspeedtest.loggger.com/~6mbfile.zip";
                    }
                    else
                    {
                        // Download 12 MB file
                        url = "http://onlyforusebyspeedtest.loggger.com/~12mbfile.zip";
                    }

                    webClient.DownloadDataAsync(new Uri(url), ip);
                }
                else
                {
                    webClient.DownloadDataAsync(new Uri(Settings.Default.DownloadURL), ip);
                }
            }
            else // An error occured downloading the string
            {
                StatusMain.Text = "Unable to test speed: " + Global.ConvertTime(DateTime.Now);

                // Set status to Offline
                newStatus  = StatusCode.Offline;
                imageIndex = 2;
                ip         = "-";

                if (!p_CurrentIP.Equals(ip))
                {
                    status      = "IP Changed";
                    imageIndex  = 1;
                    p_CurrentIP = ip;
                }

                if (!p_CurrentStatus.Equals(newStatus))
                {
                    status          = "Offline";
                    imageIndex      = 2;
                    p_CurrentStatus = newStatus;
                }

                // Create a new ListViewItem object
                ListViewItem item = new ListViewItem(Global.ConvertTime(DateTime.Now));
                item.SubItems.Add(Global.ConvertBytes(0) + "/s");
                item.SubItems.Add(p_CurrentIP);
                item.SubItems.Add("Offline");
                item.SubItems.Add(status);
                item.SubItems.Add("0");
                item.ImageIndex = imageIndex;
                item.Tag        = DateTime.Now;

                // Add the ListViewItem to the log
                Global.ConnectionLog.Add(item);

                switch (imageIndex)
                {
                case 0:     // Successful
                    if (ToolBarShowSuccessful.Pushed)
                    {
                        ListLog.Items.Insert(0, item);
                        item.EnsureVisible();
                    }
                    break;

                case 1:     // Status Changed
                    if (ToolBarShowStatusChanges.Pushed)
                    {
                        ListLog.Items.Insert(0, item);
                        item.EnsureVisible();
                    }
                    break;

                case 2:     // Error
                    if (ToolBarShowErrors.Pushed)
                    {
                        ListLog.Items.Insert(0, item);
                        item.EnsureVisible();
                    }
                    break;
                }

                // Dispose the CustomWebClient object
                webClient.Dispose();
            }
        }
Esempio n. 2
0
        private void webClient_DownloadDataCompleted(object sender, DownloadDataCompletedEventArgs e)
        {
            // Retrieve the CustomWebClient object
            CustomWebClient webClient = sender as CustomWebClient;

            if (webClient == null)
            {
                return;
            }

            if (p_TimerLog == null)
            {
                return;
            }

            // Retrieve the IP address
            string ip = (string)e.UserState;

            // Local variables
            string       status     = "-";
            StatusCode   newStatus  = StatusCode.Online;
            int          imageIndex = 0;
            ListViewItem item       = null;

            if (e.Error == null) // Data downloaded successfully
            {
                // Update the StatusBar
                StatusMain.Text = "Connection logged, size: " + Global.ConvertBytes(webClient.AllBytesDownloaded) + " speed: " + Global.ConvertBytes(webClient.CurrentSpeed) + "/s";

                if (!p_CurrentIP.Equals(ip))
                {
                    status      = "IP Changed";
                    imageIndex  = 1;
                    p_CurrentIP = ip;
                }

                if (!p_CurrentStatus.Equals(newStatus))
                {
                    status          = "Online";
                    imageIndex      = 1;
                    p_CurrentStatus = newStatus;
                }

                // Create a new ListViewItem object
                item = new ListViewItem(Global.ConvertTime(DateTime.Now));
                item.SubItems.Add(Global.ConvertBytes(webClient.CurrentSpeed) + "/s");
                item.SubItems.Add(p_CurrentIP);
                item.SubItems.Add("OK");
                item.SubItems.Add(status);
                item.SubItems.Add(webClient.CurrentSpeed.ToString());
                item.ImageIndex = imageIndex;
                item.Tag        = DateTime.Now;
            }
            else // An error occured downloading the data
            {
                // Update the StatusBar
                StatusMain.Text = "Unable to test speed: " + Global.ConvertTime(DateTime.Now);

                // Set status to Offline
                newStatus  = StatusCode.InvalidURL;
                imageIndex = 2;
                //ip = "-";

                if (!p_CurrentIP.Equals(ip))
                {
                    status      = "IP Changed";
                    imageIndex  = 1;
                    p_CurrentIP = ip;
                }

                if (!p_CurrentStatus.Equals(newStatus))
                {
                    status          = "Invalid URL";
                    imageIndex      = 2;
                    p_CurrentStatus = newStatus;
                }

                // Create a new ListViewItem object
                item = new ListViewItem(Global.ConvertTime(DateTime.Now));
                item.SubItems.Add(Global.ConvertBytes(0) + "/s");
                item.SubItems.Add(p_CurrentIP);
                item.SubItems.Add("Error");
                item.SubItems.Add(status);
                item.SubItems.Add("0");
                item.ImageIndex = imageIndex;
                item.Tag        = DateTime.Now;
            }

            if (item != null)
            {
                // Add the ListViewItem to the log
                Global.ConnectionLog.Add(item);

                switch (imageIndex)
                {
                case 0:     // Successful
                    if (ToolBarShowSuccessful.Pushed)
                    {
                        ListLog.Items.Insert(0, item);
                        item.EnsureVisible();
                    }
                    break;

                case 1:     // Status Changed
                    if (ToolBarShowStatusChanges.Pushed)
                    {
                        ListLog.Items.Insert(0, item);
                        item.EnsureVisible();
                    }
                    break;

                case 2:     // Error
                    if (ToolBarShowErrors.Pushed)
                    {
                        ListLog.Items.Insert(0, item);
                        item.EnsureVisible();
                    }
                    break;
                }
            }

            // Dispose the CustomWebClient object
            webClient.Dispose();

            // Save the speed log
            Global.SaveConnectionLog(Settings.Default.LogFile);
        }