本文整理汇总了C#中OpenRA.Server.Server类的典型用法代码示例。如果您正苦于以下问题:C# Server类的具体用法?C# Server怎么用?C# Server使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
Server类属于OpenRA.Server命名空间,在下文中一共展示了Server类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ReadDataInner
bool ReadDataInner( Server server )
{
var rx = new byte[1024];
var len = 0;
for (; ; )
{
try
{
if (0 < (len = socket.Receive(rx)))
data.AddRange(rx.Take(len));
else
{
if (len == 0)
server.DropClient(this);
break;
}
}
catch (SocketException e)
{
if (e.SocketErrorCode == SocketError.WouldBlock) break;
server.DropClient(this);
return false;
}
}
return true;
}
示例2: ReadData
public void ReadData(Server server)
{
if (ReadDataInner(server))
while (data.Count >= ExpectLength)
{
var bytes = PopBytes(ExpectLength);
switch (State)
{
case ReceiveState.Header:
{
ExpectLength = BitConverter.ToInt32(bytes, 0) - 4;
Frame = BitConverter.ToInt32(bytes, 4);
State = ReceiveState.Data;
if (ExpectLength < 0 || ExpectLength > MaxOrderLength)
{
server.DropClient(this);
Log.Write("server", "Dropping client {0} for excessive order length = {1}", PlayerIndex, ExpectLength);
return;
}
} break;
case ReceiveState.Data:
{
server.DispatchOrders(this, Frame, bytes);
MostRecentFrame = Frame;
ExpectLength = 8;
State = ReceiveState.Header;
} break;
}
}
}
示例3: ReadDataInner
bool ReadDataInner( Server server )
{
var rx = new byte[1024];
var len = 0;
for (; ; )
{
try
{
// NOTE(jsd): Poll the socket first to see if there's anything there.
// This avoids the exception with SocketErrorCode == `SocketError.WouldBlock` thrown
// from `socket.Receive(rx)`.
if (!socket.Poll(0, SelectMode.SelectRead)) break;
if (0 < (len = socket.Receive(rx)))
data.AddRange(rx.Take(len));
else
{
if (len == 0)
server.DropClient(this);
break;
}
}
catch (SocketException e)
{
// NOTE(jsd): This should no longer be needed with the socket.Poll call above.
if (e.SocketErrorCode == SocketError.WouldBlock) break;
server.DropClient(this);
return false;
}
}
return true;
}
示例4: ReadData
public void ReadData( Server server )
{
if (ReadDataInner(server))
while (data.Count >= ExpectLength)
{
var bytes = PopBytes(ExpectLength);
switch (State)
{
case ReceiveState.Header:
{
ExpectLength = BitConverter.ToInt32(bytes, 0) - 4;
Frame = BitConverter.ToInt32(bytes, 4);
State = ReceiveState.Data;
} break;
case ReceiveState.Data:
{
server.DispatchOrders(this, Frame, bytes);
MostRecentFrame = Frame;
ExpectLength = 8;
State = ReceiveState.Header;
server.UpdateInFlightFrames(this);
} break;
}
}
}
示例5: Tick
public void Tick(S server)
{
if ((Game.RunTime - lastPing > MasterPingInterval * 1000) || isInitialPing)
PingMasterServer(server);
else
lock (masterServerMessages)
while (masterServerMessages.Count > 0)
server.SendMessage(masterServerMessages.Dequeue());
}
示例6: PingMasterServer
public void PingMasterServer(S server)
{
if (isBusy || !server.Settings.AdvertiseOnline) return;
lastPing = Environment.TickCount;
isBusy = true;
var mod = server.ModData.Manifest.Mod;
// important to grab these on the main server thread, not in the worker we're about to spawn -- they may be modified
// by the main thread as clients join and leave.
var numPlayers = server.LobbyInfo.Clients.Where(c1 => c1.Bot == null).Count();
var numBots = server.LobbyInfo.Clients.Where(c1 => c1.Bot != null).Count();
Action a = () =>
{
try
{
var url = "ping.php?port={0}&name={1}&state={2}&players={3}&bots={4}&mods={5}&map={6}&maxplayers={7}";
if (isInitialPing) url += "&new=1";
using (var wc = new WebClient())
{
wc.Proxy = null;
wc.DownloadData(
server.Settings.MasterServer + url.F(
server.Settings.ExternalPort, Uri.EscapeUriString(server.Settings.Name),
(int)server.State,
numPlayers,
numBots,
"{0}@{1}".F(mod.Id, mod.Version),
server.LobbyInfo.GlobalSettings.Map,
server.Map.PlayerCount));
if (isInitialPing)
{
isInitialPing = false;
lock (masterServerMessages)
masterServerMessages.Enqueue("Master server communication established.");
}
}
}
catch (Exception ex)
{
Log.Write("server", ex.ToString());
lock (masterServerMessages)
masterServerMessages.Enqueue("Master server communication failed.");
}
isBusy = false;
};
a.BeginInvoke(null, null);
}
示例7: Main
static void Main(string[] args)
{
var arguments = new Arguments(args);
Log.AddChannel("debug", "dedicated-debug.log");
Log.AddChannel("perf", "dedicated-perf.log");
Log.AddChannel("server", "dedicated-server.log");
Log.AddChannel("nat", "dedicated-nat.log");
// Special case handling of Game.Mod argument: if it matches a real filesystem path
// then we use this to override the mod search path, and replace it with the mod id
var modArgument = arguments.GetValue("Game.Mod", null);
string customModPath = null;
if (modArgument != null && (File.Exists(modArgument) || Directory.Exists(modArgument)))
{
customModPath = modArgument;
arguments.ReplaceValue("Game.Mod", Path.GetFileNameWithoutExtension(modArgument));
}
// HACK: The engine code assumes that Game.Settings is set.
// This isn't nearly as bad as ModData, but is still not very nice.
Game.InitializeSettings(arguments);
var settings = Game.Settings.Server;
var mod = Game.Settings.Game.Mod;
var mods = new InstalledMods(customModPath);
// HACK: The engine code *still* assumes that Game.ModData is set
var modData = Game.ModData = new ModData(mods[mod], mods);
modData.MapCache.LoadMaps();
settings.Map = modData.MapCache.ChooseInitialMap(settings.Map, new MersenneTwister());
Console.WriteLine("[{0}] Starting dedicated server for mod: {1}", DateTime.Now.ToString(settings.TimestampFormat), mod);
while (true)
{
var server = new Server(new IPEndPoint(IPAddress.Any, settings.ListenPort), settings, modData, true);
while (true)
{
Thread.Sleep(1000);
if (server.State == ServerState.GameStarted && server.Conns.Count < 1)
{
Console.WriteLine("[{0}] No one is playing, shutting down...", DateTime.Now.ToString(settings.TimestampFormat));
server.Shutdown();
break;
}
}
Console.WriteLine("[{0}] Starting a new server instance...", DateTime.Now.ToString(settings.TimestampFormat));
}
}
示例8: ValidateCommand
public static bool ValidateCommand(S server, Connection conn, Session.Client client, string cmd)
{
if (server.State == ServerState.GameStarted)
{
server.SendOrderTo(conn, "Message", "Cannot change state when game started. ({0})".F(cmd));
return false;
}
else if (client.State == Session.ClientState.Ready && !(cmd.StartsWith("state") || cmd == "startgame"))
{
server.SendOrderTo(conn, "Message", "Cannot change state when marked as ready.");
return false;
}
return true;
}
示例9: PingMasterServer
public void PingMasterServer(S server)
{
if (isBusy || !server.Settings.AdvertiseOnline) return;
lastPing = Environment.TickCount;
isBusy = true;
Action a = () =>
{
try
{
var url = "ping.php?port={0}&name={1}&state={2}&players={3}&bots={4}&mods={5}&map={6}&maxplayers={7}";
if (isInitialPing) url += "&new=1";
using (var wc = new WebClient())
{
wc.Proxy = null;
wc.DownloadData(
server.Settings.MasterServer + url.F(
server.Settings.ExternalPort, Uri.EscapeUriString(server.Settings.Name),
(int) server.State,
server.lobbyInfo.Clients.Where(c1 => c1.Bot == null).Count(),
server.lobbyInfo.Clients.Where(c1 => c1.Bot != null).Count(),
Game.CurrentMods.Select(f => "{0}@{1}".F(f.Key, f.Value.Version)).JoinWith(","),
server.lobbyInfo.GlobalSettings.Map,
server.Map.PlayerCount));
if (isInitialPing)
{
isInitialPing = false;
lock (masterServerMessages)
masterServerMessages.Enqueue("Master server communication established.");
}
}
}
catch(Exception ex)
{
Log.Write("server", ex.ToString());
lock( masterServerMessages )
masterServerMessages.Enqueue( "Master server communication failed." );
}
isBusy = false;
};
a.BeginInvoke(null, null);
}
示例10: PingMasterServer
public void PingMasterServer(S server)
{
if (isBusy || !isInternetServer) return;
lastPing = Environment.TickCount;
isBusy = true;
Action a = () =>
{
try
{
var url = "ping.php?port={0}&name={1}&state={2}&players={3}&mods={4}&map={5}&maxplayers={6}";
if (isInitialPing) url += "&new=1";
using (var wc = new WebClient())
{
wc.Proxy = null;
wc.DownloadData(
masterServerUrl + url.F(
externalPort, Uri.EscapeUriString(server.Name),
server.GameStarted ? 2 : 1, // todo: post-game states, etc.
server.lobbyInfo.Clients.Count,
string.Join(",", Game.CurrentMods.Select(f => "{0}@{1}".F(f.Key, f.Value.Version)).ToArray()),
server.lobbyInfo.GlobalSettings.Map,
server.lobbyInfo.Slots.Count( s => !s.Spectator )));
if (isInitialPing)
{
isInitialPing = false;
lock (masterServerMessages)
masterServerMessages.Enqueue("Master server communication established.");
}
}
}
catch(Exception ex)
{
Log.Write("server", ex.ToString());
lock( masterServerMessages )
masterServerMessages.Enqueue( "Master server communication failed." );
}
isBusy = false;
};
a.BeginInvoke(null, null);
}
示例11: ReadDataInner
bool ReadDataInner(Server server)
{
var rx = new byte[1024];
var len = 0;
for (;;)
{
try
{
// NOTE(jsd): Poll the socket first to see if there's anything there.
// This avoids the exception with SocketErrorCode == `SocketError.WouldBlock` thrown
// from `socket.Receive(rx)`.
if (!Socket.Poll(0, SelectMode.SelectRead)) break;
if (0 < (len = Socket.Receive(rx)))
Data.AddRange(rx.Take(len));
else
{
if (len == 0)
server.DropClient(this);
break;
}
}
catch (SocketException e)
{
// NOTE(jsd): This should no longer be needed with the socket.Poll call above.
if (e.SocketErrorCode == SocketError.WouldBlock) break;
server.DropClient(this);
Log.Write("server", "Dropping client {0} because reading the data failed: {1}", PlayerIndex, e);
return false;
}
}
lastReceivedTime = Game.RunTime;
TimeoutMessageShown = false;
return true;
}
示例12: ClientJoined
public void ClientJoined(S server, Connection newConn)
{
var defaults = new GameRules.PlayerSettings();
var client = new Session.Client()
{
Index = newConn.PlayerIndex,
Color1 = defaults.Color1,
Color2 = defaults.Color2,
Name = defaults.Name,
Country = "random",
State = Session.ClientState.NotReady,
SpawnPoint = 0,
Team = 0,
Slot = ChooseFreeSlot(server),
};
var slotData = server.lobbyInfo.Slots.FirstOrDefault( x => x.Index == client.Slot );
if (slotData != null)
SyncClientToPlayerReference(client, server.Map.Players[slotData.MapPlayer]);
server.lobbyInfo.Clients.Add(client);
Log.Write("server", "Client {0}: Accepted connection from {1}",
newConn.PlayerIndex, newConn.socket.RemoteEndPoint);
server.SendChat(newConn, "has joined the game.");
server.SyncLobbyInfo();
}
示例13: ClientJoined
public void ClientJoined(S server, Connection conn)
{
var client = server.GetClient(conn);
// Validate whether color is allowed and get an alternative if it isn't
if (client.Slot == null || !server.LobbyInfo.Slots[client.Slot].LockColor)
client.Color = SanitizePlayerColor(server, client.Color, client.Index);
}
示例14: Tick
public void Tick(S server)
{
if ((Environment.TickCount - lastPing > PingInterval) || isInitialPing)
{
isInitialPing = false;
lastPing = Environment.TickCount;
foreach (var p in server.Conns)
server.SendOrderTo(p, "Ping", Environment.TickCount.ToString());
}
}
示例15: CheckAutoStart
static void CheckAutoStart(S server)
{
var playerClients = server.LobbyInfo.Clients.Where(c => c.Bot == null && c.Slot != null);
// Are all players ready?
if (!playerClients.Any() || playerClients.Any(c => c.State != Session.ClientState.Ready))
return;
// Are the map conditions satisfied?
if (server.LobbyInfo.Slots.Any(sl => sl.Value.Required && server.LobbyInfo.ClientInSlot(sl.Key) == null))
return;
server.StartGame();
}