FileSqlServerSync/Server/RemoteServer/RemoteSyncServer.cs

101 lines
2.1 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";
public static string SqlPackageAbPath = "SqlPackageAbPath";
2024-09-05 01:59:57 +00:00
#pragma warning restore CA2211 // Non-constant fields should not be visible
private StateHelpBase StateHelper;
public void SetStateHelpBase(StateHelpBase stateHelper)
{
StateHelper = stateHelper;
}
public StateHelpBase GetStateHelpBase()
{
return 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
public async Task Connect()
{
try
2024-09-05 01:59:57 +00:00
{
var rs = Pipe.Work(
(byte[] b) =>
{
return StateHelper.ReceiveMsg(b);
}
);
await foreach (var r in rs) { }
}
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
}
}
}