diff --git a/OpenRA.Game/Game.cs b/OpenRA.Game/Game.cs index c7a96b1f04..e8042526f2 100755 --- a/OpenRA.Game/Game.cs +++ b/OpenRA.Game/Game.cs @@ -350,15 +350,16 @@ namespace OpenRA { if (orderManager.world != null) orderManager.world.traitDict.PrintReport(); - - if (IsHost && server != null) - { - Console.WriteLine("Closing server"); - server.Shutdown(); - } + CloseServer(); JoinLocal(); orderManager.Dispose(); } + + public static void CloseServer() + { + if (server != null) + server.Shutdown(); + } public static T CreateObject( string name ) { diff --git a/OpenRA.Game/Server/Server.cs b/OpenRA.Game/Server/Server.cs index 64a7d47b7a..d517d8ef9d 100644 --- a/OpenRA.Game/Server/Server.cs +++ b/OpenRA.Game/Server/Server.cs @@ -73,18 +73,22 @@ namespace OpenRA.Server Log.Write("server","- {0}", m); Log.Write("server", "Initial map: {0}",lobbyInfo.GlobalSettings.Map); - - try - { - listener.Start(); - } - catch (Exception) - { - throw new InvalidOperationException( "Unable to start server: port is already in use" ); - } new Thread( _ => { + while (true) + { + try + { + listener.Start(); + break; + } + catch (Exception) + { + Thread.Sleep(100); + } + } + var timeout = ServerTraits.WithInterface().Min(t => t.TickTimeout); for( ; ; ) { @@ -94,7 +98,9 @@ namespace OpenRA.Server foreach( var c in preConns ) checkRead.Add( c.socket ); Socket.Select( checkRead, null, null, timeout ); - + if (shutdown) + break; + foreach( Socket s in checkRead ) if( s == listener.Server ) AcceptConnection(); else if (preConns.Count > 0) @@ -110,7 +116,6 @@ namespace OpenRA.Server if (shutdown) break; } - Console.WriteLine("Server loop finished"); GameStarted = false; foreach (var t in ServerTraits.WithInterface()) diff --git a/OpenRA.Mods.Cnc/Widgets/CncMenuLogic.cs b/OpenRA.Mods.Cnc/Widgets/CncMenuLogic.cs index 5587968fa3..5113b4a898 100755 --- a/OpenRA.Mods.Cnc/Widgets/CncMenuLogic.cs +++ b/OpenRA.Mods.Cnc/Widgets/CncMenuLogic.cs @@ -135,7 +135,7 @@ namespace OpenRA.Mods.Cnc.Widgets CncConnectingLogic.Connect(IPAddress.Loopback.ToString(), Game.Settings.Server.LoopbackPort, new Action(() => OpenLobbyPanel(MenuType.Main)), - new Action(() => ReturnToMenu(MenuType.Main))); + new Action(() => { Game.CloseServer(); ReturnToMenu(MenuType.Main); })); } } }