Ejemplo n.º 1
0
        private AsynTokens GetAsyncTokens(string path)
        {
            AsynTokens asynTokens;

            lock (_locker)
            {
                if (!_dictionary.TryGetValue(path, out asynTokens))
                {
                    asynTokens = new AsynTokens();
                    _dictionary.Add(path, asynTokens);
                }
            }

            asynTokens.SemaphoreSlim.Wait();
            return(asynTokens);
        }
Ejemplo n.º 2
0
 private Task<IRandomAccessStream> OpenStreamAsync(TorrentFile file, FileAccessMode access, AsynTokens asynTokens)
 {
     return Task.Run(async () =>
     {
         var token = asynTokens.CancellationTokenSource.Token;
         var fullPath = file.FullPath;
         var storageFile = await StorageHelper.CreateFileAsync(fullPath, file.TargetFolder);
         if (access == FileAccessMode.ReadWrite)
         {
             var stream = File.Open(storageFile.Path, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
             var randomAccessStream = stream.AsRandomAccessStream();
             try
             {
                 var size = (long) randomAccessStream.Size;
                 var length = file.Length - size;
                 if (length > 0L)
                 {
                     var buffer = ClientEngine.BufferManager.GetBuffer((int) Math.Min(length, 524288L));
                     try
                     {
                         randomAccessStream.Seek((ulong) size);
                         for (var i = size;
                             i < file.Length;
                             i = i + (long) buffer.Length)
                         {
                             length = length - await randomAccessStream.WriteAsync(
                                 buffer.AsBuffer(0,
                                     (int) Math.Min(length, buffer.Length)));
                             token.ThrowIfCancellationRequested();
                         }
                     }
                     finally
                     {
                         ClientEngine.BufferManager.FreeBuffer(ref buffer);
                     }
                 }
             }
             finally
             {
                 randomAccessStream?.Dispose();
             }
         }
         return await storageFile.OpenAsync(access);
     });
 }
Ejemplo n.º 3
0
        private AsynTokens GetAsyncTokens(string path)
        {
            AsynTokens asynTokens;
            lock (_locker)
            {
                if (!_dictionary.TryGetValue(path, out asynTokens))
                {
                    asynTokens = new AsynTokens();
                    _dictionary.Add(path, asynTokens);
                }
            }

            asynTokens.SemaphoreSlim.Wait();
            return asynTokens;
        }
Ejemplo n.º 4
0
 private Task <IRandomAccessStream> OpenStreamAsync(TorrentFile file, FileAccessMode access, AsynTokens asynTokens)
 {
     return(Task.Run(async() =>
     {
         var token = asynTokens.CancellationTokenSource.Token;
         var fullPath = file.FullPath;
         var storageFile = await StorageHelper.CreateFileAsync(fullPath, file.TargetFolder);
         if (access == FileAccessMode.ReadWrite)
         {
             var stream = File.Open(storageFile.Path, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
             var randomAccessStream = stream.AsRandomAccessStream();
             try
             {
                 var size = (long)randomAccessStream.Size;
                 var length = file.Length - size;
                 if (length > 0L)
                 {
                     var buffer = ClientEngine.BufferManager.GetBuffer((int)Math.Min(length, 524288L));
                     try
                     {
                         randomAccessStream.Seek((ulong)size);
                         for (var i = size;
                              i < file.Length;
                              i = i + (long)buffer.Length)
                         {
                             length = length - await randomAccessStream.WriteAsync(
                                 buffer.AsBuffer(0,
                                                 (int)Math.Min(length, buffer.Length)));
                             token.ThrowIfCancellationRequested();
                         }
                     }
                     finally
                     {
                         ClientEngine.BufferManager.FreeBuffer(ref buffer);
                     }
                 }
             }
             finally
             {
                 randomAccessStream?.Dispose();
             }
         }
         return await storageFile.OpenAsync(access);
     }));
 }