remove staticness on Server

This commit is contained in:
Bob
2010-11-14 21:26:50 +13:00
parent b2f3b8f2af
commit 6cf9939ab3
8 changed files with 85 additions and 83 deletions

View File

@@ -38,6 +38,7 @@ namespace OpenRA
public static Settings Settings; public static Settings Settings;
internal static OrderManager orderManager; internal static OrderManager orderManager;
static Server.Server server;
public static XRandom CosmeticRandom = new XRandom(); // not synced public static XRandom CosmeticRandom = new XRandom(); // not synced
@@ -258,8 +259,8 @@ namespace OpenRA
public static void Disconnect() public static void Disconnect()
{ {
if (IsHost) if (IsHost && server != null)
Server.Server.Shutdown(); server.Shutdown();
orderManager.Dispose(); orderManager.Dispose();
var shellmap = modData.Manifest.ShellmapUid; var shellmap = modData.Manifest.ShellmapUid;
@@ -317,12 +318,15 @@ namespace OpenRA
ConnectedToLobby = null; ConnectedToLobby = null;
}; };
if (isHost) if (isHost)
{ CreateAndJoinServer( Settings, map );
Server.Server.ServerMain(Game.modData, Settings, map);
JoinServer(IPAddress.Loopback.ToString(), Settings.Server.ListenPort);
}
else else
JoinServer(host, port); 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);
}
} }
} }

View File

@@ -35,7 +35,7 @@ namespace OpenRA.Server
return result.ToArray(); return result.ToArray();
} }
bool ReadDataInner() bool ReadDataInner( Server server )
{ {
var rx = new byte[1024]; var rx = new byte[1024];
var len = 0; var len = 0;
@@ -49,7 +49,7 @@ namespace OpenRA.Server
else else
{ {
if (len == 0) if (len == 0)
Server.DropClient(this, null); server.DropClient(this, null);
break; break;
} }
@@ -57,7 +57,7 @@ namespace OpenRA.Server
catch (SocketException e) catch (SocketException e)
{ {
if (e.SocketErrorCode == SocketError.WouldBlock) break; if (e.SocketErrorCode == SocketError.WouldBlock) break;
Server.DropClient(this, e); server.DropClient(this, e);
return false; return false;
} }
} }
@@ -65,9 +65,9 @@ namespace OpenRA.Server
return true; return true;
} }
public void ReadData() public void ReadData( Server server )
{ {
if (ReadDataInner()) if (ReadDataInner(server))
while (data.Count >= ExpectLength) while (data.Count >= ExpectLength)
{ {
var bytes = PopBytes(ExpectLength); var bytes = PopBytes(ExpectLength);
@@ -82,12 +82,12 @@ namespace OpenRA.Server
case ReceiveState.Data: case ReceiveState.Data:
{ {
Server.DispatchOrders(this, Frame, bytes); server.DispatchOrders(this, Frame, bytes);
MostRecentFrame = Frame; MostRecentFrame = Frame;
ExpectLength = 8; ExpectLength = 8;
State = ReceiveState.Header; State = ReceiveState.Header;
Server.UpdateInFlightFrames(this); server.UpdateInFlightFrames(this);
} break; } break;
} }
} }

View File

