public void SetRecord(DnsSection section, int index, IDnsRecord record) { CheckQuestion(section, record); object records = SectionAt(section); if (records == null) { throw new IndexOutOfRangeException($"index: {index} (expected: none)"); } if (records is IDnsRecord) { if (index == 0) { SetSection(section, record); } else { throw new IndexOutOfRangeException($"index: {index} (expected: 0)"); } } List <IDnsRecord> recordList = (List <IDnsRecord>)records; recordList[index] = record; }
public void AddRecord(DnsSection section, IDnsRecord record) { CheckQuestion(section, record); object records = SectionAt(section); if (records == null) { SetSection(section, record); return; } List <IDnsRecord> recordList; if (records is IDnsRecord) { recordList = new List <IDnsRecord>(2); recordList.Add((IDnsRecord)records); recordList.Add(record); SetSection(section, recordList); return; } recordList = (List <IDnsRecord>)records; recordList.Add(record); }
private static void CheckQuestion(DnsSection section, IDnsRecord record) { if (section == SECTION_QUESTION && record != null && !(record is IDnsQuestion)) { throw new ArgumentException($"record: {record} (expected: DnsQuestion)"); } }
private void EncodeRecords(IDnsResponse response, DnsSection section, IByteBuffer buffer) { int count = response.Count(section); for (int i = 0; i < count; i++) { recordEncoder.EncodeRecord(response.GetRecord <IDnsRecord>(section, i), buffer); } }
private void EncodeRecords(IDnsQuery query, DnsSection section, IByteBuffer buffer) { int count = query.Count(section); for (int i = 0; i < count; i++) { recordEncoder.EncodeRecord(query.GetRecord <IDnsRecord>(section, i), buffer); } }
private void DecodeRecords(IDnsQuery query, DnsSection section, IByteBuffer buffer, int count) { for (int i = count; i > 0; i--) { IDnsRecord r = recordDecoder.DecodeRecord(buffer); if (r == null) { break; } query.AddRecord(section, r); } }
private void DecodeRecords(IDnsResponse response, DnsSection section, IByteBuffer buffer, int count) { for (int i = count - 1; i > 0; i--) { IDnsRecord r = recordDecoder.DecodeRecord(buffer); if (r == null) { break; } response.AddRecord(section, r); } }
public int Count(DnsSection section) { object records = SectionAt(section); if (records == null) { return(0); } if (records is IDnsRecord) { return(1); } List <IDnsRecord> recordList = (List <IDnsRecord>)records; return(recordList.Count); }
public void AddRecord(DnsSection section, int index, IDnsRecord record) { CheckQuestion(section, record); object records = SectionAt(section); if (records == null) { if (index != 0) { throw new IndexOutOfRangeException($"index: {index} (expected: 0)"); } SetSection(section, record); return; } List <IDnsRecord> recordList; if (records is IDnsRecord) { if (index == 0) { recordList = new List <IDnsRecord>(); recordList.Add(record); recordList.Add((IDnsRecord)records); } else if (index == 1) { recordList = new List <IDnsRecord>(); recordList.Add((IDnsRecord)records); recordList.Add(record); } else { throw new IndexOutOfRangeException($"index: {index} (expected: 0 or 1)"); } SetSection(section, recordList); return; } recordList = (List <IDnsRecord>)records; recordList[index] = record; }
private object SectionAt(DnsSection section) { switch (section) { case DnsSection.QUESTION: return(questions); case DnsSection.ANSWER: return(answers); case DnsSection.AUTHORITY: return(authorities); case DnsSection.ADDITIONAL: return(additionals); default: return(null); } }
private void SetSection(DnsSection section, object value) { switch (section) { case DnsSection.QUESTION: questions = value; break; case DnsSection.ANSWER: answers = value; break; case DnsSection.AUTHORITY: authorities = value; break; case DnsSection.ADDITIONAL: additionals = value; break; } }
public void Clear(DnsSection section) { object recordOrList = SectionAt(section); SetSection(section, null); if (recordOrList is IReferenceCounted) { ((IReferenceCounted)recordOrList).Release(); } else if (recordOrList is IList) { List <IDnsRecord> list = (List <IDnsRecord>)recordOrList; if (list.Count == 0) { foreach (var r in list) { ReferenceCountUtil.Release(r); } } } }
public TRecord GetRecord <TRecord>(DnsSection section) where TRecord : IDnsRecord { object records = SectionAt(section); if (records == null) { return(default(TRecord)); } if (records is IDnsRecord) { return((TRecord)records); } List <IDnsRecord> recordList = (List <IDnsRecord>)records; if (recordList.Count == 0) { return(default(TRecord)); } return((TRecord)recordList[0]); }
public TRecord GetRecord <TRecord>(DnsSection section, int index) where TRecord : IDnsRecord { object records = SectionAt(section); if (records == null) { throw new IndexOutOfRangeException($"index: {index} (expected: none)"); } if (records is IDnsRecord) { if (index == 0) { return((TRecord)records); } throw new IndexOutOfRangeException($"index: {index} (expected: 0)"); } List <IDnsRecord> recordList = (List <IDnsRecord>)records; return((TRecord)recordList[index]); }
public void RemoveRecord(DnsSection section, int index) { object records = SectionAt(section); if (records == null) { throw new IndexOutOfRangeException($"index: {index} (expected: none)"); } if (records is IDnsRecord) { if (index != 0) { throw new IndexOutOfRangeException($"index: {index} (expected: 0)"); } SetSection(section, null); } List <IDnsRecord> recordList = (List <IDnsRecord>)records; recordList.RemoveAt(index); }
private static StringBuilder AppendRecords(this StringBuilder builder, IDnsMessage message, DnsSection section) { int count = message.Count(section); for (int i = 0; i < count; i++) { builder.Append(Environment.NewLine) .Append('\t') .Append(message.GetRecord <IDnsRecord>(section, i)); } return(builder); }
public void SetRecord(DnsSection section, IDnsRecord record) { Clear(section); SetSection(section, record); }