Inheritance: IRandomAccessStream
Beispiel #1
0
 private void CloseAndRemove(TorrentFileStream s)
 {
     lock (_locker)
     {
         Debug.WriteLine("Closing and removing: {0}", s.Path);
         if (_streams.Remove(s))
         {
             s.Dispose();
         }
     }
 }
        private void Add(TorrentFileStream stream)
        {
            lock (_locker)
            {
                Debug.WriteLine("Opening filestream: {0}", stream.Path);

                // If we have our maximum number of streams open, just dispose and dump the least recently used one
                if (_maxStreams != 0 && _streams.Count >= _streams.Capacity)
                {
                    Debug.WriteLine("We've reached capacity: {0}", _streams.Count);
                    var first = _streams.FirstOrDefault(p => p.TorrentFile.Priority != Priority.Immediate);
                    if (first != null)
                        CloseAndRemove(first);
                }
                _streams.Add(stream);
            }
        }
Beispiel #3
0
        private void Add(TorrentFileStream stream)
        {
            lock (_locker)
            {
                Debug.WriteLine("Opening filestream: {0}", stream.Path);

                // If we have our maximum number of streams open, just dispose and dump the least recently used one
                if (_maxStreams != 0 && _streams.Count >= _streams.Capacity)
                {
                    Debug.WriteLine("We've reached capacity: {0}", _streams.Count);
                    var first = _streams.FirstOrDefault(p => p.TorrentFile.Priority != Priority.Immediate);
                    if (first != null)
                    {
                        CloseAndRemove(first);
                    }
                }
                _streams.Add(stream);
            }
        }
        internal TorrentFileStream GetStream(TorrentFile file, FileAccessMode access)
        {
            var fullPath = file.FullPath;
            var asyncTokens = GetAsyncTokens(fullPath);
            try
            {
                asyncTokens.CancellationTokenSource.Token.ThrowIfCancellationRequested();
                var s = FindStream(fullPath);
                if (s != null)
                {
                    // If we are requesting write access and the current stream does not have it
                    if (access == FileAccessMode.ReadWrite && !s.CanWrite)
                    {
                        Debug.WriteLine("Didn't have write permission - reopening");
                        CloseAndRemove(s);
                    }
                    else
                    {
                        lock (_locker)
                        {
                            // Place the filestream at the end so we know it's been recently used
                            _streams.Remove(s);
                            _streams.Add(s);
                        }
                        return s;
                    }
                }

                try
                {
                    var result = OpenStreamAsync(file, access, asyncTokens).Result;
                    file.Exists = true;
                    s = new TorrentFileStream(file, result)
                    {
                        Size = (ulong) file.Length
                    };
                    Add(s);
                }
                catch (AggregateException ex)
                {
                    if (ex.InnerException is OperationCanceledException ||
                        ex.InnerException is UnauthorizedAccessException)
                        throw ex.InnerException;
                    throw;
                }
                return s;
            }
            finally
            {
                if (asyncTokens != null)
                {
                    asyncTokens.SemaphoreSlim.Release();
                    if (asyncTokens.CancellationTokenSource.IsCancellationRequested)
                        Clear(fullPath);
                }
            }
        }
 private void CloseAndRemove(TorrentFileStream s)
 {
     lock (_locker)
     {
         Debug.WriteLine("Closing and removing: {0}", s.Path);
         if (_streams.Remove(s))
             s.Dispose();
     }
 }
Beispiel #6
0
        internal TorrentFileStream GetStream(TorrentFile file, FileAccessMode access)
        {
            var fullPath    = file.FullPath;
            var asyncTokens = GetAsyncTokens(fullPath);

            try
            {
                asyncTokens.CancellationTokenSource.Token.ThrowIfCancellationRequested();
                var s = FindStream(fullPath);
                if (s != null)
                {
                    // If we are requesting write access and the current stream does not have it
                    if (access == FileAccessMode.ReadWrite && !s.CanWrite)
                    {
                        Debug.WriteLine("Didn't have write permission - reopening");
                        CloseAndRemove(s);
                    }
                    else
                    {
                        lock (_locker)
                        {
                            // Place the filestream at the end so we know it's been recently used
                            _streams.Remove(s);
                            _streams.Add(s);
                        }
                        return(s);
                    }
                }

                try
                {
                    var result = OpenStreamAsync(file, access, asyncTokens).Result;
                    file.Exists = true;
                    s           = new TorrentFileStream(file, result)
                    {
                        Size = (ulong)file.Length
                    };
                    Add(s);
                }
                catch (AggregateException ex)
                {
                    if (ex.InnerException is OperationCanceledException ||
                        ex.InnerException is UnauthorizedAccessException)
                    {
                        throw ex.InnerException;
                    }
                    throw;
                }
                return(s);
            }
            finally
            {
                if (asyncTokens != null)
                {
                    asyncTokens.SemaphoreSlim.Release();
                    if (asyncTokens.CancellationTokenSource.IsCancellationRequested)
                    {
                        Clear(fullPath);
                    }
                }
            }
        }