FileSqlServerSync/Server/LocalServer/LocalSyncServer.cs

100 lines
2.2 KiB
C#
Raw Normal View History

2024-07-31 09:14:07 +00:00
using Common;
namespace LocalServer;
public class LocalSyncServer
{
2024-09-05 01:59:57 +00:00
#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
private StateHelpBase StateHelper;
public void SetStateHelper(StateHelpBase helper)
{
StateHelper = helper;
}
public StateHelpBase GetStateHelper()
{
return StateHelper;
}
2024-07-31 09:14:07 +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
public string Name;
2024-07-31 09:14:07 +00:00
/// <summary>
/// 发布源连接
/// </summary>
public readonly AbsPipeLine LocalPipe;
2024-07-31 09:14:07 +00:00
2024-09-23 05:55:17 +00:00
public readonly AbsPipeLine RemotePipe;
2024-07-31 09:14:07 +00:00
/// <summary>
/// 父工程,用于释放资源
/// </summary>
public readonly LocalSyncServerFactory Factory;
public LocalSyncServer(
AbsPipeLine pipe,
LocalSyncServerFactory factory,
string name,
AbsPipeLine remotePipe
)
{
LocalPipe = pipe;
Factory = factory;
2024-09-05 01:59:57 +00:00
StateHelper = new ConnectAuthorityHelper(this);
Name = name;
2024-09-23 05:55:17 +00:00
RemotePipe = remotePipe;
2024-07-31 09:14:07 +00:00
Task.Run(async () =>
2024-09-05 01:59:57 +00:00
{
try
{
var rs = LocalPipe.Work(
(byte[] b) =>
{
return StateHelper.ReceiveLocalMsg(b);
}
);
await foreach (var r in rs) { }
2024-09-05 01:59:57 +00:00
}
catch (Exception e)
2024-09-05 01:59:57 +00:00
{
Close(e.Message);
2024-09-05 01:59:57 +00:00
}
});
}
2024-07-31 09:14:07 +00:00
2024-09-05 01:59:57 +00:00
public void Close(string? CloseReason)
{
try
{
LocalPipe.Close(CloseReason);
RemotePipe.Close(CloseReason);
2024-09-05 01:59:57 +00:00
}
catch (Exception e)
{
//TODO 日志
Console.WriteLine(e.Message);
}
finally
{
Factory.RemoveLocalSyncServer(this);
}
}
}