Пример #1
0
 internal MediaStream(MediaStreamManager manager, WebFile file, int id, bool linger)
 {
     this.id = id;
     this.file = file;
     this.manager = manager;
     this.currentReadOperationWaitHandle = new AutoResetEvent(false);
     manager.NewDataAvailable += manager_NewDataAvailable;
     this.linger = linger;
 }
Пример #2
0
 internal MediaStream(MediaStreamManager manager, WebFile file, int id, bool linger)
 {
     this.id      = id;
     this.file    = file;
     this.manager = manager;
     this.currentReadOperationWaitHandle = new AutoResetEvent(false);
     manager.NewDataAvailable           += manager_NewDataAvailable;
     this.linger = linger;
 }
Пример #3
0
        protected override void Dispose(bool disposing)
        {
            if (disposing && manager != null && prebuiltException == null)
            {
                if (Interlocked.Exchange(ref disposed, 1) == 0)
                {
                    manager.NotifyConsumerRemoved(id, linger);
                    manager.NewDataAvailable -= manager_NewDataAvailable;
                    readNotification.Set();
#if NET35
                    readNotification.Close();
#else
                    readNotification.Dispose();
#endif
                }
                manager = null;
            }
            base.Dispose(disposing);
        }
Пример #4
0
        protected override void Dispose(bool disposing)
        {

            if (disposing && manager != null && prebuiltException == null)
            {
                if (Interlocked.Exchange(ref disposed, 1) == 0)
                {
                    manager.NotifyConsumerRemoved(id, linger);
                    manager.NewDataAvailable -= manager_NewDataAvailable;
                    readNotification.Set();
#if NET35
                    readNotification.Close();
#else
                    readNotification.Dispose();
#endif
                }
                manager = null;
            }
            base.Dispose(disposing);
        }
Пример #5
0
        public virtual void ResetError()
        {
            manager = null;
#if DESKTOP
            prefetchingTask = null;
            var z = new LazyUri(Url);
            var cache = Caching.GetFileCachePath(z);
            if (cache != null)
            {
                Caching.DeleteFileCacheAsync(z).FireAndForget();
            }
#endif
            OnChanged();
        }
Пример #6
0
        internal async Task<Stream> OpenStreamAsync(bool synchronous, bool skipCache = false, bool linger = true)
        {


            var url = new LazyUri(Url);
            await Utils.CheckLocalFileAccessAsync(url);

            var mgr = this.manager;
            Func<long, Task<HttpResponseMessage>> createStream = null;

#if !STANDALONE && DESKTOP
            if (Caching.AzureApi != null && (mgr == null || !mgr.IsAlive) && !skipCache)
            {
                var container = Caching.GetAzureContainer(url);
                HashSet<string> files = null;
                if (synchronous)
                {
                    ObjectManager.SynchronizationContext.Send(async () =>
                    {
                        files = await Caching.GetAzureCachedFiles(container);
                    });
                }
                else
                {
                    await ObjectManager.SynchronizationContext.SendAsync(async () =>
                    {
                        files = await Caching.GetAzureCachedFiles(container);
                    });

                }
                var name = Caching.GetFileCachePath(url);
                if (files.Contains(name))
                {
                    createStream = offset => Caching.GetAzureResponseAsync(container, name, offset, this);
                }
            } else 
#endif
             if (
#if !STANDALONE && DESKTOP
                Caching.AzureApi == null && 
#endif
                !skipCache)
            {
#if DESKTOP
                var cache = Caching.GetFileCachePath(url);

                if (File.Exists(cache))
                {
                    var str = new FileStream(cache, FileMode.Open, FileAccess.Read, FileShare.Read | FileShare.Delete);
                    if (str.Length == 0)
                    {
                        var errfile = Path.ChangeExtension(cache, ".err");
                        if (File.Exists(errfile))
                        {
                            str.Dispose();
                            var errlines = File.ReadAllText(errfile);
                            return new MediaStream(MediaStream.ExceptionFromCachedResponse(errlines), this);
                        }
                    }
                    Sanity.AssertFastReadByte(str);
                    return str;
                }
#endif
            }

            lock (this)
            {
                if (manager == null)
                    manager = new MediaStreamManager(createStream ?? GetResponseAsync, true);

                var stream = manager.TryCreateStream(this, 0, linger);
                if (stream == null)
                {
                    manager = new MediaStreamManager(createStream ?? GetResponseAsync, true);
                    stream = manager.TryCreateStream(this, 0, linger);
                    Sanity.Assert(stream != null);
                }
                Sanity.AssertFastReadByte(stream);
                return stream;
            }

        }
Пример #7
0
        internal void SaveResponseInfo(HttpResponseMessage partialDownload, bool continueDownload)
        {
            if (partialDownload != null)
            {
#if NET35
                var len = partialDownload.ContentLength == -1 ? null : (long?)partialDownload.ContentLength;
#else
                var len = partialDownload.Content.Headers.ContentLength;
#endif
                if (len != null) Size = new FileSize(len.Value);

// HACK: ignore content disposition in .net 35
#if !NET35
                var contentDisposition = partialDownload.Content.Headers.ContentDisposition;
                if (contentDisposition != null) contentDispositionFileName = contentDisposition.FileName;
#endif

                contentTypeExtension = HttpUtils.GetFileExtensionFromMime(
#if NET35
                    HttpUtils.GetMimeFromContentType(partialDownload.Headers["Content-Type"])
#else
                    partialDownload.Content.Headers.ContentType?.MediaType
#endif
                );

                if (continueDownload && (manager == null || !manager.IsAlive))
                {
                    this.partialDownload = partialDownload;
                    manager = new MediaStreamManager(GetResponseAsync, true);
                }
                else
                {
                    partialDownload.AbortAndDispose();
                }
                OnChanged();
            }
        }