diff --git a/Net/NDownload/IDlConnection.cs b/Net/NDownload/IDlConnection.cs new file mode 100644 index 00000000..533b9919 --- /dev/null +++ b/Net/NDownload/IDlConnection.cs @@ -0,0 +1,29 @@ +using System.Threading.Tasks; + +namespace PCL.Core.Net.NDownload; + +/// +/// 下载连接,负责与服务器进行通信。 +/// +public interface IDlConnection +{ + /// + /// 开始连接,发起与服务器的通信。 + /// + /// 起始偏移,为 0 表示不使用分块 + /// 连接信息 + public Task StartAsync(long beginOffset); + + /// + /// 停止连接,同时停止服务器通信并释放资源。 + /// + /// + public Task StopAsync(); + + /// + /// 读取指定长度的数据,若无法继续读取则返回空数组。 + /// + /// 读取长度 + /// 字节数组形式的数据 + public Task ReadAsync(int length); +} diff --git a/Net/NDownload/IDlResourceMapping.cs b/Net/NDownload/IDlResourceMapping.cs new file mode 100644 index 00000000..db7f9db8 --- /dev/null +++ b/Net/NDownload/IDlResourceMapping.cs @@ -0,0 +1,10 @@ +namespace PCL.Core.Net.NDownload; + +/// +/// 资源 ID 映射。 +/// +/// 映射目标类型 +public interface IDlResourceMapping +{ + public TMappingValue? Parse(string resId); +} diff --git a/Net/NDownload/IDlWriter.cs b/Net/NDownload/IDlWriter.cs new file mode 100644 index 00000000..80c66b38 --- /dev/null +++ b/Net/NDownload/IDlWriter.cs @@ -0,0 +1,31 @@ +using System.IO; +using System.Threading.Tasks; + +namespace PCL.Core.Net.NDownload; + +/// +/// 下载写入器。 +/// +public interface IDlWriter +{ + /// + /// 是否支持并行写入,即是否支持多次调用 。 + /// + public bool IsSupportParallel { get; } + + /// + /// 创建写入流。 + /// + /// 写入流 + public Task CreateStreamAsync(); + + /// + /// 停止写入并释放资源。 + /// + public Task StopAsync(); + + /// + /// 完成写入,用于执行某些并行操作的收尾工作 (例如合并文件)。 + /// + public Task FinishAsync(); +} diff --git a/Net/NDownload/NDlConnectionInfo.cs b/Net/NDownload/NDlConnectionInfo.cs new file mode 100644 index 00000000..5d9ab2fe --- /dev/null +++ b/Net/NDownload/NDlConnectionInfo.cs @@ -0,0 +1,15 @@ +namespace PCL.Core.Net.NDownload; + +/// +/// 下载连接信息。 +/// +/// 内容长度,单位为字节 +/// 起始偏移 +/// 结束偏移 +/// 是否支持分块 +public record NDlConnectionInfo( + long Length, + long BeginOffset, + long EndOffset, + bool IsSupportSegment +); diff --git a/Net/NDownload/NDlFactory.cs b/Net/NDownload/NDlFactory.cs new file mode 100644 index 00000000..df099b6c --- /dev/null +++ b/Net/NDownload/NDlFactory.cs @@ -0,0 +1,65 @@ +namespace PCL.Core.Net.NDownload; + +/// +/// 无泛型的下载器构建工厂。 +/// +public abstract class NDlFactory +{ + /// + /// 新建连接。 + /// + /// 资源 ID + /// 下载连接 + public abstract IDlConnection? CreateConnection(string resId); + + /// + /// 新建写入器。 + /// + /// 资源 ID + /// 下载写入器 + public abstract IDlWriter? CreateWriter(string resId); +} + +/// +/// 下载器构建工厂。 +/// +/// 下载源参数类型 +/// 写入目标参数类型 +public abstract class NDlFactory : NDlFactory +{ + /// + /// 下载源映射。 + /// + protected abstract IDlResourceMapping SourceMapping { get; } + + /// + /// 写入目标映射。 + /// + protected abstract IDlResourceMapping TargetMapping { get; } + + /// + /// 新建连接。 + /// + /// 下载源参数 + /// 下载连接 + protected abstract IDlConnection CreateConnection(TSourceArgument source); + + public override IDlConnection? CreateConnection(string resId) + { + var source = SourceMapping.Parse(resId); + return (source == null) ? null : CreateConnection(source); + } + + /// + /// 新建写入器。 + /// + /// 写入目标 + /// 下载写入器 + protected abstract IDlWriter CreateWriter(TTargetArgument target); + + public override IDlWriter? CreateWriter(string resId) + { + var target = TargetMapping.Parse(resId); + return (target == null) ? null : CreateWriter(target); + } +} diff --git a/Net/NDownload/NDlScheduler.cs b/Net/NDownload/NDlScheduler.cs new file mode 100644 index 00000000..99d4f16d --- /dev/null +++ b/Net/NDownload/NDlScheduler.cs @@ -0,0 +1,5 @@ +namespace PCL.Core.Net.NDownload; + +public class NDlScheduler +{ +} diff --git a/Net/NDownload/NDlSourceManager.cs b/Net/NDownload/NDlSourceManager.cs new file mode 100644 index 00000000..08240d81 --- /dev/null +++ b/Net/NDownload/NDlSourceManager.cs @@ -0,0 +1,14 @@ +using System.Collections.Generic; + +namespace PCL.Core.Net.NDownload; + +public class NDlSourceManager(IList> sources) + : IDlResourceMapping +{ + public IList> Sources { get; } = sources; + + public TSourceArgument? Parse(string resId) + { + throw new System.NotImplementedException(); + } +} diff --git a/Net/NDownload/NDlSourceReport.cs b/Net/NDownload/NDlSourceReport.cs new file mode 100644 index 00000000..f19cbb82 --- /dev/null +++ b/Net/NDownload/NDlSourceReport.cs @@ -0,0 +1,13 @@ +namespace PCL.Core.Net.NDownload; + +/// +/// 下载源质量报告 +/// +/// 支持最大分块数量 +/// 重试计数 +/// 总体平均速度 +public record NDlSourceReport( + int MaxSegmentCount = 1, + int RetryCount = 0, + long AverageSpeed = -1 +); diff --git a/Net/NDownload/NDlTask.cs b/Net/NDownload/NDlTask.cs new file mode 100644 index 00000000..134f7b64 --- /dev/null +++ b/Net/NDownload/NDlTask.cs @@ -0,0 +1,5 @@ +namespace PCL.Core.Net.NDownload; + +public class NDlTask +{ +} diff --git a/Net/NDownload/NDlTaskSegment.cs b/Net/NDownload/NDlTaskSegment.cs new file mode 100644 index 00000000..f855e0c7 --- /dev/null +++ b/Net/NDownload/NDlTaskSegment.cs @@ -0,0 +1,5 @@ +namespace PCL.Core.Net.NDownload; + +public class NDlTaskSegment +{ +}