Bind skirmish servers to a random available port

This commit is contained in:
Paul Chote
2011-05-23 19:04:41 +12:00
parent a288c5df15
commit 124f19f00b
4 changed files with 23 additions and 30 deletions

View File

@@ -368,10 +368,13 @@ namespace OpenRA
public static void CreateServer(ServerSettings settings) public static void CreateServer(ServerSettings settings)
{ {
server = new Server.Server(IPAddress.Any, settings.ListenPort, Game.Settings.Game.Mods, settings, modData); server = new Server.Server(new IPEndPoint(IPAddress.Any, settings.ListenPort),
Game.Settings.Game.Mods,
settings,
modData);
} }
public static void CreateLocalServer(string map) public static int CreateLocalServer(string map)
{ {
var settings = new ServerSettings() var settings = new ServerSettings()
{ {
@@ -379,11 +382,12 @@ namespace OpenRA
AdvertiseOnline = false, AdvertiseOnline = false,
Map = map Map = map
}; };
server = new Server.Server(IPAddress.Loopback,
Game.Settings.Server.LoopbackPort, server = new Server.Server(new IPEndPoint(IPAddress.Loopback, 0),
Game.Settings.Game.Mods, Game.Settings.Game.Mods,
settings, settings,
modData); modData);
return server.Port;
} }
public static bool IsCurrentWorld(World world) public static bool IsCurrentWorld(World world)

View File

@@ -22,9 +22,8 @@ namespace OpenRA.GameRules
public class ServerSettings public class ServerSettings
{ {
public string Name = "OpenRA Game"; public string Name = "OpenRA Game";
public int LoopbackPort = 1234; // Port used for soloplay servers public int ListenPort = 1234;
public int ListenPort = 1234; // Port that we listen on public int ExternalPort = 1234;
public int ExternalPort = 1234; // Router port that the master server forwards people to
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 AllowCheats = false; public bool AllowCheats = false;
@@ -34,7 +33,6 @@ namespace OpenRA.GameRules
public ServerSettings(ServerSettings other) public ServerSettings(ServerSettings other)
{ {
Name = other.Name; Name = other.Name;
LoopbackPort = other.LoopbackPort;
ListenPort = other.ListenPort; ListenPort = other.ListenPort;
ExternalPort = other.ExternalPort; ExternalPort = other.ExternalPort;
AdvertiseOnline = other.AdvertiseOnline; AdvertiseOnline = other.AdvertiseOnline;

View File

@@ -45,22 +45,27 @@ namespace OpenRA.Server
public ModData ModData; public ModData ModData;
public Map Map; public Map Map;
bool shutdown = false; volatile bool shutdown = false;
public void Shutdown() public void Shutdown()
{ {
shutdown = true; shutdown = true;
} }
public Server(IPAddress ip, int port, string[] mods, ServerSettings settings, ModData modData) public Server(IPEndPoint endpoint, string[] mods, ServerSettings settings, ModData modData)
{ {
Log.AddChannel("server", "server.log"); Log.AddChannel("server", "server.log");
listener = new TcpListener(endpoint);
listener.Start();
var localEndpoint = (IPEndPoint)listener.LocalEndpoint;
Ip = localEndpoint.Address;
Port = localEndpoint.Port;
Settings = settings; Settings = settings;
Ip = ip;
Port = port;
listener = new TcpListener(ip, port);
randomSeed = (int)DateTime.Now.ToBinary();
ModData = modData; ModData = modData;
randomSeed = (int)DateTime.Now.ToBinary();
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) );
@@ -80,19 +85,6 @@ namespace OpenRA.Server
new Thread( _ => new Thread( _ =>
{ {
while (true)
{
try
{
listener.Start();
break;
}
catch (Exception)
{
Thread.Sleep(100);
}
}
var timeout = ServerTraits.WithInterface<ITick>().Min(t => t.TickTimeout); var timeout = ServerTraits.WithInterface<ITick>().Min(t => t.TickTimeout);
for( ; ; ) for( ; ; )
{ {

View File

@@ -143,10 +143,9 @@ namespace OpenRA.Mods.Cnc.Widgets.Logic
void StartSkirmishGame() void StartSkirmishGame()
{ {
var map = Game.modData.AvailableMaps.FirstOrDefault(m => m.Value.Selectable).Key; var map = Game.modData.AvailableMaps.FirstOrDefault(m => m.Value.Selectable).Key;
var port = Game.CreateLocalServer(map);
Game.CreateLocalServer(map);
CncConnectingLogic.Connect(IPAddress.Loopback.ToString(), CncConnectingLogic.Connect(IPAddress.Loopback.ToString(),
Game.Settings.Server.LoopbackPort, port,
() => OpenLobbyPanel(MenuType.Main), () => OpenLobbyPanel(MenuType.Main),
() => { Game.CloseServer(); Menu = MenuType.Main; }); () => { Game.CloseServer(); Menu = MenuType.Main; });
} }