private void PresenceReceiveLoop() { IPEndPoint remoteIpEndPoint; PhysicalAddressSerializable remoteMac; TTInstruction instruction; List <byte> data; while (true) { remoteIpEndPoint = null; remoteMac = null; instruction = TTInstruction.Empty; data = null; if (TTNet.UnpackBuffer(clientUDP.Receive(ref remoteIpEndPoint), ref remoteMac, ref instruction, ref data)) { //OnRecordableEvent($"Received presence from: {remoteIpEndPoint.Address.ToString()}", Console.ConsoleMessageType.Common); // Skip if from self if (remoteIpEndPoint.Address.ToString() == IPAddress.ToString()) { continue; } // Add or update device in saved list if (instruction == TTInstruction.Discovery_Hello || instruction == TTInstruction.Discovery_Present) { if (!Enum.IsDefined(typeof(DeviceType), (int)data[0])) { continue; } DeviceType deviceType = (DeviceType)data[0]; string deviceName = Encoding.UTF8.GetString(data.GetRange(1, data.Count - 1).ToArray()); Application.Current.Dispatcher.Invoke(() => { Settings.SettingsData.AddUpdateDevice(remoteMac, deviceName, remoteIpEndPoint.Address, deviceType); }); } // Keep track of online status switch (instruction) { case TTInstruction.Discovery_Hello: OnlineDevices.SetOnline(remoteMac); PresenceSend(null); break; case TTInstruction.Discovery_Present: OnlineDevices.SetOnline(remoteMac); break; case TTInstruction.Discovery_Bye: OnlineDevices.SetOffline(remoteMac); break; } } } }
private void PresenceTimeoutTick(object status) { OnlineDevices.TimeoutDevices(); }