/// <summary> /// Performs a search for UPnP devices on all active network interfaces. /// </summary> /// <param name="searchTarget">The search target. If this is null, a search /// for all UPnP devices will be performed.</param> /// <param name="timeout">The timespan after which to cancel the search /// and return the results to the caller. If this is null, the method blocks /// until the search has been completed.</param> /// <returns>An enumerable collection of discovered UPnP devices.</returns> /// <exception cref="InvalidOperationException">An error occurred while /// performing an SSDP search-operation.</exception> /// <remarks> /// A full UPnP search can take 9 seconds or longer; /// /// Possible values for the searchTarget parameter include: /// * ssdp:all (searches for all devices and services) /// * ssdp:rootdevice (searches for root devices only) /// For details on all possible values for the searchTarget parameter, refer /// to the 'UPnP Device Architecture 1.1' document, page 33. /// </remarks> static IEnumerable <UPnPDevice> FindDevices(string searchTarget = null, TimeSpan?timeout = null) { UPnPDeviceFinder deviceFinder = new UPnPDeviceFinder(); DeviceFinderCallback dfCallback = new DeviceFinderCallback(); if (searchTarget == null) { searchTarget = "ssdp:all"; } int findHandle = deviceFinder.CreateAsyncFind(searchTarget, 0, dfCallback); if (findHandle == 0) { throw new InvalidOperationException("Asynchronous search operation could " + "not be created."); } // Start the asynchronous search. deviceFinder.StartAsyncFind(findHandle); if (timeout == null) { timeout = TimeSpan.FromMilliseconds(-1); } // Wait until the search has been completed or the specified timeout has // expired. if (!dfCallback.SearchCompleted.WaitOne(timeout.Value)) { deviceFinder.CancelAsyncFind(findHandle); } // The Devices property of the DeviceFinderCallback contains the UPnP devices // that were discovered. return(dfCallback.Devices); }
/// <summary> /// Form Initialisation /// </summary> public void Initialise() { frmMainWindow.Size = new Size(300, 360); frmMainWindow.Text = "UPnP Server Browser"; tvwServerList.Location = new Point(20, 12); tvwServerList.Size = new Size(260, 268); tvwServerList.Enabled = false; btnOk.Location = new Point(115, 285); btnOk.Text = "Ok"; btnOk.Click += new EventHandler(btnOk_Click); btnCancel.Location = new Point(200, 285); btnCancel.Text = "Cancel"; btnCancel.Click += new EventHandler(btnCancel_Click); frmMainWindow.Controls.Add(tvwServerList); frmMainWindow.Controls.Add(btnOk); frmMainWindow.Controls.Add(btnCancel); discovery.StartAsyncFind(discovery.CreateAsyncFind("urn:schemas-upnp-org:device:MediaServer:1", 0, call)); }
//method which starts searching for DLNA devices asynchronously public void StartSearchingForDevices() { MediaServers = DeviceFinder.CreateAsyncFind("urn:schemas-upnp-org:device:MediaServer:1", 0, DeviceFinderCallBack = new DLNADeviceFinderCallback()); DeviceFinder.StartAsyncFind(MediaServers); }
/// <summary> /// Starts an asynchronous search using current parameters if one isnt already in progress. /// </summary> /// <returns>True if a new asynchronous search was started, false otherwise.</returns> public bool Start() { if (Logging.Enabled) { Logging.Log( this, string.Format( "Async discovery starting with - SearchURI:'{0}', ResolveNetworkInterface:'{1}', AddressFamily:'{2}'", msSearchURI, mbResolveNetworkInterface, mafAddressFamily.ToString()), 1); } try { if (miFindHandle == 0) { // Set the address family if its available if (mfFinder is IUPnPAddressFamilyControl) { if (Logging.Enabled) { Logging.Log(this, "Setting address family"); } ((IUPnPAddressFamilyControl)mfFinder).SetAddressFamily((int)mafAddressFamily); } // If we want to resolve the network interface if (Logging.Enabled) { Logging.Log(this, "Creating callback"); } if (mbResolveNetworkInterface) { // Then use the with interface device finder callback mdfcCallback = new DeviceFinderCallbackWithInterface(this); } else { // Otherwise use the standard device finder callback mdfcCallback = new DeviceFinderCallback(this); } string lsSearchURI; // If search uri isnt specified if (string.IsNullOrEmpty(msSearchURI)) { // Search for all devices if (Logging.Enabled) { Logging.Log(this, "Search URI not specified using all devices"); } lsSearchURI = csAllDevices; } else { // Otherwise use search uri lsSearchURI = msSearchURI; } // Create the find handle if (Logging.Enabled) { Logging.Log(this, "Creating find handle"); } miFindHandle = mfFinder.CreateAsyncFind(lsSearchURI, 0, mdfcCallback); // Start the search if (miFindHandle != 0) { if (Logging.Enabled) { Logging.Log(this, "Starting async find"); } mfFinder.StartAsyncFind(miFindHandle); } else if (Logging.Enabled) { Logging.Log(this, "Creation of find handle failed"); } // Return true if the search was started return(miFindHandle != 0); } else { // Already searching, return false if (Logging.Enabled) { Logging.Log(this, "Discovery already in progress"); } return(false); } } finally { if (Logging.Enabled) { Logging.Log(this, "Finished async discovery start", -1); } } }