@@ -23,34 +23,34 @@ using OpenRA.Network;
namespace OpenRA.Server namespace OpenRA.Server
{ {
public static class Server public class Server
{ {
public static List<Connection> conns = new List<Connection>(); public List<Connection> conns = new List<Connection>();
static TcpListener listener = null; TcpListener listener = null;
static Dictionary<int, List<Connection>> inFlightFrames Dictionary<int, List<Connection>> inFlightFrames
= new Dictionary<int, List<Connection>>(); = new Dictionary<int, List<Connection>>();
static TypeDictionary ServerTraits = new TypeDictionary(); TypeDictionary ServerTraits = new TypeDictionary();
public static Session lobbyInfo; public Session lobbyInfo;
public static bool GameStarted = false; public bool GameStarted = false;
public static string Name; public string Name;
static int randomSeed; int randomSeed;
public static ModData ModData; public ModData ModData;
public static Map Map; public Map Map;
public static void Shutdown() public void Shutdown()
{ {
conns.Clear(); conns.Clear();
GameStarted = false; GameStarted = false;
foreach (var t in ServerTraits.WithInterface<INotifyServerShutdown>()) foreach (var t in ServerTraits.WithInterface<INotifyServerShutdown>())
t.ServerShutdown(); t.ServerShutdown(this);
try { listener.Stop(); } try { listener.Stop(); }
catch { } catch { }
} }
public static void ServerMain(ModData modData, Settings settings, string map) public Server(ModData modData, Settings settings, string map)
{ {
Log.AddChannel("server", "server.log"); Log.AddChannel("server", "server.log");
@@ -69,7 +69,7 @@ namespace OpenRA.Server
lobbyInfo.GlobalSettings.ServerName = settings.Server.Name; lobbyInfo.GlobalSettings.ServerName = settings.Server.Name;
foreach (var t in ServerTraits.WithInterface<INotifyServerStart>()) foreach (var t in ServerTraits.WithInterface<INotifyServerStart>())
t.ServerStarted(); t.ServerStarted(this);
Log.Write("server", "Initial mods: "); Log.Write("server", "Initial mods: ");
foreach( var m in lobbyInfo.GlobalSettings.Mods ) foreach( var m in lobbyInfo.GlobalSettings.Mods )
@@ -99,10 +99,10 @@ namespace OpenRA.Server
foreach( Socket s in checkRead ) foreach( Socket s in checkRead )
if( s == listener.Server ) AcceptConnection(); 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<ITick>()) foreach (var t in ServerTraits.WithInterface<ITick>())
t.Tick(); t.Tick(this);
if (conns.Count() == 0) if (conns.Count() == 0)
{ {
@@ -119,7 +119,7 @@ namespace OpenRA.Server
* for manual spawnpoint choosing. * for manual spawnpoint choosing.
* - 256 max players is a dirty hack * - 256 max players is a dirty hack
*/ */
static int ChooseFreePlayerIndex() int ChooseFreePlayerIndex()
{ {
for (var i = 0; i < 256; i++) for (var i = 0; i < 256; i++)
if (conns.All(c => c.PlayerIndex != i)) if (conns.All(c => c.PlayerIndex != i))
@@ -128,7 +128,7 @@ namespace OpenRA.Server
throw new InvalidOperationException("Already got 256 players"); throw new InvalidOperationException("Already got 256 players");
} }
static void AcceptConnection() void AcceptConnection()
{ {
Socket newSocket = null; Socket newSocket = null;
@@ -164,12 +164,12 @@ namespace OpenRA.Server
conns.Add(newConn); conns.Add(newConn);
foreach (var t in ServerTraits.WithInterface<IClientJoined>()) foreach (var t in ServerTraits.WithInterface<IClientJoined>())
t.ClientJoined(newConn); t.ClientJoined(this, newConn);
} }
catch (Exception e) { DropClient(newConn, e); } catch (Exception e) { DropClient(newConn, e); }
} }
public static void UpdateInFlightFrames(Connection conn) public void UpdateInFlightFrames(Connection conn)
{ {
if (conn.Frame != 0) 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 try
{ {
@@ -199,7 +199,7 @@ namespace OpenRA.Server
catch( Exception e ) { DropClient( c, e ); } 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) if (frame == 0 && conn != null)
InterpretServerOrders(conn, data); 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 ms = new MemoryStream(data);
var br = new BinaryReader(ms); var br = new BinaryReader(ms);
@@ -229,23 +229,23 @@ namespace OpenRA.Server
catch (NotImplementedException) { } catch (NotImplementedException) { }
} }
public static void SendChatTo(Connection conn, string text) public void SendChatTo(Connection conn, string text)
{ {
DispatchOrdersToClient(conn, 0, 0, DispatchOrdersToClient(conn, 0, 0,
new ServerOrder("Chat", text).Serialize()); 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()); 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()); DispatchOrders(asConn, 0, new ServerOrder("Disconnected", "").Serialize());
} }
static void InterpretServerOrder(Connection conn, ServerOrder so) void InterpretServerOrder(Connection conn, ServerOrder so)
{ {
switch (so.Name) switch (so.Name)
{ {
@@ -253,7 +253,7 @@ namespace OpenRA.Server
{ {
bool handled = false; bool handled = false;
foreach (var t in ServerTraits.WithInterface<IInterpretCommand>()) foreach (var t in ServerTraits.WithInterface<IInterpretCommand>())
if ((handled = t.InterpretCommand(conn, GetClient(conn), so.Data))) if ((handled = t.InterpretCommand(this, conn, GetClient(conn), so.Data)))
break; break;
if (!handled) 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); 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); conns.Remove(toDrop);
SendChat(toDrop, "Connection Dropped"); SendChat(toDrop, "Connection Dropped");
@@ -293,28 +293,28 @@ namespace OpenRA.Server
SyncLobbyInfo(); SyncLobbyInfo();
} }
public static void SyncLobbyInfo() public void SyncLobbyInfo()
{ {
if (!GameStarted) /* don't do this while the game is running, it breaks things. */ if (!GameStarted) /* don't do this while the game is running, it breaks things. */
DispatchOrders(null, 0, DispatchOrders(null, 0,
new ServerOrder("SyncInfo", lobbyInfo.Serialize()).Serialize()); new ServerOrder("SyncInfo", lobbyInfo.Serialize()).Serialize());
foreach (var t in ServerTraits.WithInterface<INotifySyncLobbyInfo>()) foreach (var t in ServerTraits.WithInterface<INotifySyncLobbyInfo>())
t.LobbyInfoSynced(); t.LobbyInfoSynced(this);
} }
public static void StartGame() public void StartGame()
{ {
Server.GameStarted = true; GameStarted = true;
foreach( var c in Server.conns ) foreach( var c in conns )
foreach( var d in Server.conns ) foreach( var d in conns )
DispatchOrdersToClient( c, d.PlayerIndex, 0x7FFFFFFF, new byte[] { 0xBF } ); DispatchOrdersToClient( c, d.PlayerIndex, 0x7FFFFFFF, new byte[] { 0xBF } );
DispatchOrders(null, 0, DispatchOrders(null, 0,
new ServerOrder("StartGame", "").Serialize()); new ServerOrder("StartGame", "").Serialize());
foreach (var t in ServerTraits.WithInterface<IStartGame>()) foreach (var t in ServerTraits.WithInterface<IStartGame>())
t.GameStarted(); t.GameStarted(this);
} }
} }
} }

View File

@@ -13,15 +13,15 @@ using OpenRA.Network;
namespace OpenRA.Server namespace OpenRA.Server
{ {
// Returns true if order is handled // Returns true if order is handled
public interface IInterpretCommand { bool InterpretCommand(Connection conn, Session.Client client, string cmd); } public interface IInterpretCommand { bool InterpretCommand(Server server, Connection conn, Session.Client client, string cmd); }
public interface INotifySyncLobbyInfo { void LobbyInfoSynced(); } public interface INotifySyncLobbyInfo { void LobbyInfoSynced(Server server); }
public interface INotifyServerStart { void ServerStarted(); } public interface INotifyServerStart { void ServerStarted(Server server); }
public interface INotifyServerShutdown { void ServerShutdown(); } public interface INotifyServerShutdown { void ServerShutdown(Server server); }
public interface IStartGame { void GameStarted(); } public interface IStartGame { void GameStarted(Server server); }
public interface IClientJoined { void ClientJoined(Connection conn); } public interface IClientJoined { void ClientJoined(Server server, Connection conn); }
public interface ITick public interface ITick
{ {
void Tick(); void Tick(Server server);
int TickTimeout { get; } int TickTimeout { get; }
} }
@@ -29,28 +29,28 @@ namespace OpenRA.Server
public class DebugServerTrait : ServerTrait, IInterpretCommand, IStartGame, INotifySyncLobbyInfo, INotifyServerStart, INotifyServerShutdown 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); Console.WriteLine("Server received command from player {1}: {0}",cmd, conn.PlayerIndex);
return false; return false;
} }
public void GameStarted() public void GameStarted(Server server)
{ {
Console.WriteLine("GameStarted()"); Console.WriteLine("GameStarted()");
} }
public void LobbyInfoSynced() public void LobbyInfoSynced(Server server)
{ {
Console.WriteLine("LobbyInfoSynced()"); Console.WriteLine("LobbyInfoSynced()");
} }
public void ServerStarted() public void ServerStarted(Server server)
{ {
Console.WriteLine("ServerStarted()"); Console.WriteLine("ServerStarted()");
} }
public void ServerShutdown() public void ServerShutdown(Server server)
{ {
Console.WriteLine("ServerShutdown()"); Console.WriteLine("ServerShutdown()");
} }

View File

@@ -33,9 +33,7 @@ namespace OpenRA.Widgets.Delegates
settings.Server.ExternalPort = int.Parse(cs.GetWidget<TextFieldWidget>("EXTERNAL_PORT").Text); settings.Server.ExternalPort = int.Parse(cs.GetWidget<TextFieldWidget>("EXTERNAL_PORT").Text);
settings.Save(); settings.Save();
Server.Server.ServerMain(Game.modData, settings, map); Game.CreateAndJoinServer(settings, map);
Game.JoinServer(IPAddress.Loopback.ToString(), settings.Server.ListenPort);
return true; return true;
}; };

