From d218dbd2a1f6d70d3714d84bebc2fb00020151c1 Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Sun, 5 Feb 2017 15:44:08 -0500 Subject: add cancellation to socket methods --- RSSDP/SsdpDeviceLocatorBase.cs | 48 +++++++++--------------------------------- 1 file changed, 10 insertions(+), 38 deletions(-) (limited to 'RSSDP/SsdpDeviceLocatorBase.cs') diff --git a/RSSDP/SsdpDeviceLocatorBase.cs b/RSSDP/SsdpDeviceLocatorBase.cs index 60a792425..4f5eae616 100644 --- a/RSSDP/SsdpDeviceLocatorBase.cs +++ b/RSSDP/SsdpDeviceLocatorBase.cs @@ -16,7 +16,7 @@ namespace Rssdp.Infrastructure /// /// Allows you to search the network for a particular device, device types, or UPnP service types. Also listenings for broadcast notifications of device availability and raises events to indicate changes in status. /// - public abstract class SsdpDeviceLocatorBase : DisposableManagedObjectBase, ISsdpDeviceLocator + public abstract class SsdpDeviceLocatorBase : DisposableManagedObjectBase { #region Fields & Constants @@ -96,9 +96,9 @@ namespace Rssdp.Infrastructure /// Performs a search for all devices using the default search timeout. /// /// A task whose result is an of instances, representing all found devices. - public Task> SearchAsync() + public Task> SearchAsync(CancellationToken cancellationToken) { - return SearchAsync(SsdpConstants.SsdpDiscoverAllSTHeader, DefaultSearchWaitTime); + return SearchAsync(SsdpConstants.SsdpDiscoverAllSTHeader, DefaultSearchWaitTime, cancellationToken); } /// @@ -114,7 +114,7 @@ namespace Rssdp.Infrastructure /// A task whose result is an of instances, representing all found devices. public Task> SearchAsync(string searchTarget) { - return SearchAsync(searchTarget, DefaultSearchWaitTime); + return SearchAsync(searchTarget, DefaultSearchWaitTime, CancellationToken.None); } /// @@ -124,27 +124,10 @@ namespace Rssdp.Infrastructure /// A task whose result is an of instances, representing all found devices. public Task> SearchAsync(TimeSpan searchWaitTime) { - return SearchAsync(SsdpConstants.SsdpDiscoverAllSTHeader, searchWaitTime); + return SearchAsync(SsdpConstants.SsdpDiscoverAllSTHeader, searchWaitTime, CancellationToken.None); } - /// - /// Performs a search for the specified search target (criteria) and search timeout. - /// - /// The criteria for the search. Value can be; - /// - /// Root devicesupnp:rootdevice - /// Specific device by UUIDuuid:<device uuid> - /// Device typeA device namespace and type in format of urn:<device namespace>:device:<device type>:<device version> i.e urn:schemas-upnp-org:device:Basic:1 - /// Service typeA service namespace and type in format of urn:<service namespace>:service:<servicetype>:<service version> i.e urn:my-namespace:service:MyCustomService:1 - /// - /// - /// The amount of time to wait for network responses to the search request. Longer values will likely return more devices, but increase search time. A value between 1 and 5 seconds is recommended by the UPnP 1.1 specification, this method requires the value be greater 1 second if it is not zero. Specify TimeSpan.Zero to return only devices already in the cache. - /// - /// By design RSSDP does not support 'publishing services' as it is intended for use with non-standard UPnP devices that don't publish UPnP style services. However, it is still possible to use RSSDP to search for devices implemetning these services if you know the service type. - /// - /// A task whose result is an of instances, representing all found devices. - [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1804:RemoveUnusedLocals", MessageId = "expireTask", Justification = "Task is not actually required, but capturing to local variable suppresses compiler warning")] - public async Task> SearchAsync(string searchTarget, TimeSpan searchWaitTime) + public async Task> SearchAsync(string searchTarget, TimeSpan searchWaitTime, CancellationToken cancellationToken) { if (searchTarget == null) throw new ArgumentNullException("searchTarget"); if (searchTarget.Length == 0) throw new ArgumentException("searchTarget cannot be an empty string.", "searchTarget"); @@ -158,7 +141,7 @@ namespace Rssdp.Infrastructure // If searchWaitTime == 0 then we are only going to report unexpired cached items, not actually do a search. if (searchWaitTime > TimeSpan.Zero) - await BroadcastDiscoverMessage(searchTarget, SearchTimeToMXValue(searchWaitTime)).ConfigureAwait(false); + await BroadcastDiscoverMessage(searchTarget, SearchTimeToMXValue(searchWaitTime), cancellationToken).ConfigureAwait(false); lock (_SearchResultsSynchroniser) { @@ -169,7 +152,7 @@ namespace Rssdp.Infrastructure } if (searchWaitTime != TimeSpan.Zero) - await Task.Delay(searchWaitTime).ConfigureAwait(false); + await Task.Delay(searchWaitTime, cancellationToken).ConfigureAwait(false); IEnumerable retVal = null; @@ -270,17 +253,6 @@ namespace Rssdp.Infrastructure #region Public Properties - /// - /// Returns a boolean indicating whether or not a search is currently in progress. - /// - /// - /// Only one search can be performed at a time, per instance. - /// - public bool IsSearching - { - get { return _SearchResults != null; } - } - /// /// Sets or returns a string containing the filter for notifications. Notifications not matching the filter will not raise the or events. /// @@ -407,7 +379,7 @@ namespace Rssdp.Infrastructure #region Network Message Processing - private Task BroadcastDiscoverMessage(string serviceType, TimeSpan mxValue) + private Task BroadcastDiscoverMessage(string serviceType, TimeSpan mxValue, CancellationToken cancellationToken) { var values = new Dictionary(StringComparer.OrdinalIgnoreCase); @@ -427,7 +399,7 @@ namespace Rssdp.Infrastructure var message = SsdpHelper.BuildMessage(header, values); - return _CommunicationsServer.SendMulticastMessage(message); + return _CommunicationsServer.SendMulticastMessage(message, cancellationToken); } private void ProcessSearchResponseMessage(HttpResponseMessage message, IpAddressInfo localIpAddress) -- cgit v1.2.3