Skip to content

Commit

Permalink
[core] Properly store metadata for streaming torrents
Browse files Browse the repository at this point in the history
Use the same codepath to load torrents normally and when
loading them for 'streaming'. This ensure core functionality,
such as storing the metadata in the correct location, works
for all overloads.

Probably fixes #464
  • Loading branch information
alanmcgovern committed Oct 29, 2021
1 parent adcbbd9 commit 200f0ab
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 21 deletions.
24 changes: 18 additions & 6 deletions src/MonoTorrent.Tests/Client/ClientEngineTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -186,15 +186,22 @@ public async Task SaveRestoreState_NoTorrents ()
}

[Test]
public async Task SaveRestoreState_OneInMemoryTorrent ()
[TestCase(true)]
[TestCase(false)]
public async Task SaveRestoreState_OneInMemoryTorrent (bool addStreaming)
{
var pieceLength = Piece.BlockSize * 4;
using var tmpDir = TempDir.Create ();

var torrent = TestRig.CreateMultiFileTorrent (TorrentFile.Create (pieceLength, Piece.BlockSize, Piece.BlockSize * 2, Piece.BlockSize * 3), pieceLength, out BEncoding.BEncodedDictionary metadata);

var engine = new ClientEngine (EngineSettingsBuilder.CreateForTests (cacheDirectory: tmpDir.Path));
var torrentManager = await engine.AddAsync (torrent, "mySaveDirectory", new TorrentSettingsBuilder { CreateContainingDirectory = true }.ToSettings ());
TorrentManager torrentManager;
if (addStreaming)
torrentManager = await engine.AddStreamingAsync (torrent, "mySaveDirectory", new TorrentSettingsBuilder { CreateContainingDirectory = true }.ToSettings ());
else
torrentManager = await engine.AddAsync (torrent, "mySaveDirectory", new TorrentSettingsBuilder { CreateContainingDirectory = true }.ToSettings ());

await torrentManager.SetFilePriorityAsync (torrentManager.Files[0], Priority.High);
await torrentManager.MoveFileAsync (torrentManager.Files[1], Path.GetFullPath ("some_fake_path.txt"));

Expand All @@ -214,10 +221,15 @@ public async Task SaveRestoreState_OneInMemoryTorrent ()
}

