Skip to content

Commit

Permalink
[core] Create IStreamingPieceRequester
Browse files Browse the repository at this point in the history
The engine (currently) only needs two additional methods when streaming
files. Wrap these in IStreamingPieceRequester and then set/unset
TorrentManager.StreamProvider implicitly depending on whether
the piece requester implements this interface.

If support is added for streaming multiple files concurrently then
this interface will need to be extended to support this.
  • Loading branch information
alanmcgovern committed Mar 13, 2021
1 parent c379044 commit 6d6b45f
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
//
// IStreamingPieceRequester.cs
//
// Authors:
// Alan McGovern [email protected]
//
// Copyright (C) 2021 Alan McGovern
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//

namespace MonoTorrent.Client.PiecePicking
{
// FIXME: Eventually make this public API so custom streaming requesters
// can be trivially integrated with the engine. Are these two the only
// methods needed?

/// <summary>
/// Allows files to be accessed while they are downloading
/// </summary>
interface IStreamingPieceRequester : IPieceRequester
{
/// <summary>
/// Cancel any pending requests and then issue new requests so we immediately download pieces from the new high
/// priority set.
/// </summary>
/// <param name="file"></param>
/// <param name="position"></param>
void SeekToPosition (ITorrentFileInfo file, long position);

/// <summary>
/// Inform the picker that we have sequentially read data and so will need to update the high priority set without
/// cancelling pending requests.
/// </summary>
/// <param name="file"></param>
/// <param name="position"></param>
void ReadToPosition (ITorrentFileInfo file, long position);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

namespace MonoTorrent.Client.PiecePicking
{
public class StreamingPieceRequester : IPieceRequester
public class StreamingPieceRequester : IStreamingPieceRequester
{
bool RefreshAfterSeeking = false;

Expand Down Expand Up @@ -246,7 +246,7 @@ IList<BlockInfo> PriorityPick (IPeer peer, BitField available, IReadOnlyList<IPe
/// </summary>
/// <param name="file"></param>
/// <param name="position"></param>
internal void SeekToPosition (ITorrentFileInfo file, long position)
public void SeekToPosition (ITorrentFileInfo file, long position)
{
// Update the high priority set, then cancel pending requests.
var oldIndex = HighPriorityPieceIndex;
Expand All @@ -260,7 +260,7 @@ internal void SeekToPosition (ITorrentFileInfo file, long position)
/// </summary>
/// <param name="file"></param>
/// <param name="position"></param>
internal void ReadToPosition (ITorrentFileInfo file, long position)
public void ReadToPosition (ITorrentFileInfo file, long position)
{
HighPriorityPieceIndex = Math.Min (file.EndPieceIndex, TorrentData.ByteOffsetToPieceIndex (position + file.OffsetInTorrent));
}
Expand Down
6 changes: 2 additions & 4 deletions src/MonoTorrent/MonoTorrent.Client/ClientEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@

using MonoTorrent.BEncoding;
using MonoTorrent.Client.Listeners;
using MonoTorrent.Client.PiecePicking;
using MonoTorrent.Client.PieceWriters;
using MonoTorrent.Client.PortForwarding;
using MonoTorrent.Client.RateLimiters;
Expand Down Expand Up @@ -273,9 +274,7 @@ public Task<TorrentManager> AddStreamingAsync (Torrent torrent, string saveDirec
async Task<TorrentManager> AddStreamingAsync (MagnetLink magnetLink, Torrent torrent, string saveDirectory, TorrentSettings settings)
{
var manager = await AddAsync (magnetLink, torrent, saveDirectory, settings);
var picker = new PiecePicking.StreamingPieceRequester ();
await manager.ChangePickerAsync (picker);
manager.StreamProvider = new StreamProvider (manager, picker);
await manager.ChangePickerAsync (new StreamingPieceRequester ());
return manager;
}

Expand Down Expand Up @@ -446,7 +445,6 @@ public async Task PauseAll ()
public async Task Register (TorrentManager manager)
=> await Register (manager, true);


async Task Register (TorrentManager manager, bool isPublic)
{
CheckDisposed ();
Expand Down
21 changes: 11 additions & 10 deletions src/MonoTorrent/MonoTorrent.Client/Managers/TorrentManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -425,26 +425,27 @@ void CreateRateLimiters ()

#region Public Methods

internal void ChangePicker (IPieceRequester requestManager)
internal void ChangePicker (IPieceRequester requester)
{
if (requestManager == null)
throw new ArgumentNullException (nameof (requestManager));
if (requester == null)
throw new ArgumentNullException (nameof (requester));

PieceManager.ChangePicker (requestManager);
PieceManager.ChangePicker (requester);
if (requester is IStreamingPieceRequester streamingRequester)
StreamProvider = new StreamProvider (this, streamingRequester);
else
StreamProvider = null;
}

/// <summary>
/// Changes the active piece picker. This can be called when the manager is running, or when it is stopped.
/// </summary>
/// <param name="picker">The new picker to use.</param>
/// <param name="requester">The new picker to use.</param>
/// <returns></returns>
public async Task ChangePickerAsync (IPieceRequester picker)
public async Task ChangePickerAsync (IPieceRequester requester)
{
if (StreamProvider != null)
throw new InvalidOperationException ("Custom PiecePickers cannot be used for Torrents added using 'ClientEngine.AddStreamingAsync'.");

await ClientEngine.MainLoop;
ChangePicker (picker);
ChangePicker (requester);
}

public void Dispose ()
Expand Down
4 changes: 2 additions & 2 deletions src/MonoTorrent/MonoTorrent.Streaming/LocalStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ public override long Position {

TorrentManager Manager { get; }

StreamingPieceRequester Picker { get; }
IStreamingPieceRequester Picker { get; }

public LocalStream (TorrentManager manager, ITorrentFileInfo file, StreamingPieceRequester picker)
public LocalStream (TorrentManager manager, ITorrentFileInfo file, IStreamingPieceRequester picker)
{
Manager = manager;
File = file;
Expand Down
4 changes: 2 additions & 2 deletions src/MonoTorrent/MonoTorrent.Streaming/StreamProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ public class StreamProvider
LocalStream ActiveStream { get; set; }
CancellationTokenSource Cancellation { get; set; }
TorrentManager Manager { get; }
StreamingPieceRequester PieceRequester { get; }
IStreamingPieceRequester PieceRequester { get; }

internal StreamProvider (TorrentManager manager, StreamingPieceRequester pieceRequester)
internal StreamProvider (TorrentManager manager, IStreamingPieceRequester pieceRequester)
{
Cancellation = new CancellationTokenSource ();
Manager = manager;
Expand Down

0 comments on commit 6d6b45f

Please sign in to comment.