private void Listen() { while (IsListening) { if (Host.Available > 0) { //12 is the statuscode for the name and is 14 bytes long, with 2 weird bytes at the start byte[] Data = Host.Receive(ref EndPoint); Data = Data.Skip(236) .SkipWhile(b => b != 12) .Skip(2).Take(12) .ToArray(); string name = System.Text.Encoding.UTF8.GetString(Data) .Split('\n')[0] .SkipLast(1) .AsString(); if (IngoreNames.Exists(s => s == name)) { continue; } var existingDevice = Devices.FirstOrDefault(d => d.Name == name); if (existingDevice == null || existingDevice.LastRequest.OlderThan(DhcpTimeout)) { if (existingDevice == null) { existingDevice = new DhcpDevice() { Name = name, LastRequest = DateTime.Now }; Devices.Add(existingDevice); } NewDhcpConnection?.Invoke(new DhcpEventArgs(this, existingDevice)); } else { existingDevice.LastRequest = DateTime.Now; } } else { Thread.Sleep(100); } } }
public DhcpEventArgs(object sender, DhcpDevice device) { this.Sender = sender; this.Device = device; }