fixes port-in-use deadlock

This commit is contained in:
Bob
2010-03-10 15:47:50 +13:00
parent 024779d0d9
commit 106255066b
2 changed files with 21 additions and 27 deletions

View File

@@ -44,7 +44,7 @@ namespace OpenRA.Server
const int DownloadChunkInterval = 20000;
const int DownloadChunkSize = 16384;
public static int ServerMain(string[] mods, AutoResetEvent e)
public static void ServerMain(string[] mods)
{
initialMods = mods;
@@ -58,35 +58,33 @@ namespace OpenRA.Server
try
{
listener.Start();
Console.WriteLine("Server started.");
}
catch (Exception)
{
Console.WriteLine("Server failed to start.");
return 1;
throw new InvalidOperationException( "Unable to start server: port is already in use" );
}
e.Set(); // we're done starting up
for (; ; )
new Thread( _ =>
{
var checkRead = new ArrayList();
checkRead.Add(listener.Server);
foreach (var c in conns) checkRead.Add(c.socket);
for( ; ; )
{
var checkRead = new ArrayList();
checkRead.Add( listener.Server );
foreach( var c in conns ) checkRead.Add( c.socket );
var isSendingPackages = conns.Any(c => c.Stream != null);
var isSendingPackages = conns.Any( c => c.Stream != null );
/* msdn lies, -1 doesnt work. this is ~1h instead. */
Socket.Select(checkRead, null, null, isSendingPackages ? DownloadChunkInterval : -2 );
/* msdn lies, -1 doesnt work. this is ~1h instead. */
Socket.Select( checkRead, null, null, isSendingPackages ? DownloadChunkInterval : -2 );
foreach (Socket s in checkRead)
if (s == listener.Server) AcceptConnection();
else conns.Single(c => c.socket == s).ReadData();
foreach (var c in conns.Where(a => a.Stream != null).ToArray())
SendNextChunk(c);
}
foreach( Socket s in checkRead )
if( s == listener.Server ) AcceptConnection();
else conns.Single( c => c.socket == s ).ReadData();
foreach( var c in conns.Where( a => a.Stream != null ).ToArray() )
SendNextChunk( c );
}
} ) { IsBackground = true }.Start();
}
static int ChooseFreePlayerIndex()
@@ -550,11 +548,7 @@ namespace OpenRA.Server
public static void Start( string[] mods )
{
var e = new AutoResetEvent(false);
t = new Thread(() => Server.ServerMain(mods, e)) { IsBackground = true };
t.Start();
e.WaitOne(); // when the event is signaled, the server is finished initializing
Server.ServerMain( mods );
}
public static void Stop()

View File

@@ -58,7 +58,7 @@ namespace OpenRA.GlRenderer
Sdl.SDL_GL_SetAttribute(Sdl.SDL_GL_GREEN_SIZE, 8);
Sdl.SDL_GL_SetAttribute(Sdl.SDL_GL_BLUE_SIZE, 8);
Sdl.SDL_GL_SetAttribute(Sdl.SDL_GL_ALPHA_SIZE, 8);
if (Environment.OSVersion.Platform == PlatformID.Win32NT)
{
// pseudo-fullscreen, for sane debugging.
@@ -69,7 +69,7 @@ namespace OpenRA.GlRenderer
{
// OSX doesn't like this, due to quirks of their WM.
surf = Sdl.SDL_SetVideoMode(width, height, 0, Sdl.SDL_OPENGL | (windowed ? 0 : Sdl.SDL_FULLSCREEN));
}
}
Sdl.SDL_WM_SetCaption("OpenRA", "OpenRA");
Sdl.SDL_ShowCursor(0);