diff --git a/OpenRA.Game/Game.cs b/OpenRA.Game/Game.cs index f10daec77f..0a850b0274 100755 --- a/OpenRA.Game/Game.cs +++ b/OpenRA.Game/Game.cs @@ -38,6 +38,7 @@ namespace OpenRA public static Settings Settings; internal static OrderManager orderManager; + static Server.Server server; public static XRandom CosmeticRandom = new XRandom(); // not synced @@ -258,8 +259,8 @@ namespace OpenRA public static void Disconnect() { - if (IsHost) - Server.Server.Shutdown(); + if (IsHost && server != null) + server.Shutdown(); orderManager.Dispose(); var shellmap = modData.Manifest.ShellmapUid; @@ -317,12 +318,15 @@ namespace OpenRA ConnectedToLobby = null; }; if (isHost) - { - Server.Server.ServerMain(Game.modData, Settings, map); - JoinServer(IPAddress.Loopback.ToString(), Settings.Server.ListenPort); - } + CreateAndJoinServer( Settings, map ); else JoinServer(host, port); } + + public static void CreateAndJoinServer(Settings settings, string map) + { + server = new Server.Server(modData, settings, map); + JoinServer(IPAddress.Loopback.ToString(), settings.Server.ListenPort); + } } } diff --git a/OpenRA.Game/Server/Connection.cs b/OpenRA.Game/Server/Connection.cs index 425ff4523a..e3cc64004d 100644 --- a/OpenRA.Game/Server/Connection.cs +++ b/OpenRA.Game/Server/Connection.cs @@ -35,7 +35,7 @@ namespace OpenRA.Server return result.ToArray(); } - bool ReadDataInner() + bool ReadDataInner( Server server ) { var rx = new byte[1024]; var len = 0; @@ -49,7 +49,7 @@ namespace OpenRA.Server else { if (len == 0) - Server.DropClient(this, null); + server.DropClient(this, null); break; } @@ -57,7 +57,7 @@ namespace OpenRA.Server catch (SocketException e) { if (e.SocketErrorCode == SocketError.WouldBlock) break; - Server.DropClient(this, e); + server.DropClient(this, e); return false; } } @@ -65,9 +65,9 @@ namespace OpenRA.Server return true; } - public void ReadData() + public void ReadData( Server server ) { - if (ReadDataInner()) + if (ReadDataInner(server)) while (data.Count >= ExpectLength) { var bytes = PopBytes(ExpectLength); @@ -82,12 +82,12 @@ namespace OpenRA.Server case ReceiveState.Data: { - Server.DispatchOrders(this, Frame, bytes); + server.DispatchOrders(this, Frame, bytes); MostRecentFrame = Frame; ExpectLength = 8; State = ReceiveState.Header; - Server.UpdateInFlightFrames(this); + server.UpdateInFlightFrames(this); } break; } } diff --git a/OpenRA.Game/Server/Server.cs b/OpenRA.Game/Server/Server.cs index 40736c9a03..704b45344c 100644 --- a/OpenRA.Game/Server/Server.cs +++ b/OpenRA.Game/Server/Server.cs @@ -23,34 +23,34 @@ using OpenRA.Network; namespace OpenRA.Server { - public static class Server + public class Server { - public static List conns = new List(); - static TcpListener listener = null; - static Dictionary> inFlightFrames + public List conns = new List(); + TcpListener listener = null; + Dictionary> inFlightFrames = new Dictionary>(); - static TypeDictionary ServerTraits = new TypeDictionary(); - public static Session lobbyInfo; - public static bool GameStarted = false; - public static string Name; - static int randomSeed; + TypeDictionary ServerTraits = new TypeDictionary(); + public Session lobbyInfo; + public bool GameStarted = false; + public string Name; + int randomSeed; - public static ModData ModData; - public static Map Map; + public ModData ModData; + public Map Map; - public static void Shutdown() + public void Shutdown() { conns.Clear(); GameStarted = false; foreach (var t in ServerTraits.WithInterface()) - t.ServerShutdown(); + t.ServerShutdown(this); try { listener.Stop(); } catch { } } - public static void ServerMain(ModData modData, Settings settings, string map) + public Server(ModData modData, Settings settings, string map) { Log.AddChannel("server", "server.log"); @@ -69,7 +69,7 @@ namespace OpenRA.Server lobbyInfo.GlobalSettings.ServerName = settings.Server.Name; foreach (var t in ServerTraits.WithInterface()) - t.ServerStarted(); + t.ServerStarted(this); Log.Write("server", "Initial mods: "); foreach( var m in lobbyInfo.GlobalSettings.Mods ) @@ -99,10 +99,10 @@ namespace OpenRA.Server foreach( Socket s in checkRead ) if( s == listener.Server ) AcceptConnection(); - else if (conns.Count > 0) conns.Single( c => c.socket == s ).ReadData(); + else if (conns.Count > 0) conns.Single( c => c.socket == s ).ReadData( this ); foreach (var t in ServerTraits.WithInterface()) - t.Tick(); + t.Tick(this); if (conns.Count() == 0) { @@ -119,7 +119,7 @@ namespace OpenRA.Server * for manual spawnpoint choosing. * - 256 max players is a dirty hack */ - static int ChooseFreePlayerIndex() + int ChooseFreePlayerIndex() { for (var i = 0; i < 256; i++) if (conns.All(c => c.PlayerIndex != i)) @@ -128,7 +128,7 @@ namespace OpenRA.Server throw new InvalidOperationException("Already got 256 players"); } - static void AcceptConnection() + void AcceptConnection() { Socket newSocket = null; @@ -164,12 +164,12 @@ namespace OpenRA.Server conns.Add(newConn); foreach (var t in ServerTraits.WithInterface()) - t.ClientJoined(newConn); + t.ClientJoined(this, newConn); } catch (Exception e) { DropClient(newConn, e); } } - public static void UpdateInFlightFrames(Connection conn) + public void UpdateInFlightFrames(Connection conn) { if (conn.Frame != 0) { @@ -185,7 +185,7 @@ namespace OpenRA.Server } } - static void DispatchOrdersToClient(Connection c, int client, int frame, byte[] data) + void DispatchOrdersToClient(Connection c, int client, int frame, byte[] data) { try { @@ -199,7 +199,7 @@ namespace OpenRA.Server catch( Exception e ) { DropClient( c, e ); } } - public static void DispatchOrders(Connection conn, int frame, byte[] data) + public void DispatchOrders(Connection conn, int frame, byte[] data) { if (frame == 0 && conn != null) InterpretServerOrders(conn, data); @@ -211,7 +211,7 @@ namespace OpenRA.Server } } - static void InterpretServerOrders(Connection conn, byte[] data) + void InterpretServerOrders(Connection conn, byte[] data) { var ms = new MemoryStream(data); var br = new BinaryReader(ms); @@ -229,23 +229,23 @@ namespace OpenRA.Server catch (NotImplementedException) { } } - public static void SendChatTo(Connection conn, string text) + public void SendChatTo(Connection conn, string text) { DispatchOrdersToClient(conn, 0, 0, new ServerOrder("Chat", text).Serialize()); } - public static void SendChat(Connection asConn, string text) + public void SendChat(Connection asConn, string text) { DispatchOrders(asConn, 0, new ServerOrder("Chat", text).Serialize()); } - public static void SendDisconnected(Connection asConn) + public void SendDisconnected(Connection asConn) { DispatchOrders(asConn, 0, new ServerOrder("Disconnected", "").Serialize()); } - static void InterpretServerOrder(Connection conn, ServerOrder so) + void InterpretServerOrder(Connection conn, ServerOrder so) { switch (so.Name) { @@ -253,7 +253,7 @@ namespace OpenRA.Server { bool handled = false; foreach (var t in ServerTraits.WithInterface()) - if ((handled = t.InterpretCommand(conn, GetClient(conn), so.Data))) + if ((handled = t.InterpretCommand(this, conn, GetClient(conn), so.Data))) break; if (!handled) @@ -272,12 +272,12 @@ namespace OpenRA.Server } } - public static Session.Client GetClient(Connection conn) + public Session.Client GetClient(Connection conn) { return lobbyInfo.Clients.First(c => c.Index == conn.PlayerIndex); } - public static void DropClient(Connection toDrop, Exception e) + public void DropClient(Connection toDrop, Exception e) { conns.Remove(toDrop); SendChat(toDrop, "Connection Dropped"); @@ -293,28 +293,28 @@ namespace OpenRA.Server SyncLobbyInfo(); } - public static void SyncLobbyInfo() + public void SyncLobbyInfo() { if (!GameStarted) /* don't do this while the game is running, it breaks things. */ DispatchOrders(null, 0, new ServerOrder("SyncInfo", lobbyInfo.Serialize()).Serialize()); foreach (var t in ServerTraits.WithInterface()) - t.LobbyInfoSynced(); + t.LobbyInfoSynced(this); } - public static void StartGame() + public void StartGame() { - Server.GameStarted = true; - foreach( var c in Server.conns ) - foreach( var d in Server.conns ) + GameStarted = true; + foreach( var c in conns ) + foreach( var d in conns ) DispatchOrdersToClient( c, d.PlayerIndex, 0x7FFFFFFF, new byte[] { 0xBF } ); DispatchOrders(null, 0, new ServerOrder("StartGame", "").Serialize()); foreach (var t in ServerTraits.WithInterface()) - t.GameStarted(); + t.GameStarted(this); } } } diff --git a/OpenRA.Game/Server/TraitInterfaces.cs b/OpenRA.Game/Server/TraitInterfaces.cs index b3eb654338..0e6ead6f9b 100644 --- a/OpenRA.Game/Server/TraitInterfaces.cs +++ b/OpenRA.Game/Server/TraitInterfaces.cs @@ -13,15 +13,15 @@ using OpenRA.Network; namespace OpenRA.Server { // Returns true if order is handled - public interface IInterpretCommand { bool InterpretCommand(Connection conn, Session.Client client, string cmd); } - public interface INotifySyncLobbyInfo { void LobbyInfoSynced(); } - public interface INotifyServerStart { void ServerStarted(); } - public interface INotifyServerShutdown { void ServerShutdown(); } - public interface IStartGame { void GameStarted(); } - public interface IClientJoined { void ClientJoined(Connection conn); } + public interface IInterpretCommand { bool InterpretCommand(Server server, Connection conn, Session.Client client, string cmd); } + public interface INotifySyncLobbyInfo { void LobbyInfoSynced(Server server); } + public interface INotifyServerStart { void ServerStarted(Server server); } + public interface INotifyServerShutdown { void ServerShutdown(Server server); } + public interface IStartGame { void GameStarted(Server server); } + public interface IClientJoined { void ClientJoined(Server server, Connection conn); } public interface ITick { - void Tick(); + void Tick(Server server); int TickTimeout { get; } } @@ -29,28 +29,28 @@ namespace OpenRA.Server public class DebugServerTrait : ServerTrait, IInterpretCommand, IStartGame, INotifySyncLobbyInfo, INotifyServerStart, INotifyServerShutdown { - public bool InterpretCommand(Connection conn, Session.Client client, string cmd) + public bool InterpretCommand(Server server, Connection conn, Session.Client client, string cmd) { Console.WriteLine("Server received command from player {1}: {0}",cmd, conn.PlayerIndex); return false; } - public void GameStarted() + public void GameStarted(Server server) { Console.WriteLine("GameStarted()"); } - public void LobbyInfoSynced() + public void LobbyInfoSynced(Server server) { Console.WriteLine("LobbyInfoSynced()"); } - public void ServerStarted() + public void ServerStarted(Server server) { Console.WriteLine("ServerStarted()"); } - public void ServerShutdown() + public void ServerShutdown(Server server) { Console.WriteLine("ServerShutdown()"); } diff --git a/OpenRA.Game/Widgets/Delegates/CreateServerMenuDelegate.cs b/OpenRA.Game/Widgets/Delegates/CreateServerMenuDelegate.cs index c6308adfe1..8c7a5d87c4 100644 --- a/OpenRA.Game/Widgets/Delegates/CreateServerMenuDelegate.cs +++ b/OpenRA.Game/Widgets/Delegates/CreateServerMenuDelegate.cs @@ -33,9 +33,7 @@ namespace OpenRA.Widgets.Delegates settings.Server.ExternalPort = int.Parse(cs.GetWidget("EXTERNAL_PORT").Text); settings.Save(); - Server.Server.ServerMain(Game.modData, settings, map); - - Game.JoinServer(IPAddress.Loopback.ToString(), settings.Server.ListenPort); + Game.CreateAndJoinServer(settings, map); return true; }; diff --git a/OpenRA.Mods.RA/ServerTraits/LobbyCommands.cs b/OpenRA.Mods.RA/ServerTraits/LobbyCommands.cs index 16e144c2e7..d288ba4c7a 100644 --- a/OpenRA.Mods.RA/ServerTraits/LobbyCommands.cs +++ b/OpenRA.Mods.RA/ServerTraits/LobbyCommands.cs @@ -14,7 +14,7 @@ using System.Linq; using OpenRA.Network; using OpenRA.FileFormats; using OpenRA.Server; -using server = OpenRA.Server.Server; +using S = OpenRA.Server.Server; namespace OpenRA.Mods.RA.Server { @@ -22,7 +22,7 @@ namespace OpenRA.Mods.RA.Server { public static int MaxSpectators = 4; // How many spectators to allow // @todo Expose this as an option - public bool InterpretCommand(Connection conn, Session.Client client, string cmd) + public bool InterpretCommand(S server, Connection conn, Session.Client client, string cmd) { if (server.GameStarted) { @@ -52,7 +52,7 @@ namespace OpenRA.Mods.RA.Server server.SyncLobbyInfo(); if (server.conns.Count > 0 && server.conns.All(c => server.GetClient(c).State == Session.ClientState.Ready)) - InterpretCommand(conn, client, "startgame"); + InterpretCommand(server, conn, client, "startgame"); return true; }}, @@ -195,7 +195,7 @@ namespace OpenRA.Mods.RA.Server return true; } server.lobbyInfo.GlobalSettings.Map = s; - LoadMap(); + LoadMap(server); foreach(var c in server.lobbyInfo.Clients) { @@ -235,7 +235,7 @@ namespace OpenRA.Mods.RA.Server return a(cmdValue); } - public void ServerStarted() { LoadMap(); } + public void ServerStarted(S server) { LoadMap(server); } static Session.Slot MakeSlotFromPlayerReference(PlayerReference pr) { if (!pr.Playable) return null; @@ -247,7 +247,7 @@ namespace OpenRA.Mods.RA.Server }; } - public static void LoadMap() + public static void LoadMap(S server) { server.Map = new Map(server.ModData.AvailableMaps[server.lobbyInfo.GlobalSettings.Map]); server.lobbyInfo.Slots = server.Map.Players @@ -267,7 +267,7 @@ namespace OpenRA.Mods.RA.Server }); } - public void ClientJoined(Connection newConn) + public void ClientJoined(S server, Connection newConn) { var defaults = new GameRules.PlayerSettings(); @@ -281,7 +281,7 @@ namespace OpenRA.Mods.RA.Server State = Session.ClientState.NotReady, SpawnPoint = 0, Team = 0, - Slot = ChooseFreeSlot(), + Slot = ChooseFreeSlot(server), }; var slotData = server.lobbyInfo.Slots.FirstOrDefault( x => x.Index == client.Slot ); @@ -297,7 +297,7 @@ namespace OpenRA.Mods.RA.Server server.SyncLobbyInfo(); } - static int ChooseFreeSlot() + static int ChooseFreeSlot(S server) { return server.lobbyInfo.Slots.First(s => !s.Closed && s.Bot == null && !server.lobbyInfo.Clients.Any( c => c.Slot == s.Index )).Index; diff --git a/OpenRA.Mods.RA/ServerTraits/MasterServerPinger.cs b/OpenRA.Mods.RA/ServerTraits/MasterServerPinger.cs index c7be0e1951..b3455fb9c8 100644 --- a/OpenRA.Mods.RA/ServerTraits/MasterServerPinger.cs +++ b/OpenRA.Mods.RA/ServerTraits/MasterServerPinger.cs @@ -12,7 +12,7 @@ using System; using System.Collections.Generic; using System.Net; using OpenRA.Server; -using server = OpenRA.Server.Server; +using S = OpenRA.Server.Server; namespace OpenRA.Mods.RA.Server { @@ -21,10 +21,10 @@ namespace OpenRA.Mods.RA.Server const int MasterPingInterval = 60 * 3; // 3 minutes. server has a 5 minute TTL for games, so give ourselves a bit // of leeway. public int TickTimeout { get { return MasterPingInterval * 10000; } } - public void Tick() + public void Tick(S server) { if (Environment.TickCount - lastPing > MasterPingInterval * 1000) - PingMasterServer(); + PingMasterServer(server); else lock (masterServerMessages) while (masterServerMessages.Count > 0) @@ -33,8 +33,8 @@ namespace OpenRA.Mods.RA.Server } - public void LobbyInfoSynced() { PingMasterServer(); } - public void GameStarted() { PingMasterServer(); } + public void LobbyInfoSynced(S server) { PingMasterServer(server); } + public void GameStarted(S server) { PingMasterServer(server); } static int lastPing = 0; // Todo: use the settings passed to the server instead @@ -45,7 +45,7 @@ namespace OpenRA.Mods.RA.Server static volatile bool isBusy; static Queue masterServerMessages = new Queue(); - public static void PingMasterServer() + public static void PingMasterServer(S server) { if (isBusy || !isInternetServer) return; diff --git a/OpenRA.Mods.RA/ServerTraits/PlayerCommands.cs b/OpenRA.Mods.RA/ServerTraits/PlayerCommands.cs index abb3179ce7..1e7d4f9f5c 100644 --- a/OpenRA.Mods.RA/ServerTraits/PlayerCommands.cs +++ b/OpenRA.Mods.RA/ServerTraits/PlayerCommands.cs @@ -14,13 +14,13 @@ using System.Drawing; using System.Linq; using OpenRA.Network; using OpenRA.Server; -using server = OpenRA.Server.Server; +using S = OpenRA.Server.Server; namespace OpenRA.Mods.RA.Server { public class PlayerCommands : ServerTrait, IInterpretCommand { - public bool InterpretCommand(Connection conn, Session.Client client, string cmd) + public bool InterpretCommand( S server, Connection conn, Session.Client client, string cmd) { if (server.GameStarted) {