Example #1
0
        internal async Task PreloadAdAsync(IAdSource adSource, CancellationToken cancellationToken)
#endif
        {
            var handler = AdPayloadHandlers.Where(h => h.SupportedTypes.Contains(adSource.Type)).FirstOrDefault();

            if (handler == null)
            {
                throw new ArgumentException("No suitable handler was found to play this ad", "source");
            }

            // resolve the source
            if (adSource is IResolveableAdSource)
            {
                var resolveableAdSource = adSource as IResolveableAdSource;
#if SILVERLIGHT
                await resolveableAdSource.LoadPayload(cancellationToken);
#else
                await(resolveableAdSource.LoadPayload().AsTask(cancellationToken));
#endif
                cancellationToken.ThrowIfCancellationRequested();
            }
#if SILVERLIGHT
            await handler.PreloadAdAsync(adSource, cancellationToken);
#else
            await handler.PreloadAdAsync(adSource).AsTask(cancellationToken);
#endif
        }
Example #2
0
        public AdHandlerController()
        {
            var adPayloadHandlers = new ObservableCollection <IAdPayloadHandler>();

            adPayloadHandlers.CollectionChanged += AdPayloadHandlers_CollectionChanged;
            AdPayloadHandlers = adPayloadHandlers;
            // add the default ones that we already know about
            AdPayloadHandlers.Add(new VastAdPayloadHandler());
            AdPayloadHandlers.Add(new ClipAdPayloadHandler());
            AdPayloadHandlers.Add(new DocumentAdPayloadHandler());
            StartTimeout = TimeSpan.FromSeconds(8); // 8 second timeout by default.
        }
Example #3
0
        internal async Task PlayAdAsync(IAdSource adSource, CancellationToken cancellationToken, IProgress <AdStatus> progress)
#endif
        {
            try
            {
                if (ActiveHandler != null)
                {
                    if (!await ActiveHandler.CancelAd(false))
                    {
                        throw new Exception("Ad in progress and cannot be canceled.");
                    }
                }

                DateTime startTime = DateTime.Now;

                using (var timeoutTokenSource = new CancellationTokenSource())
                {
                    if (StartTimeout.HasValue)
                    {
                        timeoutTokenSource.CancelAfter(StartTimeout.Value);
                    }
                    var timeoutToken = timeoutTokenSource.Token;
                    using (var cancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, timeoutToken))
                    {
                        IsAdvertising = true;
                        var adContext = activeAdContext = new object();

                        progress.Report(AdStatus.Loading);

                        try
                        {
                            ActiveHandler = AdPayloadHandlers.Where(h => h.SupportedTypes.Contains(adSource.Type)).FirstOrDefault();
                            if (ActiveHandler == null)
                            {
                                throw new ArgumentException("No suitable handler was found to play this ad", "source");
                            }

                            // resolve the source
                            if (adSource is IResolveableAdSource)
                            {
                                var resolveableAdSource = adSource as IResolveableAdSource;
                                try
                                {
#if SILVERLIGHT
                                    await resolveableAdSource.LoadPayload(cancellationTokenSource.Token);
#else
                                    await resolveableAdSource.LoadPayload().AsTask(cancellationTokenSource.Token);
#endif
                                }
                                catch (OperationCanceledException)
                                {
                                    if (timeoutToken.IsCancellationRequested)
                                    {
                                        throw new TimeoutException();
                                    }
                                    else
                                    {
                                        throw;
                                    }
                                }
                            }

                            TimeSpan?timeRemaining = null;
                            if (StartTimeout.HasValue)
                            {
                                var timeElapsed = DateTime.Now.Subtract(startTime);
                                timeRemaining = StartTimeout.Value.Subtract(timeElapsed);
                            }

                            //var handlerProgress = new Progress<AdStatus>();
                            //handlerProgress.ProgressChanged += (s, e) => Progress_Handle(adContext, progress, e);
                            var handlerProgress = new Progress <AdStatus>(s => Progress_Handle(adContext, progress, s));
#if SILVERLIGHT && !WINDOWS_PHONE || WINDOWS_PHONE7
                            await ActiveHandler.PlayAdAsync(adSource, timeRemaining, cancellationToken, handlerProgress);

                            await TaskEx.Yield(); // let evertyhing complete before finishing
#elif WINDOWS_PHONE
                            await ActiveHandler.PlayAdAsync(adSource, timeRemaining, cancellationToken, handlerProgress);

                            await Task.Yield(); // let evertyhing complete before finishing
#else
                            await ActiveHandler.PlayAdAsync(adSource, timeRemaining).AsTask(cancellationToken, handlerProgress);

                            await Task.Yield(); // let evertyhing complete before finishing
#endif
                            // leave the progress handler alone, it reports on secondary task which could still be running.
                        }
                        catch
                        {
                            TeardownAd();
                            progress.Report(AdStatus.Unloaded);
                            throw;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                if (AdFailure != null)
                {
                    AdFailure(this, new AdFailureEventArgs(adSource, ex));
                }
                throw;
            }
        }