void InterpretServerOrders(Connection conn, byte[] data) { var ms = new MemoryStream(data); var br = new BinaryReader(ms); try { while (ms.Position < ms.Length) { var so = ServerOrder.Deserialize(br); if (so == null) { return; } InterpretServerOrder(conn, so); } } catch (EndOfStreamException) { } catch (NotImplementedException) { } }
void InterpretServerOrder(Connection conn, ServerOrder so) { if (!conn.Validated) { if (so.Name == "HandshakeResponse") { ValidateClient(conn, so.Data); } else { DropClient(conn); } return; } switch (so.Name) { case "Command": { var handleBy = serverTraits.WithInterface <IInterpretCommand>().FirstOrDefault(t => t.InterpretCommand(this, conn, GetClient(conn), so.Data)); if (handleBy == null) { SendOrderTo(conn, "Message", "Unknown server command : {0}".F(so.Data)); } break; } case "Pong": { long pingSent; if (!Exts.TryParseInt64Invariant(so.Data, out pingSent)) { break; } var client = GetClient(conn); if (client == null) { return; } var pingFromClient = LobbyInfo.PingFromClient(client); if (pingFromClient == null) { return; } var history = pingFromClient.LatencyHistory.ToList(); history.Add(WarGame.RunTime - pingSent); if (history.Count > 5) { history.RemoveRange(0, history.Count - 5); } pingFromClient.Latency = history.Sum() / history.Count; pingFromClient.LatencyJitter = (history.Max() - history.Min()) / 2; pingFromClient.LatencyHistory = history.ToArray(); SyncClientPing(); break; } } }