enhanced UPnP discovery usability

- don't search for NATs forever, 5 seconds seem to be enough
- disable the UPnP checkbox if discovery failed
- no computer industry acronyms in user GUI
- don't remove port-forwarding twice, just once on shutdown
- user-configurable settings for debugging
This commit is contained in:
Matthias Mailänder
2013-04-06 12:54:41 +02:00
parent 083d59613a
commit 0488903a15
6 changed files with 30 additions and 10 deletions

View File

@@ -267,15 +267,17 @@ namespace OpenRA
try
{
NatUtility.Logger = Log.Channels["server"].Writer;
NatUtility.Verbose = true;
NatUtility.Verbose = Settings.Server.VerboseNatDiscovery;
NatUtility.DeviceFound += DeviceFound;
NatUtility.DeviceLost += DeviceLost;
Settings.Server.NatDeviceAvailable = false;
NatUtility.StartDiscovery();
Log.Write("server", "NAT discovery started.");
}
catch (Exception e)
{
Log.Write("server", "Can't discover UPnP-enabled device: {0}", e);
Settings.Server.NatDeviceAvailable = false;
Settings.Server.AllowUPnP = false;
}
@@ -289,6 +291,18 @@ namespace OpenRA
Sound.Create(Settings.Sound.Engine);
InitializeWithMods(Settings.Game.Mods);
RunAfterDelay(Settings.Server.NatDiscoveryTimeout, () =>
{
NatUtility.StopDiscovery();
Log.Write("server", "NAT discovery stopped.");
if (natDevice == null)
{
Log.Write("server", "No NAT devices with UPnP enabled found within {0} ms deadline. Disabling automatic port forwarding.".F(Settings.Server.NatDiscoveryTimeout));
Settings.Server.NatDeviceAvailable = false;
Settings.Server.AllowUPnP = false;
}
});
}
public static void DeviceFound(object sender, DeviceEventArgs args)
@@ -302,6 +316,7 @@ namespace OpenRA
foreach (var mp in natDevice.GetAllMappings())
Log.Write("server", "Existing port mapping: protocol={0}, public={1}, private={2}", mp.Protocol, mp.PublicPort, mp.PrivatePort);
Settings.Server.NatDeviceAvailable = true;
Settings.Server.AllowUPnP = true;
}
@@ -309,9 +324,10 @@ namespace OpenRA
{
natDevice = args.Device;
Log.Write("server", "NAT device Lost");
Log.Write("server", "NAT device lost.");
Log.Write("server", "Type: {0}", natDevice.GetType());
Settings.Server.NatDeviceAvailable = false;
Settings.Server.AllowUPnP = false;
}

View File

@@ -29,7 +29,10 @@ namespace OpenRA.GameRules
public int ExternalPort = 1234;
public bool AdvertiseOnline = true;
public string MasterServer = "http://master.open-ra.org/";
public bool AllowUPnP = true;
public bool AllowUPnP = true; // let the user disable it
public bool NatDeviceAvailable = false; // internal check if discovery succeeded
public int NatDiscoveryTimeout = 1000; // ms to search for UPnP enabled NATs
public bool VerboseNatDiscovery = false; // print very detailed logs for debugging
public bool AllowCheats = false;
public string Map = null;
public string[] Ban = null;
@@ -49,6 +52,9 @@ namespace OpenRA.GameRules
AdvertiseOnline = other.AdvertiseOnline;
MasterServer = other.MasterServer;
AllowUPnP = other.AllowUPnP;
NatDeviceAvailable = other.NatDeviceAvailable;
NatDiscoveryTimeout = other.NatDiscoveryTimeout;
VerboseNatDiscovery = other.VerboseNatDiscovery;
AllowCheats = other.AllowCheats;
Map = other.Map;
Ban = other.Ban;

View File

@@ -80,8 +80,6 @@ namespace OpenRA.Server
{
foreach (var t in ServerTraits.WithInterface<IEndGame>())
t.GameEnded(this);
if (Settings.AllowUPnP)
RemovePortforward();
}
public Server(IPEndPoint endpoint, string[] mods, ServerSettings settings, ModData modData, INatDevice natDevice)
@@ -159,6 +157,8 @@ namespace OpenRA.Server
if (State == ServerState.ShuttingDown)
{
EndGame();
if (Settings.AllowUPnP)
RemovePortforward();
break;
}
}
@@ -166,9 +166,6 @@ namespace OpenRA.Server
foreach (var t in ServerTraits.WithInterface<INotifyServerShutdown>())
t.ServerShutdown(this);
if (Settings.AllowUPnP)
RemovePortforward();
preConns.Clear();
conns.Clear();
try { listener.Stop(); }

View File

@@ -69,6 +69,7 @@ namespace OpenRA.Mods.RA.Widgets.Logic
var UPnPCheckbox = panel.Get<CheckboxWidget>("UPNP_CHECKBOX");
UPnPCheckbox.IsChecked = () => allowUPnP;
UPnPCheckbox.OnClick = () => allowUPnP ^= true;
UPnPCheckbox.IsDisabled = () => !Game.Settings.Server.NatDeviceAvailable;
}
void CreateAndJoin()

View File

@@ -75,7 +75,7 @@ Container@CREATESERVER_PANEL:
Y:180
Width:300
Height:20
Text:Allow UPnP port forwarding
Text:Automatic port forwarding
Label@EXTERNAL_PORT_LABEL:
X:15
Y:219

View File

@@ -65,7 +65,7 @@ Background@CREATESERVER_BG:
Y:165
Width:300
Height:20
Text:Allow UPnP port forwarding
Text:Automatic port forwarding
Button@CREATE_BUTTON:
X:130
Y:PARENT_BOTTOM - 45