-
Notifications
You must be signed in to change notification settings - Fork 99
NatUtility 2.0
The TraceSource instance used for debugging and Troubleshooting
C# code
NatUtility.TraceSource.Switch.Level = SourceLevels.Verbose;
NatUtility.TraceSource.Listeners.Add(new ConsoleListener());
At least one trace listener has to be added to the Listeners collection if a trace is required; if no listener is added there will no be tracing to analyse.
Open.NAT only supports SourceLevels.Verbose, SourceLevels.Error, SourceLevels.Warning and SourceLevels.Information.
Starts the discovery process.
Once started, it continues searching for NAT devices and never stops. It has to be stopped invoking StopDiscovery. It is a good idea to let it run for a couple of seconds before to stop it.
Stops the discovery process.
Stopping the discovery process that no new attempts to search NAT devices will be performed by Open.NAT. However if once stopped, a NAT responses to a previous discovery request, that response is processed. It means after stopping discovery, it is possible to have a DeviceFound evented.
Release all ports opened by Open.NAT.
If ReleaseOnShutdown value is true, it release all the mappings created through the library.
Specifies if ports opened by Open.NAT should be closed as part of the process shutdown. All mappings will be released except those created as permanet. A permanent mapping can be created with Mapping.Lifetime=0. They will also be created when the router only support Permanent mappings. Default: true
Specifies the protocol that should be used for searching a NAT device. PortMapper enum For example, if the value is Upnp, the discovery process will search only for Upnp devices.
Specifies the maximun time (in milliseconds) that the discovery process can run before stop and fail. Searching timeout are notified using event DiscoveryTimeout Default: 5000 (5 seconds)
T:System.ArgumentOutOfRangeException: if value is less or equal to 0.
Occurs when a NAT device able to manage ports is found.
C# code
NatUtility.DeviceFound += (s, e)=>
Console.WriteLine("Found. Ext IP {0}", await e.Device.GetExternalIPAsync());
Currently Open.Nat is able to discover and map ports using UPNP and PMP however, those protocols don't provide the same set of features. While UPNP supports ports listing, PMP doesn't. If you try to list the opened ports with a NAT-PMP device a NotSupportedException will be thrown. Developers have to handle this situations by catching them.
The event is raised as many times as NAT devices are found in the LAN, most of the cases there is just one however, if two or more devices are discovered, it is important to identify which one should be used,
Occurs when a NAT device is not found before the elapsed time specified by DiscoveryTimeout
C# code
NatUtility.DiscoveryTimedout += (s, e)=>
Console.WriteLine("No NAT device found after {0} milliseconds", NatUtility.DiscoveryTimeout);
Before to raise this event Open.NAT stops the discovery process. Developers can increase the DiscoveryTimeout value and try again restarting the discovery with StartDiscovery method.
Occurs when occurs an exception that was not expected
C# code
NatUtility.UnhandledException += (s, e)=>
Console.WriteLine("Houston we have a problem: {0}", e.ExceptionObject);
This event should never be raised except for really exceptional situations and this iis only thrown while in the discovery stage.
The message sent to discover all uPnP devices on the network
Returns: