218 lines
7.5 KiB
C#
218 lines
7.5 KiB
C#
|
using System;
|
|||
|
using System.Threading.Tasks;
|
|||
|
using TouchSocket.Core;
|
|||
|
using TouchSocket.Rpc.TouchRpc;
|
|||
|
using TouchSocket.Sockets;
|
|||
|
|
|||
|
namespace DragonSoul.Logic
|
|||
|
{
|
|||
|
public class FileTranslationManager
|
|||
|
{
|
|||
|
private static FileTranslationManager _fileTranslationManager = new FileTranslationManager();
|
|||
|
public static FileTranslationManager instance => _fileTranslationManager;
|
|||
|
|
|||
|
private bool _active;
|
|||
|
private FileTranslationAgent _agent = new FileTranslationAgent();
|
|||
|
|
|||
|
public async Task<IResult> UploadFile(string groupName, string fileName)
|
|||
|
{
|
|||
|
if (_active)
|
|||
|
return Result.Canceled;
|
|||
|
this._active = true;
|
|||
|
this._agent.Initialize();
|
|||
|
__Log.InfoFile($"start upload:{fileName}");
|
|||
|
var result = await this._agent.UploadFile(groupName, fileName);
|
|||
|
__Log.InfoFile("upload end");
|
|||
|
_active = false;
|
|||
|
return result;
|
|||
|
}
|
|||
|
|
|||
|
public async Task<IResult> DownloadFile(string groupName, string fileName)
|
|||
|
{
|
|||
|
if (_active)
|
|||
|
return Result.Canceled;
|
|||
|
FileTranslationAgent agent = new FileTranslationAgent();
|
|||
|
agent.Initialize();
|
|||
|
__Log.InfoFile($"start download:{fileName}");
|
|||
|
var result = await agent.DownloadFile(groupName, fileName);
|
|||
|
__Log.InfoFile("download end");
|
|||
|
_active = false;
|
|||
|
return result;
|
|||
|
}
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
public class FileTranslationAgent
|
|||
|
{
|
|||
|
private TcpTouchRpcClient _client;
|
|||
|
|
|||
|
public void Initialize()
|
|||
|
{
|
|||
|
if(_client!=null)
|
|||
|
return;
|
|||
|
this._client = new TouchSocketConfig()
|
|||
|
.SetRemoteIPHost($"{MiscUtility.GetConfigIP()}:7789")
|
|||
|
.SetVerifyToken("FileTranslation")
|
|||
|
.UsePlugin()
|
|||
|
.SetRootPath(MiscUtility.GetBasePath())
|
|||
|
.ConfigureContainer(a => { ((LoggerGroup) a.Resolve<ILog>()).AddLogger((TouchSocket.Core.ILog) new TouchLogger()); })
|
|||
|
.ConfigurePlugins(a => { a.UseTouchRpcHeartbeat<TcpTouchRpcClient>(); })
|
|||
|
.BuildWithTcpTouchRpcClient();
|
|||
|
this._client.Logger.Info("连接成功");
|
|||
|
}
|
|||
|
|
|||
|
public void Dispose()
|
|||
|
{
|
|||
|
var clientID = this._client.ID;
|
|||
|
try
|
|||
|
{
|
|||
|
__Log.Info($"Close agent:{clientID}");
|
|||
|
_client.Close();
|
|||
|
}
|
|||
|
catch (Exception e)
|
|||
|
{
|
|||
|
__Log.Error(e.ToString());
|
|||
|
}
|
|||
|
try
|
|||
|
{
|
|||
|
__Log.Info($"Dispose agent:{clientID}");
|
|||
|
_client.Dispose();
|
|||
|
}
|
|||
|
catch (Exception e)
|
|||
|
{
|
|||
|
__Log.Error(e.ToString());
|
|||
|
}
|
|||
|
|
|||
|
__Log.Info($"dispose agent:{clientID}");
|
|||
|
}
|
|||
|
|
|||
|
public async Task SendFileToOtherClient(string targetId, string fileName)
|
|||
|
{
|
|||
|
FileOperator fileOperator = new FileOperator() //实例化本次传输的控制器,用于获取传输进度、速度、状态等。
|
|||
|
{
|
|||
|
SavePath = fileName, //保存路径
|
|||
|
ResourcePath = fileName, //请求路径
|
|||
|
};
|
|||
|
|
|||
|
|
|||
|
//此处的作用相当于Timer,定时每秒输出当前的传输进度和速度。
|
|||
|
LoopAction loopAction = LoopAction.CreateLoopAction(-1, 1000, (loop) =>
|
|||
|
{
|
|||
|
if (fileOperator.Result.ResultCode != ResultCode.Default)
|
|||
|
{
|
|||
|
loop.Dispose();
|
|||
|
}
|
|||
|
|
|||
|
if (_client == null || _client.Logger == null)
|
|||
|
{
|
|||
|
__Log.Info($"进度:{fileOperator.Progress},速度:{fileOperator.Speed()}");
|
|||
|
}
|
|||
|
else
|
|||
|
_client.Logger.Info($"进度:{fileOperator.Progress},速度:{fileOperator.Speed()}");
|
|||
|
});
|
|||
|
|
|||
|
loopAction.RunAsync();
|
|||
|
|
|||
|
|
|||
|
//此方法会阻塞,直到传输结束,也可以使用PullFileAsync
|
|||
|
IResult result = await this._client.PullFileAsync(targetId, fileOperator);
|
|||
|
_client.Logger.Info(result.ToString());
|
|||
|
}
|
|||
|
|
|||
|
public async Task<IResult> UploadFile(string groupName, string fileName)
|
|||
|
{
|
|||
|
FileOperator fileOperator = new FileOperator() //实例化本次传输的控制器,用于获取传输进度、速度、状态等。
|
|||
|
{
|
|||
|
SavePath = $"{groupName}/{fileName}", //保存路径
|
|||
|
ResourcePath = MiscUtility.GetValidPath(fileName), //请求路径
|
|||
|
};
|
|||
|
|
|||
|
//此处的作用相当于Timer,定时每秒输出当前的传输进度和速度。
|
|||
|
LoopAction loopAction = LoopAction.CreateLoopAction(-1, 100, (loop) =>
|
|||
|
{
|
|||
|
if (fileOperator.Result.ResultCode != ResultCode.Default)
|
|||
|
{
|
|||
|
loop.Dispose();
|
|||
|
}
|
|||
|
|
|||
|
if (_client == null || _client.Logger == null)
|
|||
|
{
|
|||
|
__Log.Info($"进度:{fileOperator.Progress},速度:{fileOperator.Speed()}");
|
|||
|
}
|
|||
|
else
|
|||
|
_client.Logger.Info($"进度:{fileOperator.Progress},速度:{fileOperator.Speed()}");
|
|||
|
});
|
|||
|
|
|||
|
try
|
|||
|
{
|
|||
|
loopAction.RunAsync();
|
|||
|
}
|
|||
|
catch (Exception e)
|
|||
|
{
|
|||
|
__Log.Error(e.ToString());
|
|||
|
}
|
|||
|
|
|||
|
IResult result = null;
|
|||
|
try
|
|||
|
{
|
|||
|
//此方法会阻塞,直到传输结束,也可以使用PullFileAsync
|
|||
|
result = await this._client.PushFileAsync(fileOperator);
|
|||
|
}
|
|||
|
catch (Exception e)
|
|||
|
{
|
|||
|
__Log.Error(e.ToString());
|
|||
|
}
|
|||
|
|
|||
|
_client.Logger.Info(result.ToString());
|
|||
|
return result;
|
|||
|
}
|
|||
|
|
|||
|
public async Task<IResult> DownloadFile(string groupName, string fileName)
|
|||
|
{
|
|||
|
FileOperator fileOperator = new FileOperator() //实例化本次传输的控制器,用于获取传输进度、速度、状态等。
|
|||
|
{
|
|||
|
SavePath = MiscUtility.GetValidPath(fileName), //保存路径
|
|||
|
ResourcePath = $"{groupName}/{fileName}", //请求路径
|
|||
|
};
|
|||
|
|
|||
|
//此处的作用相当于Timer,定时每秒输出当前的传输进度和速度。
|
|||
|
LoopAction loopAction = LoopAction.CreateLoopAction(-1, 100, (loop) =>
|
|||
|
{
|
|||
|
if (fileOperator.Result.ResultCode != ResultCode.Default)
|
|||
|
{
|
|||
|
loop.Dispose();
|
|||
|
}
|
|||
|
|
|||
|
if (_client == null || _client.Logger == null)
|
|||
|
{
|
|||
|
__Log.Info($"进度:{fileOperator.Progress},速度:{fileOperator.Speed()}");
|
|||
|
}
|
|||
|
else
|
|||
|
_client.Logger.Info($"进度:{fileOperator.Progress},速度:{fileOperator.Speed()}");
|
|||
|
});
|
|||
|
|
|||
|
try
|
|||
|
{
|
|||
|
loopAction.RunAsync();
|
|||
|
}
|
|||
|
catch (Exception e)
|
|||
|
{
|
|||
|
__Log.Error(e.ToString());
|
|||
|
}
|
|||
|
|
|||
|
IResult result = null;
|
|||
|
try
|
|||
|
{
|
|||
|
//此方法会阻塞,直到传输结束,也可以使用PullFileAsync
|
|||
|
result = await this._client.PullFileAsync(fileOperator);
|
|||
|
}
|
|||
|
catch (Exception e)
|
|||
|
{
|
|||
|
__Log.Error(e.ToString());
|
|||
|
}
|
|||
|
|
|||
|
_client.Logger.Info(result.ToString());
|
|||
|
return result;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|