-
-
Save lontivero/fe72e89d4aea753fc147 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Diagnostics; | |
using System.Threading; | |
using System.Threading.Tasks; | |
using Open.Nat; | |
namespace ConsoleApplication3 | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
Test.Initialize(); | |
} | |
} | |
public class Test | |
{ | |
private static List<Tuple<Protocol, int>> PortMapRequirements = new List<Tuple<Protocol, int>>(); | |
private static List<Task> _tasks = new List<Task>(); | |
private static ManualResetEvent _discovery = new ManualResetEvent(false); | |
public static void Initialize() | |
{ | |
PortMapRequirements.Add(new Tuple<Protocol, int>(Protocol.Udp, 27015)); | |
PortMapRequirements.Add(new Tuple<Protocol, int>(Protocol.Udp, 27016)); | |
PortMapRequirements.Add(new Tuple<Protocol, int>(Protocol.Udp, 27017)); | |
PortMapRequirements.Add(new Tuple<Protocol, int>(Protocol.Udp, 27018)); | |
PortMapRequirements.Add(new Tuple<Protocol, int>(Protocol.Udp, 27019)); | |
PortMapRequirements.Add(new Tuple<Protocol, int>(Protocol.Udp, 27020)); | |
PortMapRequirements.Add(new Tuple<Protocol, int>(Protocol.Udp, 27021)); | |
PortMapRequirements.Add(new Tuple<Protocol, int>(Protocol.Udp, 27022)); | |
NatUtility.TraceSource.Switch.Level = SourceLevels.All; | |
NatUtility.TraceSource.Listeners.Add(new ConsoleTraceListener()); | |
NatUtility.PortMapper = PortMapper.Upnp; | |
NatUtility.DeviceFound += DeviceFound; | |
NatUtility.DiscoveryTimedout += DiscoveryTimedout; | |
NatUtility.DiscoveryTimeout = 10000; // Seconds // TODO make this smaller once the slow port map issue is fixed. | |
NatUtility.ReleaseOnShutdown = false; | |
NatUtility.Initialize(); | |
NatUtility.StartDiscovery(); | |
_discovery.WaitOne(); // this stops the execution waiting for a nat device | |
Task.WaitAll(_tasks.ToArray()); // this waits for all the mapping requests | |
} | |
private static void DeviceFound(object sender, DeviceEventArgs args) | |
{ | |
var device = args.Device; | |
foreach (var portMap in PortMapRequirements) | |
{ | |
var protocol = portMap.Item1; | |
var port = portMap.Item2; | |
var t = CloseAndReopenPort(device, protocol, port); | |
_tasks.Add(t); | |
} | |
_discovery.Set(); | |
} | |
private static Task CloseAndReopenPort(NatDevice device, Protocol protocol, int port) | |
{ | |
return device.DeletePortMapAsync(new Mapping(protocol, port, port)) | |
.ContinueWith(t => device.CreatePortMapAsync(new Mapping(protocol, port, port, 0, "YourGameName"))); | |
} | |
static void DiscoveryTimedout(object sender, DiscoveryTimeoutEventArgs e) | |
{ | |
_discovery.Set(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment