Work around a race condition with local servers.

This commit is contained in:
Paul Chote
2011-05-10 17:49:33 +12:00
parent 33a4f5e29e
commit 814845730f
3 changed files with 24 additions and 18 deletions

View File

@@ -350,15 +350,16 @@ namespace OpenRA
{ {
if (orderManager.world != null) if (orderManager.world != null)
orderManager.world.traitDict.PrintReport(); orderManager.world.traitDict.PrintReport();
CloseServer();
if (IsHost && server != null)
{
Console.WriteLine("Closing server");
server.Shutdown();
}
JoinLocal(); JoinLocal();
orderManager.Dispose(); orderManager.Dispose();
} }
public static void CloseServer()
{
if (server != null)
server.Shutdown();
}
public static T CreateObject<T>( string name ) public static T CreateObject<T>( string name )
{ {

View File

@@ -73,18 +73,22 @@ namespace OpenRA.Server
Log.Write("server","- {0}", m); Log.Write("server","- {0}", m);
Log.Write("server", "Initial map: {0}",lobbyInfo.GlobalSettings.Map); 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( _ => 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( ; ; )
{ {
@@ -94,7 +98,9 @@ namespace OpenRA.Server
foreach( var c in preConns ) checkRead.Add( c.socket ); foreach( var c in preConns ) checkRead.Add( c.socket );
Socket.Select( checkRead, null, null, timeout ); Socket.Select( checkRead, null, null, timeout );
if (shutdown)
break;
foreach( Socket s in checkRead ) foreach( Socket s in checkRead )
if( s == listener.Server ) AcceptConnection(); if( s == listener.Server ) AcceptConnection();
else if (preConns.Count > 0) else if (preConns.Count > 0)
@@ -110,7 +116,6 @@ namespace OpenRA.Server
if (shutdown) if (shutdown)
break; break;
} }
Console.WriteLine("Server loop finished");
GameStarted = false; GameStarted = false;
foreach (var t in ServerTraits.WithInterface<INotifyServerShutdown>()) foreach (var t in ServerTraits.WithInterface<INotifyServerShutdown>())

View File

@@ -135,7 +135,7 @@ namespace OpenRA.Mods.Cnc.Widgets
CncConnectingLogic.Connect(IPAddress.Loopback.ToString(), CncConnectingLogic.Connect(IPAddress.Loopback.ToString(),
Game.Settings.Server.LoopbackPort, Game.Settings.Server.LoopbackPort,
new Action(() => OpenLobbyPanel(MenuType.Main)), new Action(() => OpenLobbyPanel(MenuType.Main)),
new Action(() => ReturnToMenu(MenuType.Main))); new Action(() => { Game.CloseServer(); ReturnToMenu(MenuType.Main); }));
} }
} }
} }