private void startUPnP() { Dispatcher.Invoke(() => { BitmapImage bi3 = new BitmapImage(); bi3.BeginInit(); bi3.UriSource = new Uri("/MobiController;component/Resources/x.ico", UriKind.Relative); bi3.EndInit(); imgUPnPstat.Source = bi3; prgbarUPnP.Value = 0; lblUPnPStat.Text = ""; }); bool Discovered = false; try { Discovered = UPnP.Discover(); } catch (SocketException) { Dispatcher.Invoke(() => { lblUPnPStat.Foreground = Brushes.Red; lblUPnPStat.Text = "No UPnP enabled router was found."; }); } catch (WebException) { Dispatcher.Invoke(() => { lblUPnPStat.Foreground = Brushes.Red; lblUPnPStat.Text = "No UPnP enabled router was found."; }); } int serverPort = -1; int UPnPPort = -1; try{ Dispatcher.Invoke(() => { serverPort = Convert.ToInt32(txtPort.Text); UPnPPort = Convert.ToInt32(txtUPnPPort.Text); }); }catch (FormatException) { Dispatcher.Invoke(() => { new MessageBox("Error converting ports. Choose numeric port numbers.").Show(); }); return; } if (serverPort < 1 || UPnPPort < 1) { Dispatcher.Invoke(() => new MessageBox("Port error. Please specify positive port values.").Show()); return; } if (Discovered) { Dispatcher.Invoke(() => prgbarUPnP.Value += 20); try { UPnP.deletePortMapping(UPnPPort, "TCP"); } catch (SocketException) { } catch (WebException) { } Dispatcher.Invoke(() => prgbarUPnP.Value += 20); try { UPnP.addPortMapping(UPnPPort, serverPort, myTcpServer.getLocalIP().ToString(), "TCP", "MobiController"); } catch (SocketException) { Dispatcher.Invoke(() => { lblUPnPStat.Foreground = Brushes.Red; lblUPnPStat.Text = "Error mapping port. Try a port number greater than 6000."; }); return; } catch (WebException) { Dispatcher.Invoke(() => { lblUPnPStat.Foreground = Brushes.Red; lblUPnPStat.Text = "Error mapping port. Try a port number greater than 6000."; }); return; } Dispatcher.Invoke(() => prgbarUPnP.Value += 20); string ExternalIp = ""; try { ExternalIp = UPnP.getExternalIPAddress(); } catch (SocketException) { Dispatcher.Invoke(() => { lblUPnPStat.Foreground = Brushes.Red; lblUPnPStat.Text = "Unexpected error getting external IP. The port mapping may still work."; }); return; } catch (WebException) { Dispatcher.Invoke(() => { lblUPnPStat.Foreground = Brushes.Red; lblUPnPStat.Text = "Unexpected error getting external IP. The port mapping may still work."; }); return; } Dispatcher.Invoke(() => { txtExternalIP.Text = ExternalIp; if (UPnPPort != 80) { txtExternalIP.Text += ":" + UPnPPort.ToString(); } prgbarUPnP.Value += 20; }); try { HttpBrowserEntitiy browser = new HttpBrowserEntitiy(); var conn = browser.openConnection(ExternalIp, UPnPPort); var request = new HttpRequest("/UPnP"); request.addHeader("Connection", "Close"); request.addHeader("upnptestnum", myHttpEngine.UPNP_TEST_NUMBER.ToString()); request.Method = HttpEngine.META_HEAD; conn.sendRequest(request); } catch (SocketException) { Dispatcher.Invoke(() => { lblUPnPStat.Foreground = Brushes.Red; lblUPnPStat.Text = "Error testing connection, make sure there are no other port mappings to your local port."; }); } } }
public frmMain() { browser = new HttpBrowserEntitiy(); myTcpServer.SocketException += SocketException; iconOpacityFlipper = false; Dieing = false; isOpeningControllerBrowser = false; notifyIcon = new System.Windows.Forms.NotifyIcon(); System.Windows.Forms.ContextMenu trayMenu = new System.Windows.Forms.ContextMenu(); trayMenu.MenuItems.Add("Exit", OnExit); notifyIcon.Icon = new System.Drawing.Icon(MobiController.Properties.Resources.favicon, 40, 40); notifyIcon.Visible = true; notifyIcon.ContextMenu = trayMenu; showTrayMessage = () => { if (App.Config.isShowMessageOnMinimize) { notifyIcon.BalloonTipClicked += baloonClick; notifyIcon.ShowBalloonTip(2000, "", "You can open Mobicontroller by double clicking this icon. " + lblServerStatus.Text, System.Windows.Forms.ToolTipIcon.Info); } }; InitializeComponent(); // all preceeding commands must stay before this!!! lblTime.Text = DateTime.Now.ToString("hh:mm tt"); notifyIcon.DoubleClick += (sender, e) => { Show(); WinAPI.SetForegroundWindow(HWnd); }; if (App.Config.username.Equals("")) { login(); } else { procedureLoggedin(); } try { var updateConnection = browser.openConnection(WEB_ROOT, HttpBrowserEntitiy.PORTS.HTTP); // the version.txt file on the server contains the current version of MobiController updateConnection.sendRequestAsync(new HttpRequest("/releases/MobiController/version.txt"), (response) => { string strVersion = response.Body.Trim(); if (!App.VERSION.Equals(strVersion)) { Dispatcher.Invoke(() => { var msg = new MessageBox("An update to MobiController is available. Would you like to download the the installer?", "Update"); msg.PrgBar.Visibility = System.Windows.Visibility.Visible; msg.addButton("Download", () => { Dispatcher.Invoke(() => msg.prgbar.IsIndeterminate = true); var newConnection = Browser.openConnection(WEB_ROOT, HttpBrowserEntitiy.PORTS.HTTP); newConnection.sendRequestAsync(new HttpRequest("/releases/MobiController/" + strVersion + "/MobiController_v" + strVersion + ".exe"), (dlResponse) => { try { using (var fs = File.Create(App.APP_DATA_FOLDER + "update.exe")) { newConnection.continueDownload(fs, dlResponse, (contentlen) => { Dispatcher.Invoke(() => { Dispatcher.Invoke(() => msg.prgbar.IsIndeterminate = false); msg.prgbar.Maximum = contentlen; }); }, (inc) => { Dispatcher.Invoke(() => msg.prgbar.Value += inc); }); } Dispatcher.Invoke(() => msg.prgbar.IsIndeterminate = true); App.exec(App.APP_DATA_FOLDER + "update.exe"); Dispatcher.Invoke(() => msg.prgbar.IsIndeterminate = false); } catch (System.ComponentModel.Win32Exception) { Dispatcher.Invoke(() => { var errmsg = new MessageBox("The installer could not be started."); errmsg.addButton("Open Folder Location", () => { App.exec("explorer.exe", '"' + App.APP_DATA_FOLDER + '"'); //Process explorer = new Process(); //explorer.StartInfo.FileName = "explorer.exe"; //explorer.StartInfo.Arguments = '"' + App.APP_DATA_FOLDER; //explorer.Start(); }); errmsg.Show(); }); } catch (ConnectionException) { Dispatcher.Invoke(() => { new MessageBox("There was a problem with the server. Try again later or download it manually from the site.").Show(); }); } Dispatcher.Invoke(() => msg.Close()); }); }, false); msg.addButton("Go Online", () => { //Process p = new Process(); //p.StartInfo.FileName = "http://" + WEB_ROOT; //p.Start(); App.exec("http://" + WEB_ROOT); }); msg.Show(); }); } }); } catch (SocketException) { } }