diff --git a/OpenRA.Game/Game.cs b/OpenRA.Game/Game.cs index b7e29f37f3..f7c0ea5f90 100644 --- a/OpenRA.Game/Game.cs +++ b/OpenRA.Game/Game.cs @@ -619,6 +619,7 @@ namespace OpenRA public static void Disconnect() { + orderManager.Dispose(); var shellmap = Manifest.ShellmapUid; LobbyInfo = new Session(); LobbyInfo.GlobalSettings.Mods = Settings.InitialMods; diff --git a/OpenRA.Game/Network/Connection.cs b/OpenRA.Game/Network/Connection.cs index 28dc5c198e..f47f1d6d44 100755 --- a/OpenRA.Game/Network/Connection.cs +++ b/OpenRA.Game/Network/Connection.cs @@ -74,15 +74,16 @@ namespace OpenRA.Network } } - class NetworkConnection : EchoConnection + class NetworkConnection : EchoConnection, IDisposable { TcpClient socket; int clientId; ConnectionState connectionState = ConnectionState.Connecting; + Thread t; public NetworkConnection( string host, int port ) { - new Thread( _ => + t = new Thread( _ => { try { @@ -115,7 +116,8 @@ namespace OpenRA.Network connectionState = ConnectionState.NotConnected; } } - ) { IsBackground = true }.Start(); + ) { IsBackground = true }; + t.Start(); } public override int LocalClientId { get { return clientId; } } @@ -136,6 +138,18 @@ namespace OpenRA.Network } catch (SocketException) { /* drop this on the floor; we'll pick up the disconnect from the reader thread */ } } + bool disposed = false; + public void Dispose () + { + if (disposed) return; + disposed = true; + GC.SuppressFinalize( this ); + + socket.Close(); + t.Abort(); + } + + ~NetworkConnection() { Dispose(); } } class ReplayConnection : IConnection diff --git a/OpenRA.Game/Server/Server.cs b/OpenRA.Game/Server/Server.cs index 9c63abef29..a23d1e12b2 100644 --- a/OpenRA.Game/Server/Server.cs +++ b/OpenRA.Game/Server/Server.cs @@ -90,6 +90,13 @@ namespace OpenRA.Server if (Environment.TickCount - lastPing > MasterPingInterval * 1000) PingMasterServer(); + + if (conns.Count() == 0) + { + listener.Stop(); + GameStarted = false; + break; + } } } ) { IsBackground = true }.Start(); } @@ -424,15 +431,6 @@ namespace OpenRA.Server if (conns.Count != 0) SyncLobbyInfo(); } - - public static void CloseServer() - { - while (conns.Count() != 0) - DropClient(conns.ElementAt(conns.Count() - 1), null); - if (listener != null) - listener.Stop(); - GameStarted = false; - } static void SyncLobbyInfo() { diff --git a/OpenRA.Game/Widgets/Delegates/IngameChromeDelegate.cs b/OpenRA.Game/Widgets/Delegates/IngameChromeDelegate.cs index 8e960ca1a0..7015c5fcfe 100644 --- a/OpenRA.Game/Widgets/Delegates/IngameChromeDelegate.cs +++ b/OpenRA.Game/Widgets/Delegates/IngameChromeDelegate.cs @@ -29,9 +29,7 @@ namespace OpenRA.Widgets.Delegates }; optionsBG.GetWidget("BUTTON_DISCONNECT").OnMouseUp = mi => { - optionsBG.Visible = false; - if (Game.IsHost) - Server.Server.CloseServer(); + optionsBG.Visible = false; Game.Disconnect(); return true; }; diff --git a/OpenRA.Game/Widgets/Delegates/LobbyDelegate.cs b/OpenRA.Game/Widgets/Delegates/LobbyDelegate.cs index 8646a72c5b..1e8a25ea7b 100644 --- a/OpenRA.Game/Widgets/Delegates/LobbyDelegate.cs +++ b/OpenRA.Game/Widgets/Delegates/LobbyDelegate.cs @@ -80,8 +80,6 @@ namespace OpenRA.Widgets.Delegates var disconnectButton = lobby.GetWidget("DISCONNECT_BUTTON"); disconnectButton.OnMouseUp = mi => { - if (Game.IsHost) - Server.Server.CloseServer(); Game.Disconnect(); return true; };