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; } } } }
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; }
public void Remove(FileEntry fileEntry) { filesToAdd.Remove(fileEntry); }