public void DeviceLocator_SearchAsync_HandlesConcurrentDispose() { using (var deviceLocator = new Rssdp.SsdpDeviceLocator()) { System.Threading.ThreadPool.QueueUserWorkItem((reserved) => { System.Threading.Thread.Sleep(50); deviceLocator.Dispose(); }); var t = deviceLocator.SearchAsync(); AggregateException exception = null; try { t.Wait(); } catch (AggregateException aex) { exception = aex; } Assert.AreNotEqual(null, exception); Assert.AreEqual(1, exception.InnerExceptions.Count); Assert.AreEqual(typeof(System.ObjectDisposedException), exception.InnerExceptions.First().GetType()); } }
public async Task StartDiscoveringAsync() { var runningTask = Task.Run(async () => { await Task.Delay(TimeSpan.MaxValue, _cancellationToken.Token).ContinueWith(_ => { }); }); await Task.Delay(TimeSpan.FromSeconds(5), _cancellationToken.Token).ContinueWith(_ => { }); using (var deviceLocator = new SsdpDeviceLocator()) { deviceLocator.StartListeningForNotifications(); deviceLocator.DeviceAvailable += async (s, e) => { await UpnpDeviceFound(e.DiscoveredDevice); }; while (!_cancellationToken.IsCancellationRequested) { Task<IEnumerable<DiscoveredSsdpDevice>> searchTask; try { searchTask = deviceLocator.SearchAsync("upnp:rootdevice", TimeSpan.FromSeconds(10)); await Task.WhenAny(runningTask, searchTask); } catch (Exception e) { _log.Error(e.Message, e); continue; } if (_cancellationToken.IsCancellationRequested) { break; } var devices = searchTask.Result; if (devices == null) { continue; } foreach (var device in devices) { await UpnpDeviceFound(device); } await Task.Delay(TimeSpan.FromSeconds(60), _cancellationToken.Token).ContinueWith(_ => { }); } } }
// Call this method from somewhere in your code to start the search. public void BeginSearch() { _DeviceLocator = new SsdpDeviceLocator(); // (Optional) Set the filter so we only see notifications for devices we care about // (can be any search target value i.e device type, uuid value etc - any value that appears in the // DiscoverdSsdpDevice.NotificationType property or that is used with the searchTarget parameter of the Search method). //_DeviceLocator.NotificationFilter = "upnp:rootdevice"; // Connect our event handler so we process devices as they are found _DeviceLocator.DeviceAvailable += deviceLocator_DeviceAvailable; _DeviceLocator.DeviceUnavailable += _DeviceLocator_DeviceUnavailable; // Perform a search so we don't have to wait for devices to broadcast notifications // again to get any results right away (notifications are broadcast periodically). StartAsyncSearch(); }
public async Task<ObservableCollection<Chromecast>> LocateDevicesAsync() { using (var deviceLocator = new SsdpDeviceLocator()) { var foundDevices = await deviceLocator.SearchAsync("urn:dial-multiscreen-org:device:dial:1", TimeSpan.FromMilliseconds(5000)); foreach (var foundDevice in foundDevices) { var fullDevice = await foundDevice.GetDeviceInfo(); Uri myUri; Uri.TryCreate("https://" + foundDevice.DescriptionLocation.Host, UriKind.Absolute, out myUri); var chromecast = new Chromecast { DeviceUri = myUri, FriendlyName = fullDevice.FriendlyName }; DiscoveredDevices.Add(chromecast); } } return DiscoveredDevices; }