View File

@@ -14,7 +14,7 @@ using System.Linq;
using OpenRA.Network; using OpenRA.Network;
using OpenRA.FileFormats; using OpenRA.FileFormats;
using OpenRA.Server; using OpenRA.Server;
using server = OpenRA.Server.Server; using S = OpenRA.Server.Server;
namespace OpenRA.Mods.RA.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 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) if (server.GameStarted)
{ {
@@ -52,7 +52,7 @@ namespace OpenRA.Mods.RA.Server
server.SyncLobbyInfo(); server.SyncLobbyInfo();
if (server.conns.Count > 0 && server.conns.All(c => server.GetClient(c).State == Session.ClientState.Ready)) 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; return true;
}}, }},
@@ -195,7 +195,7 @@ namespace OpenRA.Mods.RA.Server
return true; return true;
} }
server.lobbyInfo.GlobalSettings.Map = s; server.lobbyInfo.GlobalSettings.Map = s;
LoadMap(); LoadMap(server);
foreach(var c in server.lobbyInfo.Clients) foreach(var c in server.lobbyInfo.Clients)
{ {
@@ -235,7 +235,7 @@ namespace OpenRA.Mods.RA.Server
return a(cmdValue); return a(cmdValue);
} }
public void ServerStarted() { LoadMap(); } public void ServerStarted(S server) { LoadMap(server); }
static Session.Slot MakeSlotFromPlayerReference(PlayerReference pr) static Session.Slot MakeSlotFromPlayerReference(PlayerReference pr)
{ {
if (!pr.Playable) return null; 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.Map = new Map(server.ModData.AvailableMaps[server.lobbyInfo.GlobalSettings.Map]);
server.lobbyInfo.Slots = server.Map.Players 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(); var defaults = new GameRules.PlayerSettings();
@@ -281,7 +281,7 @@ namespace OpenRA.Mods.RA.Server
State = Session.ClientState.NotReady, State = Session.ClientState.NotReady,
SpawnPoint = 0, SpawnPoint = 0,
Team = 0, Team = 0,
Slot = ChooseFreeSlot(), Slot = ChooseFreeSlot(server),
}; };
var slotData = server.lobbyInfo.Slots.FirstOrDefault( x => x.Index == client.Slot ); var slotData = server.lobbyInfo.Slots.FirstOrDefault( x => x.Index == client.Slot );
@@ -297,7 +297,7 @@ namespace OpenRA.Mods.RA.Server
server.SyncLobbyInfo(); server.SyncLobbyInfo();
} }
static int ChooseFreeSlot() static int ChooseFreeSlot(S server)
{ {
return server.lobbyInfo.Slots.First(s => !s.Closed && s.Bot == null return server.lobbyInfo.Slots.First(s => !s.Closed && s.Bot == null
&& !server.lobbyInfo.Clients.Any( c => c.Slot == s.Index )).Index; && !server.lobbyInfo.Clients.Any( c => c.Slot == s.Index )).Index;

View File

@@ -12,7 +12,7 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Net; using System.Net;
using OpenRA.Server; using OpenRA.Server;
using server = OpenRA.Server.Server; using S = OpenRA.Server.Server;
namespace OpenRA.Mods.RA.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 const int MasterPingInterval = 60 * 3; // 3 minutes. server has a 5 minute TTL for games, so give ourselves a bit
// of leeway. // of leeway.
public int TickTimeout { get { return MasterPingInterval * 10000; } } public int TickTimeout { get { return MasterPingInterval * 10000; } }
public void Tick() public void Tick(S server)
{ {
if (Environment.TickCount - lastPing > MasterPingInterval * 1000) if (Environment.TickCount - lastPing > MasterPingInterval * 1000)
PingMasterServer(); PingMasterServer(server);
else else
lock (masterServerMessages) lock (masterServerMessages)
while (masterServerMessages.Count > 0) while (masterServerMessages.Count > 0)
@@ -33,8 +33,8 @@ namespace OpenRA.Mods.RA.Server
} }
public void LobbyInfoSynced() { PingMasterServer(); } public void LobbyInfoSynced(S server) { PingMasterServer(server); }
public void GameStarted() { PingMasterServer(); } public void GameStarted(S server) { PingMasterServer(server); }
static int lastPing = 0; static int lastPing = 0;
// Todo: use the settings passed to the server instead // Todo: use the settings passed to the server instead
@@ -45,7 +45,7 @@ namespace OpenRA.Mods.RA.Server
static volatile bool isBusy; static volatile bool isBusy;
static Queue<string> masterServerMessages = new Queue<string>(); static Queue<string> masterServerMessages = new Queue<string>();
public static void PingMasterServer() public static void PingMasterServer(S server)
{ {
if (isBusy || !isInternetServer) return; if (isBusy || !isInternetServer) return;

View File

@@ -14,13 +14,13 @@ using System.Drawing;
using System.Linq; using System.Linq;
using OpenRA.Network; using OpenRA.Network;
using OpenRA.Server; using OpenRA.Server;
using server = OpenRA.Server.Server; using S = OpenRA.Server.Server;
namespace OpenRA.Mods.RA.Server namespace OpenRA.Mods.RA.Server
{ {
public class PlayerCommands : ServerTrait, IInterpretCommand 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) if (server.GameStarted)
{ {