diff --git a/src/MonoTorrent.Tests/Client/StreamingPieceRequesterTests.cs b/src/MonoTorrent.Tests/Client/StreamingPieceRequesterTests.cs new file mode 100644 index 000000000..ef771fb76 --- /dev/null +++ b/src/MonoTorrent.Tests/Client/StreamingPieceRequesterTests.cs @@ -0,0 +1,109 @@ +// +// StreamingPieceRequesterTests.cs +// +// Authors: +// Alan McGovern alan.mcgovern@gmail.com +// +// 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. +// + + +using System; +using System.Collections.Generic; +using System.Linq; + +using MonoTorrent.Client.Messages; +using MonoTorrent.Client.Messages.Standard; + +using NUnit.Framework; + +namespace MonoTorrent.Client.PiecePicking +{ + [TestFixture] + public class StreamingPieceRequesterTests + { + class TorrentData : ITorrentData + { + public IList Files { get; } = TorrentFileInfo.Create (Piece.BlockSize * 8, 1024 * 1024 * 8); + public int PieceLength => Piece.BlockSize * 8; + public long Size => Files[0].Length; + } + + [Test] + public void PickFromBeforeHighPrioritySet () + { + var data = new TorrentData (); + var ignoringBitfield = new MutableBitField (data.PieceCount ()) + .SetAll (true) + .Set (0, false); + + var requester = new StreamingPieceRequester (); + requester.Initialise (data, new[] { ignoringBitfield }); + requester.SeekToPosition (data.Files[0], data.PieceLength * 3); + + var peer = PeerId.CreateNull (ignoringBitfield.Length, true, false, true); + requester.AddRequests (peer, Array.Empty ()); + Assert.AreEqual (2, peer.AmRequestingPiecesCount); + + var requests = GetRequests (peer); + Assert.AreEqual (2, requests.Count); + Assert.IsTrue (requests.All (r => r.PieceIndex == 0)); + } + + [Test] + public void PickHighestPriority () + { + var data = new TorrentData (); + var ignoringBitfield = new MutableBitField (data.PieceCount ()) + .SetAll (false); + + var requester = new StreamingPieceRequester (); + requester.Initialise (data, new[] { ignoringBitfield }); + requester.SeekToPosition (data.Files[0], data.PieceLength * 3); + + var peer = PeerId.CreateNull (ignoringBitfield.Length, true, false, true); + requester.AddRequests (peer, Array.Empty ()); + Assert.AreEqual (4, peer.AmRequestingPiecesCount); + + var requests = GetRequests (peer); + Assert.AreEqual (4, requests.Count); + Assert.IsTrue (requests.All (r => r.PieceIndex == 3)); + } + + static List GetRequests (PeerId peer) + { + List results = new List (); + while (peer.MessageQueue.QueueLength > 0) { + var message = peer.MessageQueue.TryDequeue (); + if (message is RequestMessage r) { + results.Add (r); + } else if (message is RequestBundle bundle) { + foreach (var inner in bundle.ToRequestMessages ()) + if (inner is RequestMessage req) + results.Add (req); + } + } + return results; + } + + } +} diff --git a/src/MonoTorrent/MonoTorrent.Client.PiecePicking/StreamingPieceRequester.cs b/src/MonoTorrent/MonoTorrent.Client.PiecePicking/StreamingPieceRequester.cs index e1865ec5b..57d02e1d1 100644 --- a/src/MonoTorrent/MonoTorrent.Client.PiecePicking/StreamingPieceRequester.cs +++ b/src/MonoTorrent/MonoTorrent.Client.PiecePicking/StreamingPieceRequester.cs @@ -121,7 +121,7 @@ public void AddRequests (IReadOnlyList peers) foreach (var supportsFastPeer in new[] { true, false }) { for (int i = 0; i < 4; i++) { foreach (var peer in fastestPeers.Where (p => p.SupportsFastPeer == supportsFastPeer)) { - AddRequests (peer, peers, HighPriorityPieceIndex, Math.Min (HighPriorityPieceIndex + 1, bitfield.Length - 1), 2, (i + 1) * 2); + AddRequests (peer, peers, HighPriorityPieceIndex, Math.Min (HighPriorityPieceIndex + 1, bitfield.Length - 1), 2, preferredMaxRequests : (i + 1) * 2); } } } @@ -136,19 +136,20 @@ public void AddRequests (IPeerWithMessaging peer, IReadOnlyList allPeers, int startPieceIndex, int endPieceIndex, int maxDuplicates, int? preferredMaxRequests = null) + void AddRequests (IPeerWithMessaging peer, IReadOnlyList allPeers, int startPieceIndex, int endPieceIndex, int maxDuplicates, int preferredMaxRequests) { if (!peer.CanRequestMorePieces) return; int preferredRequestAmount = peer.PreferredRequestAmount (TorrentData.PieceLength); - var maxRequests = Math.Min (preferredMaxRequests ?? 3, peer.MaxPendingRequests); + var maxRequests = Math.Min (preferredMaxRequests, peer.MaxPendingRequests); if (peer.AmRequestingPiecesCount >= maxRequests) return; @@ -185,23 +186,13 @@ void AddRequests (IPeerWithMessaging peer, IReadOnlyList all MutableBitField filtered = null; while (peer.AmRequestingPiecesCount < maxRequests) { filtered ??= GenerateAlreadyHaves ().Not ().And (peer.BitField); - IList request = PriorityPick (peer, filtered, allPeers, preferredRequestAmount, 0, TorrentData.PieceCount () - 1); + IList request = PriorityPick (peer, filtered, allPeers, preferredRequestAmount, startPieceIndex, endPieceIndex); if (request != null && request.Count > 0) peer.EnqueueRequests (request); else break; } } - - if (!peer.IsChoking && peer.AmRequestingPiecesCount == 0) { - while (peer.AmRequestingPiecesCount < maxRequests) { - BlockInfo? request = Picker.ContinueAnyExistingRequest (peer, HighPriorityPieceIndex, TorrentData.PieceCount () - 1, 1); - if (request != null) - peer.EnqueueRequest (request.Value); - else - break; - } - } } MutableBitField GenerateAlreadyHaves () @@ -234,18 +225,15 @@ IList PriorityPick (IPeer peer, BitField available, IReadOnlyList 2) - return null; + if (peer.AmRequestingPiecesCount < 2) + return LowPriorityPicker.PickPiece (peer, available, otherPeers, count, startIndex, endIndex); - return LowPriorityPicker.PickPiece (peer, available, otherPeers, count, HighPriorityPieceIndex, endIndex); + return null; } ///