VS_FileSendTool/FS/Net/ServerPlugin.cs

83 lines
3.0 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using TouchSocket.Core;
using TouchSocket.Dmtp;
using TouchSocket.Dmtp.FileTransfer;
using TouchSocket.Sockets;
namespace Net;
internal class ServerPlugin : PluginBase, IDmtpFileTransferredPlugin, IDmtpFileTransferringPlugin, ITcpConnectedPlugin,
ITcpClosedPlugin
{
private readonly ILog m_logger;
public ServerPlugin(ILog logger)
{
this.m_logger = logger;
}
/// <summary>
/// 该方法,会在每个文件被请求(推送)时第一时间触发。
/// 当请求文件时可以重新指定请求的文件路径即对e.ResourcePath直接赋值。
/// 当推送文件时可以重新指定保存文件路径即对e.SavePath直接赋值。
///
/// 注意:当文件夹不存在时,需要手动创建。
/// </summary>
/// <param name="client"></param>
/// <param name="e"></param>
/// <returns></returns>
public async Task OnDmtpFileTransferred(IDmtpActorObject client, FileTransferredEventArgs e)
{
//有可能是上传,也有可能是下载
this.m_logger.Info($"传输文件结束,请求类型={e.TransferType},文件名={e.ResourcePath},请求状态={e.Result}");
await e.InvokeNext();
}
/// <summary>
/// 该方法会在每个文件被请求推送结束时触发。传输不一定成功具体信息需要从e.Result判断状态。
/// 其次,该方法也不一定会被执行,例如:在传输过程中,直接断网,则该方法将不会执行。
/// </summary>
/// <param name="client"></param>
/// <param name="e"></param>
/// <returns></returns>
public async Task OnDmtpFileTransferring(IDmtpActorObject client, FileTransferringEventArgs e)
{
//传输但是不一定成功甚至该方法都不一定会被触发具体信息需要从e.Result判断状态。
this.m_logger.Info($"请求传输文件,请求类型={e.TransferType},请求文件名={e.ResourcePath}");
e.IsPermitOperation = true; //每次传输都需要设置true表示允许传输
await e.InvokeNext();
}
public async Task OnTcpConnected(ITcpSession client, ConnectedEventArgs e)
{
m_logger.Info($"客户端[{client.IP}]连接成功");
await e.InvokeNext();
}
public async Task OnTcpClosed(ITcpSession client, ClosedEventArgs e)
{
m_logger.Info($"客户端[{client.IP}]断开连接");
await e.InvokeNext();
}
}
internal class ClientPlugin : PluginBase, ITcpConnectedPlugin, ITcpClosedPlugin
{
private readonly ILog m_logger;
public ClientPlugin(ILog logger)
{
this.m_logger = logger;
}
public async Task OnTcpConnected(ITcpSession client, ConnectedEventArgs e)
{
m_logger.Info($"客户端[{client.IP}]连接成功");
await e.InvokeNext();
}
public async Task OnTcpClosed(ITcpSession client, ClosedEventArgs e)
{
m_logger.Info($"客户端[{client.IP}]断开连接");
await e.InvokeNext();
}
}