Better UPNP logging. Also fixes a compile error

This commit is contained in:
Scott_NZ
2013-04-06 01:04:35 +13:00
committed by Matthias Mailänder
parent 989c23e632
commit 083d59613a
4 changed files with 29 additions and 26 deletions

View File

@@ -26,7 +26,7 @@ namespace OpenRA
public static class Log public static class Log
{ {
static string LogPathPrefix = Environment.GetFolderPath(Environment.SpecialFolder.Personal) + Path.DirectorySeparatorChar; static string LogPathPrefix = Environment.GetFolderPath(Environment.SpecialFolder.Personal) + Path.DirectorySeparatorChar;
static Dictionary<string, ChannelInfo> channels = new Dictionary<string,ChannelInfo>(); public static readonly Dictionary<string, ChannelInfo> Channels = new Dictionary<string, ChannelInfo>();
public static string LogPath public static string LogPath
{ {
@@ -47,7 +47,7 @@ namespace OpenRA
public static void AddChannel(string channelName, string baseFilename) public static void AddChannel(string channelName, string baseFilename)
{ {
if (channels.ContainsKey(channelName)) return; if (Channels.ContainsKey(channelName)) return;
foreach (var filename in FilenamesForChannel(channelName, baseFilename)) foreach (var filename in FilenamesForChannel(channelName, baseFilename))
try try
@@ -55,7 +55,7 @@ namespace OpenRA
var writer = File.CreateText(filename); var writer = File.CreateText(filename);
writer.AutoFlush = true; writer.AutoFlush = true;
channels.Add(channelName, Channels.Add(channelName,
new ChannelInfo() new ChannelInfo()
{ {
Filename = filename, Filename = filename,
@@ -70,7 +70,7 @@ namespace OpenRA
public static void Write(string channel, string format, params object[] args) public static void Write(string channel, string format, params object[] args)
{ {
ChannelInfo info; ChannelInfo info;
if (!channels.TryGetValue(channel, out info)) if (!Channels.TryGetValue(channel, out info))
throw new Exception("Tried logging to non-existant channel " + channel); throw new Exception("Tried logging to non-existant channel " + channel);
info.Writer.WriteLine(format, args); info.Writer.WriteLine(format, args);

View File

@@ -264,14 +264,18 @@ namespace OpenRA
Log.AddChannel("sync", "syncreport.log"); Log.AddChannel("sync", "syncreport.log");
Log.AddChannel("server", "server.log"); Log.AddChannel("server", "server.log");
try { try
{
NatUtility.Logger = Log.Channels["server"].Writer;
NatUtility.Verbose = true;
NatUtility.DeviceFound += DeviceFound; NatUtility.DeviceFound += DeviceFound;
NatUtility.DeviceLost += DeviceLost; NatUtility.DeviceLost += DeviceLost;
NatUtility.StartDiscovery(); NatUtility.StartDiscovery();
OpenRA.Log.Write("server", "NAT discovery started."); Log.Write("server", "NAT discovery started.");
} catch (Exception e) { }
OpenRA.Log.Write("server", "Can't discover UPnP-enabled device: {0}", e); catch (Exception e)
{
Log.Write("server", "Can't discover UPnP-enabled device: {0}", e);
Settings.Server.AllowUPnP = false; Settings.Server.AllowUPnP = false;
} }
@@ -287,27 +291,26 @@ namespace OpenRA
InitializeWithMods(Settings.Game.Mods); InitializeWithMods(Settings.Game.Mods);
} }
public static void DeviceFound (object sender, DeviceEventArgs args) public static void DeviceFound(object sender, DeviceEventArgs args)
{ {
natDevice = args.Device; natDevice = args.Device;
Log.Write ("server", "NAT device discovered."); Log.Write("server", "NAT device discovered.");
Log.Write ("server", "Type: {0}", natDevice.GetType ().Name); 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());
foreach (Mapping mp in natDevice.GetAllMappings()) { foreach (var mp in natDevice.GetAllMappings())
Log.Write ("server", "Existing port mapping: protocol={0}, public={1}, private={2}", mp.Protocol, mp.PublicPort, mp.PrivatePort); Log.Write("server", "Existing port mapping: protocol={0}, public={1}, private={2}", mp.Protocol, mp.PublicPort, mp.PrivatePort);
}
Settings.Server.AllowUPnP = true; Settings.Server.AllowUPnP = true;
} }
public static void DeviceLost (object sender, DeviceEventArgs args) public static void DeviceLost(object sender, DeviceEventArgs args)
{ {
natDevice = args.Device; natDevice = args.Device;
Log.Write("server", "NAT device Lost"); Log.Write("server", "NAT device Lost");
Log.Write("server", "Type: {0}", natDevice.GetType().Name); Log.Write("server", "Type: {0}", natDevice.GetType());
Settings.Server.AllowUPnP = false; Settings.Server.AllowUPnP = false;
} }

View File

@@ -68,13 +68,13 @@
<Reference Include="SharpFont"> <Reference Include="SharpFont">
<SpecificVersion>False</SpecificVersion> <SpecificVersion>False</SpecificVersion>
<HintPath>..\thirdparty\SharpFont.dll</HintPath> <HintPath>..\thirdparty\SharpFont.dll</HintPath>
</Reference>
<Reference Include="Mono.Nat, Version=1.1.0.0, Culture=neutral"> <Reference Include="Mono.Nat, Version=1.1.0.0, Culture=neutral">
<SpecificVersion>False</SpecificVersion> <SpecificVersion>False</SpecificVersion>
<Private>False</Private> <Private>False</Private>
<Package>mono.nat</Package> <Package>mono.nat</Package>
<HintPath>..\thirdparty\Mono.Nat.dll</HintPath> <HintPath>..\thirdparty\Mono.Nat.dll</HintPath>
</Reference> </Reference>
</Reference>
<Reference Include="Tao.OpenAl, Version=1.1.0.1, Culture=neutral, PublicKeyToken=a7579dda88828311"> <Reference Include="Tao.OpenAl, Version=1.1.0.1, Culture=neutral, PublicKeyToken=a7579dda88828311">
<SpecificVersion>False</SpecificVersion> <SpecificVersion>False</SpecificVersion>
<HintPath>..\thirdparty\Tao\Tao.OpenAl.dll</HintPath> <HintPath>..\thirdparty\Tao\Tao.OpenAl.dll</HintPath>

View File

@@ -128,7 +128,7 @@ namespace OpenRA.Server
var timeout = ServerTraits.WithInterface<ITick>().Min(t => t.TickTimeout); var timeout = ServerTraits.WithInterface<ITick>().Min(t => t.TickTimeout);
for( ; ; ) for( ; ; )
{ {
var checkRead = new ArrayList(); var checkRead = new List<Socket>();
checkRead.Add( listener.Server ); checkRead.Add( listener.Server );
foreach( var c in conns ) checkRead.Add( c.socket ); foreach( var c in conns ) checkRead.Add( c.socket );
foreach( var c in preConns ) checkRead.Add( c.socket ); foreach( var c in preConns ) checkRead.Add( c.socket );
@@ -140,7 +140,7 @@ namespace OpenRA.Server
break; break;
} }
foreach( Socket s in checkRead ) foreach( var s in checkRead )
if( s == listener.Server ) AcceptConnection(); if( s == listener.Server ) AcceptConnection();
else if (preConns.Count > 0) else if (preConns.Count > 0)
{ {
@@ -181,13 +181,13 @@ namespace OpenRA.Server
{ {
try try
{ {
Mapping mapping = new Mapping (Protocol.Tcp, Settings.ExternalPort, Settings.ListenPort); var mapping = new Mapping(Protocol.Tcp, Settings.ExternalPort, Settings.ListenPort);
NatDevice.CreatePortMap(mapping); NatDevice.CreatePortMap(mapping);
Log.Write("server", "Create port mapping: protocol={0}, public={1}, private={2}", mapping.Protocol, mapping.PublicPort, mapping.PrivatePort); Log.Write("server", "Create port mapping: protocol={0}, public={1}, private={2}", mapping.Protocol, mapping.PublicPort, mapping.PrivatePort);
} }
catch (Exception e) catch (Exception e)
{ {
OpenRA.Log.Write("server", "Can not forward ports via UPnP: {0}", e); Log.Write("server", "Can not forward ports via UPnP: {0}", e);
Settings.AllowUPnP = false; Settings.AllowUPnP = false;
} }
} }
@@ -196,13 +196,13 @@ namespace OpenRA.Server
{ {
try try
{ {
Mapping mapping = new Mapping (Protocol.Tcp, Settings.ExternalPort, Settings.ListenPort); var mapping = new Mapping(Protocol.Tcp, Settings.ExternalPort, Settings.ListenPort);
NatDevice.DeletePortMap (mapping); NatDevice.DeletePortMap(mapping);
Log.Write("server", "Remove port mapping: protocol={0}, public={1}, private={2}", mapping.Protocol, mapping.PublicPort, mapping.PrivatePort); Log.Write("server", "Remove port mapping: protocol={0}, public={1}, private={2}", mapping.Protocol, mapping.PublicPort, mapping.PrivatePort);
} }
catch (Exception e) catch (Exception e)
{ {
OpenRA.Log.Write("server", "Can not remove UPnP portforwarding rules: {0}", e); Log.Write("server", "Can not remove UPnP portforwarding rules: {0}", e);
Settings.AllowUPnP = false; Settings.AllowUPnP = false;
} }
} }