public DataPacket Read(byte[] data) { string dataJSON = Encoding.UTF8.GetString(data).Replace(@"\""", """).Replace(@"<", "<").Replace(@"<", ">"); string packetName = this.GetName(dataJSON); if (!string.IsNullOrEmpty(packetName)) { PacketType packet = PacketFactory.Get(packetName); if (packet != null) { DataPacket dataPacket = new DataPacket(packet); foreach (PacketField field in packet.Fields) { string value = string.Empty; if (field.DataType != null) { IPacketDataTypeParser parser = field.DataType.GetParser(this.name); if (parser != null) { switch (field.DataType.Name) { case "bool": value = this.GetValueBool(dataJSON, field.Name); break; case "byte": case "sbyte": case "short": case "ushort": case "int": case "uint": case "long": case "ulong": case "float": case "double": case "timespan": value = this.GetValueNumber(dataJSON, field.Name); break; case "string": value = this.GetValueString(dataJSON, field.Name); break; default: Log.Add("unknown data type: " + field.DataType.Name); break; } if (!string.IsNullOrEmpty(value)) { dataPacket[field] = field.DataType.GetParser(this.name).Read(value); } } else { Log.Add("no data type (" + field.DataType.Name + ") parser for " + this.name + " protocol"); } } else { Log.Add("unknown data type, field: " + field.Name); } } return(dataPacket); } else { Log.Add("unknown packet: " + packetName); return(null); } } else { Log.Add("wrong packet (no type)"); return(null); } }