private void DisableUPnP() { 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 += 50); try { UPnP.deletePortMapping(UPnPPort, "TCP"); } catch (SocketException) { Dispatcher.Invoke(() => { lblUPnPStat.Foreground = Brushes.Red; lblUPnPStat.Text = "Error deleting port mapping. Make sure the port is set to the entry you want to remove."; }); return; } catch (WebException) { Dispatcher.Invoke(() => { lblUPnPStat.Foreground = Brushes.Red; lblUPnPStat.Text = "Error deleting port mapping. Make sure the port is set to the entry you want to remove."; }); return; } Dispatcher.Invoke(() => { prgbarUPnP.Value = prgbarUPnP.Maximum; BitmapImage bi3 = new BitmapImage(); bi3.BeginInit(); bi3.UriSource = new Uri("/MobiController;component/Resources/check.ico", UriKind.Relative); bi3.EndInit(); imgUPnPstat.Source = bi3; lblUPnPStat.Foreground = Brushes.Green; lblUPnPStat.Text = "Port mapping removed successfully."; }); } }
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."; }); } } }