FileSqlServerSync/Server/RemoteServer/StateHelper.cs

110 lines
3 KiB
C#
Raw Normal View History

2024-09-05 01:59:57 +00:00
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Text.Json;
using Common;
namespace RemoteServer;
// enum StateWhenMsg
// {
// Authority = 0,
// ConfigInfo = 1,
// LocalPackAndUpload = 2,
// RemoteUnPackAndRelease = 3,
// }
public abstract class StateHelpBase(
RemoteSyncServer context,
2024-09-05 01:59:57 +00:00
SyncProcessStep step = SyncProcessStep.Connect
)
{
protected readonly RemoteSyncServer Context = context;
2024-09-05 01:59:57 +00:00
protected readonly SyncProcessStep Step = step;
public SyncMsg CreateErrMsg(string Body)
{
return new SyncMsg(SyncMsgType.Error, Step, Body);
}
public SyncMsg CreateMsg(string body, SyncMsgType type = SyncMsgType.General)
{
return new SyncMsg(type, Step, body);
}
public bool ReceiveMsg(byte[] bytes)
2024-09-05 01:59:57 +00:00
{
var msg = AESHelper.DecryptStringFromBytes_Aes(bytes);
var syncMsg =
JsonSerializer.Deserialize<SyncMsg>(msg)
?? throw new NullReferenceException("msg is null");
if (syncMsg.Step != Step)
{
throw new Exception("Sync step error!");
}
HandleMsg(syncMsg);
return true;
2024-09-05 01:59:57 +00:00
}
protected abstract void HandleMsg(SyncMsg msg);
2024-09-05 01:59:57 +00:00
}
/// <summary>
/// 0. 链接验证
/// </summary>
/// <param name="context"></param>
public class ConnectAuthorityHelper(RemoteSyncServer context)
2024-09-05 01:59:57 +00:00
: StateHelpBase(context, SyncProcessStep.Connect)
{
protected override void HandleMsg(SyncMsg msg)
2024-09-05 01:59:57 +00:00
{
if (msg.Body == Context.Pwd)
2024-09-05 01:59:57 +00:00
{
Context.Pipe.SendMsg(CreateMsg("RemoteServer: 密码验证成功!"));
}
else
{
throw new Exception("密码错误!");
}
2024-09-05 01:59:57 +00:00
}
}
public class DiffFileHelper(RemoteSyncServer context)
: StateHelpBase(context, SyncProcessStep.DiffFileAndPack)
2024-09-05 01:59:57 +00:00
{
protected override void HandleMsg(SyncMsg msg)
2024-09-05 01:59:57 +00:00
{
Context.SyncConfig = JsonSerializer.Deserialize<Config>(msg.Body);
//文件对比
Context.NotNullSyncConfig.DirFileConfigs.ForEach(e =>
2024-09-05 01:59:57 +00:00
{
if (e.DirInfo == null)
2024-09-05 01:59:57 +00:00
{
throw new NullReferenceException("RemoteServer: 发布的文件为空!--这个异常应该永远不会发生~");
2024-09-05 01:59:57 +00:00
}
else
{
var nd = e.DirInfo.Clone();
nd.ResetRootPath(
Context.NotNullSyncConfig.LocalRootPath,
Context.NotNullSyncConfig.RemoteRootPath
);
nd.ExtractInfo(e.CherryPicks, e.Excludes);
var diff = e.DirInfo.Diff(nd);
e.DirInfo = diff;
2024-09-05 01:59:57 +00:00
}
});
//将对比结果发送到Local
Context.Pipe.SendMsg(
CreateMsg(JsonSerializer.Serialize(Context.NotNullSyncConfig.DirFileConfigs))
);
2024-09-05 01:59:57 +00:00
}
}
public class UnPackFilesHelper(RemoteSyncServer context):StateHelpBase(context,SyncProcessStep.UploadAndUnpack)
2024-09-05 01:59:57 +00:00
{
protected override void HandleMsg(SyncMsg msg)
2024-09-05 01:59:57 +00:00
{
throw new NotImplementedException();
2024-09-05 01:59:57 +00:00
}
}