/// <summary> /// Adds or removes the port forwarding mapping from the Internet to the web server. /// </summary> /// <param name="setMappingPresent"></param> /// <param name="overrideIsEnabled"></param> /// <param name="overrideExternalPort"></param> private void DoMapping(bool setMappingPresent, bool?overrideIsEnabled = null, int?overrideExternalPort = null) { if (!_Initialised) { throw new InvalidOperationException("You must initialise the UPnP manager before use"); } var isMappingPresent = PortForwardingPresent; var isEnabled = overrideIsEnabled ?? IsEnabled; var externalPort = overrideExternalPort ?? _ExternalPort; if (IsRouterPresent) { try { isMappingPresent = DoesDirectMappingExist(externalPort); if (isEnabled && isMappingPresent != setMappingPresent) { if (setMappingPresent) { Provider.AddMapping(externalPort, "TCP", WebServer.Port, WebServer.NetworkIPAddress, true, Description); } else { Provider.RemoveMapping(externalPort, "TCP"); } isMappingPresent = setMappingPresent; } } catch (Exception ex) { // Exceptions thrown by the COM stuff are discarded, see notes in Initialise Debug.WriteLine(String.Format("UniversalPlugAndPlayManager.DoMapping caught exception {0}", ex.ToString())); } } if (isMappingPresent != PortForwardingPresent) { PortForwardingPresent = setMappingPresent; OnStateChanged(EventArgs.Empty); } }