Merge pull request #3018 from Mailaender/try-catch-mono-nat
Stop Mono.Nat from wreaking havoc for some people.
This commit is contained in:
@@ -265,19 +265,27 @@ namespace OpenRA
|
|||||||
Log.AddChannel("sync", "syncreport.log");
|
Log.AddChannel("sync", "syncreport.log");
|
||||||
Log.AddChannel("server", "server.log");
|
Log.AddChannel("server", "server.log");
|
||||||
|
|
||||||
try
|
if (Settings.Server.DiscoverNatDevices)
|
||||||
{
|
{
|
||||||
NatUtility.Logger = Log.Channels["server"].Writer;
|
try
|
||||||
NatUtility.Verbose = Settings.Server.VerboseNatDiscovery;
|
{
|
||||||
NatUtility.DeviceFound += DeviceFound;
|
NatUtility.Logger = Log.Channels["server"].Writer;
|
||||||
NatUtility.DeviceLost += DeviceLost;
|
NatUtility.Verbose = Settings.Server.VerboseNatDiscovery;
|
||||||
Settings.Server.NatDeviceAvailable = false;
|
NatUtility.DeviceFound += DeviceFound;
|
||||||
NatUtility.StartDiscovery();
|
NatUtility.DeviceLost += DeviceLost;
|
||||||
Log.Write("server", "NAT discovery started.");
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
else
|
||||||
{
|
{
|
||||||
Log.Write("server", "Can't discover UPnP-enabled device: {0}", e);
|
|
||||||
Settings.Server.NatDeviceAvailable = false;
|
Settings.Server.NatDeviceAvailable = false;
|
||||||
Settings.Server.AllowUPnP = false;
|
Settings.Server.AllowUPnP = false;
|
||||||
}
|
}
|
||||||
@@ -293,22 +301,37 @@ namespace OpenRA
|
|||||||
Sound.Create(Settings.Sound.Engine);
|
Sound.Create(Settings.Sound.Engine);
|
||||||
InitializeWithMods(Settings.Game.Mods);
|
InitializeWithMods(Settings.Game.Mods);
|
||||||
|
|
||||||
RunAfterDelay(Settings.Server.NatDiscoveryTimeout, () =>
|
if (Settings.Server.DiscoverNatDevices)
|
||||||
{
|
{
|
||||||
NatUtility.StopDiscovery();
|
RunAfterDelay(Settings.Server.NatDiscoveryTimeout, () =>
|
||||||
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));
|
Log.Write("server", "Stopping NAT discovery.");
|
||||||
Settings.Server.NatDeviceAvailable = false;
|
|
||||||
Settings.Server.AllowUPnP = false;
|
try
|
||||||
}
|
{
|
||||||
});
|
NatUtility.StopDiscovery();
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
Log.Write("server", "Failed to stop NAT device discovery: {0}", e);
|
||||||
|
Settings.Server.NatDeviceAvailable = false;
|
||||||
|
Settings.Server.AllowUPnP = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
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)
|
public static void DeviceFound(object sender, DeviceEventArgs args)
|
||||||
{
|
{
|
||||||
natDevice = args.Device;
|
if (args.Device == null)
|
||||||
|
return;
|
||||||
|
|
||||||
Log.Write("server", "NAT device discovered.");
|
Log.Write("server", "NAT device discovered.");
|
||||||
|
|
||||||
@@ -317,6 +340,7 @@ namespace OpenRA
|
|||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
natDevice = args.Device;
|
||||||
Log.Write("server", "Type: {0}", natDevice.GetType());
|
Log.Write("server", "Type: {0}", natDevice.GetType());
|
||||||
Log.Write("server", "Your external IP is: {0}", natDevice.GetExternalIP());
|
Log.Write("server", "Your external IP is: {0}", natDevice.GetExternalIP());
|
||||||
|
|
||||||
@@ -335,10 +359,20 @@ namespace OpenRA
|
|||||||
|
|
||||||
public static void DeviceLost(object sender, DeviceEventArgs args)
|
public static void DeviceLost(object sender, DeviceEventArgs args)
|
||||||
{
|
{
|
||||||
natDevice = args.Device;
|
|
||||||
|
|
||||||
Log.Write("server", "NAT device lost.");
|
Log.Write("server", "NAT device lost.");
|
||||||
Log.Write("server", "Type: {0}", natDevice.GetType());
|
|
||||||
|
if (args.Device == null)
|
||||||
|
return;
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
natDevice = args.Device;
|
||||||
|
Log.Write("server", "Type: {0}", natDevice.GetType());
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
Log.Write("server", "Can't fetch type from lost NAT device: {0}", e);
|
||||||
|
}
|
||||||
|
|
||||||
Settings.Server.NatDeviceAvailable = false;
|
Settings.Server.NatDeviceAvailable = false;
|
||||||
Settings.Server.AllowUPnP = false;
|
Settings.Server.AllowUPnP = false;
|
||||||
|
|||||||
@@ -29,7 +29,8 @@ namespace OpenRA.GameRules
|
|||||||
public int ExternalPort = 1234;
|
public int ExternalPort = 1234;
|
||||||
public bool AdvertiseOnline = true;
|
public bool AdvertiseOnline = true;
|
||||||
public string MasterServer = "http://master.open-ra.org/";
|
public string MasterServer = "http://master.open-ra.org/";
|
||||||
public bool AllowUPnP = true; // let the user disable it
|
public bool DiscoverNatDevices = true; // Allow users to disable NAT discovery if problems occur
|
||||||
|
public bool AllowUPnP = true; // let the user disable it even if compatible devices are found
|
||||||
public bool NatDeviceAvailable = false; // internal check if discovery succeeded
|
public bool NatDeviceAvailable = false; // internal check if discovery succeeded
|
||||||
public int NatDiscoveryTimeout = 1000; // ms to search for UPnP enabled NATs
|
public int NatDiscoveryTimeout = 1000; // ms to search for UPnP enabled NATs
|
||||||
public bool VerboseNatDiscovery = false; // print very detailed logs for debugging
|
public bool VerboseNatDiscovery = false; // print very detailed logs for debugging
|
||||||
@@ -51,6 +52,7 @@ namespace OpenRA.GameRules
|
|||||||
ExternalPort = other.ExternalPort;
|
ExternalPort = other.ExternalPort;
|
||||||
AdvertiseOnline = other.AdvertiseOnline;
|
AdvertiseOnline = other.AdvertiseOnline;
|
||||||
MasterServer = other.MasterServer;
|
MasterServer = other.MasterServer;
|
||||||
|
DiscoverNatDevices = other.DiscoverNatDevices;
|
||||||
AllowUPnP = other.AllowUPnP;
|
AllowUPnP = other.AllowUPnP;
|
||||||
NatDeviceAvailable = other.NatDeviceAvailable;
|
NatDeviceAvailable = other.NatDeviceAvailable;
|
||||||
NatDiscoveryTimeout = other.NatDiscoveryTimeout;
|
NatDiscoveryTimeout = other.NatDiscoveryTimeout;
|
||||||
|
|||||||
Reference in New Issue
Block a user