public void AddField(string id, object value) { if (id.Length != 4) { throw new ArgumentException("id length does not equal 4"); } var field = new GameResultField(); if (value is bool) { field.DataType = 2; field.Data = BitConverter.GetBytes((bool)value); } else if (value is int) { field.DataType = 5; field.Data = BitConverter.GetBytes(IPAddress.HostToNetworkOrder((int)value)); } else if (value is string) { field.DataType = 7; field.Data = Encoding.Default.GetBytes((string)value + "\0"); } else { throw new Exception("data type not supported yet"); } field.Id = id; AddField(field); }
public void AddField(GameResultField field) { if (field.Data == null || field.Id == null) { throw new ArgumentNullException(); } if (field.Id.Length != 4) { throw new ArgumentException("id length does not equal 4"); } if (fields.FindIndex(f => f.Id == field.Id) > -1) { throw new Exception(string.Format("Field already exists ({0})", field.Id)); } fields.Add(field); }
private void ParseFields(byte[] data) { using (var memoryStream = new MemoryStream(data)) using (var binaryReader = new BinaryReader(memoryStream)) { if (binaryReader.BaseStream.Length >= 4) { binaryReader.BaseStream.Position = 4; } while (binaryReader.BaseStream.Position < binaryReader.BaseStream.Length) { var field = new GameResultField(); field.Id = Encoding.Default.GetString(binaryReader.ReadBytes(4)); field.DataType = IPAddress.HostToNetworkOrder(binaryReader.ReadInt16()); short length = IPAddress.HostToNetworkOrder(binaryReader.ReadInt16()); field.Data = binaryReader.ReadBytes(length); while (binaryReader.BaseStream.Position % 4 != 0) { binaryReader.BaseStream.Position++; } fields.Add(field); } } }
public bool RemoveField(GameResultField field) { return(RemoveField(field.Id)); }