217 lines
7.5 KiB
C#
217 lines
7.5 KiB
C#
using FileSend;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Text;
|
||
using System.Text.Json;
|
||
using System.Threading.Tasks;
|
||
using TouchSocket.Core;
|
||
using TouchSocket.Dmtp;
|
||
using TouchSocket.Dmtp.FileTransfer;
|
||
using TouchSocket.Dmtp.Rpc;
|
||
using TouchSocket.Rpc;
|
||
using TouchSocket.Sockets;
|
||
|
||
namespace Net;
|
||
|
||
public class Client
|
||
{
|
||
private TcpDmtpClient _client;
|
||
private float _progress;
|
||
private long _speed;
|
||
|
||
public Client(IPHost point)
|
||
{
|
||
this._client = new TcpDmtpClient();
|
||
var config = new TouchSocketConfig()
|
||
.SetRemoteIPHost(point)
|
||
.SetDmtpOption(new DmtpOption()
|
||
{
|
||
VerifyToken = "Dmtp"
|
||
}).ConfigurePlugins(a =>
|
||
{
|
||
a.UseDmtpRpc();
|
||
a.UseDmtpFileTransfer();
|
||
a.Add<ServerPlugin>();
|
||
}).ConfigureContainer(a =>
|
||
{
|
||
a.AddConsoleLogger();
|
||
// a.AddLogger(new TouchLogger());
|
||
}
|
||
);
|
||
this._client.Setup(config);
|
||
try
|
||
{
|
||
this._client.Connect();
|
||
}
|
||
catch (Exception e)
|
||
{
|
||
Console.WriteLine(e.ToString());
|
||
}
|
||
|
||
NetHelper.IsConnected = true;
|
||
}
|
||
|
||
|
||
public async Task<bool> SendFileAsync(string filePath)
|
||
{
|
||
var fileOperator = new FileOperator() //实例化本次传输的控制器,用于获取传输进度、速度、状态等。
|
||
{
|
||
SavePath = new FileInfo(filePath).Name, //服务器保存路径
|
||
ResourcePath = filePath, //客户端本地即将上传文件的资源路径
|
||
Timeout = TimeSpan.FromSeconds(60), //传输超时时长
|
||
TryCount = 10, //当遇到失败时,尝试次数
|
||
FileSectionSize = 1024 * 512 //分包大小,当网络较差时,应该适当减小该值
|
||
};
|
||
|
||
fileOperator.MaxSpeed = long.MaxValue;
|
||
|
||
//此处的作用相当于Timer,定时每秒输出当前的传输进度和速度。
|
||
var loopAction = LoopAction.CreateLoopAction(-1, 1000, (loop) =>
|
||
{
|
||
if (fileOperator.Result.ResultCode != ResultCode.Default)
|
||
{
|
||
loop.Dispose();
|
||
}
|
||
|
||
// selectedFileInfo.Speed = fileOperator.Speed();
|
||
// selectedFileInfo.Progress = fileOperator.Progress;
|
||
_progress = fileOperator.Progress;
|
||
var speed = fileOperator.Speed();
|
||
_speed = speed;
|
||
_client.Logger.Info($"进度:{fileOperator.Progress},速度:{speed}");
|
||
});
|
||
|
||
loopAction.RunAsync();
|
||
|
||
//此方法会阻塞,直到传输结束,也可以使用PushFileAsync
|
||
IResult result = await this._client.GetDmtpFileTransferActor().PushFileAsync(fileOperator);
|
||
if (!result.IsSuccess())
|
||
{
|
||
this._client.Logger.Info(result.Message);
|
||
return false;
|
||
}
|
||
|
||
return true;
|
||
}
|
||
|
||
public IResult SendFile(string filePath, Action<string> callback)
|
||
{
|
||
var metadata = new Metadata(); //传递到服务器的元数据
|
||
metadata.Add("1", "1");
|
||
metadata.Add("2", "2");
|
||
|
||
FileInfo fileInfo = new FileInfo(filePath);
|
||
var fileOperator = new FileOperator() //实例化本次传输的控制器,用于获取传输进度、速度、状态等。
|
||
{
|
||
SavePath = $"{NetHelper.ServerRootPath}\\{fileInfo.Name}", //服务器保存路径
|
||
ResourcePath = filePath, //客户端本地即将上传文件的资源路径
|
||
Metadata = metadata, //传递到服务器的元数据
|
||
Timeout = TimeSpan.FromSeconds(60), //传输超时时长
|
||
TryCount = 10, //当遇到失败时,尝试次数
|
||
FileSectionSize = 1024 * 512 //分包大小,当网络较差时,应该适当减小该值
|
||
};
|
||
|
||
fileOperator.MaxSpeed = 1024 * 1024 * 1024; //设置最大限速为1Gb。
|
||
|
||
//此处的作用相当于Timer,定时每秒输出当前的传输进度和速度。
|
||
var loopAction = LoopAction.CreateLoopAction(-1, 1000, (loop) =>
|
||
{
|
||
if (fileOperator.Result.ResultCode != ResultCode.Default)
|
||
{
|
||
loop.Dispose();
|
||
}
|
||
|
||
// selectedFileInfo.Speed = fileOperator.Speed();
|
||
// selectedFileInfo.Progress = fileOperator.Progress;
|
||
_progress = fileOperator.Progress;
|
||
var speed = fileOperator.Speed();
|
||
_speed = speed;
|
||
var msg = $"进度:{fileOperator.Progress},速度:{speed}";
|
||
_client.Logger.Info(msg);
|
||
callback(msg);
|
||
});
|
||
|
||
loopAction.RunAsync();
|
||
|
||
//此方法会阻塞,直到传输结束,也可以使用PushFileAsync
|
||
IResult result = this._client.GetDmtpFileTransferActor().PushFile(fileOperator);
|
||
while (!fileOperator.IsEnd && !result.IsSuccess)
|
||
{
|
||
}
|
||
|
||
_client.Logger.Info($"{fileInfo.Name} 上传{result.IsSuccess} {result.Message}!");
|
||
return result;
|
||
}
|
||
|
||
public float GetSendProgress()
|
||
{
|
||
return _progress;
|
||
}
|
||
|
||
public long GetSendSpeed()
|
||
{
|
||
return this._speed;
|
||
}
|
||
|
||
public async Task<ServerFileInfo[]?> GetAllFiles()
|
||
{
|
||
var results = await this._client.GetDmtpRpcActor()
|
||
.InvokeTAsync<byte[]?>("GetFiles", InvokeOption.WaitInvoke, null);
|
||
var serverFileInfos = JsonSerializer.Deserialize<ServerFileInfo[]?>(results);
|
||
return serverFileInfos;
|
||
}
|
||
|
||
public async Task<bool> DownloadFileAsync(SelectedFileInfo selectedFileInfo)
|
||
{
|
||
var filePath = new FileInfo(selectedFileInfo.FilePath).Name;
|
||
string savePath = NetHelper.ClientSaveRootPath;
|
||
if (!Directory.Exists(savePath))
|
||
Directory.CreateDirectory(savePath);
|
||
var saveFilePath = Path.Combine(savePath, filePath);
|
||
|
||
var fileOperator = new FileOperator() //实例化本次传输的控制器,用于获取传输进度、速度、状态等。
|
||
{
|
||
SavePath = saveFilePath, //客户端本地保存路径
|
||
ResourcePath = filePath, //请求文件的资源路径
|
||
Timeout = TimeSpan.FromSeconds(60), //传输超时时长
|
||
TryCount = 10, //当遇到失败时,尝试次数
|
||
FileSectionSize = 1024 * 512 //分包大小,当网络较差时,应该适当减小该值
|
||
};
|
||
|
||
//此处的作用相当于Timer,定时每秒输出当前的传输进度和速度。
|
||
var loopAction = LoopAction.CreateLoopAction(-1, 1000, (loop) =>
|
||
{
|
||
if (fileOperator.Result.ResultCode != ResultCode.Default)
|
||
{
|
||
loop.Dispose();
|
||
}
|
||
|
||
_progress = fileOperator.Progress;
|
||
var speed = fileOperator.Speed();
|
||
_speed = speed;
|
||
this._client.Logger.Info($"进度:{fileOperator.Progress},速度:{speed}");
|
||
selectedFileInfo.Speed = speed;
|
||
selectedFileInfo.Progress = fileOperator.Progress;
|
||
});
|
||
|
||
loopAction.RunAsync();
|
||
|
||
//此方法会阻塞,直到传输结束,也可以使用PullFileAsync
|
||
IResult result = await this._client.GetDmtpFileTransferActor().PullFileAsync(fileOperator);
|
||
if (!result.IsSuccess())
|
||
{
|
||
this._client.Logger.Info(result.Message);
|
||
return false;
|
||
}
|
||
|
||
return true;
|
||
}
|
||
}
|
||
|
||
public class SelectedFileInfo
|
||
{
|
||
public string FilePath;
|
||
public long Speed;
|
||
public float Progress;
|
||
} |