예제 #1
0
        private FileStream GetStream(FileEntry file, bool write)
        {
            const int tryCount = 5;
            int tryTime = 0;

            while (true)
            {
                lock (openStreams)
                {
                    string finalPath = Path.Combine(MainDirectory, file.Name);
                    if(!write && !FileExists(finalPath)) return null;

                    FileStream stream;
                    if(openStreams.TryGetValue(finalPath, out stream))
                    {
                        if(!write) return stream;
                        if(stream.CanWrite) return stream;
                        openStreams.TryRemove(finalPath, out stream);
                    }

                    try
                    {
                        var dir = Path.GetDirectoryName(finalPath);
                        if(dir != string.Empty && !Directory.Exists(dir))
                            Directory.CreateDirectory(dir);
                        stream = File.Open(finalPath, write ? FileMode.OpenOrCreate : FileMode.Open,
                                           write ? FileAccess.ReadWrite : FileAccess.Read);
                        openStreams.TryAdd(finalPath, stream);
                        return stream;
                    }
                    catch(Exception)
                    {
                        tryTime++;
                        if(tryTime > tryCount)
                            throw;
                    }
                }
            }
        }
예제 #2
0
 private FileEntry CreateTorrentFile(BencodedDictionary file)
 {
     string[] filePathList = (file["path"] as BencodedList).Select(s => (string) (s as BencodedString)).ToArray();
     var fileLength = file["length"] as BencodedInteger;
     CheckFileProperties(filePathList, fileLength);
     string filePath = Path.Combine(filePathList);
     var torrentFile = new FileEntry(filePath, fileLength);
     return torrentFile;
 }
예제 #3
0
 public void Remove(FileEntry fileEntry)
 {
     filesToAdd.Remove(fileEntry);
 }