Beispiel #1
0
        /// <summary>
        /// Get all queued ARGUS TV events for your client. Call this every X seconds to get informed at a regular interval about what happened.
        /// </summary>
        /// <param name="uniqueClientId">The unique ID (e.g. your DNS hostname combined with a constant GUID) to identify your client.</param>
        /// <param name="cancellationToken">The cancellation token to potentially abort the request (or CancellationToken.None).</param>
        /// <param name="timeoutSeconds">The maximum timeout of the request (default is 5 minutes).</param>
        /// <returns>Zero or more service events, or null in case your subscription has expired.</returns>
        public async Task <List <ServiceEvent> > GetServiceEvents(string uniqueClientId, CancellationToken cancellationToken, int timeoutSeconds = 300)
        {
            var request = NewRequest(HttpMethod.Get, "ServiceEvents/{0}/{1}", uniqueClientId, timeoutSeconds);

            // By default return an empty list (e.g. in case of a timeout or abort), the client will simply need to call this again.
            GetServiceEventsResult result = new GetServiceEventsResult()
            {
                Events = new List <ServiceEvent>()
            };

#if DOTNET4
            using (var timeoutCancellationSource = new CancellationTokenSource())
#else
            using (var timeoutCancellationSource = new CancellationTokenSource(TimeSpan.FromSeconds(Math.Max(timeoutSeconds, 30))))
#endif
                using (var linkedCancellationSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, timeoutCancellationSource.Token))
                {
#if DOTNET4
                    Task.Factory.StartNew(() =>
                    {
                        cancellationToken.WaitHandle.WaitOne(TimeSpan.FromSeconds(Math.Max(timeoutSeconds, 30)));
                        timeoutCancellationSource.Cancel();
                    }).ConfigureAwait(false);
#endif

                    try
                    {
                        using (var response = await _listenerClient.SendAsync(request, linkedCancellationSource.Token))
                        {
                            result = await DeserializeResponseContentAsync <GetServiceEventsResult>(response);
                        }
                    }
                    catch (AggregateException ex)
                    {
                        if (IsConnectionError(ex.InnerException))
                        {
                            throw new ArgusTVNotFoundException(ex.InnerException.InnerException.Message);
                        }
                        // Check if we're dealing with either a timeout or an explicit cancellation (same exception in both cases).
                        if (!(ex.InnerException is TaskCanceledException))
                        {
                            throw;
                        }
                    }
                }

            if (result == null ||
                result.Expired)
            {
                return(null);
            }
            return(result.Events);
        }
        /// <summary>
        /// Get all queued ARGUS TV events for your client. Call this every X seconds to get informed at a regular interval about what happened.
        /// </summary>
        /// <param name="uniqueClientId">The unique ID (e.g. your DNS hostname combined with a constant GUID) to identify your client.</param>
        /// <param name="cancellationToken">The cancellation token to potentially abort the request (or CancellationToken.None).</param>
        /// <param name="timeoutSeconds">The maximum timeout of the request (default is 5 minutes).</param>
        /// <returns>Zero or more service events, or null in case your subscription has expired.</returns>
        public async Task<List<ServiceEvent>> GetServiceEvents(string uniqueClientId, CancellationToken cancellationToken, int timeoutSeconds = 300)
        {
            var request = NewRequest(HttpMethod.Get, "ServiceEvents/{0}/{1}", uniqueClientId, timeoutSeconds);

            // By default return an empty list (e.g. in case of a timeout or abort), the client will simply need to call this again.
            GetServiceEventsResult result = new GetServiceEventsResult()
            {
                Events = new List<ServiceEvent>()
            };

#if DOTNET4
            using (var timeoutCancellationSource = new CancellationTokenSource())
#else
            using (var timeoutCancellationSource = new CancellationTokenSource(TimeSpan.FromSeconds(Math.Max(timeoutSeconds, 30))))
#endif
            using (var linkedCancellationSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, timeoutCancellationSource.Token))
            {
#if DOTNET4
                Task.Factory.StartNew(() =>
                {
                    cancellationToken.WaitHandle.WaitOne(TimeSpan.FromSeconds(Math.Max(timeoutSeconds, 30)));
                    timeoutCancellationSource.Cancel();
                }).ConfigureAwait(false);
#endif

                try
                {
                    using (var response = await _listenerClient.SendAsync(request, linkedCancellationSource.Token))
                    {
                        result = await DeserializeResponseContentAsync<GetServiceEventsResult>(response);
                    }
                }
                catch (AggregateException ex)
                {
                    if (IsConnectionError(ex.InnerException))
                    {
                        throw new ArgusTVNotFoundException(ex.InnerException.InnerException.Message);
                    }
                    // Check if we're dealing with either a timeout or an explicit cancellation (same exception in both cases).
                    if (!(ex.InnerException is TaskCanceledException))
                    {
                        throw;
                    }
                }
            }

            if (result == null
                || result.Expired)
            {
                return null;
            }
            return result.Events;
        }