Ejemplo n.º 1
0
        private void button1_ClickAsync(object sender, EventArgs e)
        {
            button1.Enabled = false;

            string ApiKey = textBox1.Text;

            string Key = Registry.Get("Key");

            new Thread(async() =>
            {
                Thread.CurrentThread.IsBackground = true;

                PingRequest Ping = new PingRequest();

                //try
                //{
                await Ping.SendAsync(this.Router, ApiKey, Key);
                //}
                //catch (Exception exc)
                //{
                //MessageBox.Show(exc.ToString());
                //}

                if (Ping.LifeTime > 0)
                {
                    Registry.Set("ApiKey", ApiKey);

                    this.Invoke(new MethodInvoker(delegate
                    {
                        this.Close();
                    }));
                }

                button1.Invoke(new MethodInvoker(delegate
                {
                    button1.Text = "INVALID!";
                }));

                Thread.Sleep(1000);

                button1.Invoke(new MethodInvoker(delegate
                {
                    Thread.Sleep(2000);
                    button1.Enabled = true;
                    button1.Text    = "Submit";
                }));
            }).Start();
        }
Ejemplo n.º 2
0
        public async void timerTickAsync(object sender, EventArgs e)
        {
            Console.WriteLine("The timer ticked at {0:HH:mm:ss.fff}", DateTime.UtcNow);

            // Get some basic settings items out of the registry
            // which are required to make an API ping call
            string Key      = Registry.Get("Key");
            string ApiKey   = Registry.Get("ApiKey");
            string Interval = Registry.Get("Interval");

            // Protect application from crashing when important
            // settings values can not be found.
            if (ApiKey == "" || Interval == "" || Key == "")
            {
                return;
            }

            string host = Dns.GetHostName();

            // Reset timer interval incase it was changed in the settings
            this.loopTimer.Interval = Int32.Parse(Interval) * 1000;

            // If the router object is still being found, skip this ping
            if (Router == null)
            {
                this.TitleMenuItem.Image = Properties.Resources.bullet_yellow;

                this.TitleMenuItem.ToolTipText = "Your router does not seem to support all required features.";
            }
            else
            {
                this.TitleMenuItem.ToolTipText = "";
            }

            // Do something if the failed number of ping attempts is to high
            if (this.FailedPings > 3)
            {
                this.TitleMenuItem.Image = Properties.Resources.bullet_red;
            }
            else
            {
                this.TitleMenuItem.Image = Properties.Resources.bullet_green;
            }

            dynamic response;

            Ping = new PingRequest();

            try
            {
                response = await Ping.SendAsync(Router, ApiKey, Key);
            }
            catch (Exception exc)
            {
                Console.WriteLine(exc.Message);

                this.FailedPings++;

                return;
            }

            // Check with router to see if port is open
            // This allows the system to detect if port is still forwarded from a previous
            try
            {
                Mapping RoutedPort = this.Router.GetSpecificMapping(Protocol.Tcp, Int32.Parse(Registry.Get("Port")));
            }
            catch (Exception exc)
            {
            }
            this.isPortOpen = Ping.IsPortMapped;

            // If account is good, make menu header icon green
            if (Ping.LifeTime > 0)
            {
                this.TitleMenuItem.Image       = Properties.Resources.bullet_green;
                this.TitleMenuItem.ToolTipText = String.Format("Your API key is valid for {0} more days", Ping.LifeTime / (60 * 60 * 24));
            }

            // Display the update available menu item if update is available
            try
            {
                this.UpdateAvailableMenuItem.Visible = Ping.Version != null && Ping.Version != StaticHelpers.GetVersion();
            }
            catch (Exception exc)
            {
                Console.WriteLine(exc.Message);
            }

            // Keep running list of discovered machines, so that expired ones can be removed after
            List <string> machineList = new List <string>();

            if (Ping.RemoteMachines != null)
            {
                foreach (KeyValuePair <string, RemoteMachine> b in Ping.RemoteMachines)
                {
                    RemoteMachine x = b.Value;

                    string menukey = "menu___" + x.host;

                    machineList.Add(menukey);

                    // Remove any existing machine from the tray icon in a thread-safe way
                    if (TrayIconContextMenu.InvokeRequired)
                    {
                        TrayIconContextMenu.Invoke(new MethodInvoker(delegate
                        {
                            TrayIconContextMenu.Items.RemoveByKey(menukey);
                        }));
                    }
                    else
                    {
                        TrayIconContextMenu.Items.RemoveByKey(menukey);
                    }

                    ToolStripMenuItem HostMenuItem = this.CreateHostMenuItem(menukey, x);

                    // Add the menu item thread-safely
                    if (TrayIconContextMenu.InvokeRequired)
                    {
                        TrayIconContextMenu.Invoke(new MethodInvoker(delegate
                        {
                            this.TrayIconContextMenu.Items.Insert(2, HostMenuItem);
                        }));
                    }
                    else
                    {
                        this.TrayIconContextMenu.Items.Insert(2, HostMenuItem);
                    }
                }
            }

            List <string> removedMenuNames = new List <string>();

            // Remove expired entries
            foreach (var b in this.TrayIconContextMenu.Items)
            {
                ToolStripMenuItem test = b as ToolStripMenuItem;
                if (test != null && test.Name.Contains("menu___") && !machineList.Contains(test.Name))
                {
                    removedMenuNames.Add(test.Name);
                }
            }

            foreach (string removedMenuName in removedMenuNames)
            {
                this.TrayIconContextMenu.Items.RemoveByKey(removedMenuName);
            }

            // If this ping request resulted in commands which were processed, then a
            // new thread should run another ping request to send any updated status
            // back to the central server
            if (Ping.CommandWasProcessed == true)
            {
                new Thread(() =>
                {
                    Thread.CurrentThread.IsBackground = true;
                    timerTickAsync(null, null);
                }).Start();
            }

            // Refresh the interval value just incase it was changed
            this.SelectIntervalSetting(Int32.Parse(Registry.Get("Interval")));

            // Once a ping sequence has been fully completed, reset the fails counter
            this.FailedPings = 0;
        }