public static Packet unPack(string s) { int a = s.Length - s.Replace("{", "").Replace("[", "").Length; int b = s.Length - s.Replace("}", "").Replace("]", "").Length; string checkS = s.Replace(Environment.NewLine, "").Trim(); if (a != b || !checkS.Substring(0, 1).Equals("{") || !checkS.Substring(checkS.Length - 3, 1).Equals("}") || !checkS.Contains("flavour") || !checkS.Contains("key")) { Console.WriteLine("Packet: invalid string"); Console.WriteLine(s); Console.WriteLine("-- end print --"); Console.WriteLine(checkS.Substring(checkS.Length - 2, 1).ToCharArray()[0]); //Console.WriteLine(checkS.Substring(checkS.Length - 3, 1)); //Console.WriteLine("a: {0}, b: {1}", a, b); throw new InvalidDataException(); } Flavour f; PacketKey k = new PacketKey(); ComData d = new ComData(); JSONDecoder jd = new JSONDecoder(s); jd.beginItem(); string flav = jd.getField()[1].Trim(); f = (Flavour)Enum.Parse(typeof(Flavour), flav); jd.getField(); // key k.fillFromJSON(jd); if (f != Flavour.ECHO) { jd.getField(); // data d.fillFromJSON(jd); } jd.endItem(); return(new Packet(f, k, d)); }
public PacketKey(PacketKey other) { value = other.value; }
private Packet(Flavour f, PacketKey k, ComData d) : this(f, k) { data = d; }
private Packet(Flavour f, PacketKey k) { flavour = f; key = new PacketKey(k); }
public Packet(Flavour f) { flavour = f; key = new PacketKey(); }
public Packet(ComData d) { data = d; flavour = Flavour.DATA; key = new PacketKey(); }
private Packet(Packet p) { flavour = p.flavour; key = p.key; data = p.data; }