internal void OnServiceAdded(ServiceInfo service) { var announcement = new ServiceAnnouncement { Hostname = service.HostName.Labels[0], Domain = service.HostName.SubName(1).ToString(), Addresses = service.Addresses, Instance = service.Name.Labels[0], NetworkInterface = service.NetworkInterface, Port = (ushort)service.Port, Txt = service.Txt, Type = service.Name.SubName(1, 2).ToString() }; _serviceAnnouncements.Add(Tuple.Create(service.NetworkInterface.Id, service.Name), announcement); SynchronizationContextPost(o => { lock (_services) { _services.Add(announcement); } if (ServiceAdded != null) { ServiceAdded(this, new ServiceAnnouncementEventArgs(announcement)); } }); }
internal void OnServiceChanged(ServiceInfo service) { ServiceAnnouncement announcement = _serviceAnnouncements[Tuple.Create(service.NetworkInterface.Id, service.Name)]; var tmpAnnouncement = new ServiceAnnouncement() { Hostname = service.HostName.Labels[0], Domain = service.HostName.SubName(1).ToString(), Addresses = service.Addresses, Instance = service.Name.Labels[0], NetworkInterface = service.NetworkInterface, Port = (ushort)service.Port, Txt = service.Txt, Type = service.Name.SubName(1, 2).ToString() }; SynchronizationContextPost(o => { announcement.Hostname = tmpAnnouncement.Hostname; announcement.Domain = tmpAnnouncement.Domain; announcement.Addresses = tmpAnnouncement.Addresses; announcement.Instance = tmpAnnouncement.Instance; announcement.NetworkInterface = tmpAnnouncement.NetworkInterface; announcement.Port = tmpAnnouncement.Port; announcement.Txt = tmpAnnouncement.Txt; announcement.Type = tmpAnnouncement.Type; if (ServiceChanged != null) { ServiceChanged(this, new ServiceAnnouncementEventArgs(announcement)); } }); }
internal void OnServiceRemoved(ServiceInfo service) { var key = Tuple.Create(service.NetworkInterface.Id, service.Name); ServiceAnnouncement announcement = _serviceAnnouncements[key]; _serviceAnnouncements.Remove(key); SynchronizationContextPost(o => { announcement.IsRemoved = true; lock (_services) { _services.Remove(announcement); } if (ServiceRemoved != null) { ServiceRemoved(this, new ServiceAnnouncementEventArgs(announcement)); } }); }
private DeviceDiscoveryReportItem ParseServiceAnnouncement(ServiceAnnouncement announcement) { IPAddress localInterface = IPAddress.Any; // Retrieve local interface address // TODO: This will need some IPv6 love at some point if (announcement.NetworkInterface.Information.OperationalStatus == System.Net.NetworkInformation.OperationalStatus.Up) { var ipv4Addresses = announcement.NetworkInterface.Information.GetIPProperties().UnicastAddresses.Where((ad) => ad.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork); localInterface = ipv4Addresses.FirstOrDefault().Address; } // Retrieve version string var versionTempString = announcement.Txt.Where((kvp) => kvp.Contains("ve=")).FirstOrDefault(); int versionNumber = 2; if (string.IsNullOrEmpty(versionTempString) == false) { versionNumber = int.Parse(versionTempString.Split('=')[1]); } // Retrieve device ID var idTempString = announcement.Txt.Where((kvp) => kvp.Contains("id=")).FirstOrDefault(); string Id = ""; if (string.IsNullOrEmpty(idTempString) == false) { Id = idTempString.Split('=')[1]; } else { // Log.Error(string.Format("Unable to get ID of device {0}", e.Announcement.Hostname)); return null; } // Optional: Verify that it's a Chromecast device var tempDeviceType = announcement.Txt.Where((kvp) => kvp.Contains("md=")).FirstOrDefault(); var deviceType = DeviceType.Generic; if (string.IsNullOrEmpty(tempDeviceType) == false && tempDeviceType.Split('=')[1] == "Chromecast") { deviceType = DeviceType.Chromecast; } else { deviceType = DeviceType.Generic; } // Build baseUrl // This might not be right as there URI scheme could be something else. var scheme = "http"; var address = announcement.Addresses.First(); var port = announcement.Port; var uri = new Uri(string.Format("{0}://{1}:{2}", scheme, address, port)); var report = new ChromecastV2DeviceDiscoveryReportItem() { DeviceType = deviceType, Version = versionNumber, Id = Id, Name = announcement.Hostname, EndPoint = new IPEndPoint(announcement.Addresses.First(), announcement.Port), Interface = localInterface, BaseUri = uri, }; return report; }
public ServiceAnnouncementEventArgs(ServiceAnnouncement announcement) { Announcement = announcement; }