LargeFileProgress
using System;
namespace Relativity.Transfer
{
public class LargeFileProgress : IProgress<LargeFileProgressEventArgs>
{
private readonly Action<LargeFileProgressEventArgs> handler;
private DateTime timestamp;
private JobTransferPath jobTransferPath;
public double LargeFileProgressRateSeconds { get; }
private DateTime Timestamp {
get {
if (jobTransferPath == null)
return timestamp;
return jobTransferPath.EndTime ?? jobTransferPath.StartTime ?? timestamp;
}
set {
if (jobTransferPath == null)
timestamp = value;
else
jobTransferPath.EndTime = value;
}
}
public LargeFileProgress(Action<LargeFileProgressEventArgs> handler, double largeFileProgressRateSeconds, JobTransferPath jobTransferPath)
: this(handler, largeFileProgressRateSeconds)
{
this.jobTransferPath = jobTransferPath;
}
public LargeFileProgress(Action<LargeFileProgressEventArgs> handler, double largeFileProgressRateSeconds)
{
LargeFileProgressRateSeconds = largeFileProgressRateSeconds;
this.handler = handler;
Timestamp = DateTime.Now;
}
public void Report(LargeFileProgressEventArgs value)
{
if (value == null)
throw new ArgumentNullException("value");
if (ShouldPublishLargeFileProgress(value.TotalTransferredBytes, value.TotalBytes, value.ChunkNumber)) {
handler(value);
Timestamp = DateTime.Now;
}
}
private bool ShouldPublishLargeFileProgress(long totalTransferredBytes, long totalBytes, int chunkNumber)
{
if (totalTransferredBytes > totalBytes || chunkNumber <= 1)
return false;
if (totalTransferredBytes == totalBytes)
return true;
return (DateTime.Now - Timestamp).TotalSeconds >= LargeFileProgressRateSeconds;
}
}
}