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;
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);
}
}
}

View File

@@ -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;
}
}

View File

@@ -23,34 +23,34 @@ using OpenRA.Network;
namespace OpenRA.Server
{
public static class Server
public class Server
{
public static List<Connection> conns = new List<Connection>();
static TcpListener listener = null;
static Dictionary<int, List<Connection>> inFlightFrames
public List<Connection> conns = new List<Connection>();
TcpListener listener = null;
Dictionary<int, List<Connection>> inFlightFrames
= new Dictionary<int, List<Connection>>();
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<INotifyServerShutdown>())
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<INotifyServerStart>())
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<ITick>())
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<IClientJoined>())
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<IInterpretCommand>())
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<INotifySyncLobbyInfo>())
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<IStartGame>())
t.GameStarted();
t.GameStarted(this);
}
}
}

View File

@@ -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()");
}

View File

@@ -33,9 +33,7 @@ namespace OpenRA.Widgets.Delegates
settings.Server.ExternalPort = int.Parse(cs.GetWidget<TextFieldWidget>("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;
};

View File

@@ -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;

View File

@@ -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<string> masterServerMessages = new Queue<string>();
public static void PingMasterServer()
public static void PingMasterServer(S server)
{
if (isBusy || !isInternetServer) return;

View File

@@ -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)
{