separate UPnP from Game/Server into it's own class
This commit is contained in:
@@ -21,10 +21,6 @@ using OpenRA.Network;
|
|||||||
using OpenRA.Support;
|
using OpenRA.Support;
|
||||||
using OpenRA.Widgets;
|
using OpenRA.Widgets;
|
||||||
|
|
||||||
using Mono.Nat;
|
|
||||||
using Mono.Nat.Pmp;
|
|
||||||
using Mono.Nat.Upnp;
|
|
||||||
|
|
||||||
using XRandom = OpenRA.Thirdparty.Random;
|
using XRandom = OpenRA.Thirdparty.Random;
|
||||||
|
|
||||||
namespace OpenRA
|
namespace OpenRA
|
||||||
@@ -38,8 +34,6 @@ namespace OpenRA
|
|||||||
public static ModData modData;
|
public static ModData modData;
|
||||||
static WorldRenderer worldRenderer;
|
static WorldRenderer worldRenderer;
|
||||||
|
|
||||||
public static INatDevice natDevice;
|
|
||||||
|
|
||||||
public static Viewport viewport;
|
public static Viewport viewport;
|
||||||
public static Settings Settings;
|
public static Settings Settings;
|
||||||
|
|
||||||
@@ -266,24 +260,7 @@ namespace OpenRA
|
|||||||
Log.AddChannel("server", "server.log");
|
Log.AddChannel("server", "server.log");
|
||||||
|
|
||||||
if (Settings.Server.DiscoverNatDevices)
|
if (Settings.Server.DiscoverNatDevices)
|
||||||
{
|
UPnP.TryNatDiscovery();
|
||||||
try
|
|
||||||
{
|
|
||||||
NatUtility.Logger = Log.Channels["server"].Writer;
|
|
||||||
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.AllowPortForward = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
Settings.Server.NatDeviceAvailable = false;
|
Settings.Server.NatDeviceAvailable = false;
|
||||||
@@ -304,80 +281,11 @@ namespace OpenRA
|
|||||||
if (Settings.Server.DiscoverNatDevices)
|
if (Settings.Server.DiscoverNatDevices)
|
||||||
{
|
{
|
||||||
RunAfterDelay(Settings.Server.NatDiscoveryTimeout, () =>
|
RunAfterDelay(Settings.Server.NatDiscoveryTimeout, () =>
|
||||||
{
|
UPnP.TryStoppingNatDiscovery()
|
||||||
Log.Write("server", "Stopping NAT discovery.");
|
);
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
NatUtility.StopDiscovery();
|
|
||||||
}
|
|
||||||
catch (Exception e)
|
|
||||||
{
|
|
||||||
Log.Write("server", "Failed to stop NAT device discovery: {0}", e);
|
|
||||||
Settings.Server.NatDeviceAvailable = false;
|
|
||||||
Settings.Server.AllowPortForward = 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.AllowPortForward = false;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void DeviceFound(object sender, DeviceEventArgs args)
|
|
||||||
{
|
|
||||||
if (args.Device == null)
|
|
||||||
return;
|
|
||||||
|
|
||||||
Log.Write("server", "NAT device discovered.");
|
|
||||||
|
|
||||||
Settings.Server.NatDeviceAvailable = true;
|
|
||||||
Settings.Server.AllowPortForward = true;
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
natDevice = args.Device;
|
|
||||||
Log.Write("server", "Type: {0}", natDevice.GetType());
|
|
||||||
Log.Write("server", "Your external IP is: {0}", natDevice.GetExternalIP());
|
|
||||||
|
|
||||||
foreach (var mp in natDevice.GetAllMappings())
|
|
||||||
Log.Write("server", "Existing port mapping: protocol={0}, public={1}, private={2}",
|
|
||||||
mp.Protocol, mp.PublicPort, mp.PrivatePort);
|
|
||||||
}
|
|
||||||
catch (Exception e)
|
|
||||||
{
|
|
||||||
Log.Write("server", "Can't fetch information from NAT device: {0}", e);
|
|
||||||
|
|
||||||
Settings.Server.NatDeviceAvailable = false;
|
|
||||||
Settings.Server.AllowPortForward = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void DeviceLost(object sender, DeviceEventArgs args)
|
|
||||||
{
|
|
||||||
Log.Write("server", "NAT device lost.");
|
|
||||||
|
|
||||||
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.AllowPortForward = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void InitializeWithMods(string[] mods)
|
public static void InitializeWithMods(string[] mods)
|
||||||
{
|
{
|
||||||
// Clear static state if we have switched mods
|
// Clear static state if we have switched mods
|
||||||
@@ -524,7 +432,7 @@ namespace OpenRA
|
|||||||
public static void CreateServer(ServerSettings settings)
|
public static void CreateServer(ServerSettings settings)
|
||||||
{
|
{
|
||||||
server = new Server.Server(new IPEndPoint(IPAddress.Any, settings.ListenPort),
|
server = new Server.Server(new IPEndPoint(IPAddress.Any, settings.ListenPort),
|
||||||
Game.Settings.Game.Mods, settings, modData, natDevice);
|
Game.Settings.Game.Mods, settings, modData);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static int CreateLocalServer(string map)
|
public static int CreateLocalServer(string map)
|
||||||
@@ -541,7 +449,7 @@ namespace OpenRA
|
|||||||
settings.AllowPortForward = false;
|
settings.AllowPortForward = false;
|
||||||
|
|
||||||
server = new Server.Server(new IPEndPoint(IPAddress.Loopback, 0),
|
server = new Server.Server(new IPEndPoint(IPAddress.Loopback, 0),
|
||||||
Game.Settings.Game.Mods, settings, modData, natDevice);
|
Game.Settings.Game.Mods, settings, modData);
|
||||||
|
|
||||||
return server.Port;
|
return server.Port;
|
||||||
}
|
}
|
||||||
|
|||||||
146
OpenRA.Game/Network/UPnP.cs
Normal file
146
OpenRA.Game/Network/UPnP.cs
Normal file
@@ -0,0 +1,146 @@
|
|||||||
|
#region Copyright & License Information
|
||||||
|
/*
|
||||||
|
* Copyright 2007-2013 The OpenRA Developers (see AUTHORS)
|
||||||
|
* This file is part of OpenRA, which is free software. It is made
|
||||||
|
* available to you under the terms of the GNU General Public License
|
||||||
|
* as published by the Free Software Foundation. For more information,
|
||||||
|
* see COPYING.
|
||||||
|
*/
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
using System;
|
||||||
|
|
||||||
|
using Mono.Nat;
|
||||||
|
using Mono.Nat.Pmp;
|
||||||
|
using Mono.Nat.Upnp;
|
||||||
|
|
||||||
|
namespace OpenRA.Network
|
||||||
|
{
|
||||||
|
class UPnP
|
||||||
|
{
|
||||||
|
public static INatDevice NatDevice;
|
||||||
|
|
||||||
|
public static void TryNatDiscovery()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
NatUtility.Logger = Log.Channels["server"].Writer;
|
||||||
|
NatUtility.Verbose = Game.Settings.Server.VerboseNatDiscovery;
|
||||||
|
NatUtility.DeviceFound += DeviceFound;
|
||||||
|
NatUtility.DeviceLost += DeviceLost;
|
||||||
|
Game.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);
|
||||||
|
Game.Settings.Server.NatDeviceAvailable = false;
|
||||||
|
Game.Settings.Server.AllowPortForward = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void TryStoppingNatDiscovery()
|
||||||
|
{
|
||||||
|
Log.Write("server", "Stopping NAT discovery.");
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
NatUtility.StopDiscovery();
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
Log.Write("server", "Failed to stop NAT device discovery: {0}", e);
|
||||||
|
Game.Settings.Server.NatDeviceAvailable = false;
|
||||||
|
Game.Settings.Server.AllowPortForward = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (NatDevice == null)
|
||||||
|
{
|
||||||
|
Log.Write("server", "No NAT devices with UPnP enabled found within {0} ms deadline. Disabling automatic port forwarding.".F(Game.Settings.Server.NatDiscoveryTimeout));
|
||||||
|
Game.Settings.Server.NatDeviceAvailable = false;
|
||||||
|
Game.Settings.Server.AllowPortForward = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void DeviceFound(object sender, DeviceEventArgs args)
|
||||||
|
{
|
||||||
|
if (args.Device == null)
|
||||||
|
return;
|
||||||
|
|
||||||
|
Log.Write("server", "NAT device discovered.");
|
||||||
|
|
||||||
|
Game.Settings.Server.NatDeviceAvailable = true;
|
||||||
|
Game.Settings.Server.AllowPortForward = true;
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
NatDevice = args.Device;
|
||||||
|
Log.Write("server", "Type: {0}", NatDevice.GetType());
|
||||||
|
Log.Write("server", "Your external IP is: {0}", NatDevice.GetExternalIP());
|
||||||
|
|
||||||
|
foreach (var mp in NatDevice.GetAllMappings())
|
||||||
|
Log.Write("server", "Existing port mapping: protocol={0}, public={1}, private={2}",
|
||||||
|
mp.Protocol, mp.PublicPort, mp.PrivatePort);
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
Log.Write("server", "Can't fetch information from NAT device: {0}", e);
|
||||||
|
|
||||||
|
Game.Settings.Server.NatDeviceAvailable = false;
|
||||||
|
Game.Settings.Server.AllowPortForward = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void DeviceLost(object sender, DeviceEventArgs args)
|
||||||
|
{
|
||||||
|
Log.Write("server", "NAT device lost.");
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
Game.Settings.Server.NatDeviceAvailable = false;
|
||||||
|
Game.Settings.Server.AllowPortForward = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void ForwardPort()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var mapping = new Mapping(Protocol.Tcp, Game.Settings.Server.ExternalPort, Game.Settings.Server.ListenPort);
|
||||||
|
NatDevice.CreatePortMap(mapping);
|
||||||
|
Log.Write("server", "Create port mapping: protocol={0}, public={1}, private={2}", mapping.Protocol, mapping.PublicPort, mapping.PrivatePort);
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
Log.Write("server", "Can not forward ports via UPnP: {0}", e);
|
||||||
|
Game.Settings.Server.AllowPortForward = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void RemovePortforward()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var mapping = new Mapping(Protocol.Tcp, Game.Settings.Server.ExternalPort, Game.Settings.Server.ListenPort);
|
||||||
|
NatDevice.DeletePortMap(mapping);
|
||||||
|
Log.Write("server", "Remove port mapping: protocol={0}, public={1}, private={2}", mapping.Protocol, mapping.PublicPort, mapping.PrivatePort);
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
Log.Write("server", "Can not remove UPnP portforwarding rules: {0}", e);
|
||||||
|
Game.Settings.Server.AllowPortForward = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -222,6 +222,7 @@
|
|||||||
<Compile Include="Network\ReplayRecorderConnection.cs" />
|
<Compile Include="Network\ReplayRecorderConnection.cs" />
|
||||||
<Compile Include="Widgets\TooltipContainerWidget.cs" />
|
<Compile Include="Widgets\TooltipContainerWidget.cs" />
|
||||||
<Compile Include="Traits\DebugPauseState.cs" />
|
<Compile Include="Traits\DebugPauseState.cs" />
|
||||||
|
<Compile Include="Network\UPnP.cs" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ProjectReference Include="..\OpenRA.FileFormats\OpenRA.FileFormats.csproj">
|
<ProjectReference Include="..\OpenRA.FileFormats\OpenRA.FileFormats.csproj">
|
||||||
|
|||||||
@@ -24,10 +24,6 @@ using OpenRA.Network;
|
|||||||
|
|
||||||
using XTimer = System.Timers.Timer;
|
using XTimer = System.Timers.Timer;
|
||||||
|
|
||||||
using Mono.Nat;
|
|
||||||
using Mono.Nat.Pmp;
|
|
||||||
using Mono.Nat.Upnp;
|
|
||||||
|
|
||||||
namespace OpenRA.Server
|
namespace OpenRA.Server
|
||||||
{
|
{
|
||||||
public enum ServerState : int
|
public enum ServerState : int
|
||||||
@@ -54,7 +50,6 @@ namespace OpenRA.Server
|
|||||||
|
|
||||||
public readonly IPAddress Ip;
|
public readonly IPAddress Ip;
|
||||||
public readonly int Port;
|
public readonly int Port;
|
||||||
public INatDevice NatDevice;
|
|
||||||
|
|
||||||
int randomSeed;
|
int randomSeed;
|
||||||
public readonly Thirdparty.Random Random = new Thirdparty.Random();
|
public readonly Thirdparty.Random Random = new Thirdparty.Random();
|
||||||
@@ -82,7 +77,7 @@ namespace OpenRA.Server
|
|||||||
t.GameEnded(this);
|
t.GameEnded(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Server(IPEndPoint endpoint, string[] mods, ServerSettings settings, ModData modData, INatDevice natDevice)
|
public Server(IPEndPoint endpoint, string[] mods, ServerSettings settings, ModData modData)
|
||||||
{
|
{
|
||||||
Log.AddChannel("server", "server.log");
|
Log.AddChannel("server", "server.log");
|
||||||
|
|
||||||
@@ -95,12 +90,11 @@ namespace OpenRA.Server
|
|||||||
|
|
||||||
Settings = settings;
|
Settings = settings;
|
||||||
ModData = modData;
|
ModData = modData;
|
||||||
NatDevice = natDevice;
|
|
||||||
|
|
||||||
randomSeed = (int)DateTime.Now.ToBinary();
|
randomSeed = (int)DateTime.Now.ToBinary();
|
||||||
|
|
||||||
if (Settings.AllowPortForward)
|
if (Settings.AllowPortForward)
|
||||||
ForwardPort();
|
UPnP.ForwardPort();
|
||||||
|
|
||||||
foreach (var trait in modData.Manifest.ServerTraits)
|
foreach (var trait in modData.Manifest.ServerTraits)
|
||||||
ServerTraits.Add( modData.ObjectCreator.CreateObject<ServerTrait>(trait) );
|
ServerTraits.Add( modData.ObjectCreator.CreateObject<ServerTrait>(trait) );
|
||||||
@@ -158,7 +152,7 @@ namespace OpenRA.Server
|
|||||||
{
|
{
|
||||||
EndGame();
|
EndGame();
|
||||||
if (Settings.AllowPortForward)
|
if (Settings.AllowPortForward)
|
||||||
RemovePortforward();
|
UPnP.RemovePortforward();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -174,36 +168,6 @@ namespace OpenRA.Server
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void ForwardPort()
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
var mapping = new Mapping(Protocol.Tcp, Settings.ExternalPort, Settings.ListenPort);
|
|
||||||
NatDevice.CreatePortMap(mapping);
|
|
||||||
Log.Write("server", "Create port mapping: protocol={0}, public={1}, private={2}", mapping.Protocol, mapping.PublicPort, mapping.PrivatePort);
|
|
||||||
}
|
|
||||||
catch (Exception e)
|
|
||||||
{
|
|
||||||
Log.Write("server", "Can not forward ports via UPnP: {0}", e);
|
|
||||||
Settings.AllowPortForward = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void RemovePortforward()
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
var mapping = new Mapping(Protocol.Tcp, Settings.ExternalPort, Settings.ListenPort);
|
|
||||||
NatDevice.DeletePortMap(mapping);
|
|
||||||
Log.Write("server", "Remove port mapping: protocol={0}, public={1}, private={2}", mapping.Protocol, mapping.PublicPort, mapping.PrivatePort);
|
|
||||||
}
|
|
||||||
catch (Exception e)
|
|
||||||
{
|
|
||||||
Log.Write("server", "Can not remove UPnP portforwarding rules: {0}", e);
|
|
||||||
Settings.AllowPortForward = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/* lobby rework TODO:
|
/* lobby rework TODO:
|
||||||
* - "teams together" option for team games -- will eliminate most need
|
* - "teams together" option for team games -- will eliminate most need
|
||||||
* for manual spawnpoint choosing.
|
* for manual spawnpoint choosing.
|
||||||
|
|||||||
Reference in New Issue
Block a user