VS_FileSendTool/FS/Net/Server.cs

67 lines
2.0 KiB
C#
Raw Normal View History

using FileSend;
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.Dmtp.Rpc;
using TouchSocket.Rpc;
using TouchSocket.Sockets;
namespace Net
{
public class Server
{
private TcpDmtpService _server;
TouchSocketConfig config;
public Server(int port = 7789)
{
if (string.IsNullOrEmpty(NetHelper.ServerRootPath) && !Directory.Exists(NetHelper.ServerRootPath))
{
Directory.CreateDirectory(NetHelper.ServerRootPath);
}
_server = new TcpDmtpService();
config = new TouchSocketConfig() //配置
.SetListenIPHosts(port)
.ConfigureContainer(a =>
{
a.AddConsoleLogger();
a.AddRpcStore(store =>
{
store.RegisterServer<GetFilesRpcServer>(); //注册服务
});
})
.SetDmtpOption(new DmtpOption()
{
VerifyToken = "Dmtp" //设定连接口令,作用类似账号密码
})
.ConfigurePlugins(a =>
{
a.UseDmtpRpc();
a.UseDmtpFileTransfer() //必须添加文件传输插件
.SetRootPath(NetHelper.ServerRootPath) //设置RootPath
.SetMaxSmallFileLength(1024 * 1024); //设置小文件的最大限制长度
a.Add<ServerPlugin>();
});
_server.Setup(config);
try
{
_server.Start();
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
_server.Logger.Info($"{_server.GetType().Name}已启动");
NetHelper.IsConnected = true;
}
}
}