165 lines
6.2 KiB
C#
165 lines
6.2 KiB
C#
using Cysharp.Threading.Tasks;
|
||
using Net;
|
||
using Server;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Text;
|
||
using System.Threading.Tasks;
|
||
using TouchSocket.Core;
|
||
using TouchSocket.Dmtp;
|
||
using TouchSocket.Dmtp.FileTransfer;
|
||
using TouchSocket.Rpc;
|
||
using TouchSocket.Sockets;
|
||
|
||
namespace FileSend
|
||
{
|
||
internal class ZServer
|
||
{
|
||
public TcpDmtpService service;
|
||
public TouchSocketConfig config;
|
||
public const string rootPath = "E:\\zRoot\\S";
|
||
|
||
|
||
public ZServer()
|
||
{
|
||
service = new TcpDmtpService();
|
||
|
||
config = new TouchSocketConfig()//配置
|
||
.SetListenIPHosts(7789)
|
||
.ConfigureContainer(a =>
|
||
{
|
||
a.AddConsoleLogger();
|
||
a.AddRpcStore(store =>
|
||
{
|
||
store.RegisterServer<GetFilesRpcServer>(); //注册服务
|
||
});
|
||
})
|
||
.ConfigurePlugins(a =>
|
||
{
|
||
//a.UseDmtpHeartbeat();
|
||
a.UseDmtpFileTransfer()//必须添加文件传输插件
|
||
.SetRootPath(rootPath)//设置RootPath
|
||
//.SetRootPath("C:\\新建文件夹")//设置RootPath
|
||
.SetMaxSmallFileLength(1024 * 1024);//设置小文件的最大限制长度
|
||
a.Add<MyPlugin>();
|
||
a.Add<ConnectAndMsgPlugin>();
|
||
})
|
||
.SetDmtpOption(new DmtpOption()
|
||
{
|
||
VerifyToken = "Channel"//连接验证口令。
|
||
});
|
||
|
||
}
|
||
|
||
public async UniTask StartAsync()
|
||
{
|
||
await service.SetupAsync(config);
|
||
await service.StartAsync();
|
||
service.Logger.Info($"{service.GetType().Name}已启动");
|
||
}
|
||
|
||
public async UniTask StopAsync()
|
||
{
|
||
await service.ClearAsync();
|
||
await service.StopAsync();
|
||
service.Dispose();
|
||
}
|
||
|
||
/// <summary>
|
||
/// 服务器向客户端请求文件
|
||
/// </summary>
|
||
/// <exception cref="Exception"></exception>
|
||
public void S2C_RequestFile()
|
||
{
|
||
string targetId = "targetId";
|
||
if (!service.TryGetClient(targetId, out var SessionClient))
|
||
{
|
||
throw new Exception($"没有找到Id={targetId}的客户端");
|
||
}
|
||
|
||
var filePath = "ServicePullFileFromClient.Test";
|
||
var saveFilePath = "SaveServicePullFileFromClient.Test";
|
||
|
||
var metadata = new Metadata();//传递到客户端的元数据
|
||
metadata.Add("1", "1");
|
||
metadata.Add("2", "2");
|
||
|
||
var fileOperator = new FileOperator()//实例化本次传输的控制器,用于获取传输进度、速度、状态等。
|
||
{
|
||
SavePath = saveFilePath,//服务器本地保存路径
|
||
ResourcePath = filePath,//请求客户端文件的资源路径
|
||
Metadata = metadata,//传递到客户端的元数据
|
||
Timeout = TimeSpan.FromSeconds(60),//传输超时时长
|
||
TryCount = 10,//当遇到失败时,尝试次数
|
||
FileSectionSize = 1024 * 512//分包大小,当网络较差时,应该适当减小该值
|
||
};
|
||
|
||
fileOperator.MaxSpeed = 1024 * 1024 * 20;//设置最大限速为20Mb。
|
||
|
||
//此处的作用相当于Timer,定时每秒输出当前的传输进度和速度。
|
||
var loopAction = LoopAction.CreateLoopAction(-1, 1000, (loop) =>
|
||
{
|
||
if (fileOperator.Result.ResultCode != ResultCode.Default)
|
||
{
|
||
loop.Dispose();
|
||
}
|
||
SessionClient.Logger.Info($"进度:{fileOperator.Progress},速度:{fileOperator.Speed()}");
|
||
});
|
||
|
||
loopAction.RunAsync();
|
||
|
||
//此方法会阻塞,直到传输结束,也可以使用PullFileAsync
|
||
IResult result = SessionClient.GetDmtpFileTransferActor().PullFile(fileOperator);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 服务器向客户端推送文件
|
||
/// </summary>
|
||
/// <param name="targetId"></param>
|
||
/// <exception cref="Exception"></exception>
|
||
public void S2C_ResposeFile(string targetId)
|
||
{
|
||
if (!service.TryGetClient(targetId, out var SessionClient))
|
||
{
|
||
throw new Exception($"没有找到Id={targetId}的客户端");
|
||
}
|
||
|
||
var filePath = "ServicePushFileFromClient.Test";
|
||
var saveFilePath = "SaveServicePushFileFromClient.Test";
|
||
|
||
var metadata = new Metadata();//传递到客户端的元数据
|
||
metadata.Add("1", "1");
|
||
metadata.Add("2", "2");
|
||
|
||
var fileOperator = new FileOperator()//实例化本次传输的控制器,用于获取传输进度、速度、状态等。
|
||
{
|
||
SavePath = saveFilePath,//客户端本地保存路径
|
||
ResourcePath = filePath,//服务器文件的资源路径
|
||
Metadata = metadata,//传递到客户端的元数据
|
||
Timeout = TimeSpan.FromSeconds(60),//传输超时时长
|
||
TryCount = 10,//当遇到失败时,尝试次数
|
||
FileSectionSize = 1024 * 512//分包大小,当网络较差时,应该适当减小该值
|
||
};
|
||
|
||
fileOperator.MaxSpeed = 1024 * 1024 * 20;//设置最大限速为20Mb。
|
||
|
||
//此处的作用相当于Timer,定时每秒输出当前的传输进度和速度。
|
||
var loopAction = LoopAction.CreateLoopAction(-1, 1000, (loop) =>
|
||
{
|
||
if (fileOperator.Result.ResultCode != ResultCode.Default)
|
||
{
|
||
loop.Dispose();
|
||
}
|
||
SessionClient.Logger.Info($"进度:{fileOperator.Progress},速度:{fileOperator.Speed()}");
|
||
});
|
||
|
||
loopAction.RunAsync();
|
||
|
||
//此方法会阻塞,直到传输结束,也可以使用PushFileAsync
|
||
IResult result = SessionClient.GetDmtpFileTransferActor().PushFile(fileOperator);
|
||
}
|
||
|
||
}
|
||
}
|