Fix server freeze on client absence.

This commit is contained in:
huwpascoe
2014-09-24 23:57:55 +01:00
parent 20a41d8803
commit d150ea1721
4 changed files with 53 additions and 20 deletions

View File

@@ -34,6 +34,11 @@ namespace OpenRA.Network
public void ClientQuit( int clientId, int lastClientFrame ) public void ClientQuit( int clientId, int lastClientFrame )
{ {
if (lastClientFrame == -1)
lastClientFrame = framePackets
.Where(x => x.Value.ContainsKey(clientId))
.Select(x => x.Key).OrderBy(x => x).LastOrDefault();
clientQuitTimes[clientId] = lastClientFrame; clientQuitTimes[clientId] = lastClientFrame;
} }

View File

@@ -25,6 +25,10 @@ namespace OpenRA.Server
public int MostRecentFrame = 0; public int MostRecentFrame = 0;
public const int MaxOrderLength = 131072; public const int MaxOrderLength = 131072;
public int TimeSinceLastResponse { get { return Game.RunTime - lastReceivedTime; } }
public bool TimeoutMessageShown = false;
int lastReceivedTime = 0;
/* client data */ /* client data */
public int PlayerIndex; public int PlayerIndex;
@@ -69,6 +73,9 @@ namespace OpenRA.Server
} }
} }
lastReceivedTime = Game.RunTime;
TimeoutMessageShown = false;
return true; return true;
} }
@@ -101,7 +108,6 @@ namespace OpenRA.Server
ExpectLength = 8; ExpectLength = 8;
State = ReceiveState.Header; State = ReceiveState.Header;
server.UpdateInFlightFrames(this);
} break; } break;
} }
} }

View File

@@ -46,8 +46,6 @@ namespace OpenRA.Server
public List<Connection> PreConns = new List<Connection>(); public List<Connection> PreConns = new List<Connection>();
TcpListener listener = null; TcpListener listener = null;
Dictionary<int, List<Connection>> inFlightFrames
= new Dictionary<int, List<Connection>>();
TypeDictionary serverTraits = new TypeDictionary(); TypeDictionary serverTraits = new TypeDictionary();
public Session LobbyInfo; public Session LobbyInfo;
@@ -389,20 +387,6 @@ namespace OpenRA.Server
SyncLobbyGlobalSettings(); SyncLobbyGlobalSettings();
} }
public void UpdateInFlightFrames(Connection conn)
{
if (conn.Frame == 0)
return;
if (!inFlightFrames.ContainsKey(conn.Frame))
inFlightFrames[conn.Frame] = new List<Connection> { conn };
else
inFlightFrames[conn.Frame].Add(conn);
if (Conns.All(c => inFlightFrames[conn.Frame].Contains(c)))
inFlightFrames.Remove(conn.Frame);
}
void DispatchOrdersToClient(Connection c, int client, int frame, byte[] data) void DispatchOrdersToClient(Connection c, int client, int frame, byte[] data)
{ {
try try
@@ -527,6 +511,11 @@ namespace OpenRA.Server
} }
public void DropClient(Connection toDrop) public void DropClient(Connection toDrop)
{
DropClient(toDrop, toDrop.MostRecentFrame);
}
public void DropClient(Connection toDrop, int frame)
{ {
if (PreConns.Contains(toDrop)) if (PreConns.Contains(toDrop))
PreConns.Remove(toDrop); PreConns.Remove(toDrop);
@@ -565,7 +554,7 @@ namespace OpenRA.Server
} }
} }
DispatchOrders(toDrop, toDrop.MostRecentFrame, new byte[] { 0xbf }); DispatchOrders(toDrop, frame, new byte[] { 0xbf });
if (!Conns.Any()) if (!Conns.Any())
{ {

View File

@@ -9,6 +9,7 @@
#endregion #endregion
using System; using System;
using System.Linq;
using OpenRA.Server; using OpenRA.Server;
using S = OpenRA.Server.Server; using S = OpenRA.Server.Server;
@@ -17,20 +18,52 @@ namespace OpenRA.Mods.Common.Server
public class PlayerPinger : ServerTrait, ITick public class PlayerPinger : ServerTrait, ITick
{ {
int PingInterval = 5000; // Ping every 5 seconds int PingInterval = 5000; // Ping every 5 seconds
int ConnReportInterval = 20000; // Report every 20 seconds
int ConnTimeout = 90000; // Drop unresponsive clients after 90 seconds
// TickTimeout is in microseconds // TickTimeout is in microseconds
public int TickTimeout { get { return PingInterval * 100; } } public int TickTimeout { get { return PingInterval * 100; } }
int lastPing = 0; int lastPing = 0;
int lastConnReport = 0;
bool isInitialPing = true; bool isInitialPing = true;
public void Tick(S server) public void Tick(S server)
{ {
if ((Game.RunTime - lastPing > PingInterval) || isInitialPing) if ((Game.RunTime - lastPing > PingInterval) || isInitialPing)
{ {
isInitialPing = false; isInitialPing = false;
lastPing = Game.RunTime; lastPing = Game.RunTime;
foreach (var p in server.Conns) foreach (var c in server.Conns.ToList())
server.SendOrderTo(p, "Ping", Game.RunTime.ToString()); {
if (c.TimeSinceLastResponse < ConnTimeout)
{
server.SendOrderTo(c, "Ping", Game.RunTime.ToString());
if (!c.TimeoutMessageShown && c.TimeSinceLastResponse > PingInterval * 2)
{
server.SendMessage(server.GetClient(c).Name + " is experiencing connection problems.");
c.TimeoutMessageShown = true;
}
}
else
{
server.SendMessage(server.GetClient(c).Name + " has been dropped after timing out.");
server.DropClient(c, -1);
}
}
}
if (Game.RunTime - lastConnReport > ConnReportInterval)
{
lastConnReport = Game.RunTime;
var timeouts = server.Conns
.Where(c => c.TimeSinceLastResponse > ConnReportInterval && c.TimeSinceLastResponse < ConnTimeout)
.OrderBy(c => c.TimeSinceLastResponse);
foreach (var c in timeouts)
server.SendMessage("{0} will be dropped in {1} seconds.".F(
server.GetClient(c).Name, (ConnTimeout - c.TimeSinceLastResponse) / 1000));
} }
} }
} }