Beispiel #1
0
        /// <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);
        }
Beispiel #2
0
        /// <summary>
        /// Stops the current asynchronous search.
        /// </summary>
        public void Stop()
        {
            if (Logging.Enabled)
            {
                Logging.Log(
                    this,
                    string.Format(
                        "Async discovery stopping with - SearchURI:'{0}', ResolveNetworkInterface:'{1}', AddressFamily:'{2}'",
                        msSearchURI, mbResolveNetworkInterface, mafAddressFamily.ToString()), 1);
            }

            try
            {
                // If we are searching
                if (miFindHandle != 0)
                {
                    // Cancel the search
                    if (Logging.Enabled)
                    {
                        Logging.Log(this, "Cancelling async find");
                    }
                    mfFinder.CancelAsyncFind(miFindHandle);

                    // Ignore the callback
                    if (Logging.Enabled)
                    {
                        Logging.Log(this, "Notifying callback to ignore");
                    }
                    mdfcCallback.Ignore();

                    // Clear the callback
                    mdfcCallback = null;

                    // Reset the find handle
                    miFindHandle = 0;
                }
            }
            finally
            {
                if (Logging.Enabled)
                {
                    Logging.Log(this, "Finished async discovery stop", -1);
                }
            }
        }
 //method to stop asynchronous search
 public void StopSearchingForDevices()
 {
     DeviceFinder.CancelAsyncFind(MediaServers);
 }