public static Parser CreateParser(Packet packet) { Type type; if (!Parsers.TryGetValue((int)packet.Code, out type)) { return UnknownParser; } return (Parser)Activator.CreateInstance(type, packet); }
public static string HexLike(Packet packet) { var length = packet.Data.Length; var dir = (packet.Direction == Direction.Client) ? "C->S" : "S->C"; var result = new StringBuilder(); result.AppendFormat("Packet {0}, {1} (0x{2:X4}), len {3}", dir, packet.Code, (ushort)packet.Code, length); result.AppendLine(); if (length == 0) { result.AppendLine("0000: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- : ................"); } else { var offset = 0; for (var i = 0; i < length; i += 0x10) { var bytes = new StringBuilder(); var chars = new StringBuilder(); for (var j = 0; j < 0x10; ++j) { if (offset < length) { int c = packet.Data[offset]; offset++; bytes.AppendFormat("{0,-3:X2}", c); chars.Append((c >= 0x20 && c < 0x80) ? (char)c : '.'); } else { bytes.Append("-- "); chars.Append('.'); } } result.AppendLine(i.ToString("X4") + ": " + bytes + ": " + chars); } } result.AppendLine(); return result.ToString(); }
protected Parser(Packet packet) { Packet = packet; }