[Test]
public async Task SaveRestoreState_OneMagnetLink ()
[TestCase(true)]
[TestCase(false)]
public async Task SaveRestoreState_OneMagnetLink (bool addStreaming)
{
var engine = new ClientEngine (EngineSettingsBuilder.CreateForTests ());
await engine.AddAsync (new MagnetLink (new InfoHash (new byte[20]), "test"), "mySaveDirectory", new TorrentSettingsBuilder { CreateContainingDirectory = false }.ToSettings ());
if (addStreaming)
await engine.AddStreamingAsync (new MagnetLink (new InfoHash (new byte[20]), "test"), "mySaveDirectory", new TorrentSettingsBuilder { CreateContainingDirectory = false }.ToSettings ());
else
await engine.AddAsync(new MagnetLink (new InfoHash (new byte[20]), "test"), "mySaveDirectory", new TorrentSettingsBuilder { CreateContainingDirectory = false }.ToSettings ());

var restoredEngine = await ClientEngine.RestoreStateAsync (await engine.SaveStateAsync ());
Assert.AreEqual (engine.Settings, restoredEngine.Settings);
Expand All @@ -239,7 +251,7 @@ public async Task SaveRestoreState_OneTorrentFile_ContainingDirectory ()
File.WriteAllBytes (metadataFile, metadata.Encode ());

var engine = new ClientEngine (EngineSettingsBuilder.CreateForTests (cacheDirectory: tmpDir.Path));
var torrentManager = await engine.AddAsync (metadataFile, "mySaveDirectory", new TorrentSettingsBuilder { CreateContainingDirectory = true }.ToSettings ());
var torrentManager = await engine.AddStreamingAsync (metadataFile, "mySaveDirectory", new TorrentSettingsBuilder { CreateContainingDirectory = true }.ToSettings ());
await torrentManager.SetFilePriorityAsync (torrentManager.Files[0], Priority.High);
await torrentManager.MoveFileAsync (torrentManager.Files[1], Path.GetFullPath ("some_fake_path.txt"));

Expand Down Expand Up @@ -269,7 +281,7 @@ public async Task SaveRestoreState_OneTorrentFile_NoContainingDirectory ()
File.WriteAllBytes (metadataFile, metadata.Encode ());

var engine = new ClientEngine (EngineSettingsBuilder.CreateForTests (cacheDirectory: tmpDir.Path));
await engine.AddAsync (metadataFile, "mySaveDirectory", new TorrentSettingsBuilder { CreateContainingDirectory = false }.ToSettings ());
await engine.AddStreamingAsync (metadataFile, "mySaveDirectory", new TorrentSettingsBuilder { CreateContainingDirectory = false }.ToSettings ());

var restoredEngine = await ClientEngine.RestoreStateAsync (await engine.SaveStateAsync ());
Assert.AreEqual (engine.Settings, restoredEngine.Settings);
Expand Down
27 changes: 12 additions & 15 deletions src/MonoTorrent/MonoTorrent.Client/ClientEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -386,29 +386,26 @@ async Task<TorrentManager> AddAsync (MagnetLink magnetLink, Torrent torrent, str
return manager;
}

public Task<TorrentManager> AddStreamingAsync (MagnetLink magnetLink, string saveDirectory)
=> AddStreamingAsync (magnetLink, saveDirectory, new TorrentSettings ());
public async Task<TorrentManager> AddStreamingAsync (MagnetLink magnetLink, string saveDirectory)
=> await MakeStreamingAsync (await AddAsync (magnetLink, saveDirectory));

public Task<TorrentManager> AddStreamingAsync (MagnetLink magnetLink, string saveDirectory, TorrentSettings settings)
=> AddStreamingAsync (magnetLink, null, saveDirectory, settings);
public async Task<TorrentManager> AddStreamingAsync (MagnetLink magnetLink, string saveDirectory, TorrentSettings settings)
=> await MakeStreamingAsync (await AddAsync (magnetLink, saveDirectory, settings));

public Task<TorrentManager> AddStreamingAsync (string metadataPath, string saveDirectory)
=> AddStreamingAsync (metadataPath, saveDirectory, new TorrentSettings ());
public async Task<TorrentManager> AddStreamingAsync (string metadataPath, string saveDirectory)
=> await MakeStreamingAsync (await AddAsync (metadataPath, saveDirectory));

public async Task<TorrentManager> AddStreamingAsync (string metadataPath, string saveDirectory, TorrentSettings settings)
=> await AddStreamingAsync (null, await Torrent.LoadAsync (metadataPath), saveDirectory, settings);
=> await MakeStreamingAsync (await AddAsync (metadataPath, saveDirectory, settings));

public Task<TorrentManager> AddStreamingAsync (Torrent torrent, string saveDirectory)
=> AddStreamingAsync (torrent, saveDirectory, new TorrentSettings ());
public async Task<TorrentManager> AddStreamingAsync (Torrent torrent, string saveDirectory)
=> await MakeStreamingAsync (await AddAsync (torrent, saveDirectory));

public Task<TorrentManager> AddStreamingAsync (Torrent torrent, string saveDirectory, TorrentSettings settings)
=> AddStreamingAsync (null, torrent, saveDirectory, settings);
public async Task<TorrentManager> AddStreamingAsync (Torrent torrent, string saveDirectory, TorrentSettings settings)
=> await MakeStreamingAsync (await AddAsync (torrent, saveDirectory, settings));

async Task<TorrentManager> AddStreamingAsync (MagnetLink magnetLink, Torrent torrent, string saveDirectory, TorrentSettings settings)
async Task<TorrentManager> MakeStreamingAsync (TorrentManager manager)
{
await MainLoop;

var manager = await AddAsync (magnetLink, torrent, saveDirectory, settings);
await manager.ChangePickerAsync (new StreamingPieceRequester ());
return manager;
}
Expand Down

0 comments on commit 200f0ab

Please sign in to comment.