FileSqlServerSync/Server/RemoteServer/RemoteSyncServer.cs

90 lines
1.9 KiB
C#
Raw Normal View History

2024-09-05 01:59:57 +00:00
using System.Net.WebSockets;
using Common;
namespace RemoteServer;
public class RemoteSyncServer
{
#pragma warning disable CA2211 // Non-constant fields should not be visible
public static string TempRootFile = "C:/TempPack";
#pragma warning restore CA2211 // Non-constant fields should not be visible
public StateHelpBase StateHelper;
2024-09-05 01:59:57 +00:00
public Config? SyncConfig;
public Config NotNullSyncConfig
{
get
2024-09-05 01:59:57 +00:00
{
if (SyncConfig == null)
{
throw new ArgumentNullException("SyncConfig");
}
return SyncConfig;
2024-09-05 01:59:57 +00:00
}
}
2024-09-05 01:59:57 +00:00
/// <summary>
/// 发布源连接
2024-09-05 01:59:57 +00:00
/// </summary>
public readonly AbsPipeLine Pipe;
2024-09-05 01:59:57 +00:00
/// <summary>
/// 父工程,用于释放资源
/// </summary>
public readonly RemoteSyncServerFactory Factory;
public string Name;
public string Pwd;
2024-09-05 01:59:57 +00:00
public RemoteSyncServer(
AbsPipeLine pipe,
RemoteSyncServerFactory factory,
string name,
string pwd
)
2024-09-05 01:59:57 +00:00
{
Pipe = pipe;
2024-09-05 01:59:57 +00:00
Factory = factory;
Name = name;
Pwd = pwd;
StateHelper = new ConnectAuthorityHelper(this);
2024-09-05 01:59:57 +00:00
Task.Run(async () =>
2024-09-05 01:59:57 +00:00
{
try
{
var rs = Pipe.Work(
(byte[] b) =>
{
return StateHelper.ReceiveMsg(b);
}
);
await foreach (var r in rs) { }
2024-09-05 01:59:57 +00:00
}
catch (Exception e)
{
Close(e.Message);
}
});
2024-09-05 01:59:57 +00:00
}
public void Close(string? CloseReason)
{
try
{
Pipe.Close(CloseReason);
2024-09-05 01:59:57 +00:00
}
catch (Exception e)
{
//TODO 日志
Console.WriteLine(e.Message);
}
finally
{
Factory.RemoveSyncServer(this);
2024-09-05 01:59:57 +00:00
}
}
}