Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions Knossos.NET/Classes/ThrottledStream.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Threading;
using System;
using System.IO;
using System;
using System.Threading;
using System.Threading.Tasks;


namespace Knossos.NET.Classes
Expand Down Expand Up @@ -150,5 +151,11 @@ public override long Seek(long offset, SeekOrigin origin)
{
return stream.Seek(offset, origin);
}

public override ValueTask<int> ReadAsync(Memory<byte> buffer, CancellationToken cancellationToken = default)
{
Limit(buffer.Length);
return stream.ReadAsync(buffer, cancellationToken);
}
}
}
27 changes: 26 additions & 1 deletion Knossos.NET/ViewModels/Templates/Tasks/DownloadFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,32 @@ private async Task<bool> Download(Uri downloadUrl, string destinationFilePath, F
throw new TaskCanceledException();
}

var bytesRead = await contentStream.ReadAsync(buffer);
int bytesRead = 0;
if (Knossos.globalSettings.antiStuck)
{
//If there is zero progress, cancel (throws exception)
using var readTimeoutCts = new CancellationTokenSource(TimeSpan.FromSeconds(ANTI_STUCK_CHECK_SECONDS));
using var linkedCts = CancellationTokenSource.CreateLinkedTokenSource(cancellationTokenSource!.Token, readTimeoutCts.Token);
try
{
bytesRead = await contentStream.ReadAsync(buffer.AsMemory(), linkedCts.Token);
}
catch (OperationCanceledException)
{
if (readTimeoutCts.IsCancellationRequested)
{
Log.Add(Log.LogSeverity.Warning, "TaskItemViewModel.Download",
$"[ANTI-STUCK] Mirror {CurrentMirror ?? downloadUrl.Host} is stuck. " +
"Changing to a diferent mirror...");
}
return false;
}
}
else
{
bytesRead = await contentStream.ReadAsync(buffer.AsMemory(), cancellationTokenSource!.Token);
}

if (bytesRead == 0)
{
isMoreToRead = false;
Expand Down